import {z} from 'zod' export type CommerceRuntimeAddress = {address1?: (string | null) | undefined, address2?: (string | null) | undefined, address3?: (string | null) | undefined, address4?: (string | null) | undefined, addressType?: (string | null) | undefined, cityOrTown?: (string | null) | undefined, countryCode?: (string | null) | undefined, isValidated?: (boolean | null) | undefined, postalOrZipCode?: (string | null) | undefined, stateOrProvince?: (string | null) | undefined}; export type CommerceRuntimePhone = {home?: (string | null) | undefined, mobile?: (string | null) | undefined, work?: (string | null) | undefined}; export type CommerceRuntimeContact = {address?: CommerceRuntimeAddress | undefined, companyOrOrganization?: (string | null) | undefined, email?: (string | null) | undefined, firstName?: (string | null) | undefined, id?: (number | null) | undefined, lastNameOrSurname?: (string | null) | undefined, middleNameOrInitial?: (string | null) | undefined, phoneNumbers?: CommerceRuntimePhone | undefined}; export type CommerceRuntimeDestination = {data?: (Record | null) | undefined, destinationContact?: CommerceRuntimeContact | undefined, id?: (string | null) | undefined, isDestinationCommercial?: (boolean | null) | undefined}; export type UpdateApiCommerceCheckoutsCheckoutIdDestinationsDestinationIdArgs = {checkoutId: string, destinationId: string, responseFields?: string | undefined, body: CommerceRuntimeDestination}; export type UpdateApiCommerceCheckoutsCheckoutIdDestinationsDestinationIdOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const commerceRuntimeAddress = z.object({address1: z.string().nullable().optional(), address2: z.string().nullable().optional(), address3: z.string().nullable().optional(), address4: z.string().nullable().optional(), addressType: z.string().nullable().optional(), cityOrTown: z.string().nullable().optional(), countryCode: z.string().nullable().optional(), isValidated: z.boolean().nullable().optional(), postalOrZipCode: z.string().nullable().optional(), stateOrProvince: z.string().nullable().optional()}); export const commerceRuntimePhone = z.object({home: z.string().nullable().optional(), mobile: z.string().nullable().optional(), work: z.string().nullable().optional()}); export const commerceRuntimeContact = z.object({address: commerceRuntimeAddress.optional(), companyOrOrganization: z.string().nullable().optional(), email: z.string().nullable().optional(), firstName: z.string().nullable().optional(), id: z.number().int().nullable().optional(), lastNameOrSurname: z.string().nullable().optional(), middleNameOrInitial: z.string().nullable().optional(), phoneNumbers: commerceRuntimePhone.optional()}); export const commerceRuntimeDestination = z.object({data: z.object({}).nullable().optional(), destinationContact: commerceRuntimeContact.optional(), id: z.string().nullable().optional(), isDestinationCommercial: z.boolean().nullable().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 Destination * * PUT /commerce/checkouts/{checkoutId}/destinations/{destinationId} */ export const updateApiCommerceCheckoutsCheckoutIdDestinationsDestinationId = async (args: UpdateApiCommerceCheckoutsCheckoutIdDestinationsDestinationIdArgs, options?: UpdateApiCommerceCheckoutsCheckoutIdDestinationsDestinationIdOptions) => { const {baseUrl = "https://t1000000.sb.usc1.gcp.kibocommerce.com/api", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.responseFields != null) query.append("responseFields", String(args.responseFields)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/commerce/checkouts/${encodeURIComponent(String(args.checkoutId))}/destinations/${encodeURIComponent(String(args.destinationId))}${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(commerceRuntimeDestination, await res.json()) };