From 7b472916087b59b66249c8ebba10541b0dbd87ba Mon Sep 17 00:00:00 2001 From: Eirini Galati Date: Thu, 3 Sep 2026 11:08:37 +0200 Subject: [PATCH] feat: add Format constants for convert media types Adds a Format namespace of media-type string constants so the types accepted by convert() are discoverable via autocomplete, including JPEG XL (image/jxl), which the API already accepts. String constants rather than a language-level enum: the JSON body must carry the media type verbatim, and an enum would serialize as its name. Existing callers passing raw strings are unaffected. --- CHANGES.md | 4 +++ src/main/java/com/tinify/Format.java | 45 ++++++++++++++++++++++++ src/test/java/com/tinify/FormatTest.java | 34 ++++++++++++++++++ 3 files changed, 83 insertions(+) create mode 100644 src/main/java/com/tinify/Format.java create mode 100644 src/test/java/com/tinify/FormatTest.java diff --git a/CHANGES.md b/CHANGES.md index 2d078e7..e9fce25 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,3 +1,7 @@ +## Unreleased + +* Added `Format` constants for the media types `convert` accepts, including JPEG XL (`image/jxl`). + ## 1.8.9 * Upgraded dependencies (okio 3.12.0, gson 2.13.1, commons-codec 1.21.0) * Upgraded all Maven plugins to latest stable versions diff --git a/src/main/java/com/tinify/Format.java b/src/main/java/com/tinify/Format.java new file mode 100644 index 0000000..c383186 --- /dev/null +++ b/src/main/java/com/tinify/Format.java @@ -0,0 +1,45 @@ +package com.tinify; + +/** + * Media types that images can be converted to. + * + *

Use these constants with {@link Source#convert(Options)}: + * + *

{@code
+ * Tinify.fromFile("input.png")
+ *       .convert(new Options().with("type", Format.JXL))
+ *       .toFile("output.jxl");
+ * }
+ * + *

Multiple types may be supplied, in which case the API returns the + * smallest result: + * + *

{@code
+ * new Options().with("type", new String[]{Format.JXL, Format.WEBP});
+ * }
+ */ +public final class Format { + /** WebP, {@code image/webp}. */ + public static final String WEBP = "image/webp"; + + /** PNG, {@code image/png}. */ + public static final String PNG = "image/png"; + + /** JPEG, {@code image/jpeg}. */ + public static final String JPEG = "image/jpeg"; + + /** JPEG, {@code image/jpg}. An alias of {@link #JPEG}. */ + public static final String JPG = "image/jpg"; + + /** AVIF, {@code image/avif}. */ + public static final String AVIF = "image/avif"; + + /** JPEG XL, {@code image/jxl}. */ + public static final String JXL = "image/jxl"; + + /** Wildcard, {@code */*}. Returns the smallest of the supported types. */ + public static final String ANY = "*/*"; + + private Format() { + } +} diff --git a/src/test/java/com/tinify/FormatTest.java b/src/test/java/com/tinify/FormatTest.java new file mode 100644 index 0000000..acd94eb --- /dev/null +++ b/src/test/java/com/tinify/FormatTest.java @@ -0,0 +1,34 @@ +package com.tinify; + +import org.junit.Test; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.equalTo; + +public class FormatTest { + @Test + public void formatShouldExposeSupportedMediaTypes() { + assertThat(Format.WEBP, is(equalTo("image/webp"))); + assertThat(Format.PNG, is(equalTo("image/png"))); + assertThat(Format.JPEG, is(equalTo("image/jpeg"))); + assertThat(Format.JPG, is(equalTo("image/jpg"))); + assertThat(Format.AVIF, is(equalTo("image/avif"))); + assertThat(Format.JXL, is(equalTo("image/jxl"))); + assertThat(Format.ANY, is(equalTo("*/*"))); + } + + @Test + public void convertWithFormatShouldSerializeMediaType() { + Options options = new Options().with("convert", new Options().with("type", Format.JXL)); + assertThat(options.toJson(), is(equalTo("{\"convert\":{\"type\":\"image/jxl\"}}"))); + } + + @Test + public void convertWithMultipleFormatsShouldSerializeMediaTypes() { + Options options = new Options().with("convert", + new Options().with("type", new String[]{Format.JXL, Format.WEBP})); + assertThat(options.toJson(), + is(equalTo("{\"convert\":{\"type\":[\"image/jxl\",\"image/webp\"]}}"))); + } +}