import {z} from 'zod' export type GetApiV1GuestsGuestIdGiftCardsArgs = {guest_id: string}; export type GetApiV1GuestsGuestIdGiftCardsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV1GuestsGuestIdGiftCardsResponse = z.object({gift_cards: z.array(z.object({id: z.string().optional(), code: z.string().optional(), type: z.number().int().optional(), sale_date: z.string().optional(), expiry_date: z.string().optional(), price: z.number().int().optional(), value: z.number().int().optional(), balance: z.number().int().optional(), tips: z.number().int().optional(), notes: z.string().optional(), source: z.number().int().optional(), is_closed: z.boolean().optional(), is_one_time_use: z.boolean().optional(), is_received: z.boolean().optional(), occassion: z.unknown(), gifted_by: z.unknown(), benefits: z.unknown(), 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()}).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 gift cards of a guest * * GET /v1/guests/{guest_id}/gift_cards */ export const getApiV1GuestsGuestIdGiftCards = async (args: GetApiV1GuestsGuestIdGiftCardsArgs, options?: GetApiV1GuestsGuestIdGiftCardsOptions) => { 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))}/gift_cards${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(getApiV1GuestsGuestIdGiftCardsResponse, await res.json()) };