Skip to content
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
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## Unreleased

* Added `tinify.Format` constants for the media types `convert` accepts. `SupportedImageTypes` is now derived from them.

## 1.8.3
* add JXL to supported image formats
* add node 26 as test target
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@

# Tinify API client for Node.js

A lightweight Node.js client for the Tinify API, used for [tinypng API](https://tinypng.com) and [TinyJPG](https://tinyjpg.com). This library lets you intelligently **compress**, **resize**, **convert**, and **store** images (AVIF, WebP, JPEG, PNG) with minimal effort. Read more at [http://tinify.com](http://tinify.com).
A lightweight Node.js client for the Tinify API, used for [tinypng API](https://tinypng.com) and [TinyJPG](https://tinyjpg.com). This library lets you intelligently **compress**, **resize**, **convert**, and **store** images (AVIF, JXL, WebP, JPEG, PNG) with minimal effort. Read more at [http://tinify.com](http://tinify.com).

## 🚀 Features

- Compress and optimize images in AVIF, WebP, JPEG, and PNG formats
- Compress and optimize images in AVIF, JXL, WebP, JPEG, and PNG formats
- Resize with intelligent cropping
- Convert between formats
- Preserve metadata (copyright, GPS, creation time)
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import tinify from "./tinify"

import Client from "./tinify/Client"
import Format from "./tinify/Format"
import Result from "./tinify/Result"
import ResultMeta from "./tinify/ResultMeta"
import Source from "./tinify/Source"
Expand All @@ -14,6 +15,7 @@ import {
} from "./tinify/Error"

tinify.Client = Client
tinify.Format = Format
tinify.ResultMeta = ResultMeta
tinify.Result = Result
tinify.Source = Source
Expand Down
2 changes: 2 additions & 0 deletions src/tinify.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {nodeify, Callback} from "./tinify/compat"

import Client from "./tinify/Client"
import Format from "./tinify/Format"
import Result from "./tinify/Result"
import ResultMeta from "./tinify/ResultMeta"
import Source from "./tinify/Source"
Expand All @@ -19,6 +20,7 @@ class Tinify {
compressionCount?: number

Client!: typeof Client
Format!: typeof Format
Source!: typeof Source
Result!: typeof Result
ResultMeta!: typeof ResultMeta
Expand Down
42 changes: 42 additions & 0 deletions src/tinify/Format.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* 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, an alias of `JPEG`. */
JPG: "image/jpg",
Comment on lines +29 to +30

/** AVIF. */
AVIF: "image/avif",

/** JPEG XL. */
JXL: "image/jxl",

/** Wildcard, returns the smallest of the supported types. */
ANY: "*/*",
} as const

export default Format
13 changes: 6 additions & 7 deletions src/tinify/Source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@ import tinify from "../tinify"
import {readFile, Callback} from "./compat"

import Client from "./Client"
import Format from "./Format"
import Result from "./Result"
import ResultMeta from "./ResultMeta"

export type SupportedImageTypes = "image/webp"
| "image/png"
| "image/jpg"
| "image/jpeg"
| "image/jxl"
| "image/avif";
type FormatValues = (typeof Format)[keyof typeof Format];

export type SupportedImageTypes = Exclude<FormatValues, typeof Format.ANY>;
Comment on lines +9 to +11

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

export type ConvertOptions = {
type: WildcardOrSupportedImageTypes | SupportedImageTypes[];
Expand Down
9 changes: 9 additions & 0 deletions test/tinify-typing-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,17 @@ 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
await tinify.fromBuffer("foo")
.resize({method: "fit", width: 150, height: 100})
.preserve("copyright", "creation")
Expand Down
Loading