import {z} from 'zod' export type GetApiBookingV1ServiceOffersArgs = {ratePlanId: string, arrival: string, departure: string, channelCode?: ('Direct' | 'BookingCom' | 'Ibe' | 'ChannelManager' | 'Expedia' | 'Homelike' | 'Hrs' | 'AltoVita' | 'DesVu' | 'Gimsi') | undefined, adults: number, childrenAges?: Array | undefined, onlyDefaultDates?: boolean | undefined, includeUnavailable?: boolean | undefined}; export type GetApiBookingV1ServiceOffersOptions = {baseUrl: string, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const monetaryValueModel = z.object({amount: z.number(), currency: z.string()}); export const serviceModel = z.object({id: z.string(), code: z.string(), name: z.string().min(1), description: z.string().min(1), pricingUnit: z.enum(["Room", "Person"]), defaultGrossPrice: monetaryValueModel}); export const amountModel = z.object({grossAmount: z.number(), netAmount: z.number(), vatType: z.enum(["Null", "VeryReduced", "Reduced", "Normal", "Without", "Special", "Special1", "Special2", "ReducedCovid19", "NormalCovid19", "Mixed"]), vatPercent: z.number(), currency: z.string()}); export const offerFeeModel = z.object({id: z.string(), code: z.string(), name: z.string().min(1), totalAmount: amountModel}); export const serviceOfferItemModel = z.object({serviceDate: z.string(), amount: amountModel, isDefaultDate: z.boolean(), isMandatory: z.boolean(), availableCount: z.number().int().nullable().optional()}); export const offerValidationMessageModel = z.object({code: z.enum(["UnitGroupFullyBooked", "UnitGroupCapacityExceeded", "RatePlanRestrictionsViolated", "RatePlanSurchargesNotSet", "RateRestrictionsViolated", "RatePlanChannelNotSet", "RatesNotSet", "BlockFullyBooked", "IncludedServicesAmountExceededRateAmount", "ServiceFullyBooked", "RateDoesNotSatifyHurdles"]), message: z.string().min(1)}); export const serviceOfferModel = z.object({service: serviceModel, count: z.number().int(), availableCount: z.number().int().nullable().optional(), totalAmount: amountModel, prePaymentAmount: monetaryValueModel, fees: z.array(offerFeeModel).nullable().optional(), dates: z.array(serviceOfferItemModel), validationMessages: z.array(offerValidationMessageModel).nullable().optional()}); export const serviceOffersModel = z.object({services: z.array(serviceOfferModel)}); 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 }; /** * Returns service offers for one specific stay. * * GET /booking/v1/service-offers */ export const getApiBookingV1ServiceOffers = async (args: GetApiBookingV1ServiceOffersArgs, options: GetApiBookingV1ServiceOffersOptions) => { const {baseUrl, fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.ratePlanId != null) query.append("ratePlanId", String(args.ratePlanId)) if (args.arrival != null) query.append("arrival", String(args.arrival)) if (args.departure != null) query.append("departure", String(args.departure)) if (args.channelCode != null) query.append("channelCode", String(args.channelCode)) if (args.adults != null) query.append("adults", String(args.adults)) for (const item of args.childrenAges ?? []) query.append("childrenAges", String(item)) if (args.onlyDefaultDates != null) query.append("onlyDefaultDates", String(args.onlyDefaultDates)) if (args.includeUnavailable != null) query.append("includeUnavailable", String(args.includeUnavailable)) if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/booking/v1/service-offers${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(serviceOffersModel, await res.json()) };