import {z} from 'zod' export type Phone = {calling_code?: string | undefined, number?: string | undefined}; export type Address = {line1?: string | undefined, line2?: string | undefined, city?: string | undefined, state?: string | undefined, country?: string | undefined, postal_code?: string | undefined}; export type MetadataInput = unknown; export type UpdateApiV3CustomerArgs = {body: {phone?: Phone | undefined, billing_address?: (Array<{name?: string | undefined, address?: Address | undefined, phone?: Phone | undefined, label?: string | undefined}>) | undefined, shipping_address?: (Array<{name?: string | undefined, address?: Address | undefined, phone?: Phone | undefined, label?: string | undefined}>) | undefined, metadata?: MetadataInput | undefined, customer_id?: string | undefined}}; export type UpdateApiV3CustomerOptions = {baseUrl?: string | undefined, sec0?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const updateApiV3CustomerResponse = z.object({status: z.string().optional(), message: z.string().optional(), data: z.object({message: z.string().optional()}).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 }; /** * Update Customer * * PUT /v3/customer/ */ export const updateApiV3Customer = async (args: UpdateApiV3CustomerArgs, options?: UpdateApiV3CustomerOptions) => { const {baseUrl = "https://service-sandbox.tazapay.com", fetch: fetchFn = fetch, sec0} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (sec0 !== undefined) headers.set('authorization', `Basic ${btoa(`${sec0.username}:${sec0.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v3/customer/${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(updateApiV3CustomerResponse, await res.json()) };