import {z} from 'zod' export type UpdateRecipientOptions = {name?: string | undefined, products?: Array | undefined, default_full_refresh_min_start_time_epoch?: number | undefined}; export type UpdateRecipientRequest = {recipient: UpdateRecipientOptions}; export type PatchApiRecipientsRecipientIdArgs = {recipient_id: string, body: UpdateRecipientRequest}; export type PatchApiRecipientsRecipientIdOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const recipientResource = z.object({id: z.string(), name: z.string(), products: z.array(z.string()), id_in_provider_system: z.string(), default_full_refresh_min_start_time_epoch: z.number().int().optional(), created_at: z.string().optional(), updated_at: z.string().optional()}); export const getRecipientResponse = z.object({recipient: recipientResource}); export const patchApiRecipientsRecipientIdResponse = z.object({status: z.literal("success"), data: getRecipientResponse, message: z.string()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Update recipient * * PATCH /recipients/{recipient_id} */ export const patchApiRecipientsRecipientId = async (args: PatchApiRecipientsRecipientIdArgs, options?: PatchApiRecipientsRecipientIdOptions) => { const {baseUrl = "https://api.prequel.co", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("X-API-KEY", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/recipients/${encodeURIComponent(String(args.recipient_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(patchApiRecipientsRecipientIdResponse, await res.json()) };