import {z} from 'zod' export type ClientCreateRequest = {fname: string, lname: string, email: string, assigned_advisor_id?: number | undefined, entity_type?: string | undefined, company_name?: string | undefined, external_user_id?: string | undefined, external_entity_id?: string | undefined, phone_number?: string | undefined, work_phone_number?: string | undefined, work_phone_extension?: string | undefined, work_email?: string | undefined, preferred_contact_method?: string | undefined, time_zone?: string | undefined, street?: string | undefined, city?: string | undefined, state?: string | undefined, zip?: string | undefined, country?: string | undefined, business_email?: string | undefined, other_email?: string | undefined}; export type CreateApiClientsArgs = {body: ClientCreateRequest}; export type CreateApiClientsOptions = {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 entityWithoutClient = z.object({id: z.number().int().optional(), entity_type: z.string().nullable().optional(), name: z.string().nullable().optional(), tax_return_name: z.string().nullable().optional(), street: z.string().nullable().optional(), state: z.string().nullable().optional(), city: z.string().nullable().optional(), zip: z.string().nullable().optional(), country: z.string().nullable().optional(), created_at: z.string().optional(), upload_link: z.string().nullable().optional(), business_email: z.string().nullable().optional(), other_email: z.string().nullable().optional(), external_entity_id: z.string().nullable().optional(), deleted_at: z.string().nullable().optional(), hash_id: z.string().nullable().optional()}); export const createApiClientsResponse = z.object({user: client.optional(), entity: entityWithoutClient.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 }; /** * Create a client * * POST /clients */ export const createApiClients = async (args: CreateApiClientsArgs, options?: CreateApiClientsOptions) => { const {baseUrl = "https://api.soraban.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/clients${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiClientsResponse, await res.json()) };