Files
payload/test/live-preview/app/live-preview/_api/getDoc.ts
James Mikrut 31b32ef941 feat: deprecates getPayloadHMR in favor of simpler getPayload (#9249)
Deprecates `getPayloadHMR` and simplifies this pattern into a single
`import { getPayload } from 'payload'`.

We will still retain the exported `getPayloadHMR` but it now will throw
a deprecation warning with instructions for how to migrate.
2024-11-16 15:30:05 -05:00

40 lines
740 B
TypeScript

import type { CollectionSlug, Where } from 'payload'
import config from '@payload-config'
import { getPayload } from 'payload'
export const getDoc = async <T>(args: {
collection: CollectionSlug
depth?: number
draft?: boolean
slug?: string
}): Promise<T> => {
const payload = await getPayload({ config })
const { slug, collection, depth = 2, draft } = args || {}
const where: Where = {}
if (slug) {
where.slug = {
equals: slug,
}
}
try {
const { docs } = await payload.find({
collection,
depth,
where,
draft,
})
if (docs[0]) {
return docs[0] as T
}
} catch (err) {
console.log('Error getting doc', err)
}
throw new Error('Error getting doc')
}