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.
package/dist/index.js ADDED
@@ -0,0 +1,173 @@
1
+ const camelCaseToParamCase = (input) => {
2
+ return input.replace(/[A-Z]/g, (match) => {
3
+ return `-${match.toLowerCase()}`;
4
+ });
5
+ };
6
+ const buildURL = (url, params) => {
7
+ const instance = new URL(url);
8
+ for (const camelCasedParamKey in params) {
9
+ const paramKey = camelCaseToParamCase(camelCasedParamKey);
10
+ const paramValue = params[camelCasedParamKey];
11
+ if (paramValue === void 0) {
12
+ instance.searchParams.delete(paramKey);
13
+ } else if (Array.isArray(paramValue)) {
14
+ instance.searchParams.set(paramKey, paramValue.join(","));
15
+ } else {
16
+ instance.searchParams.set(paramKey, `${paramValue}`);
17
+ }
18
+ }
19
+ const s = instance.searchParams.get("s");
20
+ if (s) {
21
+ instance.searchParams.delete("s");
22
+ instance.searchParams.append("s", s);
23
+ }
24
+ return instance.toString();
25
+ };
26
+
27
+ const buildPixelDensitySrcSet = (url, { pixelDensities, ...params }) => {
28
+ return pixelDensities.map((dpr) => {
29
+ return `${buildURL(url, { ...params, dpr })} ${dpr}x`;
30
+ }).join(", ");
31
+ };
32
+
33
+ const buildWidthSrcSet = (url, { widths, ...params }) => {
34
+ return widths.map((width) => {
35
+ return `${buildURL(url, { ...params, w: void 0, width })} ${width}w`;
36
+ }).join(", ");
37
+ };
38
+
39
+ class Client {
40
+ /**
41
+ * Creates a new `Client` instance for an Imgix domain.
42
+ *
43
+ * @param options - Options to instantiate a new client.
44
+ *
45
+ * @returns A `Client` instance for the given Imgix domain.
46
+ */
47
+ constructor(options) {
48
+ this.baseURL = options.baseURL;
49
+ }
50
+ /**
51
+ * Builds a URL to an Imgix image with Imgix URL API parameters for the
52
+ * client's base URL.
53
+ *
54
+ * @example
55
+ *
56
+ * ```ts
57
+ * const client = new Client({ baseURL: "https://example.imgix.net" });
58
+ * const url = client.buildURLForPath("/image.png", { width: 400 });
59
+ * // => https://example.imgix.net/image.png?width=400
60
+ * ```
61
+ *
62
+ * @example
63
+ *
64
+ * ```ts
65
+ * const client = new Client({
66
+ * baseURL: "https://example.imgix.net/folder",
67
+ * });
68
+ * const url = client.buildURLForPath("./image.png", { width: 400 });
69
+ * // => https://example.imgix.net/folder/image.png?width=400
70
+ * ```
71
+ *
72
+ * @param path - Path to the image relative to the client's base URL.
73
+ * @param params - An object of Imgix URL API parameters.
74
+ *
75
+ * @returns The full absolute URL to the image with the given Imgix URL API
76
+ * parameters applied.
77
+ */
78
+ buildURLForPath(path, params = {}) {
79
+ return buildURL(`${new URL(path, this.baseURL)}`, params);
80
+ }
81
+ /**
82
+ * Builds an `<img>` `srcset` attribute value for a given set of widths for
83
+ * the client's base URL. It can also optinally apply Imgix URL API parameters
84
+ * to the URLs.
85
+ *
86
+ * The `width` URL parameter will be applied for each `srcset` entry. If a
87
+ * `width` or `w` parameter is provided to the `params` parameter, it will be
88
+ * ignored.
89
+ *
90
+ * @example
91
+ *
92
+ * ```ts
93
+ * const client = new Client({ baseURL: "https://example.imgix.net" });
94
+ * const srcset = client.buildWidthSrcSetForPath("/image.png", {
95
+ * widths: [400, 800, 1600],
96
+ * });
97
+ * // => https://example.imgix.net/image.png?width=400 400w,
98
+ * // https://example.imgix.net/image.png?width=800 800w,
99
+ * // https://example.imgix.net/image.png?width=1600 1600w
100
+ * ```
101
+ *
102
+ * @example
103
+ *
104
+ * ```ts
105
+ * const client = new Client({
106
+ * baseURL: "https://example.imgix.net",
107
+ * });
108
+ * const srcset = client.buildWidthSrcSetForPath("/image.png", {
109
+ * widths: [400, 800, 1600],
110
+ * sat: -100,
111
+ * });
112
+ * // => https://example.imgix.net/image.png?width=400&sat=-100 400w,
113
+ * // https://example.imgix.net/image.png?width=800&sat=-100 800w,
114
+ * // https://example.imgix.net/image.png?width=1600&sat=-100 1600w
115
+ * ```
116
+ *
117
+ * @param path - Path to the image relative to the client's base URL.
118
+ * @param params - An object of Imgix URL API parameters. The `widths`
119
+ * parameter defines the resulting `srcset` widths.
120
+ *
121
+ * @returns A `srcset` attribute value for `url` with the given Imgix URL API
122
+ * parameters applied.
123
+ */
124
+ buildWidthSrcSetForPath(path, params) {
125
+ return buildWidthSrcSet(`${new URL(path, this.baseURL)}`, params);
126
+ }
127
+ /**
128
+ * Builds an `<img>` `srcset` attribute value for a given set of pixel
129
+ * densities for the client's base URL. It can also optinally apply Imgix URL
130
+ * API parameters to the URLs.
131
+ *
132
+ * The `dpr` URL parameter will be applied for each `srcset` entry. If a `dpr`
133
+ * parameter is provided to the `params` parameter, it will be ignored.
134
+ *
135
+ * @example
136
+ *
137
+ * ```ts
138
+ * const client = new Client({ baseURL: "https://example.imgix.net" });
139
+ * const srcset = client.buildPixelDensitySrcSetForPath("/image.png", {
140
+ * pixelDensities: [1, 2, 3],
141
+ * });
142
+ * // => https://example.imgix.net/image.png?dpr=1 1x,
143
+ * // https://example.imgix.net/image.png?dpr=2 2x,
144
+ * // https://example.imgix.net/image.png?dpr=3 3x
145
+ * ```
146
+ *
147
+ * @example
148
+ *
149
+ * ```ts
150
+ * const client = new Client({ baseURL: "https://example.imgix.net" });
151
+ * const srcset = client.buildPixelDensitySrcSetForPath("/image.png", {
152
+ * pixelDensities: [1, 2, 3],
153
+ * sat: -100,
154
+ * });
155
+ * // => https://example.imgix.net/image.png?dpr=1&sat=-100 1x,
156
+ * // https://example.imgix.net/image.png?dpr=2&sat=-100 2x,
157
+ * // https://example.imgix.net/image.png?dpr=3&sat=-100 3x
158
+ * ```
159
+ *
160
+ * @param path - Path to the image relative to the client's base URL.
161
+ * @param params - An object of Imgix URL API parameters. The `pixelDensities`
162
+ * parameter defines the resulting `srcset` widths.
163
+ *
164
+ * @returns A `srcset` attribute value for `url` with the given Imgix URL API
165
+ * parameters applied.
166
+ */
167
+ buildPixelDensitySrcSetForPath(path, params) {
168
+ return buildPixelDensitySrcSet(`${new URL(path, this.baseURL)}`, params);
169
+ }
170
+ }
171
+
172
+ export { Client, buildPixelDensitySrcSet, buildURL, buildWidthSrcSet };
173
+ //# sourceMappingURL=index.mjs.map
package/dist/index.mjs CHANGED
@@ -37,15 +37,133 @@ const buildWidthSrcSet = (url, { widths, ...params }) => {
37
37
  };
38
38
 
39
39
  class Client {
40
+ /**
41
+ * Creates a new `Client` instance for an Imgix domain.
42
+ *
43
+ * @param options - Options to instantiate a new client.
44
+ *
45
+ * @returns A `Client` instance for the given Imgix domain.
46
+ */
40
47
  constructor(options) {
41
48
  this.baseURL = options.baseURL;
42
49
  }
50
+ /**
51
+ * Builds a URL to an Imgix image with Imgix URL API parameters for the
52
+ * client's base URL.
53
+ *
54
+ * @example
55
+ *
56
+ * ```ts
57
+ * const client = new Client({ baseURL: "https://example.imgix.net" });
58
+ * const url = client.buildURLForPath("/image.png", { width: 400 });
59
+ * // => https://example.imgix.net/image.png?width=400
60
+ * ```
61
+ *
62
+ * @example
63
+ *
64
+ * ```ts
65
+ * const client = new Client({
66
+ * baseURL: "https://example.imgix.net/folder",
67
+ * });
68
+ * const url = client.buildURLForPath("./image.png", { width: 400 });
69
+ * // => https://example.imgix.net/folder/image.png?width=400
70
+ * ```
71
+ *
72
+ * @param path - Path to the image relative to the client's base URL.
73
+ * @param params - An object of Imgix URL API parameters.
74
+ *
75
+ * @returns The full absolute URL to the image with the given Imgix URL API
76
+ * parameters applied.
77
+ */
43
78
  buildURLForPath(path, params = {}) {
44
79
  return buildURL(`${new URL(path, this.baseURL)}`, params);
45
80
  }
81
+ /**
82
+ * Builds an `<img>` `srcset` attribute value for a given set of widths for
83
+ * the client's base URL. It can also optinally apply Imgix URL API parameters
84
+ * to the URLs.
85
+ *
86
+ * The `width` URL parameter will be applied for each `srcset` entry. If a
87
+ * `width` or `w` parameter is provided to the `params` parameter, it will be
88
+ * ignored.
89
+ *
90
+ * @example
91
+ *
92
+ * ```ts
93
+ * const client = new Client({ baseURL: "https://example.imgix.net" });
94
+ * const srcset = client.buildWidthSrcSetForPath("/image.png", {
95
+ * widths: [400, 800, 1600],
96
+ * });
97
+ * // => https://example.imgix.net/image.png?width=400 400w,
98
+ * // https://example.imgix.net/image.png?width=800 800w,
99
+ * // https://example.imgix.net/image.png?width=1600 1600w
100
+ * ```
101
+ *
102
+ * @example
103
+ *
104
+ * ```ts
105
+ * const client = new Client({
106
+ * baseURL: "https://example.imgix.net",
107
+ * });
108
+ * const srcset = client.buildWidthSrcSetForPath("/image.png", {
109
+ * widths: [400, 800, 1600],
110
+ * sat: -100,
111
+ * });
112
+ * // => https://example.imgix.net/image.png?width=400&sat=-100 400w,
113
+ * // https://example.imgix.net/image.png?width=800&sat=-100 800w,
114
+ * // https://example.imgix.net/image.png?width=1600&sat=-100 1600w
115
+ * ```
116
+ *
117
+ * @param path - Path to the image relative to the client's base URL.
118
+ * @param params - An object of Imgix URL API parameters. The `widths`
119
+ * parameter defines the resulting `srcset` widths.
120
+ *
121
+ * @returns A `srcset` attribute value for `url` with the given Imgix URL API
122
+ * parameters applied.
123
+ */
46
124
  buildWidthSrcSetForPath(path, params) {
47
125
  return buildWidthSrcSet(`${new URL(path, this.baseURL)}`, params);
48
126
  }
127
+ /**
128
+ * Builds an `<img>` `srcset` attribute value for a given set of pixel
129
+ * densities for the client's base URL. It can also optinally apply Imgix URL
130
+ * API parameters to the URLs.
131
+ *
132
+ * The `dpr` URL parameter will be applied for each `srcset` entry. If a `dpr`
133
+ * parameter is provided to the `params` parameter, it will be ignored.
134
+ *
135
+ * @example
136
+ *
137
+ * ```ts
138
+ * const client = new Client({ baseURL: "https://example.imgix.net" });
139
+ * const srcset = client.buildPixelDensitySrcSetForPath("/image.png", {
140
+ * pixelDensities: [1, 2, 3],
141
+ * });
142
+ * // => https://example.imgix.net/image.png?dpr=1 1x,
143
+ * // https://example.imgix.net/image.png?dpr=2 2x,
144
+ * // https://example.imgix.net/image.png?dpr=3 3x
145
+ * ```
146
+ *
147
+ * @example
148
+ *
149
+ * ```ts
150
+ * const client = new Client({ baseURL: "https://example.imgix.net" });
151
+ * const srcset = client.buildPixelDensitySrcSetForPath("/image.png", {
152
+ * pixelDensities: [1, 2, 3],
153
+ * sat: -100,
154
+ * });
155
+ * // => https://example.imgix.net/image.png?dpr=1&sat=-100 1x,
156
+ * // https://example.imgix.net/image.png?dpr=2&sat=-100 2x,
157
+ * // https://example.imgix.net/image.png?dpr=3&sat=-100 3x
158
+ * ```
159
+ *
160
+ * @param path - Path to the image relative to the client's base URL.
161
+ * @param params - An object of Imgix URL API parameters. The `pixelDensities`
162
+ * parameter defines the resulting `srcset` widths.
163
+ *
164
+ * @returns A `srcset` attribute value for `url` with the given Imgix URL API
165
+ * parameters applied.
166
+ */
49
167
  buildPixelDensitySrcSetForPath(path, params) {
50
168
  return buildPixelDensitySrcSet(`${new URL(path, this.baseURL)}`, params);
51
169
  }
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","sources":["../src/buildURL.ts","../src/buildPixelDensitySrcSet.ts","../src/buildWidthSrcSet.ts","../src/Client.ts"],"sourcesContent":["import type { ImgixURLParams } from \"./types.generated\";\n\n/**\n * Converts a camel case string to a param case string.\n *\n * @example\n *\n * ```ts\n * camelCaseToParamCase(\"fooBar\");\n * // => 'foo-bar'\n * ```\n *\n * @param input - Camel case string to convert.\n *\n * @returns Param case version of `input`.\n */\nconst camelCaseToParamCase = (input: string): string => {\n\treturn input.replace(/[A-Z]/g, (match) => {\n\t\treturn `-${match.toLowerCase()}`;\n\t});\n};\n\n/**\n * Builds a URL to an Imgix image with Imgix URL API parameters.\n *\n * The given URL must be a full absolute URL containing the protocol and domain.\n *\n * URL parameters already applied to the image will be retained. To remove\n * existing parameters, set the parameter to `undefined` in the `params` argument.\n *\n * @example\n *\n * ```ts\n * const url = buildURL(\"https://example.imgix.net/image.png\", {\n * \twidth: 400,\n * });\n * // => https://example.imgix.net/image.png?width=400\n * ```\n *\n * @example\n *\n * ```ts\n * const url = buildURL(\"https://example.imgix.net/image.png?width=400\", {\n * \theight: 300,\n * });\n * // => https://example.imgix.net/image.png?width=400&height=300\n * ```\n *\n * @param url - Full absolute URL to the Imgix image.\n * @param params - An object of Imgix URL API parameters.\n *\n * @returns `url` with the given Imgix URL API parameters applied.\n *\n * @see Imgix URL API reference: https://docs.imgix.com/apis/rendering\n */\nexport const buildURL = (url: string, params: ImgixURLParams): string => {\n\tconst instance = new URL(url);\n\n\tfor (const camelCasedParamKey in params) {\n\t\tconst paramKey = camelCaseToParamCase(camelCasedParamKey);\n\t\tconst paramValue = params[camelCasedParamKey as keyof typeof params];\n\n\t\tif (paramValue === undefined) {\n\t\t\tinstance.searchParams.delete(paramKey);\n\t\t} else if (Array.isArray(paramValue)) {\n\t\t\tinstance.searchParams.set(paramKey, paramValue.join(\",\"));\n\t\t} else {\n\t\t\tinstance.searchParams.set(paramKey, `${paramValue}`);\n\t\t}\n\t}\n\n\t// Ensure the `s` parameter is the last parameter, if it exists.\n\t// @see https://github.com/imgix/imgix-blueprint#securing-urls\n\tconst s = instance.searchParams.get(\"s\");\n\tif (s) {\n\t\tinstance.searchParams.delete(\"s\");\n\t\tinstance.searchParams.append(\"s\", s);\n\t}\n\n\treturn instance.toString();\n};\n","import type { ImgixURLParams } from \"./types.generated\";\nimport { buildURL } from \"./buildURL\";\n\n/**\n * Parameters for `buildPixelDensitySrcSet`.\n */\nexport type BuildPixelDensitySrcSetParams = Omit<ImgixURLParams, \"dpr\"> & {\n\t/**\n\t * The pixel densities to include in the resulting `srcset` value.\n\t *\n\t * @example\n\t *\n\t * ```ts\n\t * [1, 2, 3];\n\t * ```\n\t */\n\tpixelDensities: number[];\n};\n\n/**\n * Builds an `<img>` `srcset` attribute value for a given set of pixel\n * densities. It can also optinally apply Imgix URL API parameters to the URLs.\n *\n * The `dpr` URL parameter will be applied for each `srcset` entry. If a `dpr`\n * parameter is provided to the `params` parameter, it will be ignored.\n *\n * @example\n *\n * ```ts\n * const srcset = buildPixelDensitySrcSet(\n * \t\"https://example.imgix.net/image.png\",\n * \t{ pixelDensities: [1, 2, 3] },\n * );\n * // => https://example.imgix.net/image.png?dpr=1 1x,\n * // https://example.imgix.net/image.png?dpr=2 2x,\n * // https://example.imgix.net/image.png?dpr=3 3x\n * ```\n *\n * @example\n *\n * ```ts\n * const srcset = buildPixelDensitySrcSet(\n * \t\"https://example.imgix.net/image.png\",\n * \t{\n * \t\tpixelDensities: [1, 2, 3],\n * \t\tsat: -100,\n * \t},\n * );\n * // => https://example.imgix.net/image.png?dpr=1&sat=-100 1x,\n * // https://example.imgix.net/image.png?dpr=2&sat=-100 2x,\n * // https://example.imgix.net/image.png?dpr=3&sat=-100 3x\n * ```\n *\n * @param url - Full absolute URL to the Imgix image.\n * @param params - An object of Imgix URL API parameters. The `pixelDensities`\n * parameter defines the resulting `srcset` widths.\n *\n * @returns A `srcset` attribute value for `url` with the given Imgix URL API\n * parameters applied.\n */\nexport const buildPixelDensitySrcSet = (\n\turl: string,\n\t{ pixelDensities, ...params }: BuildPixelDensitySrcSetParams,\n): string => {\n\treturn pixelDensities\n\t\t.map((dpr) => {\n\t\t\treturn `${buildURL(url, { ...params, dpr })} ${dpr}x`;\n\t\t})\n\t\t.join(\", \");\n};\n","import type { ImgixURLParams } from \"./types.generated\";\nimport { buildURL } from \"./buildURL\";\n\n/**\n * Parameters for `buildWidthSrcSet`.\n */\nexport type BuildWidthSrcSetParams = Omit<ImgixURLParams, \"width\" | \"w\"> & {\n\t/**\n\t * The pixel widths to include in the resulting `srcset` value.\n\t *\n\t * @example\n\t *\n\t * ```ts\n\t * [400, 800, 1600];\n\t * ```\n\t */\n\twidths: number[];\n};\n\n/**\n * Builds an `<img>` `srcset` attribute value for a given set of widths. It can\n * also optinally apply Imgix URL API parameters to the URLs.\n *\n * The `width` URL parameter will be applied for each `srcset` entry. If a\n * `width` or `w` parameter is provided to the `params` parameter, it will be ignored.\n *\n * @example\n *\n * ```ts\n * const srcset = buildWidthSrcSet(\"https://example.imgix.net/image.png\", {\n * \twidths: [400, 800, 1600],\n * });\n * // => https://example.imgix.net/image.png?width=400 400w,\n * // https://example.imgix.net/image.png?width=800 800w,\n * // https://example.imgix.net/image.png?width=1600 1600w\n * ```\n *\n * @example\n *\n * ```ts\n * const srcset = buildWidthSrcSet(\"https://example.imgix.net/image.png\", {\n * \twidths: [400, 800, 1600],\n * \tsat: -100,\n * });\n * // => https://example.imgix.net/image.png?width=400&sat=-100 400w,\n * // https://example.imgix.net/image.png?width=800&sat=-100 800w,\n * // https://example.imgix.net/image.png?width=1600&sat=-100 1600w\n * ```\n *\n * @param url - Full absolute URL to the Imgix image.\n * @param params - An object of Imgix URL API parameters. The `widths` parameter\n * defines the resulting `srcset` widths.\n *\n * @returns A `srcset` attribute value for `url` with the given Imgix URL API\n * parameters applied.\n */\nexport const buildWidthSrcSet = (\n\turl: string,\n\t{ widths, ...params }: BuildWidthSrcSetParams,\n): string => {\n\treturn widths\n\t\t.map((width) => {\n\t\t\treturn `${buildURL(url, { ...params, w: undefined, width })} ${width}w`;\n\t\t})\n\t\t.join(\", \");\n};\n","import type { BuildPixelDensitySrcSetParams } from \"./buildPixelDensitySrcSet\";\nimport type { BuildWidthSrcSetParams } from \"./buildWidthSrcSet\";\nimport type { ImgixURLParams } from \"./types.generated\";\nimport { buildPixelDensitySrcSet } from \"./buildPixelDensitySrcSet\";\nimport { buildURL } from \"./buildURL\";\nimport { buildWidthSrcSet } from \"./buildWidthSrcSet\";\n\n/**\n * Options to instantiate a new client.\n */\nexport type ClientOptions = {\n\t/**\n\t * The base URL used to construct image URLs from a path. The base URL must\n\t * include the protocol, domain, and optionally a path.\n\t *\n\t * @example `https://example.imgix.net`\n\t *\n\t * @example `https://example.imgix.net/folder`\n\t */\n\tbaseURL: string;\n};\n\n/**\n * An Imgix Rendering API client. A client is paired to a single Imgix domain.\n */\nexport class Client {\n\tbaseURL: string;\n\n\t/**\n\t * Creates a new `Client` instance for an Imgix domain.\n\t *\n\t * @param options - Options to instantiate a new client.\n\t *\n\t * @returns A `Client` instance for the given Imgix domain.\n\t */\n\tconstructor(options: ClientOptions) {\n\t\tthis.baseURL = options.baseURL;\n\t}\n\n\t/**\n\t * Builds a URL to an Imgix image with Imgix URL API parameters for the\n\t * client's base URL.\n\t *\n\t * @example\n\t *\n\t * ```ts\n\t * const client = new Client({ baseURL: \"https://example.imgix.net\" });\n\t * const url = client.buildURLForPath(\"/image.png\", { width: 400 });\n\t * // => https://example.imgix.net/image.png?width=400\n\t * ```\n\t *\n\t * @example\n\t *\n\t * ```ts\n\t * const client = new Client({\n\t * \tbaseURL: \"https://example.imgix.net/folder\",\n\t * });\n\t * const url = client.buildURLForPath(\"./image.png\", { width: 400 });\n\t * // => https://example.imgix.net/folder/image.png?width=400\n\t * ```\n\t *\n\t * @param path - Path to the image relative to the client's base URL.\n\t * @param params - An object of Imgix URL API parameters.\n\t *\n\t * @returns The full absolute URL to the image with the given Imgix URL API\n\t * parameters applied.\n\t */\n\tbuildURLForPath(path: string, params: ImgixURLParams = {}) {\n\t\treturn buildURL(`${new URL(path, this.baseURL)}`, params);\n\t}\n\n\t/**\n\t * Builds an `<img>` `srcset` attribute value for a given set of widths for\n\t * the client's base URL. It can also optinally apply Imgix URL API parameters\n\t * to the URLs.\n\t *\n\t * The `width` URL parameter will be applied for each `srcset` entry. If a\n\t * `width` or `w` parameter is provided to the `params` parameter, it will be ignored.\n\t *\n\t * @example\n\t *\n\t * ```ts\n\t * const client = new Client({ baseURL: \"https://example.imgix.net\" });\n\t * const srcset = client.buildWidthSrcSetForPath(\"/image.png\", {\n\t * \twidths: [400, 800, 1600],\n\t * });\n\t * // => https://example.imgix.net/image.png?width=400 400w,\n\t * // https://example.imgix.net/image.png?width=800 800w,\n\t * // https://example.imgix.net/image.png?width=1600 1600w\n\t * ```\n\t *\n\t * @example\n\t *\n\t * ```ts\n\t * const client = new Client({\n\t * \tbaseURL: \"https://example.imgix.net\",\n\t * });\n\t * const srcset = client.buildWidthSrcSetForPath(\"/image.png\", {\n\t * \twidths: [400, 800, 1600],\n\t * \tsat: -100,\n\t * });\n\t * // => https://example.imgix.net/image.png?width=400&sat=-100 400w,\n\t * // https://example.imgix.net/image.png?width=800&sat=-100 800w,\n\t * // https://example.imgix.net/image.png?width=1600&sat=-100 1600w\n\t * ```\n\t *\n\t * @param path - Path to the image relative to the client's base URL.\n\t * @param params - An object of Imgix URL API parameters. The `widths`\n\t * parameter defines the resulting `srcset` widths.\n\t *\n\t * @returns A `srcset` attribute value for `url` with the given Imgix URL API\n\t * parameters applied.\n\t */\n\tbuildWidthSrcSetForPath(path: string, params: BuildWidthSrcSetParams) {\n\t\treturn buildWidthSrcSet(`${new URL(path, this.baseURL)}`, params);\n\t}\n\n\t/**\n\t * Builds an `<img>` `srcset` attribute value for a given set of pixel\n\t * densities for the client's base URL. It can also optinally apply Imgix URL\n\t * API parameters to the URLs.\n\t *\n\t * The `dpr` URL parameter will be applied for each `srcset` entry. If a `dpr`\n\t * parameter is provided to the `params` parameter, it will be ignored.\n\t *\n\t * @example\n\t *\n\t * ```ts\n\t * const client = new Client({ baseURL: \"https://example.imgix.net\" });\n\t * const srcset = client.buildPixelDensitySrcSetForPath(\"/image.png\", {\n\t * \tpixelDensities: [1, 2, 3],\n\t * });\n\t * // => https://example.imgix.net/image.png?dpr=1 1x,\n\t * // https://example.imgix.net/image.png?dpr=2 2x,\n\t * // https://example.imgix.net/image.png?dpr=3 3x\n\t * ```\n\t *\n\t * @example\n\t *\n\t * ```ts\n\t * const client = new Client({ baseURL: \"https://example.imgix.net\" });\n\t * const srcset = client.buildPixelDensitySrcSetForPath(\"/image.png\", {\n\t * \tpixelDensities: [1, 2, 3],\n\t * \tsat: -100,\n\t * });\n\t * // => https://example.imgix.net/image.png?dpr=1&sat=-100 1x,\n\t * // https://example.imgix.net/image.png?dpr=2&sat=-100 2x,\n\t * // https://example.imgix.net/image.png?dpr=3&sat=-100 3x\n\t * ```\n\t *\n\t * @param path - Path to the image relative to the client's base URL.\n\t * @param params - An object of Imgix URL API parameters. The `pixelDensities`\n\t * parameter defines the resulting `srcset` widths.\n\t *\n\t * @returns A `srcset` attribute value for `url` with the given Imgix URL API\n\t * parameters applied.\n\t */\n\tbuildPixelDensitySrcSetForPath(\n\t\tpath: string,\n\t\tparams: BuildPixelDensitySrcSetParams,\n\t) {\n\t\treturn buildPixelDensitySrcSet(`${new URL(path, this.baseURL)}`, params);\n\t}\n}\n"],"names":[],"mappings":"AAgBA,MAAM,uBAAuB,CAAC,UAA0B;AACvD,SAAO,MAAM,QAAQ,UAAU,CAAC,UAAU;AACzC,WAAO,IAAI,MAAM;AAAA;AAAA;MAqCN,WAAW,CAAC,KAAa,WAAmC;AACxE,QAAM,WAAW,IAAI,IAAI;AAEzB,aAAW,sBAAsB,QAAQ;AACxC,UAAM,WAAW,qBAAqB;AACtC,UAAM,aAAa,OAAO;AAE1B,QAAI,eAAe,QAAW;AAC7B,eAAS,aAAa,OAAO;AAAA,eACnB,MAAM,QAAQ,aAAa;AACrC,eAAS,aAAa,IAAI,UAAU,WAAW,KAAK;AAAA,WAC9C;AACN,eAAS,aAAa,IAAI,UAAU,GAAG;AAAA;AAAA;AAMzC,QAAM,IAAI,SAAS,aAAa,IAAI;AACpC,MAAI,GAAG;AACN,aAAS,aAAa,OAAO;AAC7B,aAAS,aAAa,OAAO,KAAK;AAAA;AAGnC,SAAO,SAAS;AAAA;;MCnBJ,0BAA0B,CACtC,KACA,EAAE,mBAAmB,aACT;AACZ,SAAO,eACL,IAAI,CAAC,QAAQ;AACb,WAAO,GAAG,SAAS,KAAK,KAAK,QAAQ,UAAU;AAAA,KAE/C,KAAK;AAAA;;MCZK,mBAAmB,CAC/B,KACA,EAAE,WAAW,aACD;AACZ,SAAO,OACL,IAAI,CAAC,UAAU;AACf,WAAO,GAAG,SAAS,KAAK,KAAK,QAAQ,GAAG,QAAW,YAAY;AAAA,KAE/D,KAAK;AAAA;;aCvCY;AAAA,EAUnB,YAAY,SAAwB;AACnC,SAAK,UAAU,QAAQ;AAAA;AAAA,EA+BxB,gBAAgB,MAAc,SAAyB,IAAI;AAC1D,WAAO,SAAS,GAAG,IAAI,IAAI,MAAM,KAAK,YAAY;AAAA;AAAA,EA6CnD,wBAAwB,MAAc,QAAgC;AACrE,WAAO,iBAAiB,GAAG,IAAI,IAAI,MAAM,KAAK,YAAY;AAAA;AAAA,EA2C3D,+BACC,MACA,QACC;AACD,WAAO,wBAAwB,GAAG,IAAI,IAAI,MAAM,KAAK,YAAY;AAAA;AAAA;;;;"}
1
+ {"version":3,"file":"index.mjs","sources":["../src/buildURL.ts","../src/buildPixelDensitySrcSet.ts","../src/buildWidthSrcSet.ts","../src/Client.ts"],"sourcesContent":["import type { ImgixURLParams } from \"./types.generated\";\n\n/**\n * Converts a camel case string to a param case string.\n *\n * @example\n *\n * ```ts\n * camelCaseToParamCase(\"fooBar\");\n * // => 'foo-bar'\n * ```\n *\n * @param input - Camel case string to convert.\n *\n * @returns Param case version of `input`.\n */\nconst camelCaseToParamCase = (input: string): string => {\n\treturn input.replace(/[A-Z]/g, (match) => {\n\t\treturn `-${match.toLowerCase()}`;\n\t});\n};\n\n/**\n * Builds a URL to an Imgix image with Imgix URL API parameters.\n *\n * The given URL must be a full absolute URL containing the protocol and domain.\n *\n * URL parameters already applied to the image will be retained. To remove\n * existing parameters, set the parameter to `undefined` in the `params`\n * argument.\n *\n * @example\n *\n * ```ts\n * const url = buildURL(\"https://example.imgix.net/image.png\", {\n * \twidth: 400,\n * });\n * // => https://example.imgix.net/image.png?width=400\n * ```\n *\n * @example\n *\n * ```ts\n * const url = buildURL(\"https://example.imgix.net/image.png?width=400\", {\n * \theight: 300,\n * });\n * // => https://example.imgix.net/image.png?width=400&height=300\n * ```\n *\n * @param url - Full absolute URL to the Imgix image.\n * @param params - An object of Imgix URL API parameters.\n *\n * @returns `url` with the given Imgix URL API parameters applied.\n *\n * @see Imgix URL API reference: https://docs.imgix.com/apis/rendering\n */\nexport const buildURL = (url: string, params: ImgixURLParams): string => {\n\tconst instance = new URL(url);\n\n\tfor (const camelCasedParamKey in params) {\n\t\tconst paramKey = camelCaseToParamCase(camelCasedParamKey);\n\t\tconst paramValue = params[camelCasedParamKey as keyof typeof params];\n\n\t\tif (paramValue === undefined) {\n\t\t\tinstance.searchParams.delete(paramKey);\n\t\t} else if (Array.isArray(paramValue)) {\n\t\t\tinstance.searchParams.set(paramKey, paramValue.join(\",\"));\n\t\t} else {\n\t\t\tinstance.searchParams.set(paramKey, `${paramValue}`);\n\t\t}\n\t}\n\n\t// Ensure the `s` parameter is the last parameter, if it exists.\n\t// @see https://github.com/imgix/imgix-blueprint#securing-urls\n\tconst s = instance.searchParams.get(\"s\");\n\tif (s) {\n\t\tinstance.searchParams.delete(\"s\");\n\t\tinstance.searchParams.append(\"s\", s);\n\t}\n\n\treturn instance.toString();\n};\n","import type { ImgixURLParams } from \"./types.generated\";\nimport { buildURL } from \"./buildURL\";\n\n/**\n * Parameters for `buildPixelDensitySrcSet`.\n */\nexport type BuildPixelDensitySrcSetParams = Omit<ImgixURLParams, \"dpr\"> & {\n\t/**\n\t * The pixel densities to include in the resulting `srcset` value.\n\t *\n\t * @example\n\t *\n\t * ```ts\n\t * [1, 2, 3];\n\t * ```\n\t */\n\tpixelDensities: number[];\n};\n\n/**\n * Builds an `<img>` `srcset` attribute value for a given set of pixel\n * densities. It can also optinally apply Imgix URL API parameters to the URLs.\n *\n * The `dpr` URL parameter will be applied for each `srcset` entry. If a `dpr`\n * parameter is provided to the `params` parameter, it will be ignored.\n *\n * @example\n *\n * ```ts\n * const srcset = buildPixelDensitySrcSet(\n * \t\"https://example.imgix.net/image.png\",\n * \t{ pixelDensities: [1, 2, 3] },\n * );\n * // => https://example.imgix.net/image.png?dpr=1 1x,\n * // https://example.imgix.net/image.png?dpr=2 2x,\n * // https://example.imgix.net/image.png?dpr=3 3x\n * ```\n *\n * @example\n *\n * ```ts\n * const srcset = buildPixelDensitySrcSet(\n * \t\"https://example.imgix.net/image.png\",\n * \t{\n * \t\tpixelDensities: [1, 2, 3],\n * \t\tsat: -100,\n * \t},\n * );\n * // => https://example.imgix.net/image.png?dpr=1&sat=-100 1x,\n * // https://example.imgix.net/image.png?dpr=2&sat=-100 2x,\n * // https://example.imgix.net/image.png?dpr=3&sat=-100 3x\n * ```\n *\n * @param url - Full absolute URL to the Imgix image.\n * @param params - An object of Imgix URL API parameters. The `pixelDensities`\n * parameter defines the resulting `srcset` widths.\n *\n * @returns A `srcset` attribute value for `url` with the given Imgix URL API\n * parameters applied.\n */\nexport const buildPixelDensitySrcSet = (\n\turl: string,\n\t{ pixelDensities, ...params }: BuildPixelDensitySrcSetParams,\n): string => {\n\treturn pixelDensities\n\t\t.map((dpr) => {\n\t\t\treturn `${buildURL(url, { ...params, dpr })} ${dpr}x`;\n\t\t})\n\t\t.join(\", \");\n};\n","import type { ImgixURLParams } from \"./types.generated\";\nimport { buildURL } from \"./buildURL\";\n\n/**\n * Parameters for `buildWidthSrcSet`.\n */\nexport type BuildWidthSrcSetParams = Omit<ImgixURLParams, \"width\" | \"w\"> & {\n\t/**\n\t * The pixel widths to include in the resulting `srcset` value.\n\t *\n\t * @example\n\t *\n\t * ```ts\n\t * [400, 800, 1600];\n\t * ```\n\t */\n\twidths: number[];\n};\n\n/**\n * Builds an `<img>` `srcset` attribute value for a given set of widths. It can\n * also optinally apply Imgix URL API parameters to the URLs.\n *\n * The `width` URL parameter will be applied for each `srcset` entry. If a\n * `width` or `w` parameter is provided to the `params` parameter, it will be\n * ignored.\n *\n * @example\n *\n * ```ts\n * const srcset = buildWidthSrcSet(\"https://example.imgix.net/image.png\", {\n * \twidths: [400, 800, 1600],\n * });\n * // => https://example.imgix.net/image.png?width=400 400w,\n * // https://example.imgix.net/image.png?width=800 800w,\n * // https://example.imgix.net/image.png?width=1600 1600w\n * ```\n *\n * @example\n *\n * ```ts\n * const srcset = buildWidthSrcSet(\"https://example.imgix.net/image.png\", {\n * \twidths: [400, 800, 1600],\n * \tsat: -100,\n * });\n * // => https://example.imgix.net/image.png?width=400&sat=-100 400w,\n * // https://example.imgix.net/image.png?width=800&sat=-100 800w,\n * // https://example.imgix.net/image.png?width=1600&sat=-100 1600w\n * ```\n *\n * @param url - Full absolute URL to the Imgix image.\n * @param params - An object of Imgix URL API parameters. The `widths` parameter\n * defines the resulting `srcset` widths.\n *\n * @returns A `srcset` attribute value for `url` with the given Imgix URL API\n * parameters applied.\n */\nexport const buildWidthSrcSet = (\n\turl: string,\n\t{ widths, ...params }: BuildWidthSrcSetParams,\n): string => {\n\treturn widths\n\t\t.map((width) => {\n\t\t\treturn `${buildURL(url, { ...params, w: undefined, width })} ${width}w`;\n\t\t})\n\t\t.join(\", \");\n};\n","import type { BuildPixelDensitySrcSetParams } from \"./buildPixelDensitySrcSet\";\nimport type { BuildWidthSrcSetParams } from \"./buildWidthSrcSet\";\nimport type { ImgixURLParams } from \"./types.generated\";\nimport { buildPixelDensitySrcSet } from \"./buildPixelDensitySrcSet\";\nimport { buildURL } from \"./buildURL\";\nimport { buildWidthSrcSet } from \"./buildWidthSrcSet\";\n\n/**\n * Options to instantiate a new client.\n */\nexport type ClientOptions = {\n\t/**\n\t * The base URL used to construct image URLs from a path. The base URL must\n\t * include the protocol, domain, and optionally a path.\n\t *\n\t * @example `https://example.imgix.net`\n\t *\n\t * @example `https://example.imgix.net/folder`\n\t */\n\tbaseURL: string;\n};\n\n/**\n * An Imgix Rendering API client. A client is paired to a single Imgix domain.\n */\nexport class Client {\n\tbaseURL: string;\n\n\t/**\n\t * Creates a new `Client` instance for an Imgix domain.\n\t *\n\t * @param options - Options to instantiate a new client.\n\t *\n\t * @returns A `Client` instance for the given Imgix domain.\n\t */\n\tconstructor(options: ClientOptions) {\n\t\tthis.baseURL = options.baseURL;\n\t}\n\n\t/**\n\t * Builds a URL to an Imgix image with Imgix URL API parameters for the\n\t * client's base URL.\n\t *\n\t * @example\n\t *\n\t * ```ts\n\t * const client = new Client({ baseURL: \"https://example.imgix.net\" });\n\t * const url = client.buildURLForPath(\"/image.png\", { width: 400 });\n\t * // => https://example.imgix.net/image.png?width=400\n\t * ```\n\t *\n\t * @example\n\t *\n\t * ```ts\n\t * const client = new Client({\n\t * \tbaseURL: \"https://example.imgix.net/folder\",\n\t * });\n\t * const url = client.buildURLForPath(\"./image.png\", { width: 400 });\n\t * // => https://example.imgix.net/folder/image.png?width=400\n\t * ```\n\t *\n\t * @param path - Path to the image relative to the client's base URL.\n\t * @param params - An object of Imgix URL API parameters.\n\t *\n\t * @returns The full absolute URL to the image with the given Imgix URL API\n\t * parameters applied.\n\t */\n\tbuildURLForPath(path: string, params: ImgixURLParams = {}) {\n\t\treturn buildURL(`${new URL(path, this.baseURL)}`, params);\n\t}\n\n\t/**\n\t * Builds an `<img>` `srcset` attribute value for a given set of widths for\n\t * the client's base URL. It can also optinally apply Imgix URL API parameters\n\t * to the URLs.\n\t *\n\t * The `width` URL parameter will be applied for each `srcset` entry. If a\n\t * `width` or `w` parameter is provided to the `params` parameter, it will be\n\t * ignored.\n\t *\n\t * @example\n\t *\n\t * ```ts\n\t * const client = new Client({ baseURL: \"https://example.imgix.net\" });\n\t * const srcset = client.buildWidthSrcSetForPath(\"/image.png\", {\n\t * \twidths: [400, 800, 1600],\n\t * });\n\t * // => https://example.imgix.net/image.png?width=400 400w,\n\t * // https://example.imgix.net/image.png?width=800 800w,\n\t * // https://example.imgix.net/image.png?width=1600 1600w\n\t * ```\n\t *\n\t * @example\n\t *\n\t * ```ts\n\t * const client = new Client({\n\t * \tbaseURL: \"https://example.imgix.net\",\n\t * });\n\t * const srcset = client.buildWidthSrcSetForPath(\"/image.png\", {\n\t * \twidths: [400, 800, 1600],\n\t * \tsat: -100,\n\t * });\n\t * // => https://example.imgix.net/image.png?width=400&sat=-100 400w,\n\t * // https://example.imgix.net/image.png?width=800&sat=-100 800w,\n\t * // https://example.imgix.net/image.png?width=1600&sat=-100 1600w\n\t * ```\n\t *\n\t * @param path - Path to the image relative to the client's base URL.\n\t * @param params - An object of Imgix URL API parameters. The `widths`\n\t * parameter defines the resulting `srcset` widths.\n\t *\n\t * @returns A `srcset` attribute value for `url` with the given Imgix URL API\n\t * parameters applied.\n\t */\n\tbuildWidthSrcSetForPath(path: string, params: BuildWidthSrcSetParams) {\n\t\treturn buildWidthSrcSet(`${new URL(path, this.baseURL)}`, params);\n\t}\n\n\t/**\n\t * Builds an `<img>` `srcset` attribute value for a given set of pixel\n\t * densities for the client's base URL. It can also optinally apply Imgix URL\n\t * API parameters to the URLs.\n\t *\n\t * The `dpr` URL parameter will be applied for each `srcset` entry. If a `dpr`\n\t * parameter is provided to the `params` parameter, it will be ignored.\n\t *\n\t * @example\n\t *\n\t * ```ts\n\t * const client = new Client({ baseURL: \"https://example.imgix.net\" });\n\t * const srcset = client.buildPixelDensitySrcSetForPath(\"/image.png\", {\n\t * \tpixelDensities: [1, 2, 3],\n\t * });\n\t * // => https://example.imgix.net/image.png?dpr=1 1x,\n\t * // https://example.imgix.net/image.png?dpr=2 2x,\n\t * // https://example.imgix.net/image.png?dpr=3 3x\n\t * ```\n\t *\n\t * @example\n\t *\n\t * ```ts\n\t * const client = new Client({ baseURL: \"https://example.imgix.net\" });\n\t * const srcset = client.buildPixelDensitySrcSetForPath(\"/image.png\", {\n\t * \tpixelDensities: [1, 2, 3],\n\t * \tsat: -100,\n\t * });\n\t * // => https://example.imgix.net/image.png?dpr=1&sat=-100 1x,\n\t * // https://example.imgix.net/image.png?dpr=2&sat=-100 2x,\n\t * // https://example.imgix.net/image.png?dpr=3&sat=-100 3x\n\t * ```\n\t *\n\t * @param path - Path to the image relative to the client's base URL.\n\t * @param params - An object of Imgix URL API parameters. The `pixelDensities`\n\t * parameter defines the resulting `srcset` widths.\n\t *\n\t * @returns A `srcset` attribute value for `url` with the given Imgix URL API\n\t * parameters applied.\n\t */\n\tbuildPixelDensitySrcSetForPath(\n\t\tpath: string,\n\t\tparams: BuildPixelDensitySrcSetParams,\n\t) {\n\t\treturn buildPixelDensitySrcSet(`${new URL(path, this.baseURL)}`, params);\n\t}\n}\n"],"names":[],"mappings":"AAgBA,MAAM,uBAAuB,CAAC,UAA0B;AACvD,SAAO,MAAM,QAAQ,UAAU,CAAC,UAAU;AACzC,WAAO,IAAI,MAAM;AAAY,GAC7B;AACF;MAoCa,WAAW,CAAC,KAAa,WAAmC;AACxE,QAAM,WAAW,IAAI,IAAI,GAAG;AAE5B,aAAW,sBAAsB,QAAQ;AACxC,UAAM,WAAW,qBAAqB,kBAAkB;AACxD,UAAM,aAAa,OAAO,kBAAyC;AAEnE,QAAI,eAAe,QAAW;AAC7B,eAAS,aAAa,OAAO,QAAQ;AAAA,eAC3B,MAAM,QAAQ,UAAU,GAAG;AACrC,eAAS,aAAa,IAAI,UAAU,WAAW,KAAK,GAAG,CAAC;AAAA,WAClD;AACN,eAAS,aAAa,IAAI,UAAU,GAAG,YAAY;AAAA;AACpD;AAKD,QAAM,IAAI,SAAS,aAAa,IAAI,GAAG;AACvC,MAAI,GAAG;AACN,aAAS,aAAa,OAAO,GAAG;AAChC,aAAS,aAAa,OAAO,KAAK,CAAC;AAAA;AAGpC,SAAO,SAAS;AACjB;;MCrBa,0BAA0B,CACtC,KACA,EAAE,gBAAgB,GAAG,aACT;AACZ,SAAO,eACL,IAAI,CAAC,QAAQ;AACb,WAAO,GAAG,SAAS,KAAK,EAAE,GAAG,QAAQ,KAAK,KAAK;AAAA,GAC/C,EACA,KAAK,IAAI;AACZ;;MCZa,mBAAmB,CAC/B,KACA,EAAE,QAAQ,GAAG,aACD;AACZ,SAAO,OACL,IAAI,CAAC,UAAU;AACf,WAAO,GAAG,SAAS,KAAK,EAAE,GAAG,QAAQ,GAAG,QAAW,OAAO,KAAK;AAAA,GAC/D,EACA,KAAK,IAAI;AACZ;;MCzCa,OAAO;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUnB,YAAY,SAAwB;AACnC,SAAK,UAAU,QAAQ;AAAA;AACxB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EA8BA,gBAAgB,MAAc,SAAyB,IAAI;AAC1D,WAAO,SAAS,GAAG,IAAI,IAAI,MAAM,KAAK,OAAO,KAAK,MAAM;AAAA;AACzD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EA6CA,wBAAwB,MAAc,QAAgC;AACrE,WAAO,iBAAiB,GAAG,IAAI,IAAI,MAAM,KAAK,OAAO,KAAK,MAAM;AAAA;AACjE;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EA0CA,+BACC,MACA,QACC;AACD,WAAO,wBAAwB,GAAG,IAAI,IAAI,MAAM,KAAK,OAAO,KAAK,MAAM;AAAA;AAEzE;;;;"}