import {z} from 'zod' export type CheckoutUpdateRequest = {customer?: ({firstName?: (string | null) | undefined, lastName?: (string | null) | undefined, phoneNumber?: (string | null) | undefined, dateOfBirth?: (string | null) | undefined} | null) | undefined, fulfilment?: ({time?: ({asap?: boolean | undefined, scheduled?: ({from: string, to: string} | null) | undefined} | null) | undefined, location?: ({address?: ({lines?: (Array | null) | undefined, locality?: (string | null) | undefined, administrativeArea?: (string | null) | undefined, postalCode?: (string | null) | undefined} | null) | undefined, geolocation?: ({latitude: number, longitude: number} | null) | undefined} | null) | undefined} | null) | undefined, notes?: (Array<{type: 'delivery', note: string}> | null) | undefined}; export type PatchApiCheckoutTenantCheckoutIdArgs = {tenant: string, checkoutId: string, 'User-Agent': string, body: CheckoutUpdateRequest}; export type PatchApiCheckoutTenantCheckoutIdOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const checkoutIssue = z.object({code: z.enum(["RESTAURANT_NOT_TAKING_ORDERS", "SERVICE_TYPE_UNAVAILABLE", "ITEMS_UNORDERABLE", "ADDITIONAL_ITEMS_REQUIRED", "MINIMUM_ORDER_VALUE_NOT_MET", "FULFILMENT_TIME_UNAVAILABLE", "LOCATION_UNDELIVERABLE", "AGE_VERIFICATION_FAILED", "FIRST_NAME_REQUIRED", "PHONE_NUMBER_REQUIRED", "DATE_OF_BIRTH_REQUIRED", "FULFILMENT_TIME_REQUIRED", "ADDRESS_LINES_REQUIRED", "LOCALITY_REQUIRED", "ADMINISTRATIVE_AREA_REQUIRED", "POSTAL_CODE_REQUIRED", "GEOLOCATION_REQUIRED"])}); export const checkoutUpdateResponse = z.object({isFulfillable: z.boolean(), issues: z.array(checkoutIssue)}); 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 Checkout * * PATCH /checkout/{tenant}/{checkoutId} */ export const patchApiCheckoutTenantCheckoutId = async (args: PatchApiCheckoutTenantCheckoutIdArgs, options?: PatchApiCheckoutTenantCheckoutIdOptions) => { const {baseUrl = "https://uk.api.just-eat.io", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json-patch+json") headers.set('accept', "application/json") if (args['User-Agent'] != null) headers.set("User-Agent", String(args['User-Agent'])) if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/checkout/${encodeURIComponent(String(args.tenant))}/${encodeURIComponent(String(args.checkoutId))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(checkoutUpdateResponse, await res.json()) };