import {z} from 'zod' export type Destination = {country?: string | undefined, googlePlaceId?: string | undefined, state?: string | undefined}; 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 AddressDto = {addressLine1: string, addressLine2?: string | undefined, city: string, state: string, zip: string, country: string}; export type ProspectiveCustomerDto = {firstName: string, lastName: string, email?: string | undefined, dob: string}; export type UtmDto = {key: string, value: string}; export type CreatePurchaseDto = {advisorId?: string | undefined, departureDate: string, returnDate: string, destination: Array, tripCost?: number | undefined, coverageOptions?: AddonsRequestDto | undefined, productId?: ('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') | undefined, productIds?: Array | undefined, productCode: string, timezone?: string | undefined, address: AddressDto, primaryEmail: string, travelerDetails: Array, paymentToken?: string | undefined, paymentIntent?: string | undefined, paymentMethod?: string | undefined, paymentCustomerId?: string | undefined, paymentService?: ('stripe' | 'checkOut') | undefined, expectedPrice: number, externalId?: string | undefined, noTravellers?: number | undefined, utms?: Array | undefined}; export type CreateApiV1PurchaseArgs = {Authorization?: string | undefined, body: CreatePurchaseDto}; export type CreateApiV1PurchaseOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const policyholderDto = z.object({name: z.string(), planId: z.string()}); export const purchasePolicyDto = z.object({purchaseId: z.string(), plans: z.array(policyholderDto).optional(), totalPrice: z.number().multipleOf(0.01), paymentIntent: z.string().optional(), policyUrl: z.string().optional(), receiptUrl: z.string().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 }; /** * Purchase coverage * * POST /v1/purchase */ export const createApiV1Purchase = async (args: CreateApiV1PurchaseArgs, options?: CreateApiV1PurchaseOptions) => { 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(/\/+$/, '')}/v1/purchase${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(purchasePolicyDto, await res.json()) };