import {z} from 'zod' export type GetApiClientsArgs = {show_inactive?: boolean | undefined, email?: string | undefined, fname?: string | undefined, lname?: string | undefined, external_user_id?: string | undefined, page?: number | undefined, per_page?: number | undefined}; export type GetApiClientsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const entityTag = z.object({id: z.number().int().optional(), name: z.string().optional()}); export const entityTagList = z.array(entityTag); export const clientEntityMembership = z.object({entity_id: z.number().int().optional(), entity_name: z.string().nullable().optional(), entity_type: z.string().nullable().optional(), external_entity_id: z.string().nullable().optional(), role: z.string().nullable().optional(), firm_role_id: z.number().int().nullable().optional(), tags: entityTagList.optional()}); export const client = z.object({id: z.number().int().optional(), email: z.string().nullable().optional(), fname: z.string().nullable().optional(), lname: z.string().nullable().optional(), full_name: z.string().nullable().optional(), time_zone: z.string().nullable().optional(), phone_number: z.string().nullable().optional(), preferred_name: z.string().nullable().optional(), created_at: z.string().optional(), last_activity_at: z.string().nullable().optional(), preferred_sign_in_method: z.string().nullable().optional(), active: z.boolean().nullable().optional(), work_phone_number: z.string().nullable().optional(), work_phone_extension: z.string().nullable().optional(), work_email: z.string().nullable().optional(), external_user_id: z.string().nullable().optional(), entities: z.array(clientEntityMembership).optional(), assigned_advisor_id: z.number().int().nullable().optional(), assigned_advisor: z.string().nullable().optional()}); export const getApiClientsResponse = z.array(client); 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 }; /** * List clients * * GET /clients */ export const getApiClients = async (args: GetApiClientsArgs, options?: GetApiClientsOptions) => { const {baseUrl = "https://api.soraban.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.show_inactive != null) query.append("show_inactive", String(args.show_inactive)) if (args.email != null) query.append("email", String(args.email)) if (args.fname != null) query.append("fname", String(args.fname)) if (args.lname != null) query.append("lname", String(args.lname)) if (args.external_user_id != null) query.append("external_user_id", String(args.external_user_id)) if (args.page != null) query.append("page", String(args.page)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/clients${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(getApiClientsResponse, await res.json()) };