import {z} from 'zod' export type LocationType = 'Gateway' | 'Resort'; export type DetailedLocation = {name?: (string | null) | undefined, translatedName?: (string | null) | undefined, type: LocationType, id?: (string | null) | undefined, countryCode: string, gatewayCode?: (string | null) | undefined, latitude: number, longitude: number, addressParts?: (Array | null) | undefined}; export type VehicleCode = string; export type QuoteVehicle = {code: VehicleCode, type: string, title: string, isShared: boolean, isWheelchairAccessible: boolean, maxPassengers?: (number | null) | undefined}; export type QuotePassengers = {total: number, adults: number, children: number, infants: number, ages?: (Array | null) | undefined}; export type DateTimeLocal = string; export type QuoteExtra = {id: string, name: string, translatedName: string, quantity: number, unitRate: number, notes?: (string | null) | undefined}; export type QuoteTransferRequest = {origin: DetailedLocation, destination: DetailedLocation, vehicle: QuoteVehicle, passengers: QuotePassengers, transferDateTime: DateTimeLocal, extras?: (Array | null) | undefined}; export type QuoteRequest = {transfers: Array}; export type CreateApiV1QuotesArgs = {body: QuoteRequest}; export type CreateApiV1QuotesOptions = {baseUrl?: string | undefined, Basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const vehicleCode = z.string().min(1).max(30); export const quoteVehicle = z.object({code: vehicleCode, type: z.string().min(1).max(100), title: z.string().min(1).max(100), isShared: z.boolean(), isWheelchairAccessible: z.boolean(), maxPassengers: z.number().int().gte(1).lte(100).nullable().optional()}); export const dateTimeUtc = z.string().min(20).max(33); export const quoteTransferPrice = z.object({baseRate: z.number(), extras: z.number().nullable().optional(), nightSupplement: z.number().nullable().optional(), weekdaySupplement: z.number().nullable().optional(), totalRate: z.number()}); export const quoteExtra = z.object({id: z.string().min(1).max(10), name: z.string().min(1).max(250), translatedName: z.string().min(1).max(250), quantity: z.number().int().gte(1).lte(100), unitRate: z.number(), notes: z.string().min(1).max(1000).nullable().optional()}); export const transferQuote = z.object({id: z.string().min(1).max(1024), vehicle: quoteVehicle, validUntilUtc: dateTimeUtc, price: quoteTransferPrice, subSupplierName: z.string().min(1).max(100).nullable().optional(), extras: z.array(quoteExtra).nullable().optional()}); export const quoteTransferResponse = z.object({quotes: z.array(transferQuote)}); export const quoteResponse = z.object({transfers: z.array(quoteTransferResponse)}); 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 }; /** * Creates a Booking Quote. * * POST /v1/quotes */ export const createApiV1Quotes = async (args: CreateApiV1QuotesArgs, options?: CreateApiV1QuotesOptions) => { const {baseUrl = "https://your.domain.com", fetch: fetchFn = fetch, Basic} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Basic !== undefined) headers.set('authorization', `Basic ${btoa(`${Basic.username}:${Basic.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/quotes${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(quoteResponse, await res.json()) };