UNPKG

imgix-url-builder

Version:

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

1 lines 3 kB
{"version":3,"file":"buildSignedWidthSrcSet.cjs","sources":["../../../src/node/buildSignedWidthSrcSet.ts"],"sourcesContent":["import type { BuildWidthSrcSetParams } from \"../buildWidthSrcSet\";\nimport { buildSignedURL } from \"./buildSignedURL\";\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. The URLs are\n * signed by appending a signature to their URL parameters. This locks the URLs\n * and their parameters to the signature to prevent URL tampering.\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 * **Important**: This function should only be used in a trusted environment and\n * never in a browser. The `secureURLToken` parameter is a secret and should not\n * be exposed to the public.\n *\n * @example\n *\n * ```ts\n * const srcset = buildSignedWidthSrcSet(\n * \t\"https://example.imgix.net/image.png\",\n * \t\"example-token\",\n * \t{ widths: [400, 800, 1600] },\n * );\n * // => https://example.imgix.net/image.png?width=400&s=def3e221c3f4c4debda091b8e49420ea 400w,\n * // https://example.imgix.net/image.png?width=800&s=f12c7c39333410c10c2930b57116a943 800w,\n * // https://example.imgix.net/image.png?width=1600&s=3a975b5087ab7ad2ab91fe66072fd628 1600w\n * ```\n *\n * @example\n *\n * ```ts\n * const srcset = buildSignedWidthSrcSet(\n * \t\"https://example.imgix.net/image.png\",\n * \t\"example-token\",\n * \t{\n * \t\twidths: [400, 800, 1600],\n * \t\tsat: -100,\n * \t},\n * );\n * // => https://example.imgix.net/image.png?width=400&sat=-100&s=def3e221c3f4c4debda091b8e49420ea 400w,\n * // https://example.imgix.net/image.png?width=800&sat=-100&s=f12c7c39333410c10c2930b57116a943 800w,\n * // https://example.imgix.net/image.png?width=1600&sat=-100&s=3a975b5087ab7ad2ab91fe66072fd628 1600w\n * ```\n *\n * @param url - Full absolute URL to the Imgix image.\n * @param secureURLToken - The secret secure URL token for the image's Imgix\n * source.\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 buildSignedWidthSrcSet = (\n\turl: string,\n\tsecureURLToken: string,\n\t{ widths, ...params }: BuildWidthSrcSetParams,\n): string => {\n\treturn widths\n\t\t.map((width) => {\n\t\t\treturn `${buildSignedURL(url, secureURLToken, {\n\t\t\t\t...params,\n\t\t\t\tw: undefined,\n\t\t\t\twidth,\n\t\t\t})} ${width}w`;\n\t\t})\n\t\t.join(\", \");\n};\n"],"names":["buildSignedURL"],"mappings":";;;AAuDa,MAAA,yBAAyB,CACrC,KACA,gBACA,EAAE,QAAQ,GAAG,aACF;AACJ,SAAA,OACL,IAAI,CAAC,UAAS;AACP,WAAA,GAAGA,eAAAA,eAAe,KAAK,gBAAgB;AAAA,MAC7C,GAAG;AAAA,MACH,GAAG;AAAA,MACH;AAAA,IACA,CAAA,CAAC,IAAI,KAAK;AAAA,EAAA,CACX,EACA,KAAK,IAAI;AACZ;;"}