import {z} from 'zod' export type GetApiApi20LabOrderTestsArgs = {code?: (Array) | undefined, compendium?: Array | undefined, lab_vendor?: Array | undefined, limit?: number | undefined, name?: Array | undefined, offset?: number | undefined, order_by?: string | undefined, practice?: Array | undefined, practice_created?: Array | undefined}; export type GetApiApi20LabOrderTestsOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const labOrderAOEQuestionChoice = z.object({code: z.string().max(100), created_date: z.string().nullable().optional(), deleted_date: z.string().nullable().optional(), id: z.number().int().optional(), value: z.string()}); export const questionTypeEnum = z.enum(["enum", "enum_multiselect", "freetext", "y_or_n", "numeric", "yyyymmdd", "2_numeric_1_decimal"]); export const labOrderAOEQuestion = z.object({choices: z.array(labOrderAOEQuestionChoice), code: z.string().max(100), created_date: z.string().nullable().optional(), deleted_date: z.string().nullable().optional(), example_format: z.string().nullable().optional(), id: z.number().int().optional(), max_length: z.number().int().gte(0).lte(65535).nullable().optional(), question_type: questionTypeEnum.optional(), secondary_code: z.string().max(100).nullable().optional(), value: z.string()}); export const labOrderTestToLabOrderAOEQuestion = z.object({question: labOrderAOEQuestion, required: z.boolean().optional(), sequence: z.number().int().gte(0).lte(65535), test: z.number().int().optional()}); export const labOrderTest = z.object({code: z.string().max(50).nullable().optional(), compendium: z.number().int().nullable().optional(), cpts: z.array(z.string()).optional(), created_date: z.string().nullable().optional(), deleted_date: z.string().nullable().optional(), id: z.number().int().optional(), lab_vendor: z.number().int().nullable().optional(), name: z.string().max(255), practice_created: z.number().int().nullable().optional(), procedure_class: z.string().max(50).nullable().optional(), questions: z.array(labOrderTestToLabOrderAOEQuestion).optional(), synonyms: z.array(z.string()).optional()}); export const paginatedLabOrderTestList = z.object({count: z.number().int().optional(), next: z.string().nullable().optional(), previous: z.string().nullable().optional(), results: z.array(labOrderTest)}); 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 Lab Order Tests * * GET /api/2.0/lab_order_tests/ */ export const getApiApi20LabOrderTests = async (args: GetApiApi20LabOrderTestsArgs, options?: GetApiApi20LabOrderTestsOptions) => { 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.code != null) query.append("code", args.code.join(',')) if (args.compendium != null) query.append("compendium", args.compendium.join(',')) if (args.lab_vendor != null) query.append("lab_vendor", args.lab_vendor.join(',')) if (args.limit != null) query.append("limit", String(args.limit)) if (args.name != null) query.append("name", args.name.join(',')) if (args.offset != null) query.append("offset", String(args.offset)) if (args.order_by != null) query.append("order_by", String(args.order_by)) if (args.practice != null) query.append("practice", args.practice.join(',')) if (args.practice_created != null) query.append("practice_created", args.practice_created.join(',')) if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/2.0/lab_order_tests/${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(paginatedLabOrderTestList, await res.json()) };