@prismicio/client
Version:
The official JavaScript + TypeScript client library for Prismic
106 lines (104 loc) • 3.64 kB
JavaScript
import { RichTextNodeType } from "../types/value/richText.js";
import { LinkType } from "../types/value/link.js";
//#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 === 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 !== 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 === 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 === 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
export { filledContentRelationship, filledImage, filledLinkToMedia, prismicDocument, rtImageNode };
//# sourceMappingURL=isValue.js.map