import {z} from 'zod' export type GetApiPlansPlanIdAccountsAccountIdTransactionsArgs = {plan_id: string, account_id: string, since_date?: string | undefined, until_date?: string | undefined, type?: ('uncategorized' | 'unapproved') | undefined, last_knowledge_of_server?: number | undefined}; export type GetApiPlansPlanIdAccountsAccountIdTransactionsOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const transactionClearedStatus = z.enum(["cleared", "uncleared", "reconciled"]); export const transactionFlagColor = z.enum(["red", "orange", "yellow", "green", "blue", "purple", "", "null"]).nullable(); export const transactionFlagName = z.string().nullable(); export const subTransaction = z.object({id: z.string(), transaction_id: z.string(), amount: z.number().int(), memo: z.string().nullable().optional(), payee_id: z.string().nullable().optional(), payee_name: z.string().nullable().optional(), category_id: z.string().nullable().optional(), category_name: z.string().nullable().optional(), transfer_account_id: z.string().nullable().optional(), transfer_transaction_id: z.string().nullable().optional(), deleted: z.boolean(), amount_formatted: z.string().optional(), amount_currency: z.number().optional()}); export const transactionDetail = z.object({id: z.string(), date: z.string(), amount: z.number().int(), memo: z.string().nullable().optional(), cleared: transactionClearedStatus, approved: z.boolean(), flag_color: transactionFlagColor.optional(), flag_name: transactionFlagName.optional(), account_id: z.string(), payee_id: z.string().nullable().optional(), category_id: z.string().nullable().optional(), transfer_account_id: z.string().nullable().optional(), transfer_transaction_id: z.string().nullable().optional(), matched_transaction_id: z.string().nullable().optional(), import_id: z.string().nullable().optional(), import_payee_name: z.string().nullable().optional(), import_payee_name_original: z.string().nullable().optional(), debt_transaction_type: z.enum(["payment", "refund", "fee", "interest", "escrow", "balanceAdjustment", "credit", "charge", "null"]).nullable().optional(), deleted: z.boolean(), amount_formatted: z.string().optional(), amount_currency: z.number().optional(), account_name: z.string(), payee_name: z.string().nullable().optional(), category_name: z.string().nullable().optional(), subtransactions: z.array(subTransaction)}); export const transactionsResponse = z.object({data: z.object({transactions: z.array(transactionDetail), server_knowledge: z.number().int()})}); 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 }; /** * Get account transactions * * GET /plans/{plan_id}/accounts/{account_id}/transactions */ export const getApiPlansPlanIdAccountsAccountIdTransactions = async (args: GetApiPlansPlanIdAccountsAccountIdTransactionsArgs, options?: GetApiPlansPlanIdAccountsAccountIdTransactionsOptions) => { const {baseUrl = "https://api.ynab.com/v1", fetch: fetchFn = fetch, bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.since_date != null) query.append("since_date", String(args.since_date)) if (args.until_date != null) query.append("until_date", String(args.until_date)) if (args.type != null) query.append("type", String(args.type)) if (args.last_knowledge_of_server != null) query.append("last_knowledge_of_server", String(args.last_knowledge_of_server)) if (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/plans/${encodeURIComponent(String(args.plan_id))}/accounts/${encodeURIComponent(String(args.account_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(transactionsResponse, await res.json()) };