import {z} from 'zod' export type BiDiLabConnection = {alias?: (string | null) | undefined, enabled?: boolean | undefined, id?: number | undefined, priority?: number | undefined, protocol?: (string | null) | undefined}; export type LabOrderTestCompendium = {code?: (string | null) | undefined, created_date?: (string | null) | undefined, deleted_date?: (string | null) | undefined, id?: number | undefined, lab_vendor: number, last_updated?: string | undefined, name: string}; export type LabVendor = {bidi_connections?: Array | undefined, compendiums?: (Array | null) | undefined, default_compendium?: (LabOrderTestCompendium | null) | undefined, display_name: string, has_order_compendium?: boolean | undefined, has_test_compendium?: boolean | undefined, id?: number | undefined, is_bidi?: boolean | undefined, is_in_house?: boolean | undefined, name: string, orders_integration_available?: boolean | undefined, practice_created?: (number | null) | undefined, results_integration_available?: boolean | undefined}; export type CreateApiApi20LabVendorsArgs = {body: LabVendor}; export type CreateApiApi20LabVendorsOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const biDiLabConnection = z.object({alias: z.string().nullable().optional(), enabled: z.boolean().optional(), id: z.number().int().optional(), priority: z.number().int().gte(0).lte(65535).optional(), protocol: z.string().nullable().optional()}); export const labOrderTestCompendium = z.object({code: z.string().max(200).nullable().optional(), created_date: z.string().nullable().optional(), deleted_date: z.string().nullable().optional(), id: z.number().int().optional(), lab_vendor: z.number().int(), last_updated: z.string().optional(), name: z.string().max(200)}); export const labVendor = z.object({bidi_connections: z.array(biDiLabConnection).optional(), compendiums: z.array(labOrderTestCompendium).nullable().optional(), default_compendium: labOrderTestCompendium.nullable().optional(), display_name: z.string().max(100), has_order_compendium: z.boolean().optional(), has_test_compendium: z.boolean().optional(), id: z.number().int().optional(), is_bidi: z.boolean().optional(), is_in_house: z.boolean().optional(), name: z.string().max(50), orders_integration_available: z.boolean().optional(), practice_created: z.number().int().nullable().optional(), results_integration_available: z.boolean().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 }; /** * Create Lab Vendor * * POST /api/2.0/lab_vendors/ */ export const createApiApi20LabVendors = async (args: CreateApiApi20LabVendorsArgs, options?: CreateApiApi20LabVendorsOptions) => { const {baseUrl = "https://sandbox.elationemr.com/api/2.0", fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/2.0/lab_vendors/${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(labVendor, await res.json()) };