@prismicio/client
Version:
The official JavaScript + TypeScript client library for Prismic
1 lines • 1.81 kB
Source Map (JSON)
{"version":3,"file":"asImageSrc.cjs","sources":["../../../src/helpers/asImageSrc.ts"],"sourcesContent":["import type { ImgixURLParams } from \"imgix-url-builder\"\nimport { buildURL } from \"imgix-url-builder\"\n\nimport type { ImageFieldImage } from \"../types/value/image\"\n\nimport { imageThumbnail as isImageThumbnailFilled } from \"./isFilled\"\n\n/**\n * The return type of `asImageSrc()`.\n */\ntype AsImageSrcReturnType<Field extends ImageFieldImage | null | undefined> =\n\tField extends ImageFieldImage<\"filled\"> ? string : null\n\n/**\n * Returns the URL of an image field with optional image transformations (via\n * Imgix URL parameters).\n *\n * @example\n *\n * ```ts\n * const src = asImageSrc(document.data.imageField, { sat: -100 })\n * // => https://images.prismic.io/repo/image.png?sat=-100\n * ```\n *\n * @param field - Image field (or one of its responsive views) from which to get\n * an image URL.\n * @param config - An object of Imgix URL API parameters to transform the image.\n *\n * @returns The image field's image URL with transformations applied (if given).\n * If the image field is empty, `null` is returned.\n *\n * @see Imgix URL parameters reference: https://docs.imgix.com/apis/rendering\n */\nexport const asImageSrc = <Field extends ImageFieldImage | null | undefined>(\n\tfield: Field,\n\tconfig: ImgixURLParams = {},\n): AsImageSrcReturnType<Field> => {\n\tif (field && isImageThumbnailFilled(field)) {\n\t\treturn buildURL(field.url, config) as AsImageSrcReturnType<Field>\n\t} else {\n\t\treturn null as AsImageSrcReturnType<Field>\n\t}\n}\n"],"names":["isImageThumbnailFilled","buildURL"],"mappings":";;;;AAiCO,MAAM,aAAa,CACzB,OACA,SAAyB,OACO;AAC5B,MAAA,SAASA,wBAAuB,KAAK,GAAG;AACpC,WAAAC,yBAAS,MAAM,KAAK,MAAM;AAAA,EAAA,OAC3B;AACC,WAAA;AAAA,EACR;AACD;;"}