import {z} from 'zod' export type GetApiV1PolicyCodePolicyCodeEmailEmailArgs = {Authorization?: string | undefined, policyCode: string, email: unknown}; export type GetApiV1PolicyCodePolicyCodeEmailEmailOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const destination = z.object({country: z.string().optional(), googlePlaceId: z.string().optional(), state: z.string().optional()}); export const policyTraveler = z.object({policyId: z.string(), firstName: z.string(), lastName: z.string(), dateOfBirth: z.string(), email: z.string()}); export const policyGroup = z.object({created: z.string(), groupCode: z.string(), status: z.string(), cancelReason: z.string().optional(), productId: 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"]).optional(), tripCost: z.number(), purchaseDate: z.string(), departureDate: z.string(), returnDate: z.string(), destination: z.array(destination), premium: z.number(), advisorId: z.string(), advisorEmail: z.string(), policyURL: z.string(), policiesInGroup: z.array(policyTraveler)}); export const getPolicyResponse = z.object({policy: policyGroup, relatedPolicies: z.array(policyGroup)}); 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 policy by policyCode and email * * GET /v1/policy/code/{policyCode}/email/{email} */ export const getApiV1PolicyCodePolicyCodeEmailEmail = async (args: GetApiV1PolicyCodePolicyCodeEmailEmailArgs, options?: GetApiV1PolicyCodePolicyCodeEmailEmailOptions) => { 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(/\/+$/, '')}/v1/policy/code/${encodeURIComponent(String(args.policyCode))}/email/${encodeURIComponent(String(args.email))}${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(getPolicyResponse, await res.json()) };