import {z} from 'zod' export type GetApiUserPatientPatientIdDocumentsArgs = {TENANT: string, 'API-KEY': string, patient_id: string, page?: (number | null) | undefined, per_page?: (number | null) | undefined, sort_by?: (string | null) | undefined, sort_direction?: ('asc' | 'desc' | null) | undefined, location_id?: (string | null) | undefined}; export type GetApiUserPatientPatientIdDocumentsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const documentType = z.enum(["LAB", "PHARMACY", "IMAGE", "REPORT", "OTHER", "CONSENT", "NOTE_UPLOAD"]); export const documentSummary = z.object({id: z.string(), patientId: z.string(), filePath: z.string(), fileType: z.string(), title: z.string(), summary: z.string().nullable(), docType: documentType, signatureUrl: z.string().nullable()}); export const pageDocumentSummary = z.object({data: z.array(documentSummary), total: z.number().int()}); 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 Patient Documents * * GET /user/patient/{patient_id}/documents */ export const getApiUserPatientPatientIdDocuments = async (args: GetApiUserPatientPatientIdDocumentsArgs, options: GetApiUserPatientPatientIdDocumentsOptions) => { 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'])) if (args.page != null) query.append("page", String(args.page)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (args.sort_by != null) query.append("sort_by", String(args.sort_by)) if (args.sort_direction != null) query.append("sort_direction", String(args.sort_direction)) if (args.location_id != null) query.append("location_id", String(args.location_id)) const url = `${baseUrl.replace(/\/+$/, '')}/user/patient/${encodeURIComponent(String(args.patient_id))}/documents${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(pageDocumentSummary, await res.json()) };