import {z} from 'zod' export type GetApiV1GuestsGuestIdProductsArgs = {guest_id: string}; export type GetApiV1GuestsGuestIdProductsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV1GuestsGuestIdProductsResponse = z.object({products: z.array(z.object({id: z.string().optional(), name: z.string().optional(), quantity: z.number().int().optional(), sale_by: z.string().optional(), sale_date: z.string().optional(), price: z.number().int().optional(), discount: z.number().int().optional(), taxes: z.number().int().optional(), price_paid: z.number().int().optional(), promotion: z.unknown(), payment_type: z.string().optional(), invoice: z.object({id: z.string().optional(), source: z.number().int().optional(), status: z.string().optional(), invoice_number: z.string().optional(), receipt_number: z.string().optional(), invoice_item_id: z.string().optional()}).optional(), center: z.object({id: z.string().optional(), name: z.string().optional()}).optional()})).optional(), page_info: z.object({total: z.number().int().optional(), page: z.number().int().optional(), size: z.number().int().optional()}).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 }; /** * List all products purchased by a guest * * GET /v1/guests/{guest_id}/products */ export const getApiV1GuestsGuestIdProducts = async (args: GetApiV1GuestsGuestIdProductsArgs, options?: GetApiV1GuestsGuestIdProductsOptions) => { const {baseUrl = "https://api.zenoti.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v1/guests/${encodeURIComponent(String(args.guest_id))}/products${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(getApiV1GuestsGuestIdProductsResponse, await res.json()) };