import {z} from 'zod' export type GetApiV2CustomerCustomerIdArgs = {customerId: string}; export type GetApiV2CustomerCustomerIdOptions = {baseUrl?: string | undefined, BasicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const billing = z.object({address_1: z.string().max(40).optional(), address_2: z.string().max(40).optional(), address_3: z.string().max(40).optional(), city: z.string().max(28).optional(), country_code: z.string().optional(), first_name: z.string().max(22).optional(), middle_name: z.string().max(22).optional(), last_name: z.string().max(22).optional(), phone: z.string().max(25).optional(), postal_code: z.string().max(10).optional(), state: z.string().max(35).optional()}); export const shipping = z.object({address_1: z.string().max(40).optional(), address_2: z.string().max(40).optional(), address_3: z.string().max(40).optional(), city: z.string().max(28).optional(), first_name: z.string().max(22).optional(), middle_name: z.string().max(22).optional(), last_name: z.string().max(22).optional(), phone: z.string().max(25).optional(), country_code: z.string().optional(), postal_code: z.string().max(10).optional(), state: z.string().max(35).optional()}); export const tax = z.object({identifier: z.string().max(9).optional()}); export const customerResponse = z.object({id: z.string(), billing: billing.optional(), company_name: z.string().max(100).optional(), company_registration_number: z.string().max(24).optional(), email_address: z.string().max(255).optional(), entity_id: z.string(), phone_number: z.string().max(25).optional(), shipping: shipping.optional(), title: z.string().max(120).optional(), work_phone: z.string().max(25).optional(), tax: tax.optional(), language: z.string().optional(), created_at: z.string(), updated_at: z.string()}); 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 Customer * * GET /v2/customer/{customerId} * * Also accepts auth: BearerAuth */ export const getApiV2CustomerCustomerId = async (args: GetApiV2CustomerCustomerIdArgs, options?: GetApiV2CustomerCustomerIdOptions) => { const {baseUrl = "https://emea.gsc.verifone.cloud/oidc/checkout-service", fetch: fetchFn = fetch, BasicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (BasicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${BasicAuth.username}:${BasicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/customer/${encodeURIComponent(String(args.customerId))}${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(customerResponse, await res.json()) };