import {z} from 'zod' export type CustomerEmail = string; export type CustomerName = string; export type CustomerPhone = string; export type Country = string; export type Address = {country?: Country | undefined, city?: string | undefined, line1?: string | undefined, line2?: string | undefined, zip?: string | undefined, state?: string | undefined}; export type PaymentBillingDetails = {name?: string | undefined, email?: string | undefined, phone?: string | undefined, company?: string | undefined, taxId?: string | undefined, address?: Address | undefined}; export type PaymentShippingDetails = {name?: string | undefined, email?: string | undefined, phone?: string | undefined, company?: string | undefined, taxId?: string | undefined, address?: Address | undefined}; export type Metadata = Record; export type CustomerDefaultTokenId = string; export type UpdateCustomerRequest = {email?: CustomerEmail | undefined, name?: CustomerName | undefined, phone?: CustomerPhone | undefined, billingDetails?: PaymentBillingDetails | undefined, shippingDetails?: PaymentShippingDetails | undefined, metadata?: Metadata | undefined, defaultTokenId?: CustomerDefaultTokenId | undefined}; export type UpdateApiCustomersIdArgs = {body: UpdateCustomerRequest}; export type UpdateApiCustomersIdOptions = {baseUrl?: string | undefined, APIKey?: string | undefined, fetch?: typeof fetch | undefined}; export const customerId = z.string(); export const customerEmail = z.string(); export const customerName = z.string(); export const customerPhone = z.string(); export const country = z.string(); export const address = z.object({country: country.optional(), city: z.string().optional(), line1: z.string().optional(), line2: z.string().optional(), zip: z.string().optional(), state: z.string().optional()}); export const paymentBillingDetails = z.object({name: z.string().optional(), email: z.string().optional(), phone: z.string().optional(), company: z.string().optional(), taxId: z.string().optional(), address: address.optional()}); export const paymentShippingDetails = z.object({name: z.string().optional(), email: z.string().optional(), phone: z.string().optional(), company: z.string().optional(), taxId: z.string().optional(), address: address.optional()}); export const metadata = z.object({}); export const customerDefaultTokenId = z.string(); export const customerCreatedAt = z.string(); export const customerUpdatedAt = z.string(); export const customer = z.object({id: customerId, email: customerEmail.optional(), name: customerName.optional(), phone: customerPhone.optional(), billingDetails: paymentBillingDetails.optional(), shippingDetails: paymentShippingDetails.optional(), metadata: metadata.optional(), defaultTokenId: customerDefaultTokenId.optional(), createdAt: customerCreatedAt.optional(), updatedAt: customerUpdatedAt.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 /customers/{id} */ export const updateApiCustomersId = async (args: UpdateApiCustomersIdArgs, options?: UpdateApiCustomersIdOptions) => { const {baseUrl = "https://api.monei.com/v1", fetch: fetchFn = fetch, APIKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (APIKey !== undefined) headers.set("Authorization", APIKey) const url = `${baseUrl.replace(/\/+$/, '')}/customers/${encodeURIComponent(String(args.id))}${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(customer, await res.json()) };