import {z} from 'zod' export type GetApiApi20VaccineArgs = {cdc_type?: string | undefined, creation_type?: ('elation' | 'medispan' | 'user') | undefined, cvx?: number | undefined, limit?: number | undefined, name?: string | undefined, offset?: number | undefined, order_by?: string | undefined, practice?: Array | undefined}; export type GetApiApi20VaccineOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const vaccineCreationType = z.enum(["elation", "medispan", "user"]); export const vaccine = z.object({cdc_type: z.string().nullable().optional(), created_date: z.string().nullable().optional(), creation_type: vaccineCreationType.optional(), cvx: z.number().int().nullable().optional(), deleted_date: z.string().nullable().optional(), description: z.string().optional(), id: z.number().int().optional(), is_user_created: z.boolean().optional(), name: z.string().optional(), ndc: z.string().nullable().optional(), ndc_values: z.array(z.string()).optional(), practice: z.number().int().nullable().optional()}); export const paginatedVaccineList = z.object({count: z.number().int().optional(), next: z.string().nullable().optional(), previous: z.string().nullable().optional(), results: z.array(vaccine)}); 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 Vaccines * * GET /api/2.0/vaccine/ */ export const getApiApi20Vaccine = async (args: GetApiApi20VaccineArgs, options?: GetApiApi20VaccineOptions) => { 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.cdc_type != null) query.append("cdc_type", String(args.cdc_type)) if (args.creation_type != null) query.append("creation_type", String(args.creation_type)) if (args.cvx != null) query.append("cvx", String(args.cvx)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.name != null) query.append("name", String(args.name)) 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 (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/2.0/vaccine/${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(paginatedVaccineList, await res.json()) };