import {z} from 'zod' export type Document = {authoring_practice: number, chart_date: string, created_date?: (string | null) | undefined, deleted_date?: (string | null) | undefined, document_date: string, document_type?: (number | null) | undefined, id: number, last_modified?: string | undefined, patient: number, sign_date?: (string | null) | undefined, signed_by?: number | undefined}; export type ThreadMessage = {body: string, id?: number | undefined, patient?: number | undefined, practice?: number | undefined, send_date: string, sender: number, thread?: number | undefined, to_document?: Document | undefined}; export type CreateApiApi20ThreadMessagesArgs = {body: ThreadMessage}; export type CreateApiApi20ThreadMessagesOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const document = z.object({authoring_practice: z.number().int(), chart_date: z.string(), created_date: z.string().nullable().optional(), deleted_date: z.string().nullable().optional(), document_date: z.string(), document_type: z.number().int().nullable().optional(), id: z.number().int(), last_modified: z.string().optional(), patient: z.number().int(), sign_date: z.string().nullable().optional(), signed_by: z.number().int().optional()}); export const threadMessage = z.object({body: z.string(), id: z.number().int().optional(), patient: z.number().int().optional(), practice: z.number().int().optional(), send_date: z.string(), sender: z.number().int(), thread: z.number().int().optional(), to_document: document.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 Thread Message * * POST /api/2.0/thread_messages/ */ export const createApiApi20ThreadMessages = async (args: CreateApiApi20ThreadMessagesArgs, options?: CreateApiApi20ThreadMessagesOptions) => { 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/thread_messages/${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(threadMessage, await res.json()) };