import {z} from 'zod' export type GetApiOcapiV1OrdersCompletedOrderIdCustomerArgs = {orderId: string, 'Connect-Region-Code'?: string | undefined}; export type GetApiOcapiV1OrdersCompletedOrderIdCustomerOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const ocapiV1PersonName = z.object({givenName: z.string().nullable().optional(), familyName: z.string().nullable().optional(), middleName: z.string().nullable().optional()}); export const ocapiV1OrdersCustomersOrderCustomerPreferences = z.object({languageTag: z.string().nullable().optional()}); export const ocapiV1OrdersCustomersOrderCustomerTaxDetails = z.object({name: z.string().nullable().optional(), number: z.string().nullable().optional()}); export const ocapiV1OrdersCustomersOrderCustomerAddress = z.object({name: ocapiV1PersonName.optional(), companyName: z.string().nullable().optional(), phoneNumber: z.string().nullable().optional(), line1: z.string().nullable().optional(), suburb: z.string().nullable().optional(), city: z.string().nullable().optional(), state: z.string().nullable().optional(), postCode: z.string().nullable().optional(), country: z.string().nullable().optional()}); export const ocapiV1OrdersCustomersOrderCustomer = z.object({name: ocapiV1PersonName.optional(), email: z.string().max(255), phoneNumber: z.string().max(25).nullable().optional(), preferences: ocapiV1OrdersCustomersOrderCustomerPreferences, taxDetails: ocapiV1OrdersCustomersOrderCustomerTaxDetails, deliveryAddress: ocapiV1OrdersCustomersOrderCustomerAddress.optional(), billingAddress: ocapiV1OrdersCustomersOrderCustomerAddress.optional()}); export const ocapiV1OrdersCustomersOrderCustomerDetails = z.object({customer: ocapiV1OrdersCustomersOrderCustomer.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 }; /** * Gets the customer set on a completed standard order or completed gift shop order * * GET /ocapi/v1/orders/completed/{orderId}/customer */ export const getApiOcapiV1OrdersCompletedOrderIdCustomer = async (args: GetApiOcapiV1OrdersCompletedOrderIdCustomerArgs, options: GetApiOcapiV1OrdersCompletedOrderIdCustomerOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['Connect-Region-Code'] != null) headers.set("Connect-Region-Code", String(args['Connect-Region-Code'])) const url = `${baseUrl.replace(/\/+$/, '')}/ocapi/v1/orders/completed/${encodeURIComponent(String(args.orderId))}/customer${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(ocapiV1OrdersCustomersOrderCustomerDetails, await res.json()) };