import {z} from 'zod' export type Name = {title?: ('MR' | 'MRS' | 'MS') | undefined, firstName: string, lastName: string}; export type Contact = {phone: string, email: string}; export type Stakeholder = {id?: number | undefined, name: Name, contact: Contact, hotelRewardsMember?: string | undefined}; export type Payment = {id?: number | undefined, method: 'creditCard', card?: {vendorCode: string, cardNumber: string, expiryDate: string} | undefined}; export type CreateApiBookingHotelBookingsArgs = {body: {data: {offerId: string, guests: Array, payments?: Array | undefined, rooms?: (Array<{guestIds?: Array | undefined, paymentId?: number | undefined, specialRequest?: string | undefined}>) | undefined}}}; export type CreateApiBookingHotelBookingsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const warning = z.object({code: z.number().int(), title: z.string(), detail: z.string().optional()}); export const associatedRecord = z.object({reference: z.string().min(6).max(6), originSystemCode: z.literal("GDS")}); export const hotelBookingLight = z.object({type: z.string(), id: z.string().min(5).max(19), providerConfirmationId: z.string().min(2).max(16), associatedRecords: z.array(associatedRecord).optional(), self: z.string().optional()}); export const createApiBookingHotelBookingsResponse = z.object({warnings: z.array(warning).optional(), data: z.array(hotelBookingLight).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 }; /** * Create Orders associated to the Hotel Offers * * POST /booking/hotel-bookings */ export const createApiBookingHotelBookings = async (args: CreateApiBookingHotelBookingsArgs, options?: CreateApiBookingHotelBookingsOptions) => { const {baseUrl = "https://test.api.amadeus.com/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/vnd.amadeus+json") headers.set('accept', "application/vnd.amadeus+json") const url = `${baseUrl.replace(/\/+$/, '')}/booking/hotel-bookings${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(createApiBookingHotelBookingsResponse, await res.json()) };