import {z} from 'zod' export type GetApiV1AccountsAccountIdArgs = {accountId: string}; export type GetApiV1AccountsAccountIdOptions = {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 accountContact = z.object({name: z.string().nullable().optional(), email: z.string().nullable().optional(), title: z.string().nullable().optional(), phone: z.string().nullable().optional()}); export const accountDetail = 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(), contacts: z.array(accountContact).optional(), syncedCrmFields: z.record(z.string(), z.union([z.string(), z.number().int(), z.number(), z.boolean(), z.array(z.unknown()), z.record(z.string(), z.unknown())]).nullable()).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 }; /** * Get Account * * GET /v1/accounts/{accountId} */ export const getApiV1AccountsAccountId = async (args: GetApiV1AccountsAccountIdArgs, options?: GetApiV1AccountsAccountIdOptions) => { 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 (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/accounts/${encodeURIComponent(String(args.accountId))}${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(accountDetail, await res.json()) };