UNPKG

imgix-url-builder

Version:

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

1 lines 3 kB
{"version":3,"file":"buildSignedPixelDensitySrcSet.cjs","sources":["../../../src/node/buildSignedPixelDensitySrcSet.ts"],"sourcesContent":["import type { BuildPixelDensitySrcSetParams } from \"../buildPixelDensitySrcSet\";\nimport { buildSignedURL } from \"./buildSignedURL\";\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 * The URLs are signed by appending a signature to their URL parameters. This\n * locks the URLs and their parameters to the signature to prevent URL\n * tampering.\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 * **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 = buildSignedPixelDensitySrcSet(\n * \t\"https://example.imgix.net/image.png\",\n * \t\"example-token\",\n * \t{ pixelDensities: [1, 2, 3] },\n * );\n * // => https://example.imgix.net/image.png?dpr=1&s=def3e221c3f4c4debda091b8e49420ea 1x,\n * // https://example.imgix.net/image.png?dpr=2&s=f12c7c39333410c10c2930b57116a943 2x,\n * // https://example.imgix.net/image.png?dpr=3&s=3a975b5087ab7ad2ab91fe66072fd628 3x\n * ```\n *\n * @example\n *\n * ```ts\n * const srcset = buildSignedPixelDensitySrcSet(\n * \t\"https://example.imgix.net/image.png\",\n * \t\"example-token\",\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&s=def3e221c3f4c4debda091b8e49420ea 1x,\n * // https://example.imgix.net/image.png?dpr=2&sat=-100&s=f12c7c39333410c10c2930b57116a943 2x,\n * // https://example.imgix.net/image.png?dpr=3&sat=-100&s=3a975b5087ab7ad2ab91fe66072fd628 3x\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 `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 buildSignedPixelDensitySrcSet = (\n\turl: string,\n\tsecureURLToken: string,\n\t{ pixelDensities, ...params }: BuildPixelDensitySrcSetParams,\n): string => {\n\treturn pixelDensities\n\t\t.map((dpr) => {\n\t\t\treturn `${buildSignedURL(url, secureURLToken, {\n\t\t\t\t...params,\n\t\t\t\tdpr,\n\t\t\t})} ${dpr}x`;\n\t\t})\n\t\t.join(\", \");\n};\n"],"names":["buildSignedURL"],"mappings":";;;AAuDa,MAAA,gCAAgC,CAC5C,KACA,gBACA,EAAE,gBAAgB,GAAG,aACV;AACJ,SAAA,eACL,IAAI,CAAC,QAAO;AACL,WAAA,GAAGA,eAAAA,eAAe,KAAK,gBAAgB;AAAA,MAC7C,GAAG;AAAA,MACH;AAAA,IACA,CAAA,CAAC,IAAI,GAAG;AAAA,EAAA,CACT,EACA,KAAK,IAAI;AACZ;;"}