import {z} from 'zod' export type FlightOfferSource = 'GDS'; export type TravelClass = 'ECONOMY' | 'PREMIUM_ECONOMY' | 'BUSINESS' | 'FIRST'; export type Co2Emission = {weight?: number | undefined, weightUnit?: string | undefined, cabin?: TravelClass | undefined}; export type FlightEndPoint = {iataCode?: string | undefined, terminal?: string | undefined, at?: string | undefined}; export type AircraftEquipment = {code?: string | undefined}; export type OperatingFlight = {carrierCode?: string | undefined}; export type FlightStop = {iataCode?: string | undefined, duration?: string | undefined, arrivalAt?: string | undefined, departureAt?: string | undefined}; export type Segment = {id?: string | undefined, numberOfStops?: number | undefined, blacklistedInEU?: boolean | undefined, co2Emissions?: Array | undefined, departure?: FlightEndPoint | undefined, arrival?: FlightEndPoint | undefined, carrierCode?: string | undefined, number?: string | undefined, aircraft?: AircraftEquipment | undefined, operating?: OperatingFlight | undefined, duration?: string | undefined, stops?: Array | undefined}; export type AdditionalServiceType = 'CHECKED_BAGS' | 'MEALS' | 'SEATS' | 'OTHER_SERVICES'; export type FeeType = 'TICKETING' | 'FORM_OF_PAYMENT' | 'SUPPLIER'; export type Fee = {amount?: string | undefined, type?: FeeType | undefined}; export type Tax = {amount?: string | undefined, code?: string | undefined}; export type ExtendedPrice = {margin?: string | undefined, grandTotal?: string | undefined, billingCurrency?: string | undefined, additionalServices?: (Array<{amount?: string | undefined, type?: AdditionalServiceType | undefined}>) | undefined, currency?: string | undefined, total?: string | undefined, base?: string | undefined, fees?: Array | undefined, taxes?: Array | undefined, refundableTaxes?: string | undefined}; export type PricingOptionsFareType = Array<'PUBLISHED'>; export type TravelerPricingFareOption = 'STANDARD' | 'INCLUSIVE_TOUR' | 'SPANISH_MELILLA_RESIDENT' | 'SPANISH_CEUTA_RESIDENT' | 'SPANISH_CANARY_RESIDENT' | 'SPANISH_BALEARIC_RESIDENT' | 'AIR_FRANCE_METROPOLITAN_DISCOUNT_PASS' | 'AIR_FRANCE_DOM_DISCOUNT_PASS' | 'AIR_FRANCE_COMBINED_DISCOUNT_PASS' | 'AIR_FRANCE_FAMILY' | 'ADULT_WITH_COMPANION' | 'COMPANION'; export type TravelerType = 'ADULT' | 'CHILD' | 'SENIOR' | 'YOUNG' | 'HELD_INFANT' | 'SEATED_INFANT' | 'STUDENT'; export type Price = {currency?: string | undefined, total?: string | undefined, base?: string | undefined, fees?: Array | undefined, taxes?: Array | undefined, refundableTaxes?: string | undefined}; export type AllotmentDetails = {tourName?: string | undefined, tourReference?: string | undefined}; export type SliceDiceIndicator = 'LOCAL_AVAILABILITY' | 'SUB_OD_AVAILABILITY_1' | 'SUB_OD_AVAILABILITY_2'; export type BaggageAllowance = {quantity?: number | undefined, weight?: number | undefined, weightUnit?: string | undefined}; export type ChargeableCheckdBags = {quantity?: number | undefined, weight?: number | undefined, weightUnit?: string | undefined, id?: string | undefined}; export type ChargeableSeat = {id?: string | undefined, number?: string | undefined}; export type ServiceName = 'PRIORITY_BOARDING' | 'AIRPORT_CHECKIN'; export type FlightOffer = {type: string, id: string, source?: FlightOfferSource | undefined, instantTicketingRequired?: boolean | undefined, disablePricing?: boolean | undefined, nonHomogeneous?: boolean | undefined, oneWay?: boolean | undefined, paymentCardRequired?: boolean | undefined, lastTicketingDate?: string | undefined, lastTicketingDateTime?: string | undefined, numberOfBookableSeats?: number | undefined, itineraries?: (Array<{duration?: string | undefined, segments: Array}>) | undefined, price?: ExtendedPrice | undefined, pricingOptions?: ({fareType?: PricingOptionsFareType | undefined, includedCheckedBagsOnly?: boolean | undefined, refundableFare?: boolean | undefined, noRestrictionFare?: boolean | undefined, noPenaltyFare?: boolean | undefined}) | undefined, validatingAirlineCodes?: Array | undefined, travelerPricings?: (Array<{travelerId: string, fareOption: TravelerPricingFareOption, travelerType: TravelerType, associatedAdultId?: string | undefined, price?: Price | undefined, fareDetailsBySegment: Array<{segmentId: string, cabin?: TravelClass | undefined, fareBasis?: string | undefined, brandedFare?: string | undefined, class?: string | undefined, isAllotment?: boolean | undefined, allotmentDetails?: AllotmentDetails | undefined, sliceDiceIndicator?: SliceDiceIndicator | undefined, includedCheckedBags?: BaggageAllowance | undefined, additionalServices?: ({chargeableCheckedBags?: ChargeableCheckdBags | undefined, chargeableSeat?: ChargeableSeat | undefined, chargeableSeatNumber?: string | undefined, otherServices?: Array | undefined}) | undefined}>}>) | undefined}; export type PaymentBrand = 'VISA' | 'AMERICAN_EXPRESS' | 'MASTERCARD' | 'VISA_ELECTRON' | 'VISA_DEBIT' | 'MASTERCARD_DEBIT' | 'MAESTRO' | 'DINERS' | 'MASTERCARD_IXARIS' | 'VISA_IXARIS' | 'MASTERCARD_AIRPLUS' | 'UATP_AIRPLUS'; export type StakeholderGender = 'MALE' | 'FEMALE'; export type Name = {firstName?: string | undefined, lastName?: string | undefined, middleName?: string | undefined, secondLastName?: string | undefined}; export type DocumentType = 'VISA' | 'PASSPORT' | 'IDENTITY_CARD' | 'KNOWN_TRAVELER' | 'REDRESS'; export type IdentityDocument = {number?: string | undefined, issuanceDate?: string | undefined, expiryDate?: string | undefined, issuanceCountry?: string | undefined, issuanceLocation?: string | undefined, nationality?: string | undefined, birthPlace?: string | undefined, documentType?: DocumentType | undefined, validityCountry?: string | undefined, birthCountry?: string | undefined, holder?: boolean | undefined}; export type EmergencyContact = {addresseeName?: string | undefined, countryCode?: string | undefined, number?: string | undefined, text?: string | undefined}; export type LoyaltyProgram = {programOwner?: string | undefined, id?: string | undefined}; export type DiscountType = 'SPANISH_RESIDENT' | 'AIR_FRANCE_DOMESTIC' | 'AIR_FRANCE_COMBINED' | 'AIR_FRANCE_METROPOLITAN'; export type DiscountTravelerType = 'SPANISH_CITIZEN' | 'EUROPEAN_CITIZEN' | 'GOVERNMENT_WORKER' | 'MILITARY' | 'MINOR_WITHOUT_ID'; export type Discount = {subType?: DiscountType | undefined, cityName?: string | undefined, travelerType?: DiscountTravelerType | undefined, cardNumber?: string | undefined, certificateNumber?: string | undefined}; export type Address = {lines?: Array | undefined, postalCode?: string | undefined, countryCode?: string | undefined, cityName?: string | undefined, stateName?: string | undefined, postalBox?: string | undefined}; export type ContactPurpose = 'STANDARD' | 'INVOICE' | 'STANDARD_WITHOUT_TRANSMISSION'; export type PhoneDeviceType = 'MOBILE' | 'LANDLINE' | 'FAX'; export type Phone = {deviceType?: PhoneDeviceType | undefined, countryCallingCode?: string | undefined, number?: string | undefined}; export type Contact = {addresseeName?: Name | undefined, address?: Address | undefined, language?: string | undefined, purpose?: ContactPurpose | undefined, phones?: Array | undefined, companyName?: string | undefined, emailAddress?: string | undefined}; export type Traveler = {id?: string | undefined, dateOfBirth?: string | undefined, gender?: StakeholderGender | undefined, name?: Name | undefined, documents?: Array | undefined, emergencyContact?: EmergencyContact | undefined, loyaltyPrograms?: Array | undefined, discountEligibility?: Array | undefined, contact?: Contact | undefined}; export type FlightOfferPricingIn = {type: string, flightOffers: Array, payments?: (Array<{brand?: PaymentBrand | undefined, binNumber?: number | undefined, flightOfferIds?: Array | undefined}>) | undefined, travelers?: Array | undefined}; export type CreateApiShoppingFlightOffersPricingArgs = {'X-HTTP-Method-Override': string, include?: (Array<'credit-card-fees' | 'bags' | 'other-services' | 'detailed-fare-rules'>) | undefined, forceClass?: boolean | undefined, body: {data: FlightOfferPricingIn}}; export type CreateApiShoppingFlightOffersPricingOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const issue = z.object({status: z.number().int().optional(), code: z.number().int().optional(), title: z.string().optional(), detail: z.string().optional(), source: z.object({pointer: z.string().optional(), parameter: z.string().optional(), example: z.string().optional()}).optional()}); export const flightOfferSource = z.literal("GDS"); export const travelClass = z.enum(["ECONOMY", "PREMIUM_ECONOMY", "BUSINESS", "FIRST"]); export const co2Emission = z.object({weight: z.number().int().optional(), weightUnit: z.string().optional(), cabin: travelClass.optional()}); export const flightEndPoint = z.object({iataCode: z.string().optional(), terminal: z.string().optional(), at: z.string().optional()}); export const aircraftEquipment = z.object({code: z.string().optional()}); export const operatingFlight = z.object({carrierCode: z.string().min(1).max(2).optional()}); export const flightStop = z.object({iataCode: z.string().optional(), duration: z.string().optional(), arrivalAt: z.string().optional(), departureAt: z.string().optional()}); export const segment = z.object({id: z.string().optional(), numberOfStops: z.number().int().optional(), blacklistedInEU: z.boolean().optional(), co2Emissions: z.array(co2Emission).optional(), departure: flightEndPoint.optional(), arrival: flightEndPoint.optional(), carrierCode: z.string().min(1).max(2).optional(), number: z.string().min(1).max(4).optional(), aircraft: aircraftEquipment.optional(), operating: operatingFlight.optional(), duration: z.string().optional(), stops: z.array(flightStop).optional()}); export const additionalServiceType = z.enum(["CHECKED_BAGS", "MEALS", "SEATS", "OTHER_SERVICES"]); export const feeType = z.enum(["TICKETING", "FORM_OF_PAYMENT", "SUPPLIER"]); export const fee = z.object({amount: z.string().optional(), type: feeType.optional()}); export const tax = z.object({amount: z.string().optional(), code: z.string().optional()}); export const extendedPrice = z.object({margin: z.string().optional(), grandTotal: z.string().optional(), billingCurrency: z.string().optional(), additionalServices: z.array(z.object({amount: z.string().optional(), type: additionalServiceType.optional()})).optional(), currency: z.string().optional(), total: z.string().optional(), base: z.string().optional(), fees: z.array(fee).optional(), taxes: z.array(tax).optional(), refundableTaxes: z.string().optional()}); export const pricingOptionsFareType = z.array(z.literal("PUBLISHED")); export const travelerPricingFareOption = z.enum(["STANDARD", "INCLUSIVE_TOUR", "SPANISH_MELILLA_RESIDENT", "SPANISH_CEUTA_RESIDENT", "SPANISH_CANARY_RESIDENT", "SPANISH_BALEARIC_RESIDENT", "AIR_FRANCE_METROPOLITAN_DISCOUNT_PASS", "AIR_FRANCE_DOM_DISCOUNT_PASS", "AIR_FRANCE_COMBINED_DISCOUNT_PASS", "AIR_FRANCE_FAMILY", "ADULT_WITH_COMPANION", "COMPANION"]); export const travelerType = z.enum(["ADULT", "CHILD", "SENIOR", "YOUNG", "HELD_INFANT", "SEATED_INFANT", "STUDENT"]); export const price = z.object({currency: z.string().optional(), total: z.string().optional(), base: z.string().optional(), fees: z.array(fee).optional(), taxes: z.array(tax).optional(), refundableTaxes: z.string().optional()}); export const allotmentDetails = z.object({tourName: z.string().optional(), tourReference: z.string().optional()}); export const sliceDiceIndicator = z.enum(["LOCAL_AVAILABILITY", "SUB_OD_AVAILABILITY_1", "SUB_OD_AVAILABILITY_2"]); export const baggageAllowance = z.object({quantity: z.number().int().optional(), weight: z.number().int().optional(), weightUnit: z.string().optional()}); export const chargeableCheckdBags = z.object({quantity: z.number().int().optional(), weight: z.number().int().optional(), weightUnit: z.string().optional(), id: z.string().optional()}); export const chargeableSeat = z.object({id: z.string().optional(), number: z.string().optional()}); export const serviceName = z.enum(["PRIORITY_BOARDING", "AIRPORT_CHECKIN"]); export const flightOffer = z.object({type: z.string(), id: z.string(), source: flightOfferSource.optional(), instantTicketingRequired: z.boolean().optional(), disablePricing: z.boolean().optional(), nonHomogeneous: z.boolean().optional(), oneWay: z.boolean().optional(), paymentCardRequired: z.boolean().optional(), lastTicketingDate: z.string().optional(), lastTicketingDateTime: z.string().optional(), numberOfBookableSeats: z.number().gte(1).lte(9).optional(), itineraries: z.array(z.object({duration: z.string().optional(), segments: z.array(segment)})).optional(), price: extendedPrice.optional(), pricingOptions: z.object({fareType: pricingOptionsFareType.optional(), includedCheckedBagsOnly: z.boolean().optional(), refundableFare: z.boolean().optional(), noRestrictionFare: z.boolean().optional(), noPenaltyFare: z.boolean().optional()}).optional(), validatingAirlineCodes: z.array(z.string()).optional(), travelerPricings: z.array(z.object({travelerId: z.string(), fareOption: travelerPricingFareOption, travelerType: travelerType, associatedAdultId: z.string().optional(), price: price.optional(), fareDetailsBySegment: z.array(z.object({segmentId: z.string(), cabin: travelClass.optional(), fareBasis: z.string().optional(), brandedFare: z.string().optional(), class: z.string().optional(), isAllotment: z.boolean().optional(), allotmentDetails: allotmentDetails.optional(), sliceDiceIndicator: sliceDiceIndicator.optional(), includedCheckedBags: baggageAllowance.optional(), additionalServices: z.object({chargeableCheckedBags: chargeableCheckdBags.optional(), chargeableSeat: chargeableSeat.optional(), chargeableSeatNumber: z.string().optional(), otherServices: z.array(serviceName).optional()}).optional()}))})).optional()}); export const flightOfferPricingOut = z.object({type: z.string(), flightOffers: z.array(flightOffer), bookingRequirements: z.object({invoiceAddressRequired: z.boolean().optional(), mailingAddressRequired: z.boolean().optional(), emailAddressRequired: z.boolean().optional(), phoneCountryCodeRequired: z.boolean().optional(), mobilePhoneNumberRequired: z.boolean().optional(), phoneNumberRequired: z.boolean().optional(), postalCodeRequired: z.boolean().optional(), travelerRequirements: z.array(z.object({travelerId: z.string().optional(), genderRequired: z.boolean().optional(), documentRequired: z.boolean().optional(), documentIssuanceCityRequired: z.boolean().optional(), dateOfBirthRequired: z.boolean().optional(), redressRequiredIfAny: z.boolean().optional(), airFranceDiscountRequired: z.boolean().optional(), spanishResidentDiscountRequired: z.boolean().optional(), residenceRequired: z.boolean().optional()})).optional()}).optional()}); export const locationEntry = z.unknown(); export const aircraftEntry = z.unknown(); export const currencyEntry = z.unknown(); export const carrierEntry = z.unknown(); export const dictionaries = z.object({locations: locationEntry.optional(), aircraft: aircraftEntry.optional(), currencies: currencyEntry.optional(), carriers: carrierEntry.optional()}); export const createApiShoppingFlightOffersPricingResponse = z.object({warnings: z.array(issue).optional(), data: flightOfferPricingOut, included: z.object({"credit-card-fees": z.unknown(), bags: z.unknown(), "other-services": z.unknown(), "detailed-fare-rules": z.unknown()}).optional(), dictionaries: dictionaries.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 }; /** * Confirm pricing of given flightOffers. * * POST /shopping/flight-offers/pricing */ export const createApiShoppingFlightOffersPricing = async (args: CreateApiShoppingFlightOffersPricingArgs, options?: CreateApiShoppingFlightOffersPricingOptions) => { 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") if (args['X-HTTP-Method-Override'] != null) headers.set("X-HTTP-Method-Override", String(args['X-HTTP-Method-Override'])) for (const item of args.include ?? []) query.append("include", String(item)) if (args.forceClass != null) query.append("forceClass", String(args.forceClass)) const url = `${baseUrl.replace(/\/+$/, '')}/shopping/flight-offers/pricing${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(createApiShoppingFlightOffersPricingResponse, await res.json()) };