import {z} from 'zod' export type JsonNode = {empty?: boolean | undefined, array?: boolean | undefined, null?: boolean | undefined, object?: boolean | undefined, float?: boolean | undefined, string?: boolean | undefined, number?: boolean | undefined, missingNode?: boolean | undefined, valueNode?: boolean | undefined, nodeType?: ('ARRAY' | 'BINARY' | 'BOOLEAN' | 'MISSING' | 'NULL' | 'NUMBER' | 'OBJECT' | 'POJO' | 'STRING') | undefined, pojo?: boolean | undefined, integralNumber?: boolean | undefined, floatingPointNumber?: boolean | undefined, short?: boolean | undefined, int?: boolean | undefined, long?: boolean | undefined, double?: boolean | undefined, bigDecimal?: boolean | undefined, bigInteger?: boolean | undefined, textual?: boolean | undefined, boolean?: boolean | undefined, binary?: boolean | undefined, container?: boolean | undefined, embeddedValue?: boolean | undefined} | null; export type BankAccountPostDto = {id?: string | undefined, name?: string | undefined, status?: ('ACTIVE' | 'INACTIVE' | 'REMOVED') | undefined, apiName?: ('PLAID' | 'UNIT' | 'STRIPE' | 'SHOPIFY' | 'SQUARE' | 'RUTTER' | 'UBER' | 'MOOV' | 'QUALPAY' | 'MANUAL_ENTRY' | 'CSV_IMPORT' | 'PARTNER_IMPLEMENTATION' | 'QBO' | 'XERO' | 'BENCH' | 'WAVE') | undefined, iconUrl?: string | undefined, mask?: string | undefined, type?: ('DEPOSITORY' | 'CREDIT' | 'EQUITY' | 'LOAN' | 'INVESTMENT' | 'OTHER') | undefined, syncTransactions?: boolean | undefined, tranStartDate?: string | undefined, glAccountId?: string | undefined, beginningBalance?: number | undefined, customData?: JsonNode | undefined}; export type MultiParamBankAccountPostDto = {data: Array}; export type CreateApiV6BanksAccountsArgs = {body: MultiParamBankAccountPostDto}; export type CreateApiV6BanksAccountsOptions = {baseUrl?: string | undefined, EmbedToken?: string | undefined, fetch?: typeof fetch | undefined}; export const httpStatus = z.union([z.literal(200), z.literal(400), z.literal(401), z.literal(403), z.literal(404), z.literal(500)]); 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 errorItem = z.object({type: z.enum(["UNKNOWN_ERROR", "INVALID_TOKEN", "EXPIRED_TOKEN", "FORBIDDEN", "BAD_REQUEST", "JSON_ERROR", "DUPLICATE", "REQUIRED", "INVALID_VALUE", "NOT_MODIFIABLE", "INVALID_FORMAT", "NOT_FOUND"]).optional(), message: z.string().optional(), param: z.string().optional()}); export const multiResponseBankAccountGetDto = z.object({result: z.enum(["SUCCESS", "FAILURE"]).optional(), statusCode: httpStatus.optional(), data: bankAccountGetDto.optional(), error: z.array(errorItem).optional()}); export const responseListMultiResponseBankAccountGetDto = z.object({result: z.enum(["SUCCESS", "FAILURE"]).optional(), data: z.array(multiResponseBankAccountGetDto).optional(), error: z.array(errorItem).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 }; /** * Save bank accounts * * POST /v6/banks/accounts */ export const createApiV6BanksAccounts = async (args: CreateApiV6BanksAccountsArgs, options?: CreateApiV6BanksAccountsOptions) => { 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.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(responseListMultiResponseBankAccountGetDto, await res.json()) };