import {z} from 'zod' export type StandardCoverageOptionRequestDto = {selected?: boolean | undefined}; export type InitialDepositBaseCoverageOptionRequestDto = {selected?: boolean | undefined, initialDepositDate?: string | undefined}; export type CarDateDto = {startDate: string, endDate: string, noOfCars?: number | undefined}; export type CarRentalCoverageRequestDto = {selected?: boolean | undefined, carDatesDto: Array, noOfCars?: number | undefined}; export type AddonsRequestDto = {petOption?: StandardCoverageOptionRequestDto | undefined, trcOption?: StandardCoverageOptionRequestDto | undefined, cfarOption?: InitialDepositBaseCoverageOptionRequestDto | undefined, cfwrOption?: InitialDepositBaseCoverageOptionRequestDto | undefined, adventureOption?: StandardCoverageOptionRequestDto | undefined, vacationRentalOption?: StandardCoverageOptionRequestDto | undefined, medicalUpgrade?: StandardCoverageOptionRequestDto | undefined, carRentalOption?: CarRentalCoverageRequestDto | undefined}; export type PlaceDetailsDtoV2 = {country?: (string | null) | undefined, areaLevel1?: (string | null) | undefined, locality?: (string | null) | undefined, label?: (string | null) | undefined, googleId?: (string | null) | undefined, fullCountryName?: (string | null) | undefined}; export type ResidencyDtoV2 = {country?: (string | null) | undefined, areaLevel1?: (string | null) | undefined, locality?: (string | null) | undefined, label?: (string | null) | undefined, googleId?: (string | null) | undefined, fullCountryName?: (string | null) | undefined, streetAndNumber?: (string | null) | undefined, city?: (string | null) | undefined, apartment?: (string | null) | undefined, zipCode?: (string | null) | undefined}; export type TravellerDtoV2 = {dateOfBirth: string, firstName?: string | undefined, email?: string | undefined, lastName?: string | undefined, isDriver?: boolean | undefined, mainTraveller?: boolean | undefined, profileImage?: string | undefined, isPlaceHolderTraveller?: boolean | undefined}; export type UtmDto = {key: string, value: string}; export type CreateMultiQuoteDtoV2 = {advisorId?: string | undefined, departureDate: string, returnDate: string, tripCost?: number | undefined, coverageOptions?: AddonsRequestDto | undefined, timezone?: string | undefined, destination: Array, residence?: string | undefined, residency?: ResidencyDtoV2 | undefined, travellers: Array, utms?: Array | undefined, noTravellers?: number | undefined}; export type CreateApiV2QuoteMultiArgs = {Authorization?: string | undefined, body: CreateMultiQuoteDtoV2}; export type CreateApiV2QuoteMultiOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const standardCoverageOptionDto = z.object({available: z.boolean().optional(), selected: z.boolean().optional(), price: z.number().multipleOf(0.01).optional()}); export const carRentalCoverageDto = z.object({available: z.boolean().optional(), selected: z.boolean().optional(), price: z.number().multipleOf(0.01).optional(), pricePerCarPerDay: z.number().multipleOf(0.01)}); export const addonsDto = z.object({petOption: standardCoverageOptionDto, trcOption: standardCoverageOptionDto, cfarOption: standardCoverageOptionDto, cfwrOption: standardCoverageOptionDto, adventureOption: standardCoverageOptionDto, vacationRentalOption: standardCoverageOptionDto, carRentalOption: carRentalCoverageDto, medicalUpgrade: standardCoverageOptionDto}); export const createQuoteResponseDto = z.object({currentPrice: z.number().multipleOf(0.01), coverageOptions: addonsDto, policyExampleUrl: z.string(), quoteUrl: z.string(), quotePageUrl: z.string(), quoteHash: z.string(), offerDescription: z.object({})}); export const multiQuoteItemDto = z.object({product: z.enum(["PR-STANDARD-01", "PR-SPORTS-01", "PR-CFAR-01", "PR-STANDARD-DO", "PR-SPORTS-DO", "PR-CFAR-DO", "PR-STR", "PR-GENERAL-STR", "RETAIL", "PR-FirstCountrywide", "PR-ForwardCountrywide", "PR-FlagshipCountrywide"]), productName: z.string(), quote: createQuoteResponseDto.optional(), error: z.string().optional()}); export const multiQuoteResponseDto = z.object({groupHash: z.string().optional(), quotes: z.array(multiQuoteItemDto), isCompleted: z.boolean().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 }; /** * Generate multiple quotes (V2) * * POST /v2/quote/multi */ export const createApiV2QuoteMulti = async (args: CreateApiV2QuoteMultiArgs, options?: CreateApiV2QuoteMultiOptions) => { const {baseUrl = "https://partnerapi.withfaye.com", fetch: fetchFn = fetch, bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) if (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/quote/multi${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(multiQuoteResponseDto, await res.json()) };