Repository navigation
feat: add Format constants for convert media types, including JXL #55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| /** | ||
| * Media types that images can be converted to. | ||
| * | ||
| * ```js | ||
| * const tinify = require("tinify") | ||
| * | ||
| * tinify.fromFile("input.png") | ||
| * .convert({type: tinify.Format.JXL}) | ||
| * .toFile("output.jxl") | ||
| * ``` | ||
| * | ||
| * Multiple types may be supplied, in which case the API returns the smallest | ||
| * result: | ||
| * | ||
| * ```js | ||
| * .convert({type: [tinify.Format.JXL, tinify.Format.WEBP]}) | ||
| * ``` | ||
| */ | ||
| const Format = { | ||
| /** WebP. */ | ||
| WEBP: "image/webp", | ||
|
|
||
| /** PNG. */ | ||
| PNG: "image/png", | ||
|
|
||
| /** JPEG. */ | ||
| JPEG: "image/jpeg", | ||
|
|
||
| /** | ||
| * JPEG, under the alternative `image/jpg` media type the API also accepts. | ||
| * The API normalises this to `image/jpeg`, so the result is identical to | ||
| * `JPEG`. The two constants hold different strings but select the same | ||
| * format. | ||
| */ | ||
| JPG: "image/jpg", | ||
|
|
||
| /** AVIF. */ | ||
| AVIF: "image/avif", | ||
|
|
||
| /** JPEG XL. */ | ||
| JXL: "image/jxl", | ||
|
|
||
| /** Wildcard, returns the smallest of the supported types. */ | ||
| ANY: "*/*", | ||
| } as const | ||
|
|
||
| export default Format |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,8 +15,24 @@ async function run() { | |
| await tinify.fromFile("/foo/bar").convert({ type: "image/webp" }) | ||
| await tinify.fromFile("/foo/bar").convert({ type: "image/png" }) | ||
| await tinify.fromFile("/foo/bar").convert({ type: "image/jpg" }) | ||
| await tinify.fromFile("/foo/bar").convert({ type: "image/jxl" }) | ||
| await tinify.fromFile("/foo/bar").convert({ type: "image/avif" }) | ||
| await tinify.fromFile("/foo/bar").convert({ type: "*/*" }) | ||
|
|
||
| // The Format constants are accepted everywhere a literal is. | ||
| await tinify.fromFile("/foo/bar").convert({ type: tinify.Format.JXL }) | ||
| await tinify.fromFile("/foo/bar").convert({ type: tinify.Format.ANY }) | ||
| await tinify.fromFile("/foo/bar").convert({ | ||
| type: [tinify.Format.JXL, tinify.Format.WEBP], | ||
| }) | ||
|
|
||
|
Comment on lines
20
to
+28
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in 080cf91 — the two assertions are now in this file: // @ts-expect-error an unknown media type is not assignable
await tinify.fromFile("/foo/bar").convert({ type: "image/bogus" })
// @ts-expect-error the "*/*" wildcard is not valid inside the array form
await tinify.fromFile("/foo/bar").convert({ type: ["*/*"] })They are self-enforcing: an |
||
| // SupportedImageTypes is derived from Format; these assert the derived | ||
| // union is no wider than the literal list it replaced. | ||
| // @ts-expect-error an unknown media type is not assignable | ||
| await tinify.fromFile("/foo/bar").convert({ type: "image/bogus" }) | ||
| // @ts-expect-error the "*/*" wildcard is not valid inside the array form | ||
| await tinify.fromFile("/foo/bar").convert({ type: ["*/*"] }) | ||
|
|
||
| await tinify.fromBuffer("foo") | ||
| .resize({method: "fit", width: 150, height: 100}) | ||
| .preserve("copyright", "creation") | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not changing this one — the premise looks wrong. This file already uses semicolons on exactly these declarations, on
master, before this PR:The convention here is semicolon-free statements (imports, expressions) but semicolons terminating type alias and member declarations. The new aliases follow that, so removing them would be the inconsistent change. Happy to go either way if the maintainers prefer a different rule, but this matches what is already there.