import {z} from 'zod' export type GetApiConvTripsTripIdHotelFlightDiscountArgs = {trip_id: string, destination?: (string | null) | undefined, nights?: (number | null) | undefined, departure_date?: (string | null) | undefined}; export type GetApiConvTripsTripIdHotelFlightDiscountOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const money = z.object({amount: z.number().int(), currency: z.string()}); export const hotelFlightCashbackTarget = z.object({flight_booking_id: z.string(), flight_booking_session_id: z.string(), payment_intent_id: z.string(), maximum: money}); export const latLng = z.object({latitude: z.number().gte(-90).lte(90), longitude: z.number().gte(-180).lte(180)}); export const viewport = z.object({sw: latLng, ne: latLng}); export const tripHotelFlightDiscount = z.object({discount: money, destination: z.string().min(3).max(3).nullable().optional(), nights: z.number().int().gt(0), average_stay_total: money.nullable().optional(), sample_size: z.number().int().gte(1).nullable().optional(), sample_bounds: viewport.nullable().optional(), hotel_booking_id: z.string().nullable().optional(), cashback_target: hotelFlightCashbackTarget.nullable().optional(), cashback_status: z.enum(["pending", "processing", "refunded", "review_required"]).nullable().optional(), cashback_obligation_id: z.string().nullable().optional(), cashback_refund_id: z.string().nullable().optional()}); export const hotelInProgress = z.object({hotel_search_booking_session_id: z.string(), hotel_search_session_id: z.string().nullable().optional(), hotel_id: z.number().int(), hotel_name: z.string().nullable().optional(), check_in: z.string(), check_out: z.string(), checkout_step: z.string()}); export const tripHotelFlightDiscountResponse = z.object({money_back_offer_eligible: z.boolean().optional(), cashback_target: hotelFlightCashbackTarget.nullable().optional(), offer_set_id: z.string().nullable().optional(), hotel_flight_discount: tripHotelFlightDiscount.nullable().optional(), destination: z.string().nullable().optional(), nights: z.number().int().nullable().optional(), destination_city: z.string().nullable().optional(), flight_discount_eligible: z.boolean().optional(), flight_booked: z.boolean().nullable().optional(), hotel_in_progress: hotelInProgress.nullable().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 }; /** * Get Hotel Flight Discount * * GET /conv/trips/{trip_id}/hotel-flight-discount */ export const getApiConvTripsTripIdHotelFlightDiscount = async (args: GetApiConvTripsTripIdHotelFlightDiscountArgs, options: GetApiConvTripsTripIdHotelFlightDiscountOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.destination != null) query.append("destination", String(args.destination)) if (args.nights != null) query.append("nights", String(args.nights)) if (args.departure_date != null) query.append("departure_date", String(args.departure_date)) if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/conv/trips/${encodeURIComponent(String(args.trip_id))}/hotel-flight-discount${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(tripHotelFlightDiscountResponse, await res.json()) };