imgix-url-builder
Version:
JavaScript/TypeScript Imgix URL builders for browsers and Node.js
1 lines • 6.96 kB
Source Map (JSON)
{"version":3,"file":"Client.cjs","sources":["../../src/Client.ts"],"sourcesContent":["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":["buildURL","buildWidthSrcSet","buildPixelDensitySrcSet"],"mappings":";;;;;;;;MAyBa,OAAM;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUlB,YAAY,SAAsB;AATlC;AAUC,SAAK,UAAU,QAAQ;AAAA,EACxB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA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