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 CreateApiV3CustomerArgs = {body: {name: string, email: string, country: string, reference_id?: string | undefined, 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}}; export type CreateApiV3CustomerOptions = {baseUrl?: string | undefined, sec0?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const phone = z.object({calling_code: z.string().optional(), number: z.string().optional()}); export const address = z.object({line1: z.string().optional(), line2: z.string().optional(), city: z.string().optional(), state: z.string().optional(), country: z.string().optional(), postal_code: z.string().optional()}); export const metadata = z.record(z.string(), z.string()).nullable(); export const createApiV3CustomerResponse = z.object({status: z.string().optional(), message: z.string().optional(), data: z.object({id: z.string().optional(), object: z.string().optional(), created_at: z.string().optional(), name: z.string().optional(), email: z.string().optional(), country: z.string().optional(), phone: phone.optional(), billing_address: z.array(z.object({name: z.string().optional(), address: address.optional(), phone: phone.optional(), label: z.string().optional()})).optional(), shipping_address: z.array(z.object({name: z.string().optional(), address: address.optional(), phone: phone.optional(), label: z.string().optional()})).optional(), metadata: metadata.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 }; /** * Create Customer * * POST /v3/customer */ export const createApiV3Customer = async (args: CreateApiV3CustomerArgs, options?: CreateApiV3CustomerOptions) => { 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: "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(createApiV3CustomerResponse, await res.json()) };