import {z} from 'zod' export type AccountListSortField = 'name' | 'population' | 'signals'; export type AccountListSortDirection = 'asc' | 'desc'; export type AccountListSort = {field?: AccountListSortField | undefined, direction?: AccountListSortDirection | undefined}; export type AccountListFilters = {territory_ids?: (Array | null) | undefined, exclude_territory_ids?: (Array | null) | undefined, account_types?: (Array | null) | undefined, states?: (Array | null) | undefined, counties?: (Array | null) | undefined, cities?: (Array | null) | undefined, population_min?: (number | null) | undefined, population_max?: (number | null) | undefined, has_signals?: (boolean | null) | undefined}; export type AccountListRequest = {limit?: number | undefined, cursor?: (string | null) | undefined, search?: (string | null) | undefined, sort?: AccountListSort | undefined, filters?: AccountListFilters | undefined}; export type CreateApiApiV3AccountsListArgs = {body: AccountListRequest}; export type CreateApiApiV3AccountsListOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const accountContactPreview = z.object({id: z.string(), full_name: z.string().nullable().optional()}); export const accountTerritoryResponse = z.object({id: z.string(), name: z.string()}); export const accountCRMResponse = z.object({crm_type: z.string(), crm_id: z.string()}); export const accountListItem = z.object({institution_id: z.string(), name: z.string().nullable(), account_type: z.string(), city: z.string().nullable(), state: z.string().nullable(), population: z.number().nullable(), signal_count: z.number().int().optional(), contact_count: z.number().int().optional(), contacts: z.array(accountContactPreview).optional(), territories: z.array(accountTerritoryResponse).optional(), crms: z.array(accountCRMResponse).optional()}); export const accountListResponse = z.object({items: z.array(accountListItem), total: z.number().int(), has_more: z.boolean(), next_cursor: z.string().nullable().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 }; /** * List Accounts * * POST /api/v3/accounts/list */ export const createApiApiV3AccountsList = async (args: CreateApiApiV3AccountsListArgs, options: CreateApiApiV3AccountsListOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v3/accounts/list${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(accountListResponse, await res.json()) };