import {z} from 'zod' export type EaterOrderHistoryRequest = {limit: number, pageToken?: string | undefined, eaterId: string, source?: string | undefined}; export type CreateApiV1EaterOrderHistoryArgs = {body: EaterOrderHistoryRequest}; export type CreateApiV1EaterOrderHistoryOptions = {baseUrl?: string | undefined, OAuth20?: string | undefined, fetch?: typeof fetch | undefined}; export const item3 = z.object({quantity: z.number().int().gte(1).lte(1000).optional(), id: z.string().nullable().optional(), name: z.string().nullable()}); export const location = z.object({latitude: z.number(), longitude: z.number()}).nullable(); export const address = z.object({fullAddress: z.string().nullable().optional(), postalCode: z.string().optional(), city: z.string().optional(), state: z.string().optional(), countryCode: z.string().optional(), addressLines: z.array(z.string()).nullable().optional(), linesOfAddress: z.array(z.string()).nullable().optional(), location: location.optional()}).nullable(); export const store2 = z.object({id: z.string(), name: z.string(), logoUrl: z.string().optional(), bannerUrl: z.string().optional(), phoneNumber: z.string().optional(), address: address}); export const eaterOrder = z.object({id: z.string().optional(), status: z.enum(["NEW_ORDER", "CONFIRMED", "PICKED_UP", "CANCELED", "FULFILLED", "PREPARED", "REJECTED", "UNKNOWN"]), items: z.array(item3).optional(), orderedAt: z.string().nullable().optional(), orderTotal: z.number().nullable().optional(), currencyCode: z.string().min(3).max(3), pickupTime: z.string().nullable().optional(), store: store2.optional(), orderAgainUrl: z.string().optional()}); export const eaterOrderHistoryResponse = z.object({orders: z.array(eaterOrder), nextPageToken: z.string().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 }; /** * query eater's related history orders * * POST /v1/eater/order-history */ export const createApiV1EaterOrderHistory = async (args: CreateApiV1EaterOrderHistoryArgs, options?: CreateApiV1EaterOrderHistoryOptions) => { const {baseUrl = "https://partners.tryotter.com", fetch: fetchFn = fetch, OAuth20} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/x-www-form-urlencoded") headers.set('accept', "application/json") if (OAuth20 !== undefined) headers.set('authorization', `Bearer ${OAuth20}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/eater/order-history${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: new URLSearchParams(Object.entries(args.body).flatMap(([key, value]) => (value == null ? [] : [[key, String(value)]])))}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(eaterOrderHistoryResponse, await res.json()) };