import {z} from 'zod' export type GetApiTreatmentsAllSinceArgs = {since: number}; export type GetApiTreatmentsAllSinceOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const prescriptionReferenceDto = z.object({caption: z.string().optional(), uuid: z.string()}); export const therapyReferenceDto = z.object({caption: z.string().optional(), uuid: z.string()}); export const treatmentDto = z.object({additionalNotes: z.string().max(4096).optional(), changeDate: z.string().optional(), creationDate: z.string().optional(), dose: z.string().max(512).optional(), executingClinician: z.string().max(512).optional(), inJurisdiction: z.boolean().optional(), prescription: prescriptionReferenceDto.optional(), pseudonymized: z.boolean().optional(), route: z.enum(["ORAL", "IV", "RECTAL", "TOPICAL", "OTHER"]).optional(), routeDetails: z.string().max(512).optional(), therapy: therapyReferenceDto.optional(), treatmentDateTime: z.string().optional(), treatmentDetails: z.string().max(512).optional(), treatmentType: z.enum(["DRUG_INTAKE", "ORAL_REHYDRATION_SALTS", "BLOOD_TRANSFUSION", "RENAL_REPLACEMENT_THERAPY", "IV_FLUID_THERAPY", "OXYGEN_THERAPY", "INVASIVE_MECHANICAL_VENTILATION", "VASOPRESSORS_INOTROPES", "OTHER"]).optional(), typeOfDrug: z.enum(["ANTIMICROBIAL", "ANTIVIRAL", "ANTIBIOTIC", "OTHER"]).optional(), uuid: z.string().min(20).max(36).optional()}); export const getApiTreatmentsAllSinceResponse = z.array(treatmentDto); 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 /treatments/all/{since} */ export const getApiTreatmentsAllSince = async (args: GetApiTreatmentsAllSinceArgs, options: GetApiTreatmentsAllSinceOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json; charset=UTF-8") const url = `${baseUrl.replace(/\/+$/, '')}/treatments/all/${encodeURIComponent(String(args.since))}${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(getApiTreatmentsAllSinceResponse, await res.json()) };