import {z} from 'zod' export type EditCity = {name?: string | undefined}; export type EditState = {name?: string | undefined}; export type EditCountry = {iso2?: string | undefined}; export type UpdateOrder = {is_as_soon_as_delivery?: number | undefined, later_delivery_date?: string | undefined, comment?: unknown | undefined, address?: unknown | undefined, city?: EditCity | undefined, state?: EditState | undefined, country?: EditCountry | undefined, zip_code?: unknown | undefined, phone?: unknown | undefined, voucher_id?: number | undefined, restaurant_offer_id?: number | undefined, payment_gateway_id?: number | undefined, gateway_id?: number | undefined, restaurant_branch_id?: number | undefined, credit_card_number?: number | undefined, credit_card_expire?: unknown | undefined, credit_card_name_on_card?: unknown | undefined, credit_card_code?: number | undefined, payment_note?: unknown | undefined}; export type UpdateApiOrdersOrderIdArgs = {orderId: number, restaurant_id?: number | undefined, body: UpdateOrder}; export type UpdateApiOrdersOrderIdOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const editCity = z.object({name: z.string().optional()}); export const editState = z.object({name: z.string().optional()}); export const editCountry = z.object({iso2: z.string().optional()}); export const updateOrder = z.object({is_as_soon_as_delivery: z.number().int().optional(), later_delivery_date: z.string().optional(), comment: z.string().optional(), address: z.string().optional(), city: editCity.optional(), state: editState.optional(), country: editCountry.optional(), zip_code: z.string().optional(), phone: z.string().optional(), voucher_id: z.number().int().optional(), restaurant_offer_id: z.number().int().optional(), payment_gateway_id: z.number().int().optional(), gateway_id: z.number().int().optional(), restaurant_branch_id: z.number().int().optional(), credit_card_number: z.number().int().optional(), credit_card_expire: z.string().optional(), credit_card_name_on_card: z.string().optional(), credit_card_code: z.number().int().optional(), payment_note: z.string().optional()}); export const updateApiOrdersOrderIdResponse = z.array(updateOrder); 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 order by its id * * PUT /orders/{orderId} */ export const updateApiOrdersOrderId = async (args: UpdateApiOrdersOrderIdArgs, options: UpdateApiOrdersOrderIdOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.restaurant_id != null) query.append("restaurant_id", String(args.restaurant_id)) const url = `${baseUrl.replace(/\/+$/, '')}/orders/${encodeURIComponent(String(args.orderId))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(updateApiOrdersOrderIdResponse, await res.json()) };