UNPKG

@prismicio/client

Version:

The official JavaScript + TypeScript client library for Prismic

110 lines (108 loc) 3.83 kB
const require_richText = require('../types/value/richText.cjs'); const require_link = require('../types/value/link.cjs'); //#region src/lib/isValue.ts /** * Checks if a value is a link to media field. * * @param value - Value to check. * * @returns `true` if `value` is a link to media field, `false` otherwise. * * @internal * This is not an official helper function and it's only designed to work with internal processes. */ const filledLinkToMedia = (value) => { if (value && typeof value === "object" && !("version" in value)) { if ("link_type" in value && value.link_type === require_link.LinkType.Media && "id" in value && "name" in value && "kind" in value && "url" in value && "size" in value) return true; } return false; }; /** * Checks if a value is like an image field. * * @param value - Value to check. * * @returns `true` if `value` is like an image field, `false` otherwise. * * @internal * This is not an official helper function and it's only designed to work with internal processes. */ const imageLike = (value) => { if (value && typeof value === "object" && (!("version" in value) || typeof value.version === "object")) { if ("id" in value && "url" in value && typeof value.url === "string" && "dimensions" in value && "edit" in value && "alt" in value && "copyright" in value) return true; } return false; }; /** * Checks if a value is an image field. * * @param value - Value to check. * * @returns `true` if `value` is an image field, `false` otherwise. * * @internal * This is not an official helper function and it's only designed to work with internal processes. */ const filledImage = (value) => { if (imageLike(value) && (!("type" in value) || value.type !== require_richText.RichTextNodeType.image)) return true; return false; }; /** * Checks if a value is a rich text image node. * * @param value - Value to check. * * @returns `true` if `value` is a rich text image node, `false` otherwise. * * @internal * This is not an official helper function and it's only designed to work with internal processes. */ const rtImageNode = (value) => { if (imageLike(value) && "type" in value && value.type === require_richText.RichTextNodeType.image) return true; return false; }; /** * Checks if a value is a content relationship field. * * @remarks * The return value includes `OptionalLinkProperties` because * `FilledContentRelationshipField` may be a link field, not strictly a content * relationship field. * * @param value - Value to check. * * @returns `true` if `value` is a content relationship, `false` otherwise. * * @internal * This is not an official helper function and it's only designed to work with internal processes. */ const filledContentRelationship = (value) => { if (value && typeof value === "object" && !("version" in value)) { if ("link_type" in value && value.link_type === require_link.LinkType.Document && "id" in value && "type" in value && "tags" in value && "lang" in value) return true; } return false; }; /** * Checks if a value is a Prismic document. * * @param value - Value to check. * * @returns `true` if `value` is a Prismic document, `false` otherwise. * * @internal * This is not an official helper function and it's only designed to work with internal processes. */ const prismicDocument = (value) => { try { return typeof value === "object" && value !== null && "id" in value && "href" in value && typeof value.href === "string" && new URL(value.href) && "type" in value && "lang" in value && "tags" in value && Array.isArray(value.tags); } catch { return false; } }; //#endregion exports.filledContentRelationship = filledContentRelationship; exports.filledImage = filledImage; exports.filledLinkToMedia = filledLinkToMedia; exports.prismicDocument = prismicDocument; exports.rtImageNode = rtImageNode; //# sourceMappingURL=isValue.cjs.map