import type { Config } from '@/payload-types' import configPromise from '@payload-config' import { revalidateTag, unstable_cache } from 'next/cache' import { getPayload } from 'payload' type Collection = keyof Config['collections'] export const getDocuments = (collection: Slug, depth?: number) => getPayload({ config: configPromise }).then((payload) => payload.find({ collection, depth })) export const getDocumentById = ( collection: Slug, id: string | number, depth?: number, ) => getPayload({ config: configPromise }).then((payload) => payload.findByID({ collection, id, depth }), ) const getTag = (collection: Slug, id: string | number) => `document_${collection}:${id}` export const getCachedDocumentById = ( collection: Slug, id: string | number, depth?: number, ) => unstable_cache(() => getDocumentById(collection, id, depth), [getTag(collection, id)], { tags: [getTag(collection, id)], }) export const revalidateDocumentById = ( collection: Slug, id: string | number, ) => revalidateTag(getTag(collection, id)) const getListingTag = (collection: Slug) => `${collection}s` export const revalidateCollectionListing = (collection: Slug) => revalidateTag(getListingTag(collection)) export const revalidatePostsListing = () => revalidateCollectionListing('post')