import {z} from 'zod' export type GetApiPrescriptionsAllSinceSizeLastSynchronizedUuidArgs = {since: number, size: number, lastSynchronizedUuid: string}; export type GetApiPrescriptionsAllSinceSizeLastSynchronizedUuidOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const therapyReferenceDto = z.object({caption: z.string().optional(), uuid: z.string()}); export const prescriptionDto = z.object({additionalNotes: z.string().max(4096).optional(), changeDate: z.string().optional(), creationDate: z.string().optional(), dose: z.string().max(512).optional(), frequency: z.string().max(512).optional(), inJurisdiction: z.boolean().optional(), prescribingClinician: z.string().max(512).optional(), prescriptionDate: z.string().optional(), prescriptionDetails: z.string().max(512).optional(), prescriptionEnd: z.string().optional(), prescriptionStart: z.string().optional(), prescriptionType: z.enum(["DRUG_INTAKE", "ORAL_REHYDRATION_SALTS", "BLOOD_TRANSFUSION", "RENAL_REPLACEMENT_THERAPY", "IV_FLUID_THERAPY", "OXYGEN_THERAPY", "INVASIVE_MECHANICAL_VENTILATION", "VASOPRESSORS_INOTROPES", "OTHER"]).optional(), pseudonymized: z.boolean().optional(), route: z.enum(["ORAL", "IV", "RECTAL", "TOPICAL", "OTHER"]).optional(), routeDetails: z.string().max(512).optional(), therapy: therapyReferenceDto.optional(), typeOfDrug: z.enum(["ANTIMICROBIAL", "ANTIVIRAL", "ANTIBIOTIC", "OTHER"]).optional(), uuid: z.string().min(20).max(36).optional()}); export const getApiPrescriptionsAllSinceSizeLastSynchronizedUuidResponse = z.array(prescriptionDto); 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 /prescriptions/all/{since}/{size}/{lastSynchronizedUuid} */ export const getApiPrescriptionsAllSinceSizeLastSynchronizedUuid = async (args: GetApiPrescriptionsAllSinceSizeLastSynchronizedUuidArgs, options: GetApiPrescriptionsAllSinceSizeLastSynchronizedUuidOptions) => { 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(/\/+$/, '')}/prescriptions/all/${encodeURIComponent(String(args.since))}/${encodeURIComponent(String(args.size))}/${encodeURIComponent(String(args.lastSynchronizedUuid))}${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(getApiPrescriptionsAllSinceSizeLastSynchronizedUuidResponse, await res.json()) };