import {z} from 'zod' export type CreateApiCustomersArgs = {body: {email: string, full_name?: string | undefined, name?: string | undefined, phone?: string | undefined, address?: string | undefined, nit?: string | undefined, metadata?: {[key: string]: string} | undefined, test_clock_id?: string | undefined}}; export type CreateApiCustomersOptions = {baseUrl?: string | undefined, secretKey?: string | undefined, fetch?: typeof fetch | undefined}; export const customer = z.object({id: z.string().optional(), user_id: z.string().optional(), email: z.string().optional(), name: z.string().optional(), phone: z.string().nullable().optional(), address: z.string().nullable().optional(), nit: z.string().nullable().optional(), metadata: z.record(z.string(), z.unknown()).nullable().optional(), test_clock_id: z.string().nullable().optional()}); 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 }; /** * Crear un cliente * * POST /customers */ export const createApiCustomers = async (args: CreateApiCustomersArgs, options?: CreateApiCustomersOptions) => { const {baseUrl = "https://app.recurrente.com/api", fetch: fetchFn = fetch, secretKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (secretKey !== undefined) headers.set("X-SECRET-KEY", secretKey) const url = `${baseUrl.replace(/\/+$/, '')}/customers${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(customer, await res.json()) };