import {z} from 'zod' export type GetApiV1MerchantsMerchantIdReservationsArgs = {merchantId: number, offset?: number | undefined, limit?: number | undefined, earliest?: string | undefined, latest?: string | undefined}; export type GetApiV1MerchantsMerchantIdReservationsOptions = {baseUrl: string, APITOKEN?: string | undefined, fetch?: typeof fetch | undefined}; export const linkRelationDto = z.object({href: z.string().optional(), method: z.string().optional(), rel: z.enum(["DETAILS", "WIDGET", "WIDGET_DETAILS", "GET_MERCHANT", "PARENT", "SELF", "SETTINGS", "AVAILABILITY_DAYS", "AVAILABILITIES", "CREATE_RESERVATION", "GET_RESERVATION", "UPDATE_RESERVATION", "GET_CUSTOMER_RESERVATION", "GET_REVIEW", "PORTAL_PREORDER_FORM", "RESERVATION_CHECKOUT_PAGE", "CREDIT_CARD_DETAILS_PAGE", "CUSTOMER_EDIT_RESERVATION"]).optional()}); export const reservationData = z.object({id: z.string(), number: z.number().int().optional(), reservationNumber: z.string().optional(), quandooId: z.string(), status: z.string(), updatedAt: z.string().optional(), capacity: z.number().int(), merchantId: z.number().int(), customerId: z.string(), promocode: z.string().optional(), extraInfo: z.string().optional(), links: z.array(linkRelationDto), reservationTags: z.array(z.number().int()).optional()}); export const merchantReservationDataList = z.object({reservations: z.array(reservationData).optional(), offset: z.number().int().optional(), limit: 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 }; /** * Get merchant reservations * * GET /v1/merchants/{merchantId}/reservations */ export const getApiV1MerchantsMerchantIdReservations = async (args: GetApiV1MerchantsMerchantIdReservationsArgs, options: GetApiV1MerchantsMerchantIdReservationsOptions) => { const {baseUrl, fetch: fetchFn = fetch, APITOKEN} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.offset != null) query.append("offset", String(args.offset)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.earliest != null) query.append("earliest", String(args.earliest)) if (args.latest != null) query.append("latest", String(args.latest)) if (APITOKEN !== undefined) headers.set("X-Quandoo-AuthToken", APITOKEN) const url = `${baseUrl.replace(/\/+$/, '')}/v1/merchants/${encodeURIComponent(String(args.merchantId))}/reservations${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(merchantReservationDataList, await res.json()) };