pnpm add @payload-enchants/sdk
import { PayloadApiClient } from '@payload-enchants/sdk';
// Import it from generated types
import { Config } from 'payload-types';
const client = new PayloadApiClient<Config>({
apiURL: 'http://localhost:3000/api',
});
// The syntax is the same as in the Local API, except for `updateById` and `deleteById` (additional operations)
const data = await client.find({ collection: 'pages' });
To rewrite headers, you can pass your fetcher
const client = new PayloadApiClient<Config>({
apiURL: '',
fetcher: (url, init) => {
return fetch(url, {
...(init ?? {}),
headers: {
...(init?.headers ?? {}),
Authorization: 'API-Key users some-api-key',
},
});
},
});
This package provides integratation with @payload-enchants/fields-select plugin
const { docs } = await client.find({ collection: 'pages', select: ['id', 'createdAt'] });
docs[0].createdAt; // ok
docs[0].id; // ok
docs[0].updatedAt; // err, Property 'updatedAt' does not exist on type 'Pick<Page, "id" | "createdAt">'