import {z} from 'zod' export type GetApiCasesArgs = {id_gt?: (number | null) | undefined, id_lt?: (number | null) | undefined, update_gt?: (string | string) | undefined, update_lt?: (string | string) | undefined, include_archived?: ('true' | 'false' | null) | undefined, patientId?: (number | null) | undefined, practitionerId?: (number | null) | undefined, contactId?: (number | null) | undefined, trackType?: ('Appointments' | 'Hours' | 'Budget' | null) | undefined, status?: ('Open' | 'Closed' | 'Expired' | null) | undefined}; export type GetApiCasesOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiCasesResponse = z.object({data: z.array(z.object({id: z.number().int().gt(0), caseNumber: z.string().optional(), budget: z.number().nullable().optional(), appointmentCount: z.number().nullable().optional(), contactId: z.number().int().gt(0).nullable(), providerNumber: z.string().nullable().optional(), referralPeriod: z.string().nullable().optional(), issueDate: z.union([z.string(), z.string(), z.unknown()]), expiryDate: z.union([z.string(), z.string(), z.unknown()]), notes: z.string().nullable().optional(), trackType: z.enum(["Appointments", "Hours", "Budget"]).optional(), hours: z.number().nullable(), includeDidNotArrive: z.boolean().nullable().optional(), includeCancelled: z.boolean().nullable().optional(), practitionerId: z.number().int().gt(0).nullable(), items: z.array(z.object({typeId: z.number().int().gt(0), type: z.enum(["Appointment", "SupportActivity", "File", "SupportingActivity", "Letter"])})), organisationId: z.number().int().gt(0), patientId: z.number().int().gt(0), isOpen: z.boolean().nullable().optional(), hasAlert: z.boolean().nullable().optional(), remindTime: z.string().nullable().optional(), utilisationAlert: z.number().nullable().optional(), isAlertUtilizationSent: z.boolean().nullable().optional(), createdAt: z.union([z.string(), z.string()]).optional(), updatedAt: z.union([z.string(), z.string()]).optional()})), links: z.object({previousPage: z.string().optional(), nextPage: z.string().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 }; /** * Get a paged array of cases * * GET /cases */ export const getApiCases = async (args: GetApiCasesArgs, options?: GetApiCasesOptions) => { const {baseUrl = "https://api.splose.com/v1", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.id_gt != null) query.append("id_gt", String(args.id_gt)) if (args.id_lt != null) query.append("id_lt", String(args.id_lt)) if (args.update_gt != null) query.append("update_gt", String(args.update_gt)) if (args.update_lt != null) query.append("update_lt", String(args.update_lt)) if (args.include_archived != null) query.append("include_archived", String(args.include_archived)) if (args.patientId != null) query.append("patientId", String(args.patientId)) if (args.practitionerId != null) query.append("practitionerId", String(args.practitionerId)) if (args.contactId != null) query.append("contactId", String(args.contactId)) if (args.trackType != null) query.append("trackType", String(args.trackType)) if (args.status != null) query.append("status", String(args.status)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/cases${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(getApiCasesResponse, await res.json()) };