import {z} from 'zod' export type GetApiContactsArgs = {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, phoneNumber?: (string | null) | undefined}; export type GetApiContactsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiContactsResponse = z.object({data: z.array(z.object({id: z.number().int().gt(0), type: z.string().nullable(), name: z.string().nullable(), title: z.string().nullable(), firstName: z.string().nullable(), lastName: z.string().nullable(), preferredName: z.string().nullable(), occupation: z.string().nullable(), companyName: z.string().nullable(), providerNumber: z.string().nullable(), doctorType: z.string().nullable(), associatedPatientIds: z.array(z.number()).nullable(), associatedInvoiceIds: z.array(z.number()).nullable(), associatedCaseIds: z.array(z.number()).nullable(), email: z.string().nullable(), addressL1: z.string().nullable(), addressL2: z.string().nullable(), addressL3: z.string().nullable(), suburb: z.string().nullable(), state: z.string().nullable(), postalCode: z.string().nullable(), country: z.string().nullable(), phoneNumbers: z.array(z.object({type: z.enum(["Mobile", "Home", "Work", "Fax", "Other"]).nullable().optional(), code: z.string().nullable().optional(), phoneNumber: z.string()})).optional(), archived: 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 contacts * * GET /contacts */ export const getApiContacts = async (args: GetApiContactsArgs, options?: GetApiContactsOptions) => { 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.phoneNumber != null) query.append("phoneNumber", String(args.phoneNumber)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/contacts${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(getApiContactsResponse, await res.json()) };