import {z} from 'zod' export type GetApiV2QuoteMultiGroupHashHashcodeArgs = {Authorization?: string | undefined, groupHash: string, hashcode: string}; export type GetApiV2QuoteMultiGroupHashHashcodeOptions = {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 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 quote from multi-quote by hashcode * * GET /v2/quote/multi/{groupHash}/{hashcode} */ export const getApiV2QuoteMultiGroupHashHashcode = async (args: GetApiV2QuoteMultiGroupHashHashcodeArgs, options?: GetApiV2QuoteMultiGroupHashHashcodeOptions) => { const {baseUrl = "https://partnerapi.withfaye.com", fetch: fetchFn = fetch, bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() 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/${encodeURIComponent(String(args.groupHash))}/${encodeURIComponent(String(args.hashcode))}${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(createQuoteResponseDto, await res.json()) };