UNPKG

@prismicio/client

Version:

The official JavaScript + TypeScript client library for Prismic

1 lines 1.84 kB
{"version":3,"file":"asImageSrc.cjs","names":["isImageThumbnailFilled"],"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.photo, { sat: -100 })\n * // => \"https://images.prismic.io/repo/image.png?sat=-100\"\n * ```\n *\n * @param field - An image field (or one of its responsive views) from which to\n * get an image URL.\n * @param config - An object of imgix URL API parameters to transform the image.\n *\n * @returns The image field's URL with transformations applied, or `null` if the\n * field is empty.\n *\n * @see Learn how to optimize images with imgix: {@link https://prismic.io/docs/fields/image}\n * @see imgix URL parameters reference: {@link 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"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAkCA,MAAa,cACZ,OACA,SAAyB,EAAE,KACM;AACjC,KAAI,SAASA,gCAAuB,MAAM,CACzC,wCAAgB,MAAM,KAAK,OAAO;KAElC,QAAO"}