import {z} from 'zod' export type GetApiNotesArgs = {limit?: number | undefined, offset?: number | undefined, practice_id?: (number | null) | undefined, is_draft?: (string | null) | undefined, patient?: (number | null) | undefined, providers?: (string | null) | undefined}; export type GetApiNotesOptions = {baseUrl?: string | undefined, RequireAuthentication?: string | undefined, fetch?: typeof fetch | undefined}; export const cODETYPE = z.enum(["CPT", "SNOMED", "HL7", "ICD9", "LOINC", "CVX", "RXNORM", "ICD10", "CPTII", "HCPCS", "EL8"]); export const doctag = z.object({code: z.string().nullable().optional(), code_type: cODETYPE.nullable().optional(), concept_name: z.string().nullable().optional(), description: z.string().nullable().optional(), id: z.number().int(), is_cpc_plus: z.boolean(), is_mips: z.boolean(), practice: z.number().int().nullable().optional(), shared: z.boolean(), snomed_result_code: z.string().nullable().optional(), value: z.string()}); export const noteMetadata = z.object({bill: z.number().int().nullable().optional(), category: z.number().int(), confidential: z.boolean().optional(), doctags: z.array(doctag).optional(), id: z.number().int(), is_draft: z.boolean(), metadata: z.unknown(), note_v1_id: z.number().int().nullable().optional(), patient: z.number().int(), practice: z.number().int(), provider: z.number().int(), serviced_at: z.string().nullable()}); export const paginatedResponseNoteMetadata = z.object({count: z.number().int(), limit: z.number().int(), next: z.string().nullable().optional(), offset: z.number().int(), previous: z.string().nullable().optional(), results: z.array(noteMetadata)}); 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 Notes * * GET /notes * * Also accepts auth: RequirePracticeAuthorization */ export const getApiNotes = async (args: GetApiNotesArgs, options?: GetApiNotesOptions) => { const {baseUrl = "https://sandbox.elationemr.com/api/2.0", fetch: fetchFn = fetch, RequireAuthentication} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.practice_id != null) query.append("practice_id", String(args.practice_id)) if (args.is_draft != null) query.append("is_draft", String(args.is_draft)) if (args.patient != null) query.append("patient", String(args.patient)) if (args.providers != null) query.append("providers", String(args.providers)) if (RequireAuthentication !== undefined) headers.set('authorization', `Bearer ${RequireAuthentication}`) const url = `${baseUrl.replace(/\/+$/, '')}/notes${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(paginatedResponseNoteMetadata, await res.json()) };