import type { PaginatedDocs, SendEmailOptions } from 'payload' import type { CreateArgs, DeleteArgs, FetchOptions, FindArgs, GeneratedTypes, LoginArgs, UpdateArgs, UpdateGlobalArgs, } from './types.js' type Args = { serverURL: string } export class PayloadTestSDK> { private fetch = async ({ jwt, reduceJSON, args, operation }: FetchOptions): Promise => { const headers: HeadersInit = { 'Content-Type': 'application/json', } if (jwt) { headers.Authorization = `JWT ${jwt}` } const json: T = await fetch(`${this.serverURL}/api/local-api`, { method: 'post', headers, body: JSON.stringify({ args, operation, }), }).then((res) => res.json()) if (reduceJSON) { return reduceJSON(json) } return json } create = async ({ jwt, ...args }: CreateArgs) => { return this.fetch({ operation: 'create', args, jwt, }) } delete = async ({ jwt, ...args }: DeleteArgs) => { return this.fetch>({ operation: 'delete', args, jwt, }) } find = async ({ jwt, ...args }: FindArgs) => { return this.fetch>({ operation: 'find', args, jwt, }) } findVersions = async ({ jwt, ...args }: FindArgs) => { return this.fetch>({ operation: 'findVersions', args, jwt, }) } login = async ({ jwt, ...args }: LoginArgs) => { return this.fetch({ operation: 'login', args, jwt, }) } sendEmail = async ({ jwt, ...args }: { jwt?: string } & SendEmailOptions): Promise => { return this.fetch({ operation: 'sendEmail', args, jwt, }) } serverURL: string update = async ({ jwt, ...args }: UpdateArgs) => { return this.fetch({ operation: 'update', args, jwt, }) } updateGlobal = async ({ jwt, ...args }: UpdateGlobalArgs) => { return this.fetch({ operation: 'updateGlobal', args, jwt, }) } constructor({ serverURL }: Args) { this.serverURL = serverURL } }