import {z} from 'zod' export type BookingFinishPartner = {amount_sell_b2b2c?: string | undefined, comment?: string | undefined, partner_order_id?: string | undefined}; export type BookingFinishPaymentType = {amount?: string | undefined, currency_code?: string | undefined, init_uuid?: string | undefined, pay_uuid?: string | undefined, type?: string | undefined}; export type BookingFinishGuest = {age?: number | undefined, first_name?: string | undefined, gender?: string | undefined, is_child?: boolean | undefined, last_name?: string | undefined}; export type BookingFinishRoom = {guests?: Array | undefined}; export type BookingFinishSupplierData = {email?: string | undefined, first_name_original?: string | undefined, last_name_original?: string | undefined, phone?: string | undefined}; export type BookingFinishUpsellAttributes = {checkin_datetime?: string | undefined, checkout_datetime?: string | undefined}; export type BookingFinishUpsell = {attributes?: BookingFinishUpsellAttributes | undefined, name?: string | undefined, rule_id?: number | undefined, uid?: string | undefined}; export type BookingFinishUser = {comment?: string | undefined, email?: string | undefined, phone?: string | undefined}; export type BookingFinishRequest = {arrival_datetime?: string | undefined, book_timeout?: number | undefined, language?: string | undefined, partner?: BookingFinishPartner | undefined, payment_type?: BookingFinishPaymentType | undefined, return_path?: string | undefined, rooms?: Array | undefined, supplier_data?: BookingFinishSupplierData | undefined, upsell_data?: Array | undefined, user?: BookingFinishUser | undefined}; export type CreateApiApiB2bV3HotelOrderBookingFinishArgs = {body: BookingFinishRequest}; export type CreateApiApiB2bV3HotelOrderBookingFinishOptions = {baseUrl: string, BasicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const v3DebugEndpoint = z.object({endpoint: z.string().optional(), is_active: z.boolean().optional(), is_limited: z.boolean().optional(), remaining: z.number().int().optional(), requests_number: z.number().int().optional(), reset: z.string().optional(), seconds_number: z.number().int().optional()}); export const v3Debug = z.object({api_endpoint: v3DebugEndpoint.optional(), api_key_id: z.number().int().optional(), key_id: z.number().int().optional(), method: z.string().optional(), real_ip: z.string().optional(), request: z.array(z.number().int()).optional(), request_id: z.string().optional(), utcnow: z.string().optional(), validation_error: z.string().optional()}); export const v3Response = z.object({data: z.unknown(), debug: v3Debug.optional(), error: z.string().optional(), status: z.string().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 }; /** * Booking finish * * POST /api/b2b/v3/hotel/order/booking/finish */ export const createApiApiB2bV3HotelOrderBookingFinish = async (args: CreateApiApiB2bV3HotelOrderBookingFinishArgs, options: CreateApiApiB2bV3HotelOrderBookingFinishOptions) => { const {baseUrl, fetch: fetchFn = fetch, BasicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BasicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${BasicAuth.username}:${BasicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/b2b/v3/hotel/order/booking/finish${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(v3Response, await res.json()) };