import {z} from 'zod' export type GetApiPatientsArgs = {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, firstname?: (string | null) | undefined, lastname?: (string | null) | undefined, email?: (string | null) | undefined, birthdate?: (string | null) | undefined, phoneNumber?: (string | null) | undefined, patientTag?: (string | null) | undefined}; export type GetApiPatientsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiPatientsResponse = z.object({data: z.array(z.object({firstname: z.string(), lastname: z.string(), middleName: z.string().nullable().optional(), preferredName: z.string().nullable().optional(), title: z.string().nullable().optional(), email: z.string().nullable().optional(), sex: z.string().nullable().optional(), genderIdentity: z.string().nullable().optional(), pronouns: z.string().nullable().optional(), alert: z.string().nullable().optional(), invoiceRecipientId: z.number().int().gt(0).nullable().optional(), birthdate: z.union([z.string(), z.string(), z.unknown()]).optional(), timezone: z.string().nullable().optional(), extraInfo: z.string().nullable().optional(), extraBillingInfo: z.string().nullable().optional(), addressL1: z.string().nullable().optional(), addressL2: z.string().nullable().optional(), addressL3: z.string().nullable().optional(), city: z.string().nullable().optional(), state: z.string().nullable().optional(), postalCode: z.string().nullable().optional(), country: z.string().nullable().optional(), phoneNumbers: z.array(z.object({type: z.enum(["Mobile", "Home", "Work", "Fax", "Other"]).nullable().optional(), code: z.string().nullable().optional(), phoneNumber: z.string()})).nullable().optional(), privacyPolicy: z.enum(["No response", "Accepted", "Rejected", ""]).nullable().optional(), ndisNumber: z.string().nullable().optional(), ndisInfo: z.object({diagnosis: z.string().nullable().optional(), nomineeEmail: z.string().nullable().optional(), fundManagement: z.string().nullable().optional(), nomineeLastName: z.string().nullable().optional(), nomineeFirstName: z.string().nullable().optional(), nomineeMobileCode: z.string().nullable().optional(), nomineeMobileNumber: z.string().nullable().optional(), startDate: z.string().nullable().optional(), endDate: z.string().nullable().optional()}).nullable().optional(), medicareNum: z.string().nullable().optional(), irn: z.string().nullable().optional(), veteransFileNumber: z.string().nullable().optional(), emergencyContactNumber: z.string().nullable().optional(), emergencyContactName: z.string().nullable().optional(), emergencyContactRelationship: z.string().nullable().optional(), communicationPreference: z.enum(["NONE", "SMS", "SMS_EMAIL", "EMAIL"]).optional(), archived: z.boolean().nullable().optional(), id: z.number().int().gt(0), patientTags: z.array(z.string()).nullable().optional(), customFields: z.array(z.object({name: z.string(), value: z.union([z.string(), z.number()])})).nullable().optional(), healthFund: z.object({name: z.string(), membershipNumber: z.string().nullable().optional(), patientNumber: z.string().nullable().optional(), cardIssueNumber: z.string().nullable().optional()}).nullable().optional(), createdAt: z.union([z.string(), z.string()]).optional(), updatedAt: z.union([z.string(), z.string()]).optional(), deletedAt: z.union([z.string(), z.string(), z.unknown()])})), 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 patients * * GET /patients */ export const getApiPatients = async (args: GetApiPatientsArgs, options?: GetApiPatientsOptions) => { 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.firstname != null) query.append("firstname", String(args.firstname)) if (args.lastname != null) query.append("lastname", String(args.lastname)) if (args.email != null) query.append("email", String(args.email)) if (args.birthdate != null) query.append("birthdate", String(args.birthdate)) if (args.phoneNumber != null) query.append("phoneNumber", String(args.phoneNumber)) if (args.patientTag != null) query.append("patientTag", String(args.patientTag)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/patients${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(getApiPatientsResponse, await res.json()) };