Files
payload/test/live-preview/next-app/app/_api/fetchDocs.ts
2023-10-06 13:16:56 -04:00

21 lines
593 B
TypeScript

import type { Config } from '../../payload-types'
import { PAYLOAD_SERVER_URL } from './serverURL'
export const fetchDocs = async <T>(collection: keyof Config['collections']): Promise<T[]> => {
const docs: T[] = await fetch(`${PAYLOAD_SERVER_URL}/api/${collection}?depth=0&limit=100`, {
method: 'GET',
cache: 'no-store',
headers: {
'Content-Type': 'application/json',
},
})
?.then((res) => res.json())
?.then((res) => {
if (res.errors) throw new Error(res?.errors?.[0]?.message ?? 'Error fetching docs')
return res?.docs
})
return docs
}