import {z} from 'zod' export type GetApiCheckoutTenantCheckoutIdArgs = {tenant: string, checkoutId: string, 'User-Agent': string}; export type GetApiCheckoutTenantCheckoutIdOptions = {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 checkoutGetResponse = z.object({restaurant: z.object({id: z.string(), availabilityId: z.string()}), serviceType: z.enum(["delivery", "collection"]), customer: z.object({firstName: z.string().min(1).max(100).nullable().optional(), lastName: z.string().max(100).nullable().optional(), phoneNumber: z.string().min(3).max(16).nullable().optional()}), fulfilment: z.object({time: z.object({asap: z.boolean(), scheduled: z.object({from: z.string(), to: z.string()}).nullable().optional()}).nullable().optional(), location: z.object({address: z.object({lines: z.array(z.string().min(1).max(100)).optional(), locality: z.string().min(1).max(50).nullable().optional(), administrativeArea: z.string().min(1).max(50).nullable().optional(), postalCode: z.string().min(1).max(50).nullable().optional()}).nullable().optional(), geolocation: z.object({latitude: z.number(), longitude: z.number()}).nullable().optional()}).nullable().optional()}), 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 }; /** * Get Checkout * * GET /checkout/{tenant}/{checkoutId} */ export const getApiCheckoutTenantCheckoutId = async (args: GetApiCheckoutTenantCheckoutIdArgs, options?: GetApiCheckoutTenantCheckoutIdOptions) => { const {baseUrl = "https://uk.api.just-eat.io", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() 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: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(checkoutGetResponse, await res.json()) };