import {z} from 'zod' export type GetApiOndOrdersIdArgs = {id: number}; export type GetApiOndOrdersIdOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const userBasic = z.object({active: z.boolean().optional()}); 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 streetAddress = 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()}); export const ondOrderTimeslot = z.object({name: z.string(), start: z.string(), end: z.string()}); export const ondDeliveryPreferences = z.object({callAhead: z.boolean(), keyId: z.string().nullable()}); export const feeDiscountFeeDiscount = z.object({}); export const appliedDiscount = z.object({isMembershipDiscount: z.boolean().optional(), isSubscriptionDiscount: z.boolean().optional(), isCouponDiscount: z.boolean().optional(), amount: z.number()}); export const totalsTotalWarning = z.object({title: z.string(), details: z.string()}); export const totals = z.object({amountBelowMinimumOrderValue: z.number(), subtotal: z.number(), totalDiscount: z.number(), credits: z.number(), totalSubscriptionDiscount: z.number(), potentialSubscriptionDiscount: z.number(), totalMembershipDiscount: z.number(), potentialMembershipDiscount: z.number(), totalFees: z.number(), totalTax: z.number(), grandTotal: z.number(), fees: z.array(feeDiscountFeeDiscount), discounts: z.array(appliedDiscount), warnings: z.array(totalsTotalWarning)}); export const packagingType = z.enum(["SINGLE_USE", "REUSABLE"]); export const deliveryProvider = z.object({code: z.string(), name: z.string()}); export const deliveryDriver = z.object({externalId: z.string(), displayName: z.string()}); export const deliveryRouteInfo = z.object({date: z.string(), code: z.string(), routeSequence: z.number().int(), stopSequence: z.number().int(), barcode: z.string(), provider: deliveryProvider, driver: deliveryDriver}); export const customerTier = z.enum(["A_LA_CARTE", "PRODUCT_SUBSCRIBER", "BAG_SUBSCRIBER", "MEMBER"]); export const ondOrder = z.object({id: z.number().int(), date: z.string(), created: z.string(), modified: z.string(), visitedAt: z.string(), user: userBasic, deliveryAddress: streetAddress, timeslot: ondOrderTimeslot.nullable(), deliveryPreferences: ondDeliveryPreferences, totals: totals, packagingType: packagingType, isPrime: z.boolean(), isSubstitutionAllowed: z.boolean(), hasAlcohol: z.boolean(), estimatedBinsRequired: z.number().int(), estimatedBinsReturned: z.number().int(), routeInfo: deliveryRouteInfo.nullable(), ordinality: z.number().int(), tier: customerTier.nullable(), packingOrder: z.number().int().nullable()}); 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 /ond/orders/{id} */ export const getApiOndOrdersId = async (args: GetApiOndOrdersIdArgs, options: GetApiOndOrdersIdOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/ond/orders/${encodeURIComponent(String(args.id))}${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(ondOrder, await res.json()) };