import {z} from 'zod' export type CreateApiTakeawayOrderArgs = {body: {cash_register: number, discount?: number | undefined, discount_type?: number | undefined, is_final_price?: boolean | undefined, is_discount_group?: boolean | undefined, number_of_persons?: number | undefined, discount_reason_id?: number | undefined, discount_margin?: number | undefined, staff: number, customer?: number | undefined, type: number, pickup_time: string, order_type: 'pickup' | 'dinein' | 'delivery', delivery_price?: number | undefined, note?: string | undefined, provider?: number | undefined, basket_items?: (Array<{uid?: number | undefined, course?: number | undefined, model?: number | undefined, menu?: number | undefined, product?: number | undefined, product_variance?: number | undefined, is_final_price?: boolean | undefined, name?: string | undefined, discount?: number | undefined, discount_type?: number | undefined, discount_margin?: number | undefined, custom_price?: number | undefined, quantity?: number | undefined, note?: string | undefined, printed?: boolean | undefined, sent?: boolean | undefined, tax?: number | undefined, printer_Types?: Array | undefined, amount?: number | undefined, item_hash?: string | undefined, discount_reason_id?: number | undefined, tax_type?: number | undefined, ticket?: {event_name: string, event_address: string, date: string, time: string, type: string, auditorium: string, code: string} | undefined}>) | undefined, address?: ({country?: number | undefined, city?: string | undefined, zipCode?: number | undefined, street?: string | undefined, apartment?: string | undefined}) | undefined}}; export type CreateApiTakeawayOrderOptions = {baseUrl?: string | undefined, TokenAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const tableBasket = z.object({uid: z.number().int().optional(), crdate: z.number().int().optional(), tstamp: z.number().int().optional(), crdate_formatted: z.string().optional(), tstamp_formatted: z.string().optional(), table: z.number().int().optional(), cash_register: z.number().int().optional(), total: z.number().int().optional(), total_no_discount: z.number().int().optional(), discount: z.number().int().optional(), discount_margin: z.number().int().optional(), discount_type: z.number().int().optional(), item_count: z.number().int().optional(), payment_count: z.number().int().optional(), order_hash: z.string().optional(), type: z.number().int().optional(), auto_print_failed: z.number().int().optional(), external_id: z.string().optional(), is_final_price: z.number().int().optional(), device_uid: z.string().optional(), number_of_persons: z.number().int().optional(), extra_data: z.object({}).optional(), editable: z.number().int().optional(), klipkort_voucher: z.number().int().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 }; /** * POST /takeaway/order */ export const createApiTakeawayOrder = async (args: CreateApiTakeawayOrderArgs, options?: CreateApiTakeawayOrderOptions) => { const {baseUrl = "https://api-dev.shopbox.com/api/v3", fetch: fetchFn = fetch, TokenAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (TokenAuth !== undefined) query.set("accessToken", TokenAuth) const url = `${baseUrl.replace(/\/+$/, '')}/takeaway/order${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(tableBasket, await res.json()) };