import {z} from 'zod' export type GetApiNotesCustomBlocksArgs = {limit?: number | undefined, offset?: number | undefined}; export type GetApiNotesCustomBlocksOptions = {baseUrl?: string | undefined, RequireAuthentication?: string | undefined, fetch?: typeof fetch | undefined}; export const aiGenerationInfo = z.object({is_finalized: z.boolean().optional(), transcript_source_text: z.string().nullable().optional()}); export const dynamicFormAttrs = z.object({ai_generation_info: aiGenerationInfo.nullable().optional(), creation_type: z.enum(["ai", "template"]).nullable().optional(), data: z.record(z.string(), z.unknown()).optional(), dataSchema: z.unknown(), is_new: z.boolean().nullable().optional(), label: z.string(), lastEditedBy: z.record(z.string(), z.enum(["user", "api", "note-prediction", "template"])).optional(), ref_id: z.string().nullable().optional(), uiSchema: z.unknown()}); export const dynamicFormNodeOutput = z.object({attrs: dynamicFormAttrs, type: z.literal("dynamicForm").optional()}).and(z.record(z.string(), z.unknown())); export const customBlockResponse = z.object({aliases: z.array(z.string()).nullable().optional(), content: z.array(dynamicFormNodeOutput).optional(), created_by: z.number().int().nullable().optional(), id: z.number().int(), is_system_template: z.boolean(), label: z.string(), practice_id: z.number().int()}); export const paginatedResponseCustomBlockResponse = 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(customBlockResponse)}); 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 Custom Blocks * * GET /notes/custom-blocks */ export const getApiNotesCustomBlocks = async (args: GetApiNotesCustomBlocksArgs, options?: GetApiNotesCustomBlocksOptions) => { 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 (RequireAuthentication !== undefined) headers.set('authorization', `Bearer ${RequireAuthentication}`) const url = `${baseUrl.replace(/\/+$/, '')}/notes/custom-blocks${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(paginatedResponseCustomBlockResponse, await res.json()) };