UNPKG

imgix-url-builder

Version:

JavaScript/TypeScript Imgix URL builders for browsers and Node.js

144 lines (143 loc) 5.34 kB
"use strict"; var __defProp = Object.defineProperty; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); const buildPixelDensitySrcSet = require("./buildPixelDensitySrcSet.cjs"); const buildURL = require("./buildURL.cjs"); const buildWidthSrcSet = require("./buildWidthSrcSet.cjs"); class Client { /** * Creates a new `Client` instance for an Imgix domain. * * @param options - Options to instantiate a new client. * * @returns A `Client` instance for the given Imgix domain. */ constructor(options) { __publicField(this, "baseURL"); this.baseURL = options.baseURL; } /** * Builds a URL to an Imgix image with Imgix URL API parameters for the * client's base URL. * * @example * * ```ts * const client = new Client({ baseURL: "https://example.imgix.net" }); * const url = client.buildURLForPath("/image.png", { width: 400 }); * // => https://example.imgix.net/image.png?width=400 * ``` * * @example * * ```ts * const client = new Client({ * baseURL: "https://example.imgix.net/folder", * }); * const url = client.buildURLForPath("./image.png", { width: 400 }); * // => https://example.imgix.net/folder/image.png?width=400 * ``` * * @param path - Path to the image relative to the client's base URL. * @param params - An object of Imgix URL API parameters. * * @returns The full absolute URL to the image with the given Imgix URL API * parameters applied. */ buildURLForPath(path, params = {}) { return buildURL.buildURL(`${new URL(path, this.baseURL)}`, params); } /** * Builds an `<img>` `srcset` attribute value for a given set of widths for * the client's base URL. It can also optinally apply Imgix URL API parameters * to the URLs. * * The `width` URL parameter will be applied for each `srcset` entry. If a * `width` or `w` parameter is provided to the `params` parameter, it will be * ignored. * * @example * * ```ts * const client = new Client({ baseURL: "https://example.imgix.net" }); * const srcset = client.buildWidthSrcSetForPath("/image.png", { * widths: [400, 800, 1600], * }); * // => https://example.imgix.net/image.png?width=400 400w, * // https://example.imgix.net/image.png?width=800 800w, * // https://example.imgix.net/image.png?width=1600 1600w * ``` * * @example * * ```ts * const client = new Client({ * baseURL: "https://example.imgix.net", * }); * const srcset = client.buildWidthSrcSetForPath("/image.png", { * widths: [400, 800, 1600], * sat: -100, * }); * // => https://example.imgix.net/image.png?width=400&sat=-100 400w, * // https://example.imgix.net/image.png?width=800&sat=-100 800w, * // https://example.imgix.net/image.png?width=1600&sat=-100 1600w * ``` * * @param path - Path to the image relative to the client's base URL. * @param params - An object of Imgix URL API parameters. The `widths` * parameter defines the resulting `srcset` widths. * * @returns A `srcset` attribute value for `url` with the given Imgix URL API * parameters applied. */ buildWidthSrcSetForPath(path, params) { return buildWidthSrcSet.buildWidthSrcSet(`${new URL(path, this.baseURL)}`, params); } /** * Builds an `<img>` `srcset` attribute value for a given set of pixel * densities for the client's base URL. It can also optinally apply Imgix URL * API parameters to the URLs. * * The `dpr` URL parameter will be applied for each `srcset` entry. If a `dpr` * parameter is provided to the `params` parameter, it will be ignored. * * @example * * ```ts * const client = new Client({ baseURL: "https://example.imgix.net" }); * const srcset = client.buildPixelDensitySrcSetForPath("/image.png", { * pixelDensities: [1, 2, 3], * }); * // => https://example.imgix.net/image.png?dpr=1 1x, * // https://example.imgix.net/image.png?dpr=2 2x, * // https://example.imgix.net/image.png?dpr=3 3x * ``` * * @example * * ```ts * const client = new Client({ baseURL: "https://example.imgix.net" }); * const srcset = client.buildPixelDensitySrcSetForPath("/image.png", { * pixelDensities: [1, 2, 3], * sat: -100, * }); * // => https://example.imgix.net/image.png?dpr=1&sat=-100 1x, * // https://example.imgix.net/image.png?dpr=2&sat=-100 2x, * // https://example.imgix.net/image.png?dpr=3&sat=-100 3x * ``` * * @param path - Path to the image relative to the client's base URL. * @param params - An object of Imgix URL API parameters. The `pixelDensities` * parameter defines the resulting `srcset` widths. * * @returns A `srcset` attribute value for `url` with the given Imgix URL API * parameters applied. */ buildPixelDensitySrcSetForPath(path, params) { return buildPixelDensitySrcSet.buildPixelDensitySrcSet(`${new URL(path, this.baseURL)}`, params); } } exports.Client = Client; //# sourceMappingURL=Client.cjs.map