import {z} from 'zod' export type GetApiUsersUserIdAddressesArgs = {userId: number}; export type GetApiUsersUserIdAddressesOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const streetAddressType = z.enum(["USER", "GUEST", "STORE"]); export const province = z.enum(["ALBERTA", "BRITISH_COLUMBIA", "MANITOBA", "NEW_BRUNSWICK", "NEWFOUNDLAND_AND_LABRADOR", "NORTHWEST_TERRITORIES", "NOVA_SCOTIA", "NUNAVUT", "ONTARIO", "PRINCE_EDWARD_ISLAND", "QUEBEC", "SASKATCHEWAN", "YUKON_TERRITORY"]); export const phoneNumberServiceNormalizedPhoneNumberNormalizedE164PhoneNumber = z.object({value: z.string()}); export const userStreetAddress = z.object({type: streetAddressType, name: z.string(), line1: z.string(), line2: z.string(), city: z.string(), province: province, postalCode: z.string(), telephone: phoneNumberServiceNormalizedPhoneNumberNormalizedE164PhoneNumber, instructions: z.string(), buildingType: z.number().int(), latitude: z.number(), longitude: z.number(), id: z.number().int().optional(), userId: z.number().int().optional()}); export const userDeliveryAndBillingAddress = z.object({deliveryAddress: userStreetAddress, billingAddress: userStreetAddress, sameAsDelivery: z.boolean()}); 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 /users/{userId}/addresses */ export const getApiUsersUserIdAddresses = async (args: GetApiUsersUserIdAddressesArgs, options: GetApiUsersUserIdAddressesOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/users/${encodeURIComponent(String(args.userId))}/addresses${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(userDeliveryAndBillingAddress, await res.json()) };