forked from tinify/tinify-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSource.ts
More file actions
112 lines (91 loc) · 3.24 KB
/
Source.ts
File metadata and controls
112 lines (91 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import tinify from "../tinify"
import {readFile, Callback} from "./compat"
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>
/** @internal */
private _commands: object
/** @internal */
constructor(url: Promise<string>, commands?: object) {
this._url = url
this._commands = commands || {}
}
static fromFile(path: string): Source {
const location = readFile(path).then(data => {
const response = (tinify.client as Client).request("post", "/shrink", data)
return response.then(res => res.headers.location!)
})
return new tinify.Source(location)
}
static fromBuffer(data: string | Uint8Array): Source {
const response = (tinify.client as Client).request("post", "/shrink", data)
const location = response.then(res => res.headers.location!)
return new tinify.Source(location)
}
static fromUrl(url: string): Source {
const response = (tinify.client as Client).request("post", "/shrink", {source: {url}})
const location = response.then(res => res.headers.location!)
return new tinify.Source(location)
}
preserve(options: string[]): Source
preserve(...options: string[]): Source
preserve(...options: any[]): Source {
if (Array.isArray(options[0])) options = options[0]
return new tinify.Source(this._url, Object.assign({preserve: options}, this._commands))
}
resize(options: object): Source {
return new tinify.Source(this._url, Object.assign({resize: options}, this._commands))
}
store(options: object): ResultMeta {
const commands = Object.assign({store: options}, this._commands)
const response = this._url.then(url => {
return tinify.client.request("post", url, commands)
})
return new tinify.ResultMeta(
response.then(res => res.headers)
)
}
result(): Result {
const commands = this._commands
const response = this._url.then(url => {
return tinify.client.request("get", url, commands)
})
return new tinify.Result(
response.then(res => res.headers),
response.then(res => res.body)
)
}
toFile(path: string): Promise<void>
toFile(path: string, callback: Callback): void
toFile(path: string, callback?: Callback): Promise<void> | void {
return this.result().toFile(path, callback!)
}
toBuffer(): Promise<Uint8Array>
toBuffer(callback: Callback<Uint8Array>): void
toBuffer(callback?: Callback<Uint8Array>): Promise<Uint8Array> | void {
return this.result().toBuffer(callback!)
}
convert(options: ConvertOptions): Source {
return new tinify.Source(
this._url,
Object.assign({ convert: options }, this._commands)
)
}
transform(options: object): Source {
return new tinify.Source(
this._url,
Object.assign({ transform: options }, this._commands)
);
}
}