import {z} from 'zod' export type GetApiCustomersArgs = {page?: number | undefined, per_page?: number | undefined}; export type GetApiCustomersOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const emailAddress = z.string().min(3).max(254); export const billingEntityCode = z.string(); export const address = z.object({line1: z.string().min(1).max(254).optional(), line2: z.string().min(1).max(512).optional(), city: z.string().min(1).max(512).optional(), state: z.string().min(1).max(512).optional(), postal_code: z.string().min(1).max(512).optional(), country: z.string().min(2).max(2).optional()}); export const paymentMethodToken = z.string().min(1).max(36); export const paymentMethodType = z.enum(["CARD", "PAYPAL", "ACH_DEBIT", "APPLE_PAY"]); export const taxCode = z.string(); export const taxResponse = z.object({name: z.string(), rate: z.string().optional(), description: z.string().optional(), code: taxCode.optional(), id: z.string().optional(), created_at: z.string().optional()}); export const customerMetadata = z.object({key: z.string().max(20), value: z.string().max(100), display_in_invoice: z.boolean()}); export const customerWithId = z.object({name: z.string().min(1).max(512), external_id: z.string().min(1).max(64), email: emailAddress, billing_entity_code: billingEntityCode.optional(), address: address.optional(), phone: z.string().optional(), payment_method_token: paymentMethodToken.optional(), payment_method_type: paymentMethodType.optional(), taxes: z.array(taxResponse).optional(), metadata: z.array(customerMetadata).optional(), id: z.string().optional()}); export const paginationMetadata = z.object({total_count: z.number().int().optional(), total_pages: z.number().int().optional(), current_page: z.number().int().optional()}); export const getApiCustomersResponse = z.object({customers: z.array(customerWithId).optional(), metadata: paginationMetadata.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 }; /** * Get all customers * * GET /customers */ export const getApiCustomers = async (args: GetApiCustomersArgs, options?: GetApiCustomersOptions) => { const {baseUrl = "https://api-m.sandbox.paypal.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/customers${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiCustomersResponse, await res.json()) };