import {z} from 'zod' export type GetApiV1FinancialConnectionsTransactionsArgs = {account: string, ending_before?: string | undefined, expand?: Array | undefined, limit?: number | undefined, starting_after?: string | undefined, transacted_at?: ({gt?: number | undefined, gte?: number | undefined, lt?: number | undefined, lte?: number | undefined} | number) | undefined, transaction_refresh?: {after: string} | undefined, body: Record}; export type GetApiV1FinancialConnectionsTransactionsOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const bankConnectionsResourceTransactionResourceStatusTransitions = z.object({posted_at: z.number().int().nullable().optional(), void_at: z.number().int().nullable().optional()}); export const financialConnectionsTransaction = z.object({account: z.string().max(5000), amount: z.number().int(), currency: z.string().max(5000), description: z.string().max(5000), id: z.string().max(5000), livemode: z.boolean(), object: z.literal("financial_connections.transaction"), status: z.enum(["pending", "posted", "void"]), status_transitions: bankConnectionsResourceTransactionResourceStatusTransitions, transacted_at: z.number().int(), transaction_refresh: z.string().max(5000), updated: z.number().int()}); export const getApiV1FinancialConnectionsTransactionsResponse = z.object({data: z.array(financialConnectionsTransaction), has_more: z.boolean(), object: z.literal("list"), url: z.string().max(5000)}); 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 Transactions * * GET /v1/financial_connections/transactions * * Also accepts auth: bearerAuth */ export const getApiV1FinancialConnectionsTransactions = async (args: GetApiV1FinancialConnectionsTransactionsArgs, options?: GetApiV1FinancialConnectionsTransactionsOptions) => { const {baseUrl = "https://api.stripe.com", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/x-www-form-urlencoded") headers.set('accept', "application/json") if (args.account != null) query.append("account", String(args.account)) if (args.ending_before != null) query.append("ending_before", String(args.ending_before)) for (const item of args.expand ?? []) query.append("expand", String(item)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.starting_after != null) query.append("starting_after", String(args.starting_after)) if (args.transacted_at != null) query.append("transacted_at", String(args.transacted_at)) if (args.transaction_refresh != null) query.append("transaction_refresh", JSON.stringify(args.transaction_refresh)) if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/financial_connections/transactions${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers, body: new URLSearchParams(Object.entries(args.body).flatMap(([key, value]) => (value == null ? [] : [[key, String(value)]])))}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiV1FinancialConnectionsTransactionsResponse, await res.json()) };