import {z} from 'zod' export type PatientDetails = {first_name: string, last_name: string, dob: string, gender: 'male' | 'female', zip: string, city: string, state?: string | undefined}; export type Model214 = {patient_token?: string | undefined, patient_details?: PatientDetails | undefined}; export type CreateApiApiV1InsuranceEligibilityCheckArgs = {body: Model214}; export type CreateApiApiV1InsuranceEligibilityCheckOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const model215 = z.object({cardholder_id: z.string().optional(), bin: z.string().optional(), group_number: z.string().optional(), pcn: z.string().optional(), plan_type: z.enum(["commercial", "medicare", "medicaid", "other"]).optional(), effective_date: z.string().optional(), termination_date: z.string().optional(), payer_name: z.string().optional(), person_code: z.string().optional(), patient_relationship: 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 }; /** * Check Insurance Eligibility * * POST /api/v1/insurance/eligibility-check */ export const createApiApiV1InsuranceEligibilityCheck = async (args: CreateApiApiV1InsuranceEligibilityCheckArgs, options?: CreateApiApiV1InsuranceEligibilityCheckOptions) => { const {baseUrl = "https://api.truepill.com", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiKey !== undefined) headers.set("Authorization", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/insurance/eligibility-check${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(model215, await res.json()) };