import {z} from 'zod' export type GetApiAccountsIdTransactionsArgs = {id: string, limit?: number | undefined, pageKey?: string | undefined, startTime?: string | undefined, endTime?: string | undefined}; export type GetApiAccountsIdTransactionsOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const page = z.object({nextPageKey: z.string().optional(), totalElements: z.number().int().optional()}); export const accountCategory = z.literal("DEPOSIT_ACCOUNT"); export const transactionDirection = z.enum(["DEBIT", "CREDIT"]); export const transactionStatus = z.enum(["PENDING", "POSTED", "AUTHORIZATION", "MEMO"]); export const lineItem = z.object({description: z.string().optional(), amount: z.number().optional(), checkNumber: z.number().int().optional(), memo: z.string().optional(), reference: z.string().optional()}); export const transactionReward = z.object({categoryId: z.string().optional(), accrued: z.number().optional(), adjusted: z.number().optional()}); export const link = z.object({href: z.string()}); export const fiAttribute = z.object({name: z.string(), value: z.string()}); export const depositTransactionType = z.enum(["ADJUSTMENT", "ATMDEPOSIT", "ATMWITHDRAWAL", "BILLPAYMENT", "CHECK", "DEPOSIT", "DIRECTDEPOSIT", "DIVIDEND", "FEE", "INTEREST", "POSCREDIT", "POSDEBIT", "PREAUTHORIZEDDEPOSIT", "PREAUTHORIZEDWITHDRAWAL", "TRANSFER", "WITHDRAWAL"]); export const depositTransaction = z.object({accountCategory: accountCategory, accountId: z.string(), transactionId: z.string(), transactionTimestamp: z.string(), postedTimestamp: z.string().optional(), description: z.string(), memo: z.string().optional(), debitCreditMemo: transactionDirection, category: z.string().optional(), subCategory: z.string().optional(), status: transactionStatus, amount: z.number(), lineItem: z.array(lineItem).optional(), reward: transactionReward.optional(), reference: z.string().optional(), links: z.array(link).optional(), fiAttributes: z.array(fiAttribute).optional(), transactionType: depositTransactionType.optional(), payee: z.string().optional(), checkNumber: z.number().int().optional()}); export const listTransactionsResponse = z.object({page: page.optional(), transactions: z.array(depositTransaction)}); 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 account transactions * * GET /accounts/{id}/transactions */ export const getApiAccountsIdTransactions = async (args: GetApiAccountsIdTransactionsArgs, options?: GetApiAccountsIdTransactionsOptions) => { const {baseUrl = "https://api.givechariot.com/fdx/v6", fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.pageKey != null) query.append("pageKey", String(args.pageKey)) if (args.startTime != null) query.append("startTime", String(args.startTime)) if (args.endTime != null) query.append("endTime", String(args.endTime)) if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/accounts/${encodeURIComponent(String(args.id))}/transactions${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(listTransactionsResponse, await res.json()) };