import {z} from 'zod' export type GetApiMedicationsArgs = {TENANT: string, 'API-KEY': string}; export type GetApiMedicationsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const unit = z.enum(["AMPOULES", "APPLICATORS", "BOTTLES", "BOXES", "CAPSULES", "CUPS", "DOSES", "G_PER_ML", "GRAMS", "IU", "IU_PER_ML", "OUNCES", "FLUID_OUNCES", "KG", "KITS", "LITERS", "MCG", "MG", "MG_PER_ML", "ML", "MMOL", "MICROGRAM_PER_ML", "PACKS", "PAIRS", "PATCHES", "PERCENTAGE", "PIECES", "POUCHES", "ROLLS", "SACHETS", "SHEETS", "SPRAYS", "STRIPS", "SYRINGES", "TABLETS", "TABLETS_PER_DAY", "TESTS", "TUBES", "UNITS", "VIALS", "TREATMENTS", "SESSIONS", "PULSES", "JOULES", "CENTIMETERS", "THREAD_COUNT", "INJECTIONS", "WRINKLE_UNITS", "POUNDS", "FEET", "INCHES"]); export const medicationTemplateSummary = z.object({name: z.string(), amount: z.number().nullable().optional(), unit: unit.nullable().optional(), administrationLocation: z.string().nullable().optional(), frequency: z.string().nullable().optional(), quantity: z.number().optional(), comment: z.string().nullable().optional(), lotNumber: z.string().nullable().optional(), id: z.string()}); export const medicationSummary = z.object({name: z.string(), unit: unit.nullable().optional(), brandName: z.string().nullable().optional(), description: z.string().nullable().optional(), price: z.number().int().optional(), category: z.string().nullable().optional(), internalNotes: z.string().nullable().optional(), sortOrder: z.number().int().nullable().optional(), taxable: z.boolean(), allowCustomPricing: z.boolean(), isUsedAsTemplate: z.boolean().optional(), templateAmount: z.number().nullable().optional(), templateAdministrationLocation: z.string().nullable().optional(), templateFrequency: z.string().nullable().optional(), templateQuantity: z.number().nullable().optional(), templateComment: z.string().nullable().optional(), templateLotNumber: z.string().nullable().optional(), id: z.string(), createdDate: z.string(), updatedDate: z.string().nullable().optional(), isPhysical: z.boolean(), isArchived: z.boolean(), template: medicationTemplateSummary.nullable().optional()}); export const getApiMedicationsResponse = z.array(medicationSummary); 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 }; /** * Get Medications * * GET /medications */ export const getApiMedications = async (args: GetApiMedicationsArgs, options: GetApiMedicationsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.TENANT != null) headers.set("TENANT", String(args.TENANT)) if (args['API-KEY'] != null) headers.set("API-KEY", String(args['API-KEY'])) const url = `${baseUrl.replace(/\/+$/, '')}/medications${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(getApiMedicationsResponse, await res.json()) };