import {z} from 'zod' export type BankAccountSort = {orderBy?: ('ID' | 'NAME') | undefined, direction?: ('ASC' | 'DESC') | undefined}; export type BankAccountStatusListFilter = {in?: (Array<'ACTIVE' | 'INACTIVE' | 'REMOVED'>) | undefined, notIn?: (Array<'ACTIVE' | 'INACTIVE' | 'REMOVED'>) | undefined, equal?: ('ACTIVE' | 'INACTIVE' | 'REMOVED') | undefined, notEqual?: ('ACTIVE' | 'INACTIVE' | 'REMOVED') | undefined, greaterThan?: ('ACTIVE' | 'INACTIVE' | 'REMOVED') | undefined, greaterThanEqual?: ('ACTIVE' | 'INACTIVE' | 'REMOVED') | undefined, lessThan?: ('ACTIVE' | 'INACTIVE' | 'REMOVED') | undefined, lessThanEqual?: ('ACTIVE' | 'INACTIVE' | 'REMOVED') | undefined}; export type JournalEntryFilter = {expand?: boolean | undefined}; export type IntegrationFilter = {expand?: boolean | undefined}; export type ApiNameListFilter = {in?: (Array<'BOX' | 'CLOVER' | 'STRIPE' | 'PLAID' | 'KNOX' | 'UBER' | 'FRESHBOOKS' | 'HARVEST' | 'KW' | 'FRESHBOOKS2' | 'CAPITAL_ONE' | 'SQUARE' | 'DOTLOOP' | 'REALOGY' | 'UPWORK' | 'SHOPIFY' | 'DEDUCTR' | 'ENTERPRISE' | 'MOXIWORKS' | 'APPDIRECT' | 'LONEWOLF' | 'LONEWOLF_TRANSACTIONDESK' | 'SAMPLE_BANK' | 'TAXBOT' | 'GUSTO' | 'XERO' | 'UNIT' | 'MOOV' | 'QBO' | 'QUALPAY' | 'BENCH' | 'PARTNER_IMPLEMENTATION' | 'CHECK' | 'RUTTER' | 'WAVE' | 'MANUAL_ENTRY' | 'CSV_IMPORT' | 'ADYEN' | 'PAYABLI'>) | undefined, notIn?: (Array<'BOX' | 'CLOVER' | 'STRIPE' | 'PLAID' | 'KNOX' | 'UBER' | 'FRESHBOOKS' | 'HARVEST' | 'KW' | 'FRESHBOOKS2' | 'CAPITAL_ONE' | 'SQUARE' | 'DOTLOOP' | 'REALOGY' | 'UPWORK' | 'SHOPIFY' | 'DEDUCTR' | 'ENTERPRISE' | 'MOXIWORKS' | 'APPDIRECT' | 'LONEWOLF' | 'LONEWOLF_TRANSACTIONDESK' | 'SAMPLE_BANK' | 'TAXBOT' | 'GUSTO' | 'XERO' | 'UNIT' | 'MOOV' | 'QBO' | 'QUALPAY' | 'BENCH' | 'PARTNER_IMPLEMENTATION' | 'CHECK' | 'RUTTER' | 'WAVE' | 'MANUAL_ENTRY' | 'CSV_IMPORT' | 'ADYEN' | 'PAYABLI'>) | undefined, equal?: ('BOX' | 'CLOVER' | 'STRIPE' | 'PLAID' | 'KNOX' | 'UBER' | 'FRESHBOOKS' | 'HARVEST' | 'KW' | 'FRESHBOOKS2' | 'CAPITAL_ONE' | 'SQUARE' | 'DOTLOOP' | 'REALOGY' | 'UPWORK' | 'SHOPIFY' | 'DEDUCTR' | 'ENTERPRISE' | 'MOXIWORKS' | 'APPDIRECT' | 'LONEWOLF' | 'LONEWOLF_TRANSACTIONDESK' | 'SAMPLE_BANK' | 'TAXBOT' | 'GUSTO' | 'XERO' | 'UNIT' | 'MOOV' | 'QBO' | 'QUALPAY' | 'BENCH' | 'PARTNER_IMPLEMENTATION' | 'CHECK' | 'RUTTER' | 'WAVE' | 'MANUAL_ENTRY' | 'CSV_IMPORT' | 'ADYEN' | 'PAYABLI') | undefined, notEqual?: ('BOX' | 'CLOVER' | 'STRIPE' | 'PLAID' | 'KNOX' | 'UBER' | 'FRESHBOOKS' | 'HARVEST' | 'KW' | 'FRESHBOOKS2' | 'CAPITAL_ONE' | 'SQUARE' | 'DOTLOOP' | 'REALOGY' | 'UPWORK' | 'SHOPIFY' | 'DEDUCTR' | 'ENTERPRISE' | 'MOXIWORKS' | 'APPDIRECT' | 'LONEWOLF' | 'LONEWOLF_TRANSACTIONDESK' | 'SAMPLE_BANK' | 'TAXBOT' | 'GUSTO' | 'XERO' | 'UNIT' | 'MOOV' | 'QBO' | 'QUALPAY' | 'BENCH' | 'PARTNER_IMPLEMENTATION' | 'CHECK' | 'RUTTER' | 'WAVE' | 'MANUAL_ENTRY' | 'CSV_IMPORT' | 'ADYEN' | 'PAYABLI') | undefined}; export type BankAccountFilterParam = {status?: BankAccountStatusListFilter | undefined, journalEntry?: JournalEntryFilter | undefined, integration?: IntegrationFilter | undefined, apiName?: ApiNameListFilter | undefined}; export type BankAccountQueryParam = {cursor?: (string | null) | undefined, limit?: number | undefined, sort?: BankAccountSort | undefined, filter?: BankAccountFilterParam | undefined}; export type CreateApiV6BanksAccountsQueryArgs = {body: BankAccountQueryParam}; export type CreateApiV6BanksAccountsQueryOptions = {baseUrl?: string | undefined, EmbedToken?: string | undefined, fetch?: typeof fetch | undefined}; export const integrationExpanded = z.object({id: z.string().nullable().optional(), apiInstitutionId: z.string().nullable().optional()}).nullable(); export const journalEntryExpanded = z.object({id: z.string().nullable(), tranDate: z.string().nullable()}).nullable(); export const beginningBalanceDto = z.object({journalEntry: journalEntryExpanded.nullable().optional(), amount: z.number().int().nullable().optional()}); export const bankAccountGetDto = z.object({id: z.string(), status: z.enum(["ACTIVE", "INACTIVE", "REMOVED"]), apiName: z.enum(["PLAID", "UNIT", "STRIPE", "SHOPIFY", "SQUARE", "RUTTER", "UBER", "MOOV", "QUALPAY", "MANUAL_ENTRY", "CSV_IMPORT", "PARTNER_IMPLEMENTATION", "QBO", "XERO", "BENCH", "WAVE"]), iconUrl: z.string().nullable(), name: z.string(), mask: z.string().nullable(), integration: integrationExpanded.nullable(), type: z.enum(["DEPOSITORY", "CREDIT", "EQUITY", "LOAN", "INVESTMENT", "OTHER"]), subType: z.string().nullable(), lastSyncedDate: z.string().nullable(), syncTransactions: z.boolean(), isSupported: z.boolean(), tranStartDate: z.string().nullable(), tranMinStartDate: z.string().nullable(), latestTransactionDate: z.string().nullable(), glAccountId: z.string().nullable(), beginningBalanceData: beginningBalanceDto, accountBalance: z.number().int().nullable()}); export const responseCursor = z.object({after: z.string().optional(), before: z.string().optional()}); export const paginatedResponseBankAccountGetDto = z.object({result: z.enum(["SUCCESS", "FAILURE"]).optional(), data: z.array(bankAccountGetDto).optional(), cursor: responseCursor.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 }; /** * Query bank accounts * * POST /v6/banks/accounts/query */ export const createApiV6BanksAccountsQuery = async (args: CreateApiV6BanksAccountsQueryArgs, options?: CreateApiV6BanksAccountsQueryOptions) => { const {baseUrl = "https://sandbox.tight.com", fetch: fetchFn = fetch, EmbedToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (EmbedToken !== undefined) headers.set('authorization', `Bearer ${EmbedToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/v6/banks/accounts/query${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(paginatedResponseBankAccountGetDto, await res.json()) };