imgix-url-builder 0.0.2 → 0.0.4

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