import {z} from 'zod' export type GetApiTransactionsUnenrichedArgs = {ids?: Array | undefined, limit?: number | undefined, offset?: number | undefined, startDate?: string | undefined, endDate?: string | undefined, startDateModified?: string | undefined, endDateModified?: string | undefined, text?: string | undefined, accountId?: string | undefined, accountIds?: Array | undefined, creditDebitIndicator?: string | undefined, onlyCount?: boolean | undefined}; export type GetApiTransactionsUnenrichedOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const transactionUnenriched = z.object({accountId: z.string().optional(), amount: z.object({value: z.number().int(), majorUnits: z.number().int().optional(), minorUnits: z.number().int().optional(), currency: z.string()}), date: z.string(), dateModified: z.string(), dateCreated: z.string().optional(), valueDate: z.string().optional(), id: z.string(), longDescription: z.string(), providerId: z.string().optional(), status: z.enum(["posted", "pending"]), transactionCode: z.object({code: z.string(), subCode: z.string()}).optional(), proprietaryTransactionCode: z.object({code: z.string(), issuer: z.string().optional()}).optional(), balance: z.object({value: z.number().int().optional(), majorUnits: z.number().int().optional(), minorUnits: z.number().int().optional(), currency: z.string().optional()}).optional(), balanceType: z.string().optional(), statementReference: z.string().optional(), merchantName: z.string().optional(), merchantCategoryCode: z.string().optional(), cardInstrument: z.object({name: z.string().optional(), pan: z.string().optional(), cardSchemeName: z.string().optional(), authorisationType: z.string().optional()}).optional(), creditorAccount: z.object({name: z.string().optional(), sortCode: z.string().optional(), accountNumber: z.string().optional(), iban: z.string().optional(), pan: z.string().optional()}).optional(), creditorAgent: z.object({name: z.string().optional(), postalAddress: z.record(z.string(), z.unknown()).optional()}).optional(), debtorAccount: z.object({name: z.string().optional(), sortCode: z.string().optional(), accountNumber: z.string().optional(), iban: z.string().optional(), pan: z.string().optional()}).optional(), debtorAgent: z.object({name: z.string().optional(), postalAddress: z.record(z.string(), z.unknown()).optional()}).optional(), transactionReference: z.string().optional(), transactionInformation: z.string().optional()}); export const links = z.object({next: z.string().optional(), prev: z.string().optional(), self: z.string()}); export const getApiTransactionsUnenrichedResponse = z.object({data: z.array(transactionUnenriched).optional(), links: links.optional(), meta: z.object({}).optional()}); 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 }; /** * Retrieve all transactions for a user - does not include data-enriched transaction properties * * GET /transactions/unenriched */ export const getApiTransactionsUnenriched = async (args: GetApiTransactionsUnenrichedArgs, options?: GetApiTransactionsUnenrichedOptions) => { const {baseUrl = "https://api.moneyhub.co.uk/v2.0", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") for (const item of args.ids ?? []) query.append("ids", String(item)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.startDate != null) query.append("startDate", String(args.startDate)) if (args.endDate != null) query.append("endDate", String(args.endDate)) if (args.startDateModified != null) query.append("startDateModified", String(args.startDateModified)) if (args.endDateModified != null) query.append("endDateModified", String(args.endDateModified)) if (args.text != null) query.append("text", String(args.text)) if (args.accountId != null) query.append("accountId", String(args.accountId)) for (const item of args.accountIds ?? []) query.append("accountIds", String(item)) if (args.creditDebitIndicator != null) query.append("creditDebitIndicator", String(args.creditDebitIndicator)) if (args.onlyCount != null) query.append("onlyCount", String(args.onlyCount)) if (Bearer !== undefined) headers.set("Authorization", Bearer) const url = `${baseUrl.replace(/\/+$/, '')}/transactions/unenriched${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(getApiTransactionsUnenrichedResponse, await res.json()) };