import {z} from 'zod' export type MedicationWithDoseCreate = {name: string, unit?: (string | null) | undefined, patientId: string, providerId?: (string | null) | undefined, doseDate?: (string | null) | undefined, sideEffects?: (string | null) | undefined, administrationLocation?: (string | null) | undefined, amount?: (number | null) | undefined, quantity?: number | undefined, frequency?: (string | null) | undefined, comment?: (string | null) | undefined, lotNumber?: (string | null) | undefined, stockId?: (string | null) | undefined, noteId?: (string | null) | undefined, signedOffProviderId?: (string | null) | undefined, signedOffDate?: (string | null) | undefined, isUsedAsTemplate?: boolean | undefined}; export type CreateApiUserPatientDosesArgs = {TENANT: string, 'API-KEY': string, body: MedicationWithDoseCreate}; export type CreateApiUserPatientDosesOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const userType = z.enum(["PROVIDER", "PATIENT", "ASSISTANT"]); export const userTiny = z.object({id: z.string(), firstName: z.string().nullable().optional(), lastName: z.string().nullable().optional(), phoneNumber: z.string().nullable().optional(), email: z.string().nullable().optional(), type: userType, locationId: z.string().nullable().optional()}); export const doseSummary = z.object({medicationId: z.string(), patientId: z.string(), providerId: z.string().nullable().optional(), doseDate: z.string().nullable().optional(), name: z.string().nullable().optional(), unit: z.string().nullable().optional(), sideEffects: z.string().nullable().optional(), administrationLocation: z.string().nullable().optional(), amount: z.number().nullable().optional(), quantity: z.number().optional(), frequency: z.string().nullable().optional(), comment: z.string().nullable().optional(), lotNumber: z.string().nullable().optional(), stockId: z.string().nullable().optional(), noteId: z.string().nullable().optional(), signedOffProviderId: z.string().nullable().optional(), signedOffDate: z.string().nullable().optional(), isArchived: z.boolean().optional(), id: z.string(), createdDate: z.string(), updatedDate: z.string().nullable().optional(), provider: userTiny.nullable().optional(), signedOffProvider: userTiny.nullable().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 Dose * * POST /user/patient/doses */ export const createApiUserPatientDoses = async (args: CreateApiUserPatientDosesArgs, options: CreateApiUserPatientDosesOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") 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(/\/+$/, '')}/user/patient/doses${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(doseSummary, await res.json()) };