next-sanity
Version:
Sanity.io toolkit for Next.js
42 lines (41 loc) • 1.36 kB
JavaScript
import { t as cacheTagPrefix } from "./constants.js";
/**
* Prefixes live event tags according to the conventions used by `defineLive().sanityFetch()`
* so that they can be used with `import {updateTag} from 'next/cache'`.
*
* @example
* ```tsx
* import {updateTag} from 'next/cache'
* import {parseTags} from 'next-sanity/live'
* import {SanityLive} from '#sanity/live
*
* <SanityLive
* action={async (event, context) => {
* 'use server'
*
* for (const tag of parseTags(event.tags, context)) {
* updateTag(tag)
* }
* }}
* />
* ```
*/
function parseTags(unsafeTags) {
if (!Array.isArray(unsafeTags)) throw new TypeError("tags must be an array", { cause: { unsafeTags } });
if (unsafeTags.length === 0) throw new TypeError("tags must be an non-empty array", { cause: { unsafeTags } });
if (unsafeTags.some((tag) => typeof tag !== "string")) throw new TypeError("tags must be an array of strings", { cause: { unsafeTags } });
const tags = [];
const tagsWithoutPrefix = [];
for (const tag of unsafeTags) {
if (!tag.startsWith("sanity:")) throw new TypeError("tag must start with a valid prefix", { cause: { tag } });
tags.push(tag);
tagsWithoutPrefix.push(tag.slice(cacheTagPrefix.length));
}
return {
tags,
tagsWithoutPrefix,
prefix: cacheTagPrefix
};
}
export { parseTags as t };
//# sourceMappingURL=parseTags.js.map