import {z} from 'zod' export type GeneratedFlightLaterDatesQuery = {origin: string, destination: string, departure_date: string, days?: number | undefined, minimum_nights?: number | undefined, round_trip?: boolean | undefined}; export type CreateApiConvFlightOffersCalendarLaterArgs = {body: GeneratedFlightLaterDatesQuery}; export type CreateApiConvFlightOffersCalendarLaterOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const generatedFlightLaterService = z.object({airline_code: z.string().nullable(), airline_name: z.string().nullable(), stops: z.number().int(), connection_airports: z.array(z.string())}); export const generatedFlightLaterOption = z.object({amount: z.number().int().gt(0), services: z.array(z.number().int())}); export const generatedFlightLaterPrice = z.object({amount: z.number().int().gt(0), services: z.array(z.number().int()), nights: z.number().int().gte(0).lte(14), alternative: generatedFlightLaterOption.nullable().optional()}); export const generatedFlightLaterDay = z.object({departure_date: z.string(), one_way: generatedFlightLaterPrice.nullable(), round_trips: z.array(generatedFlightLaterPrice)}); export const generatedFlightLaterDates = z.object({issued_at: z.string().nullable(), services: z.array(generatedFlightLaterService), days: z.array(generatedFlightLaterDay)}); 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 }; /** * Later Dates * * POST /conv/flight-offers/calendar/later */ export const createApiConvFlightOffersCalendarLater = async (args: CreateApiConvFlightOffersCalendarLaterArgs, options: CreateApiConvFlightOffersCalendarLaterOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/conv/flight-offers/calendar/later${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(generatedFlightLaterDates, await res.json()) };