import {z} from 'zod' export type TransactionLocation = {address: string | null, city: string | null, country: string | null, lat: number | null, lon: number | null, postal_code: string | null, region: string | null, store_number: string | null} | null; export type CreateApiApiTransactionsArgs = {body: {accountId: string, amount: number, categoryId?: (string | null) | undefined, currency?: (string | null) | undefined, date: string, id?: string | undefined, merchantName?: (string | null) | undefined, name: string, notes?: (string | null) | undefined, website?: (string | null) | undefined, location?: TransactionLocation | undefined}}; export type CreateApiApiTransactionsOptions = {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 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 }; /** * Create a transaction * * POST /api/transactions */ export const createApiApiTransactions = async (args: CreateApiApiTransactionsArgs, options: CreateApiApiTransactionsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/api/transactions${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(transactionResponse, await res.json()) };