import {z} from 'zod' export type GetApiApi20ContactsArgs = {limit?: number | undefined, npi?: Array | undefined, offset?: number | undefined}; export type GetApiApi20ContactsOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const medicalSpecialtyCategory = z.object({abbreviation: z.string().max(20).nullable().optional(), created_date: z.string().nullable().optional(), id: z.number().int().optional(), name: z.string().max(100)}); export const medicalSpecialty = z.object({abbreviation: z.string().max(20), category: medicalSpecialtyCategory.optional(), created_date: z.string().nullable().optional(), id: z.number().int().optional(), name: z.string().max(100), practice_created: z.number().int().nullable().optional()}); export const contact = z.object({accepting_insurance: z.boolean().nullable().optional(), accepting_new_patients: z.boolean().nullable().optional(), address: z.string().max(200).optional(), back_office_phone: z.string().max(20).optional(), cell_phone: z.string().max(20).optional(), city: z.string().max(50).optional(), contact_type: z.string().max(15), created_date: z.string().nullable().optional(), credentials: z.string().max(200), deleted_date: z.string().nullable().optional(), direct_address: z.string().max(500).optional(), email: z.string().optional(), fax: z.string().max(15).optional(), first_name: z.string().max(50), id: z.number().int().optional(), is_elation_confirmed: z.boolean().nullable().optional(), is_verified: z.boolean().nullable().optional(), last_name: z.string().max(60), middle_name: z.string().max(50), npi: z.string().optional(), org_name: z.string().max(100).nullable().optional(), other_specialties: z.array(medicalSpecialty).optional(), phone: z.string().max(20).optional(), practice: z.number().int(), specialty: medicalSpecialty.optional(), specialty_categories: z.array(z.unknown()).optional(), state: z.string().max(2).optional(), suite: z.string().max(20).optional(), user: z.number().int().optional(), zip: z.string().max(10).optional()}); export const paginatedContactList = z.object({count: z.number().int().optional(), next: z.string().nullable().optional(), previous: z.string().nullable().optional(), results: z.array(contact)}); 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 }; /** * List Contacts * * GET /api/2.0/contacts/ */ export const getApiApi20Contacts = async (args: GetApiApi20ContactsArgs, options?: GetApiApi20ContactsOptions) => { const {baseUrl = "https://sandbox.elationemr.com/api/2.0", fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) for (const item of args.npi ?? []) query.append("npi", String(item)) if (args.offset != null) query.append("offset", String(args.offset)) if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/2.0/contacts/${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(paginatedContactList, await res.json()) };