import {z} from 'zod' export type MerchantReservationEnquiry = {id: string, merchantId: number, capacity: number, startDateTime: string, endDateTime: string, message: string}; export type MerchantSubscription = {id?: ('QUANDOO' | 'MERCHANT') | undefined}; export type MerchantCustomer = {id?: string | undefined, firstName?: string | undefined, lastName?: string | undefined, emailAddress?: string | undefined, phoneNumber: string, locale: string, country: string, subscriptions?: Array | undefined}; export type AgentTracking = {id: number}; export type RestCookie = {tracking?: string | undefined, referenceId?: string | undefined}; export type Tracking = {agent: AgentTracking, cookie?: RestCookie | undefined}; export type MerchantReservationEnquiryData = {reservationEnquiry: MerchantReservationEnquiry, customer: MerchantCustomer, tracking: Tracking}; export type UpdateApiV1ReservationEnquiriesArgs = {body: MerchantReservationEnquiryData}; export type UpdateApiV1ReservationEnquiriesOptions = {baseUrl: string, APITOKEN?: string | undefined, fetch?: typeof fetch | undefined}; export const createdReservationEnquiry = z.object({id: z.string()}); export const createdCustomer = z.object({id: z.string()}); 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 createdReservationEnquiryData = z.object({reservationEnquiry: createdReservationEnquiry, customer: createdCustomer, links: z.array(linkRelationDto)}); 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 }; /** * Create reservation enquiries * * PUT /v1/reservation-enquiries */ export const updateApiV1ReservationEnquiries = async (args: UpdateApiV1ReservationEnquiriesArgs, options: UpdateApiV1ReservationEnquiriesOptions) => { const {baseUrl, fetch: fetchFn = fetch, APITOKEN} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (APITOKEN !== undefined) headers.set("X-Quandoo-AuthToken", APITOKEN) const url = `${baseUrl.replace(/\/+$/, '')}/v1/reservation-enquiries${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(createdReservationEnquiryData, await res.json()) };