imgix-url-builder 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +108 -0
- package/dist/index.cjs +59 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +2602 -0
- package/dist/index.mjs +52 -0
- package/dist/index.mjs.map +1 -0
- package/dist/node/index.cjs +118 -0
- package/dist/node/index.cjs.map +1 -0
- package/dist/node/index.d.ts +3053 -0
- package/dist/node/index.d.ts.map +1 -0
- package/dist/node/index.mjs +109 -0
- package/dist/node/index.mjs.map +1 -0
- package/package.json +81 -0
- package/src/Client.ts +164 -0
- package/src/buildPixelDensitySrcSet.ts +70 -0
- package/src/buildURL.ts +63 -0
- package/src/buildWidthSrcSet.ts +66 -0
- package/src/index.ts +17 -0
- package/src/node/SecureClient.ts +565 -0
- package/src/node/buildSignedPixelDensitySrcSet.ts +67 -0
- package/src/node/buildSignedURL.ts +56 -0
- package/src/node/buildSignedWidthSrcSet.ts +68 -0
- package/src/node/index.ts +19 -0
- package/src/node/signURL.ts +44 -0
- package/src/types.generated.ts +2822 -0
|
@@ -0,0 +1,565 @@
|
|
|
1
|
+
import type { BuildPixelDensitySrcSetParams } from "../buildPixelDensitySrcSet";
|
|
2
|
+
import type { BuildWidthSrcSetParams } from "../buildWidthSrcSet";
|
|
3
|
+
import type { ImgixURLParams } from "../types.generated";
|
|
4
|
+
import { buildPixelDensitySrcSet } from "../buildPixelDensitySrcSet";
|
|
5
|
+
import { buildSignedPixelDensitySrcSet } from "./buildSignedPixelDensitySrcSet";
|
|
6
|
+
import { buildSignedURL } from "./buildSignedURL";
|
|
7
|
+
import { buildSignedWidthSrcSet } from "./buildSignedWidthSrcSet";
|
|
8
|
+
import { buildURL } from "../buildURL";
|
|
9
|
+
import { buildWidthSrcSet } from "../buildWidthSrcSet";
|
|
10
|
+
import { signURL } from "./signURL";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Options to instantiate a new secure client.
|
|
14
|
+
*/
|
|
15
|
+
export type SecureClientOptions = {
|
|
16
|
+
/**
|
|
17
|
+
* The base URL used to construct image URLs from a path. The base URL must
|
|
18
|
+
* include the protocol, domain, and optionally a path.
|
|
19
|
+
*
|
|
20
|
+
* @example `https://example.imgix.net`
|
|
21
|
+
*
|
|
22
|
+
* @example `https://example.imgix.net/folder`
|
|
23
|
+
*/
|
|
24
|
+
baseURL: string;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* The secret secure URL token for the configured Imgix domain.
|
|
28
|
+
*
|
|
29
|
+
* @see How to get a token: https://docs.imgix.com/setup/securing-images
|
|
30
|
+
*/
|
|
31
|
+
secureURLToken: string;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* An Imgix Rendering API client with automatic URL signing. A client is paired
|
|
36
|
+
* to a single Imgix domain and secure URL token.
|
|
37
|
+
*
|
|
38
|
+
* **Important**: This client should only be used in a trusted environment and
|
|
39
|
+
* never in a browser. The `secureURLToken` parameter is a secret and should not
|
|
40
|
+
* be exposed to the public.
|
|
41
|
+
*/
|
|
42
|
+
export class SecureClient {
|
|
43
|
+
/**
|
|
44
|
+
* The base URL used to construct image URLs from a path. The base URL must
|
|
45
|
+
* include the protocol, domain, and optionally a path.
|
|
46
|
+
*/
|
|
47
|
+
baseURL: string;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* The secret secure URL token for the configured Imgix domain.
|
|
51
|
+
*/
|
|
52
|
+
secureURLToken: string;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Creates a new `SecureClient` instance for an Imgix domain.
|
|
56
|
+
*
|
|
57
|
+
* @param options - Options to instantiate a new client.
|
|
58
|
+
*
|
|
59
|
+
* @returns A `SecureClient` instance for the given Imgix domain.
|
|
60
|
+
*/
|
|
61
|
+
constructor(options: SecureClientOptions) {
|
|
62
|
+
this.baseURL = options.baseURL;
|
|
63
|
+
this.secureURLToken = options.secureURLToken;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Builds a Web Proxy URL that proxies a non-Imgix URL to Imgix. The client's
|
|
68
|
+
* Imgix domain must be configured as a Web Proxy source. All Web Proxy URLs
|
|
69
|
+
* will be signed automatically.
|
|
70
|
+
*
|
|
71
|
+
* **Important**: The given URL must not be URI encoded. If it is, decode it
|
|
72
|
+
* before passing it to `buildWebProxyURL()`.
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
*
|
|
76
|
+
* ```ts
|
|
77
|
+
* const client = new SecureClient({
|
|
78
|
+
* baseURL: "https://example.imgix.net",
|
|
79
|
+
* secureURLToken: "example-token",
|
|
80
|
+
* });
|
|
81
|
+
* const url = client.buildWebProxyURL("https://www.this.com/pic.jpg", {
|
|
82
|
+
* width: 400,
|
|
83
|
+
* });
|
|
84
|
+
* // => https://example.imgix.net/https%3A%2F%2Fwww.this.com%2Fpic.jpg?width=400&s=def3e221c3f4c4debda091b8e49420ea
|
|
85
|
+
* ```
|
|
86
|
+
*
|
|
87
|
+
* @param url - Full absolute URL to the source image to proxy.
|
|
88
|
+
* @param params - An object of Imgix URL API parameters.
|
|
89
|
+
*
|
|
90
|
+
* @returns An Imgix url proxying `url` to the client's Imgix domain.
|
|
91
|
+
*/
|
|
92
|
+
buildWebProxyURL(url: string, params: ImgixURLParams = {}): string {
|
|
93
|
+
return this.buildSignedURLForPath(encodeURIComponent(url), params);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Builds a URL to an Imgix image with Imgix URL API parameters for the
|
|
98
|
+
* client's base URL.
|
|
99
|
+
*
|
|
100
|
+
* @example
|
|
101
|
+
*
|
|
102
|
+
* ```ts
|
|
103
|
+
* const client = new SecureClient({
|
|
104
|
+
* baseURL: "https://example.imgix.net",
|
|
105
|
+
* secureURLToken: "example-token",
|
|
106
|
+
* });
|
|
107
|
+
* const url = client.buildURLForPath("/image.png", { width: 400 });
|
|
108
|
+
* // => https://example.imgix.net/image.png?width=400&s=def3e221c3f4c4debda091b8e49420ea
|
|
109
|
+
* ```
|
|
110
|
+
*
|
|
111
|
+
* @example
|
|
112
|
+
*
|
|
113
|
+
* ```ts
|
|
114
|
+
* const client = new SecureClient({
|
|
115
|
+
* baseURL: "https://example.imgix.net/folder",
|
|
116
|
+
* secureURLToken: "example-token",
|
|
117
|
+
* });
|
|
118
|
+
* const url = client.buildURLForPath("./image.png", { width: 400 });
|
|
119
|
+
* // => https://example.imgix.net/folder/image.png?width=400&s=f12c7c39333410c10c2930b57116a943
|
|
120
|
+
* ```
|
|
121
|
+
*
|
|
122
|
+
* @param path - Path to the image relative to the client's base URL.
|
|
123
|
+
* @param params - An object of Imgix URL API parameters.
|
|
124
|
+
*
|
|
125
|
+
* @returns The full absolute URL to the image with the given Imgix URL API
|
|
126
|
+
* parameters applied.
|
|
127
|
+
*/
|
|
128
|
+
buildSignedURLForPath(path: string, params: ImgixURLParams = {}): string {
|
|
129
|
+
return this.signURL(this.buildURLForPath(path, params));
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Builds a URL to an Imgix image with Imgix URL API parameters for the
|
|
134
|
+
* client's base URL.
|
|
135
|
+
*
|
|
136
|
+
* Note: The returned URL is not signed. See `buildSignedURL` if a signature
|
|
137
|
+
* is required.
|
|
138
|
+
*
|
|
139
|
+
* @example
|
|
140
|
+
*
|
|
141
|
+
* ```ts
|
|
142
|
+
* const client = new SecureClient({
|
|
143
|
+
* baseURL: "https://example.imgix.net",
|
|
144
|
+
* secureURLToken: "example-token",
|
|
145
|
+
* });
|
|
146
|
+
* const url = client.buildURLForPath("/image.png", { width: 400 });
|
|
147
|
+
* // => https://example.imgix.net/image.png?width=400
|
|
148
|
+
* ```
|
|
149
|
+
*
|
|
150
|
+
* @example
|
|
151
|
+
*
|
|
152
|
+
* ```ts
|
|
153
|
+
* const client = new SecureClient({
|
|
154
|
+
* baseURL: "https://example.imgix.net/folder",
|
|
155
|
+
* secureURLToken: "example-token",
|
|
156
|
+
* });
|
|
157
|
+
* const url = client.buildURLForPath("./image.png", { width: 400 });
|
|
158
|
+
* // => https://example.imgix.net/folder/image.png?width=400
|
|
159
|
+
* ```
|
|
160
|
+
*
|
|
161
|
+
* @param path - Path to the image relative to the client's base URL.
|
|
162
|
+
* @param params - An object of Imgix URL API parameters.
|
|
163
|
+
*
|
|
164
|
+
* @returns The full absolute URL to the image with the given Imgix URL API
|
|
165
|
+
* parameters applied.
|
|
166
|
+
*/
|
|
167
|
+
buildURLForPath(path: string, params: ImgixURLParams = {}): string {
|
|
168
|
+
return buildURL(`${new URL(path, this.baseURL)}`, params);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Builds a URL to an Imgix image with Imgix URL API parameters. The URL is
|
|
173
|
+
* signed by appending a signature to the URL parameters. This locks the URL
|
|
174
|
+
* and its parameters to the signature to prevent URL tampering.
|
|
175
|
+
*
|
|
176
|
+
* The given URL must be a full absolute URL containing the protocol and domain.
|
|
177
|
+
*
|
|
178
|
+
* URL parameters already applied to the image will be retained. To remove
|
|
179
|
+
* existing parameters, set the parameter to `undefined` in the `params` argument.
|
|
180
|
+
*
|
|
181
|
+
* @example
|
|
182
|
+
*
|
|
183
|
+
* ```ts
|
|
184
|
+
* const url = client.buildSignedURL(
|
|
185
|
+
* "https://example.imgix.net/image.png",
|
|
186
|
+
* { width: 400 },
|
|
187
|
+
* );
|
|
188
|
+
* // => https://example.imgix.net/image.png?width=400&s=def3e221c3f4c4debda091b8e49420ea
|
|
189
|
+
* ```
|
|
190
|
+
*
|
|
191
|
+
* @example
|
|
192
|
+
*
|
|
193
|
+
* ```ts
|
|
194
|
+
* const url = client.buildSignedURL(
|
|
195
|
+
* "https://example.imgix.net/image.png?width=400",
|
|
196
|
+
* { height: 300 },
|
|
197
|
+
* );
|
|
198
|
+
* // => https://example.imgix.net/image.png?width=400&height=300&s=f12c7c39333410c10c2930b57116a943
|
|
199
|
+
* ```
|
|
200
|
+
*
|
|
201
|
+
* @param url - Full absolute URL to the Imgix image.
|
|
202
|
+
* @param params - An object of Imgix URL API parameters.
|
|
203
|
+
*
|
|
204
|
+
* @returns `url` with the given Imgix URL API parameters applied.
|
|
205
|
+
*
|
|
206
|
+
* @see Imgix URL API reference: https://docs.imgix.com/apis/rendering
|
|
207
|
+
* @see Details on securing Imgix images: https://docs.imgix.com/setup/securing-images
|
|
208
|
+
*/
|
|
209
|
+
buildSignedURL(url: string, params: ImgixURLParams = {}): string {
|
|
210
|
+
return buildSignedURL(url, this.secureURLToken, params);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Signs an Imgix image URL by appending a signature to the URL parameters.
|
|
215
|
+
* This locks the URL and its parameters to the signature to prevent URL tampering.
|
|
216
|
+
*
|
|
217
|
+
* @example
|
|
218
|
+
*
|
|
219
|
+
* ```ts
|
|
220
|
+
* const url = client.signURL(
|
|
221
|
+
* "https://example.imgix.net/image.png?width=400",
|
|
222
|
+
* );
|
|
223
|
+
* // => https://example.imgix.net/image.png?width=400&s=def3e221c3f4c4debda091b8e49420ea
|
|
224
|
+
* ```
|
|
225
|
+
*
|
|
226
|
+
* @param url - Full absolute URL to the Imgix image to sign.
|
|
227
|
+
*
|
|
228
|
+
* @returns `url` with a signature appended via an `s` URL parameter.
|
|
229
|
+
*
|
|
230
|
+
* @see Details on securing Imgix images: https://docs.imgix.com/setup/securing-images
|
|
231
|
+
*/
|
|
232
|
+
signURL(url: string): string {
|
|
233
|
+
return signURL(url, this.secureURLToken);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Builds an `<img>` `srcset` attribute value for a given set of widths. It
|
|
238
|
+
* can also optinally apply Imgix URL API parameters to the URLs. The URLs are
|
|
239
|
+
* signed by appending a signature to their URL parameters. This locks the
|
|
240
|
+
* URLs and their parameters to the signature to prevent URL tampering.
|
|
241
|
+
*
|
|
242
|
+
* The `width` URL parameter will be applied for each `srcset` entry. If a
|
|
243
|
+
* `width` or `w` parameter is provided to the `params` parameter, it will be ignored.
|
|
244
|
+
*
|
|
245
|
+
* @example
|
|
246
|
+
*
|
|
247
|
+
* ```ts
|
|
248
|
+
* const srcset = client.buildSignedWidthSrcSet(
|
|
249
|
+
* "https://example.imgix.net/image.png",
|
|
250
|
+
* { widths: [400, 800, 1600] },
|
|
251
|
+
* );
|
|
252
|
+
* // => https://example.imgix.net/image.png?width=400&s=def3e221c3f4c4debda091b8e49420ea 400w,
|
|
253
|
+
* // https://example.imgix.net/image.png?width=800&s=f12c7c39333410c10c2930b57116a943 800w,
|
|
254
|
+
* // https://example.imgix.net/image.png?width=1600&s=3a975b5087ab7ad2ab91fe66072fd628 1600w
|
|
255
|
+
* ```
|
|
256
|
+
*
|
|
257
|
+
* @example
|
|
258
|
+
*
|
|
259
|
+
* ```ts
|
|
260
|
+
* const srcset = client.buildSignedWidthSrcSet(
|
|
261
|
+
* "https://example.imgix.net/image.png",
|
|
262
|
+
* {
|
|
263
|
+
* widths: [400, 800, 1600],
|
|
264
|
+
* sat: -100,
|
|
265
|
+
* },
|
|
266
|
+
* );
|
|
267
|
+
* // => https://example.imgix.net/image.png?width=400&sat=-100&s=def3e221c3f4c4debda091b8e49420ea 400w,
|
|
268
|
+
* // https://example.imgix.net/image.png?width=800&sat=-100&s=f12c7c39333410c10c2930b57116a943 800w,
|
|
269
|
+
* // https://example.imgix.net/image.png?width=1600&sat=-100&s=3a975b5087ab7ad2ab91fe66072fd628 1600w
|
|
270
|
+
* ```
|
|
271
|
+
*
|
|
272
|
+
* @param url - Full absolute URL to the Imgix image.
|
|
273
|
+
* @param params - An object of Imgix URL API parameters. The `widths`
|
|
274
|
+
* parameter defines the resulting `srcset` widths.
|
|
275
|
+
*
|
|
276
|
+
* @returns A `srcset` attribute value for `url` with the given Imgix URL API
|
|
277
|
+
* parameters applied.
|
|
278
|
+
*/
|
|
279
|
+
buildSignedWidthSrcSet(url: string, params: BuildWidthSrcSetParams): string {
|
|
280
|
+
return buildSignedWidthSrcSet(url, this.secureURLToken, params);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* Builds an `<img>` `srcset` attribute value for a given set of widths for
|
|
285
|
+
* the client's base URL. It can also optinally apply Imgix URL API parameters
|
|
286
|
+
* to the URLs.
|
|
287
|
+
*
|
|
288
|
+
* The `width` URL parameter will be applied for each `srcset` entry. If a
|
|
289
|
+
* `width` or `w` parameter is provided to the `params` parameter, it will be ignored.
|
|
290
|
+
*
|
|
291
|
+
* Note: The returned URLs are not signed. See `buildSignedWidthSrcSet` if
|
|
292
|
+
* signatures are required.
|
|
293
|
+
*
|
|
294
|
+
* @example
|
|
295
|
+
*
|
|
296
|
+
* ```ts
|
|
297
|
+
* const client = new SecureClient({
|
|
298
|
+
* baseURL: "https://example.imgix.net",
|
|
299
|
+
* secureURLToken: "example-token",
|
|
300
|
+
* });
|
|
301
|
+
* const srcset = client.buildWidthSrcSetForPath("/image.png", {
|
|
302
|
+
* widths: [400, 800, 1600],
|
|
303
|
+
* });
|
|
304
|
+
* // => https://example.imgix.net/image.png?width=400 400w,
|
|
305
|
+
* // https://example.imgix.net/image.png?width=800 800w,
|
|
306
|
+
* // https://example.imgix.net/image.png?width=1600 1600w
|
|
307
|
+
* ```
|
|
308
|
+
*
|
|
309
|
+
* @example
|
|
310
|
+
*
|
|
311
|
+
* ```ts
|
|
312
|
+
* const client = new SecureClient({
|
|
313
|
+
* baseURL: "https://example.imgix.net",
|
|
314
|
+
* secureURLToken: "example-token",
|
|
315
|
+
* });
|
|
316
|
+
* const srcset = client.buildWidthSrcSetForPath("/image.png", {
|
|
317
|
+
* widths: [400, 800, 1600],
|
|
318
|
+
* sat: -100,
|
|
319
|
+
* });
|
|
320
|
+
* // => https://example.imgix.net/image.png?width=400&sat=-100 400w,
|
|
321
|
+
* // https://example.imgix.net/image.png?width=800&sat=-100 800w,
|
|
322
|
+
* // https://example.imgix.net/image.png?width=1600&sat=-100 1600w
|
|
323
|
+
* ```
|
|
324
|
+
*
|
|
325
|
+
* @param path - Path to the image relative to the client's base URL.
|
|
326
|
+
* @param params - An object of Imgix URL API parameters. The `widths`
|
|
327
|
+
* parameter defines the resulting `srcset` widths.
|
|
328
|
+
*
|
|
329
|
+
* @returns A `srcset` attribute value for `url` with the given Imgix URL API
|
|
330
|
+
* parameters applied.
|
|
331
|
+
*/
|
|
332
|
+
buildWidthSrcSetForPath(
|
|
333
|
+
path: string,
|
|
334
|
+
params: BuildWidthSrcSetParams,
|
|
335
|
+
): string {
|
|
336
|
+
return buildWidthSrcSet(`${new URL(path, this.baseURL)}`, params);
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
/**
|
|
340
|
+
* Builds an `<img>` `srcset` attribute value for a given set of widths for
|
|
341
|
+
* the client's base URL. It can also optinally apply Imgix URL API parameters
|
|
342
|
+
* to the URLs. The URLs are signed by appending a signature to their URL
|
|
343
|
+
* parameters. This locks the URLs and their parameters to the signature to
|
|
344
|
+
* prevent URL tampering.
|
|
345
|
+
*
|
|
346
|
+
* The `width` URL parameter will be applied for each `srcset` entry. If a
|
|
347
|
+
* `width` or `w` parameter is provided to the `params` parameter, it will be ignored.
|
|
348
|
+
*
|
|
349
|
+
* @example
|
|
350
|
+
*
|
|
351
|
+
* ```ts
|
|
352
|
+
* const client = new SecureClient({
|
|
353
|
+
* baseURL: "https://example.imgix.net",
|
|
354
|
+
* secureURLToken: "example-token",
|
|
355
|
+
* });
|
|
356
|
+
* const srcset = client.buildSignedWidthSrcSetForPath("/image.png", {
|
|
357
|
+
* widths: [400, 800, 1600],
|
|
358
|
+
* });
|
|
359
|
+
* // => https://example.imgix.net/image.png?width=400&s=def3e221c3f4c4debda091b8e49420ea 400w,
|
|
360
|
+
* // https://example.imgix.net/image.png?width=800&s=f12c7c39333410c10c2930b57116a943 800w,
|
|
361
|
+
* // https://example.imgix.net/image.png?width=1600&s=3a975b5087ab7ad2ab91fe66072fd628 1600w
|
|
362
|
+
* ```
|
|
363
|
+
*
|
|
364
|
+
* @example
|
|
365
|
+
*
|
|
366
|
+
* ```ts
|
|
367
|
+
* const client = new SecureClient({
|
|
368
|
+
* baseURL: "https://example.imgix.net",
|
|
369
|
+
* secureURLToken: "example-token",
|
|
370
|
+
* });
|
|
371
|
+
* const srcset = client.buildSignedWidthSrcSetForPath("/image.png", {
|
|
372
|
+
* widths: [400, 800, 1600],
|
|
373
|
+
* sat: -100,
|
|
374
|
+
* });
|
|
375
|
+
* // => https://example.imgix.net/image.png?width=400&sat=-100&s=def3e221c3f4c4debda091b8e49420ea 400w,
|
|
376
|
+
* // https://example.imgix.net/image.png?width=800&sat=-100&s=f12c7c39333410c10c2930b57116a943 800w,
|
|
377
|
+
* // https://example.imgix.net/image.png?width=1600&sat=-100&s=3a975b5087ab7ad2ab91fe66072fd628 1600w
|
|
378
|
+
* ```
|
|
379
|
+
*
|
|
380
|
+
* @param path - Path to the image relative to the client's base URL.
|
|
381
|
+
* @param params - An object of Imgix URL API parameters. The `widths`
|
|
382
|
+
* parameter defines the resulting `srcset` widths.
|
|
383
|
+
*
|
|
384
|
+
* @returns A `srcset` attribute value for `url` with the given Imgix URL API
|
|
385
|
+
* parameters applied.
|
|
386
|
+
*/
|
|
387
|
+
buildSignedWidthSrcSetForPath(
|
|
388
|
+
path: string,
|
|
389
|
+
params: BuildWidthSrcSetParams,
|
|
390
|
+
): string {
|
|
391
|
+
return this.buildSignedWidthSrcSet(
|
|
392
|
+
`${new URL(path, this.baseURL)}`,
|
|
393
|
+
params,
|
|
394
|
+
);
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
/**
|
|
398
|
+
* Builds an `<img>` `srcset` attribute value for a given set of pixel
|
|
399
|
+
* densities. It can also optinally apply Imgix URL API parameters to the
|
|
400
|
+
* URLs. The URLs are signed by appending a signature to their URL parameters.
|
|
401
|
+
* This locks the URLs and their parameters to the signature to prevent URL tampering.
|
|
402
|
+
*
|
|
403
|
+
* The `dpr` URL parameter will be applied for each `srcset` entry. If a `dpr`
|
|
404
|
+
* parameter is provided to the `params` parameter, it will be ignored.
|
|
405
|
+
*
|
|
406
|
+
* @example
|
|
407
|
+
*
|
|
408
|
+
* ```ts
|
|
409
|
+
* const srcset = client.buildSignedPixelDensitySrcSet(
|
|
410
|
+
* "https://example.imgix.net/image.png",
|
|
411
|
+
* { pixelDensities: [1, 2, 3] },
|
|
412
|
+
* );
|
|
413
|
+
* // => https://example.imgix.net/image.png?dpr=1&s=def3e221c3f4c4debda091b8e49420ea 1x,
|
|
414
|
+
* // https://example.imgix.net/image.png?dpr=2&s=f12c7c39333410c10c2930b57116a943 2x,
|
|
415
|
+
* // https://example.imgix.net/image.png?dpr=3&s=3a975b5087ab7ad2ab91fe66072fd628 3x
|
|
416
|
+
* ```
|
|
417
|
+
*
|
|
418
|
+
* @example
|
|
419
|
+
*
|
|
420
|
+
* ```ts
|
|
421
|
+
* const srcset = client.buildSignedPixelDensitySrcSet(
|
|
422
|
+
* "https://example.imgix.net/image.png",
|
|
423
|
+
* {
|
|
424
|
+
* pixelDensities: [1, 2, 3],
|
|
425
|
+
* sat: -100,
|
|
426
|
+
* },
|
|
427
|
+
* );
|
|
428
|
+
* // => https://example.imgix.net/image.png?dpr=1&sat=-100&s=def3e221c3f4c4debda091b8e49420ea 1x,
|
|
429
|
+
* // https://example.imgix.net/image.png?dpr=2&sat=-100&s=f12c7c39333410c10c2930b57116a943 2x,
|
|
430
|
+
* // https://example.imgix.net/image.png?dpr=3&sat=-100&s=3a975b5087ab7ad2ab91fe66072fd628 3x
|
|
431
|
+
* ```
|
|
432
|
+
*
|
|
433
|
+
* @param url - Full absolute URL to the Imgix image.
|
|
434
|
+
* @param params - An object of Imgix URL API parameters. The `pixelDensities`
|
|
435
|
+
* parameter defines the resulting `srcset` widths.
|
|
436
|
+
*
|
|
437
|
+
* @returns A `srcset` attribute value for `url` with the given Imgix URL API
|
|
438
|
+
* parameters applied.
|
|
439
|
+
*/
|
|
440
|
+
buildSignedPixelDensitySrcSet(
|
|
441
|
+
url: string,
|
|
442
|
+
params: BuildPixelDensitySrcSetParams,
|
|
443
|
+
): string {
|
|
444
|
+
return buildSignedPixelDensitySrcSet(url, this.secureURLToken, params);
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
/**
|
|
448
|
+
* Builds an `<img>` `srcset` attribute value for a given set of pixel
|
|
449
|
+
* densities for the client's base URL. It can also optinally apply Imgix URL
|
|
450
|
+
* API parameters to the URLs.
|
|
451
|
+
*
|
|
452
|
+
* The `dpr` URL parameter will be applied for each `srcset` entry. If a `dpr`
|
|
453
|
+
* parameter is provided to the `params` parameter, it will be ignored.
|
|
454
|
+
*
|
|
455
|
+
* Note: The returned URLs are not signed. See `buildSignedPixelDensitySrcSet`
|
|
456
|
+
* if signatures are required.
|
|
457
|
+
*
|
|
458
|
+
* @example
|
|
459
|
+
*
|
|
460
|
+
* ```ts
|
|
461
|
+
* const client = new SecureClient({
|
|
462
|
+
* baseURL: "https://example.imgix.net",
|
|
463
|
+
* secureURLToken: "example-token",
|
|
464
|
+
* });
|
|
465
|
+
* const srcset = client.buildPixelDensitySrcSetForPath("/image.png", {
|
|
466
|
+
* pixelDensities: [1, 2, 3],
|
|
467
|
+
* });
|
|
468
|
+
* // => https://example.imgix.net/image.png?dpr=1 1x,
|
|
469
|
+
* // https://example.imgix.net/image.png?dpr=2 2x,
|
|
470
|
+
* // https://example.imgix.net/image.png?dpr=3 3x
|
|
471
|
+
* ```
|
|
472
|
+
*
|
|
473
|
+
* @example
|
|
474
|
+
*
|
|
475
|
+
* ```ts
|
|
476
|
+
* const client = new SecureClient({
|
|
477
|
+
* baseURL: "https://example.imgix.net",
|
|
478
|
+
* secureURLToken: "example-token",
|
|
479
|
+
* });
|
|
480
|
+
* const srcset = client.buildPixelDensitySrcSetForPath("/image.png", {
|
|
481
|
+
* pixelDensities: [1, 2, 3],
|
|
482
|
+
* sat: -100,
|
|
483
|
+
* });
|
|
484
|
+
* // => https://example.imgix.net/image.png?dpr=1&sat=-100 1x,
|
|
485
|
+
* // https://example.imgix.net/image.png?dpr=2&sat=-100 2x,
|
|
486
|
+
* // https://example.imgix.net/image.png?dpr=3&sat=-100 3x
|
|
487
|
+
* ```
|
|
488
|
+
*
|
|
489
|
+
* @param path - Path to the image relative to the client's base URL.
|
|
490
|
+
* @param params - An object of Imgix URL API parameters. The `pixelDensities`
|
|
491
|
+
* parameter defines the resulting `srcset` widths.
|
|
492
|
+
*
|
|
493
|
+
* @returns A `srcset` attribute value for `url` with the given Imgix URL API
|
|
494
|
+
* parameters applied.
|
|
495
|
+
*/
|
|
496
|
+
buildPixelDensitySrcSetForPath(
|
|
497
|
+
path: string,
|
|
498
|
+
params: BuildPixelDensitySrcSetParams,
|
|
499
|
+
): string {
|
|
500
|
+
return buildPixelDensitySrcSet(`${new URL(path, this.baseURL)}`, params);
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
/**
|
|
504
|
+
* Builds an `<img>` `srcset` attribute value for a given set of pixel
|
|
505
|
+
* densities for the client's base URL. It can also optinally apply Imgix URL
|
|
506
|
+
* API parameters to the URLs. The URLs are signed by appending a signature to
|
|
507
|
+
* their URL parameters. This locks the URLs and their parameters to the
|
|
508
|
+
* signature to prevent URL tampering.
|
|
509
|
+
*
|
|
510
|
+
* The `dpr` URL parameter will be applied for each `srcset` entry. If a `dpr`
|
|
511
|
+
* parameter is provided to the `params` parameter, it will be ignored.
|
|
512
|
+
*
|
|
513
|
+
* @example
|
|
514
|
+
*
|
|
515
|
+
* ```ts
|
|
516
|
+
* const client = new SecureClient({
|
|
517
|
+
* baseURL: "https://example.imgix.net",
|
|
518
|
+
* secureURLToken: "example-token",
|
|
519
|
+
* });
|
|
520
|
+
* const srcset = client.buildSignedPixelDensitySrcSetForPath(
|
|
521
|
+
* "/image.png",
|
|
522
|
+
* { pixelDensities: [1, 2, 3] },
|
|
523
|
+
* );
|
|
524
|
+
* // => https://example.imgix.net/image.png?dpr=1&s=def3e221c3f4c4debda091b8e49420ea 1x,
|
|
525
|
+
* // https://example.imgix.net/image.png?dpr=2&s=f12c7c39333410c10c2930b57116a943 2x,
|
|
526
|
+
* // https://example.imgix.net/image.png?dpr=3&s=3a975b5087ab7ad2ab91fe66072fd628 3x
|
|
527
|
+
* ```
|
|
528
|
+
*
|
|
529
|
+
* @example
|
|
530
|
+
*
|
|
531
|
+
* ```ts
|
|
532
|
+
* const client = new SecureClient({
|
|
533
|
+
* baseURL: "https://example.imgix.net",
|
|
534
|
+
* secureURLToken: "example-token",
|
|
535
|
+
* });
|
|
536
|
+
* const srcset = client.buildSignedPixelDensitySrcSetForPath(
|
|
537
|
+
* "/image.png",
|
|
538
|
+
* {
|
|
539
|
+
* pixelDensities: [1, 2, 3],
|
|
540
|
+
* sat: -100,
|
|
541
|
+
* },
|
|
542
|
+
* );
|
|
543
|
+
* // => https://example.imgix.net/image.png?dpr=1&sat=-100&s=def3e221c3f4c4debda091b8e49420ea 1x,
|
|
544
|
+
* // https://example.imgix.net/image.png?dpr=2&sat=-100&s=f12c7c39333410c10c2930b57116a943 2x,
|
|
545
|
+
* // https://example.imgix.net/image.png?dpr=3&sat=-100&s=3a975b5087ab7ad2ab91fe66072fd628 3x
|
|
546
|
+
* ```
|
|
547
|
+
*
|
|
548
|
+
* @param path - Path to the image relative to the client's base URL.
|
|
549
|
+
* @param params - An object of Imgix URL API parameters. The `pixelDensities`
|
|
550
|
+
* parameter defines the resulting `srcset` widths.
|
|
551
|
+
*
|
|
552
|
+
* @returns A `srcset` attribute value for `url` with the given Imgix URL API
|
|
553
|
+
* parameters applied.
|
|
554
|
+
*/
|
|
555
|
+
buildSignedPixelDensitySrcSetForPath(
|
|
556
|
+
path: string,
|
|
557
|
+
params: BuildPixelDensitySrcSetParams,
|
|
558
|
+
): string {
|
|
559
|
+
return buildSignedPixelDensitySrcSet(
|
|
560
|
+
`${new URL(path, this.baseURL)}`,
|
|
561
|
+
this.secureURLToken,
|
|
562
|
+
params,
|
|
563
|
+
);
|
|
564
|
+
}
|
|
565
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import type { BuildPixelDensitySrcSetParams } from "../buildPixelDensitySrcSet";
|
|
2
|
+
import { buildSignedURL } from "./buildSignedURL";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Builds an `<img>` `srcset` attribute value for a given set of pixel
|
|
6
|
+
* densities. It can also optinally apply Imgix URL API parameters to the URLs.
|
|
7
|
+
* The URLs are signed by appending a signature to their URL parameters. This
|
|
8
|
+
* locks the URLs and their parameters to the signature to prevent URL tampering.
|
|
9
|
+
*
|
|
10
|
+
* The `dpr` URL parameter will be applied for each `srcset` entry. If a `dpr`
|
|
11
|
+
* parameter is provided to the `params` parameter, it will be ignored.
|
|
12
|
+
*
|
|
13
|
+
* **Important**: This function should only be used in a trusted environment and
|
|
14
|
+
* never in a browser. The `secureURLToken` parameter is a secret and should not
|
|
15
|
+
* be exposed to the public.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
*
|
|
19
|
+
* ```ts
|
|
20
|
+
* const srcset = buildSignedPixelDensitySrcSet(
|
|
21
|
+
* "https://example.imgix.net/image.png",
|
|
22
|
+
* "example-token",
|
|
23
|
+
* { pixelDensities: [1, 2, 3] },
|
|
24
|
+
* );
|
|
25
|
+
* // => https://example.imgix.net/image.png?dpr=1&s=def3e221c3f4c4debda091b8e49420ea 1x,
|
|
26
|
+
* // https://example.imgix.net/image.png?dpr=2&s=f12c7c39333410c10c2930b57116a943 2x,
|
|
27
|
+
* // https://example.imgix.net/image.png?dpr=3&s=3a975b5087ab7ad2ab91fe66072fd628 3x
|
|
28
|
+
* ```
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
*
|
|
32
|
+
* ```ts
|
|
33
|
+
* const srcset = buildSignedPixelDensitySrcSet(
|
|
34
|
+
* "https://example.imgix.net/image.png",
|
|
35
|
+
* "example-token",
|
|
36
|
+
* {
|
|
37
|
+
* pixelDensities: [1, 2, 3],
|
|
38
|
+
* sat: -100,
|
|
39
|
+
* },
|
|
40
|
+
* );
|
|
41
|
+
* // => https://example.imgix.net/image.png?dpr=1&sat=-100&s=def3e221c3f4c4debda091b8e49420ea 1x,
|
|
42
|
+
* // https://example.imgix.net/image.png?dpr=2&sat=-100&s=f12c7c39333410c10c2930b57116a943 2x,
|
|
43
|
+
* // https://example.imgix.net/image.png?dpr=3&sat=-100&s=3a975b5087ab7ad2ab91fe66072fd628 3x
|
|
44
|
+
* ```
|
|
45
|
+
*
|
|
46
|
+
* @param url - Full absolute URL to the Imgix image.
|
|
47
|
+
* @param secureURLToken - The secret secure URL token for the image's Imgix source.
|
|
48
|
+
* @param params - An object of Imgix URL API parameters. The `pixelDensities`
|
|
49
|
+
* parameter defines the resulting `srcset` widths.
|
|
50
|
+
*
|
|
51
|
+
* @returns A `srcset` attribute value for `url` with the given Imgix URL API
|
|
52
|
+
* parameters applied.
|
|
53
|
+
*/
|
|
54
|
+
export const buildSignedPixelDensitySrcSet = (
|
|
55
|
+
url: string,
|
|
56
|
+
secureURLToken: string,
|
|
57
|
+
{ pixelDensities, ...params }: BuildPixelDensitySrcSetParams,
|
|
58
|
+
): string => {
|
|
59
|
+
return pixelDensities
|
|
60
|
+
.map((dpr) => {
|
|
61
|
+
return `${buildSignedURL(url, secureURLToken, {
|
|
62
|
+
...params,
|
|
63
|
+
dpr,
|
|
64
|
+
})} ${dpr}x`;
|
|
65
|
+
})
|
|
66
|
+
.join(", ");
|
|
67
|
+
};
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type { ImgixURLParams } from "../types.generated";
|
|
2
|
+
import { buildURL } from "../buildURL";
|
|
3
|
+
import { signURL } from "./signURL";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Builds a URL to an Imgix image with Imgix URL API parameters. The URL is
|
|
7
|
+
* signed by appending a signature to the URL parameters. This locks the URL and
|
|
8
|
+
* its parameters to the signature to prevent URL tampering.
|
|
9
|
+
*
|
|
10
|
+
* The given URL must be a full absolute URL containing the protocol and domain.
|
|
11
|
+
*
|
|
12
|
+
* URL parameters already applied to the image will be retained. To remove
|
|
13
|
+
* existing parameters, set the parameter to `undefined` in the `params` argument.
|
|
14
|
+
*
|
|
15
|
+
* **Important**: This function should only be used in a trusted environment and
|
|
16
|
+
* never in a browser. The `secureURLToken` parameter is a secret and should not
|
|
17
|
+
* be exposed to the public.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
*
|
|
21
|
+
* ```ts
|
|
22
|
+
* const url = buildSignedURL(
|
|
23
|
+
* "https://example.imgix.net/image.png",
|
|
24
|
+
* "example-token",
|
|
25
|
+
* { width: 400 },
|
|
26
|
+
* );
|
|
27
|
+
* // => https://example.imgix.net/image.png?width=400&s=def3e221c3f4c4debda091b8e49420ea
|
|
28
|
+
* ```
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
*
|
|
32
|
+
* ```ts
|
|
33
|
+
* const url = buildSignedURL(
|
|
34
|
+
* "https://example.imgix.net/image.png?width=400",
|
|
35
|
+
* "example-token",
|
|
36
|
+
* { height: 300 },
|
|
37
|
+
* );
|
|
38
|
+
* // => https://example.imgix.net/image.png?width=400&height=300&s=f12c7c39333410c10c2930b57116a943
|
|
39
|
+
* ```
|
|
40
|
+
*
|
|
41
|
+
* @param url - Full absolute URL to the Imgix image.
|
|
42
|
+
* @param secureURLToken - The secret secure URL token for the image's Imgix source.
|
|
43
|
+
* @param params - An object of Imgix URL API parameters.
|
|
44
|
+
*
|
|
45
|
+
* @returns `url` with the given Imgix URL API parameters applied.
|
|
46
|
+
*
|
|
47
|
+
* @see Imgix URL API reference: https://docs.imgix.com/apis/rendering
|
|
48
|
+
* @see Details on securing Imgix images: https://docs.imgix.com/setup/securing-images
|
|
49
|
+
*/
|
|
50
|
+
export const buildSignedURL = (
|
|
51
|
+
url: string,
|
|
52
|
+
secureURLToken: string,
|
|
53
|
+
params: ImgixURLParams,
|
|
54
|
+
): string => {
|
|
55
|
+
return signURL(buildURL(url, params), secureURLToken);
|
|
56
|
+
};
|