import {z} from 'zod' export type GetApiV1AccountsArgs = {name?: (string | null) | undefined, website?: (string | null) | undefined, owner?: (string | null) | undefined, createdBefore?: (string | null) | undefined, createdAfter?: (string | null) | undefined, lastActivityBefore?: (string | null) | undefined, lastActivityAfter?: (string | null) | undefined, limit?: number | undefined, cursor?: (string | null) | undefined}; export type GetApiV1AccountsOptions = {baseUrl?: string | undefined, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const accountOwner = z.object({name: z.string().nullable().optional(), email: z.string().nullable().optional()}); export const latestDeal = z.object({dealId: z.string(), name: z.string().nullable().optional(), stage: z.string().nullable().optional(), amount: z.number().nullable().optional(), closeDate: z.string().nullable().optional()}); export const accountListItem = z.object({accountId: z.string(), name: z.string().nullable().optional(), website: z.string().nullable().optional(), owner: accountOwner.nullable().optional(), createdDate: z.string().nullable().optional(), lastActivityDate: z.string().nullable().optional(), latestDeal: latestDeal.nullable().optional()}); export const paginationInfo = z.object({nextCursor: z.string().nullable().optional(), hasMore: z.boolean().optional()}); export const listAccountsResponse = z.object({accounts: z.array(accountListItem), pagination: paginationInfo}); 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 * * GET /v1/accounts */ export const getApiV1Accounts = async (args: GetApiV1AccountsArgs, options?: GetApiV1AccountsOptions) => { const {baseUrl = "https://api.sybill.ai", fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.name != null) query.append("name", String(args.name)) if (args.website != null) query.append("website", String(args.website)) if (args.owner != null) query.append("owner", String(args.owner)) if (args.createdBefore != null) query.append("createdBefore", String(args.createdBefore)) if (args.createdAfter != null) query.append("createdAfter", String(args.createdAfter)) if (args.lastActivityBefore != null) query.append("lastActivityBefore", String(args.lastActivityBefore)) if (args.lastActivityAfter != null) query.append("lastActivityAfter", String(args.lastActivityAfter)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/accounts${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(listAccountsResponse, await res.json()) };