import {z} from 'zod' export type GetApiApiTransactionsArgs = {accountId?: (string | Array) | undefined, accountSubtype?: (string | Array) | undefined, accountType?: string | undefined, categoryGroup?: (string | Array) | undefined, categoryId?: (string | Array) | undefined, cursor?: string | undefined, endDate?: string | undefined, limit?: number | undefined, maxAmount?: (number | null) | undefined, minAmount?: (number | null) | undefined, pendingFilter?: ('true' | 'false') | undefined, searchQuery?: string | undefined, startDate?: string | undefined, tagId?: (string | Array) | undefined}; export type GetApiApiTransactionsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const transactionLocation = z.object({address: z.string().nullable(), city: z.string().nullable(), country: z.string().nullable(), lat: z.number().nullable(), lon: z.number().nullable(), postal_code: z.string().nullable(), region: z.string().nullable(), store_number: z.string().nullable()}).nullable(); export const transactionLockedFields = z.array(z.enum(["amount", "category", "date", "location", "merchantName", "name", "notes", "paymentChannel", "tags"])); export const transactionResponse = z.object({accountId: z.string(), accountLogoDomain: z.string().nullable(), accountName: z.string(), accountSubtype: z.string().nullable(), accountType: z.string(), amount: z.number(), authorizedDate: z.string().nullable(), category: z.object({groupName: z.string(), groupSystemKey: z.string().nullable(), iconKey: z.string(), id: z.string(), name: z.string(), systemKey: z.string().nullable()}).nullable(), counterparties: z.array(z.object({account_numbers: z.object({bacs: z.object({account: z.string().nullable().optional(), sort_code: z.string().nullable().optional()}).nullable().optional(), international: z.object({bic: z.string().nullable().optional(), iban: z.object({}).nullable().optional()}).nullable().optional()}).nullable().optional(), confidence_level: z.string().nullable().optional(), entity_id: z.string().nullable().optional(), logo_url: z.string().nullable(), name: z.string(), type: z.enum(["merchant", "financial_institution", "payment_app", "marketplace", "payment_terminal", "income_source"]), website: z.string().nullable()})).nullable(), date: z.string(), id: z.string(), institutionLogo: z.string().nullable(), institutionName: z.string().nullable(), institutionUrl: z.string().nullable(), location: transactionLocation, lockedFields: transactionLockedFields, logoUrl: z.string().nullable(), merchantName: z.string().nullable(), name: z.string(), notes: z.string().max(100000).nullable(), paymentChannel: z.enum(["in store", "online", "other", "null"]).nullable(), pending: z.boolean(), plaidAccountId: z.string().nullable(), source: z.enum(["plaid", "manual"]), tagIds: z.array(z.string()), website: z.string().nullable()}); export const transactionsResponse = z.object({hasMore: z.boolean(), nextCursor: z.string().nullable(), transactions: z.array(transactionResponse)}); 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 /api/transactions */ export const getApiApiTransactions = async (args: GetApiApiTransactionsArgs, options: GetApiApiTransactionsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.accountId != null) query.append("accountId", String(args.accountId)) if (args.accountSubtype != null) query.append("accountSubtype", String(args.accountSubtype)) if (args.accountType != null) query.append("accountType", String(args.accountType)) if (args.categoryGroup != null) query.append("categoryGroup", String(args.categoryGroup)) if (args.categoryId != null) query.append("categoryId", String(args.categoryId)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.endDate != null) query.append("endDate", String(args.endDate)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.maxAmount != null) query.append("maxAmount", String(args.maxAmount)) if (args.minAmount != null) query.append("minAmount", String(args.minAmount)) if (args.pendingFilter != null) query.append("pendingFilter", String(args.pendingFilter)) if (args.searchQuery != null) query.append("searchQuery", String(args.searchQuery)) if (args.startDate != null) query.append("startDate", String(args.startDate)) if (args.tagId != null) query.append("tagId", String(args.tagId)) const url = `${baseUrl.replace(/\/+$/, '')}/api/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(transactionsResponse, await res.json()) };