import {z} from 'zod' export type LodgifyApiPublicReservationsModelsV1BookingStatusDto = 'Open' | 'Booked' | 'Declined' | 'Tentative'; export type LodgifyApiPublicReservationsModelsV12GuestBreakdownDto = {adults?: number | undefined, children?: number | undefined, infants?: number | undefined, pets?: number | undefined}; export type LodgifyApiPublicReservationsModelsV1CreateBookingRoomTypeDto = {room_type_id?: number | undefined, guest_breakdown: LodgifyApiPublicReservationsModelsV12GuestBreakdownDto, people?: number | undefined, key_code?: (string | null) | undefined}; export type LodgifyApiPublicReservationsModelsV1GuestNameDto = {first_name?: (string | null) | undefined, last_name?: (string | null) | undefined, full_name?: (string | null) | undefined}; export type LodgifyApiPublicReservationsModelsV1CreateBookingGuestDto = {guest_name: LodgifyApiPublicReservationsModelsV1GuestNameDto, name?: (string | null) | undefined, external_id?: (string | null) | undefined, email?: (string | null) | undefined, phone?: (string | null) | undefined, locale?: (string | null) | undefined, street_address1?: (string | null) | undefined, street_address2?: (string | null) | undefined, city?: (string | null) | undefined, country_code?: (string | null) | undefined, postal_code?: (string | null) | undefined, state?: (string | null) | undefined}; export type LodgifyApiPublicReservationsModelsV1BookingThreadMessageTypeDto = 'Owner' | 'Comment' | 'Renter'; export type LodgifyApiPublicReservationsModelsV1CreateBookingMessageDto = {subject?: (string | null) | undefined, message?: (string | null) | undefined, type?: LodgifyApiPublicReservationsModelsV1BookingThreadMessageTypeDto | undefined, send_notification?: boolean | undefined}; export type LodgifyApiPublicReservationsModelsV1BookabilityEnum = 'InstantBooking' | 'BookingRequest' | 'EnquiryOnly'; export type LodgifyApiPublicReservationsModelsV1CreateBookingDto = {source_text?: (string | null) | undefined, arrival?: string | undefined, departure?: string | undefined, property_id?: number | undefined, status?: LodgifyApiPublicReservationsModelsV1BookingStatusDto | undefined, rooms?: (Array | null) | undefined, guest?: LodgifyApiPublicReservationsModelsV1CreateBookingGuestDto | undefined, messages?: (Array | null) | undefined, payment_type?: (string | null) | undefined, payment_address?: (string | null) | undefined, payment_website_id?: (number | null) | undefined, bookability?: LodgifyApiPublicReservationsModelsV1BookabilityEnum | undefined, ip_created?: (string | null) | undefined, total?: (number | null) | undefined, currency_code?: (string | null) | undefined, origin?: (string | null) | undefined, thread_id?: (string | null) | undefined}; export type CreateApiV1ReservationBookingArgs = {from?: number | undefined, body: LodgifyApiPublicReservationsModelsV1CreateBookingDto}; export type CreateApiV1ReservationBookingOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV1ReservationBookingResponse = z.number().int(); 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 }; /** * Creates a new booking * * POST /v1/reservation/booking */ export const createApiV1ReservationBooking = async (args: CreateApiV1ReservationBookingArgs, options?: CreateApiV1ReservationBookingOptions) => { const {baseUrl = "https://api.lodgify.com", fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.from != null) query.append("from", String(args.from)) if (ApiKey !== undefined) headers.set("X-ApiKey", ApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/reservation/booking${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(createApiV1ReservationBookingResponse, await res.json()) };