UNPKG

imgix-url-builder

Version:

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

1 lines 2.27 kB
{"version":3,"file":"buildSignedURL.cjs","sources":["../../../src/node/buildSignedURL.ts"],"sourcesContent":["import type { ImgixURLParams } from \"../types.generated\";\nimport { buildURL } from \"../buildURL\";\nimport { signURL } from \"./signURL\";\n\n/**\n * Builds a URL to an Imgix image with Imgix URL API parameters. The URL is\n * signed by appending a signature to the URL parameters. This locks the URL and\n * its parameters to the signature to prevent URL tampering.\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 * **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 url = buildSignedURL(\n * \t\"https://example.imgix.net/image.png\",\n * \t\"example-token\",\n * \t{ width: 400 },\n * );\n * // => https://example.imgix.net/image.png?width=400&s=def3e221c3f4c4debda091b8e49420ea\n * ```\n *\n * @example\n *\n * ```ts\n * const url = buildSignedURL(\n * \t\"https://example.imgix.net/image.png?width=400\",\n * \t\"example-token\",\n * \t{ height: 300 },\n * );\n * // => https://example.imgix.net/image.png?width=400&height=300&s=f12c7c39333410c10c2930b57116a943\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.\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 * @see Details on securing Imgix images: https://docs.imgix.com/setup/securing-images\n */\nexport const buildSignedURL = (\n\turl: string,\n\tsecureURLToken: string,\n\tparams: ImgixURLParams,\n): string => {\n\treturn signURL(buildURL(url, params), secureURLToken);\n};\n"],"names":["signURL","buildURL"],"mappings":";;;;AAmDO,MAAM,iBAAiB,CAC7B,KACA,gBACA,WACW;AACX,SAAOA,QAAAA,QAAQC,SAAAA,SAAS,KAAK,MAAM,GAAG,cAAc;AACrD;;"}