import {z} from 'zod' export type GetApiV1ReservationsReservationIdArgs = {reservationId: string}; export type GetApiV1ReservationsReservationIdOptions = {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 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 reservation by id * * GET /v1/reservations/{reservationId} */ export const getApiV1ReservationsReservationId = async (args: GetApiV1ReservationsReservationIdArgs, options: GetApiV1ReservationsReservationIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, APITOKEN} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (APITOKEN !== undefined) headers.set("X-Quandoo-AuthToken", APITOKEN) const url = `${baseUrl.replace(/\/+$/, '')}/v1/reservations/${encodeURIComponent(String(args.reservationId))}${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(reservationData, await res.json()) };