Skip to content
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

feat: convert TypeScript types made more specific #41

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 1.7.2
* Refine convert TypeScript types and add type tests

## 1.7.0
* Added convert and transform functions
* Added function to get the file extension
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tinify",
"version": "1.7.1",
"version": "1.7.2",
"description": "Node.js client for the Tinify API. Tinify compresses your images intelligently. Read more at https://tinify.com.",
"keywords": [
"tinify",
Expand Down
13 changes: 12 additions & 1 deletion src/tinify/Source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@ import Client from "./Client"
import Result from "./Result"
import ResultMeta from "./ResultMeta"

export type SupportedImageTypes = "image/webp"
| "image/png"
| "image/jpg";

export type WildcardOrSupportedImageTypes = SupportedImageTypes
| "*/*"; // The wildcard "*/*" returns the smallest of Tinify's supported image types, currently JPEG, PNG and WebP.

export type ConvertOptions = {
type: WildcardOrSupportedImageTypes | SupportedImageTypes[];
}

export default class Source {
/** @internal */
private _url: Promise<string>
Expand Down Expand Up @@ -85,7 +96,7 @@ export default class Source {
return this.result().toBuffer(callback!)
}

convert(options: object): Source {
convert(options: ConvertOptions): Source {
return new tinify.Source(
this._url,
Object.assign({ convert: options }, this._commands)
Expand Down
6 changes: 6 additions & 0 deletions test/tinify-typing-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ async function run() {
await tinify.fromBuffer(Buffer.from("foobar")).toBuffer()
await tinify.fromUrl("https://tinypng.com/images/panda-happy.png").toBuffer()

await tinify.fromFile("/foo/bar").convert({ type: ["image/webp", "image/png", "image/jpg"] })
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: "*/*" })

await tinify.fromBuffer("foo")
.resize({method: "fit", width: 150, height: 100})
.preserve("copyright", "creation")
Expand Down