From 69448e29a85cad3a94b3be3ad33efbc52764528f Mon Sep 17 00:00:00 2001 From: ivar Date: Mon, 9 Mar 2026 23:05:38 +0100 Subject: Add wip cli --- cli/src/sb1.ts | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 cli/src/sb1.ts (limited to 'cli/src/sb1.ts') diff --git a/cli/src/sb1.ts b/cli/src/sb1.ts new file mode 100644 index 0000000..36c638c --- /dev/null +++ b/cli/src/sb1.ts @@ -0,0 +1,37 @@ +import { getAccessToken } from "./tokens" +import type { Config } from "./config" +import type { Sb1Account, Sb1Transaction } from "./types" + +export function createSb1Client(config: Config["sb1"]) { + async function token() { + return getAccessToken(config.clientId, config.clientSecret) + } + + return { + async getAccounts(): Promise { + const res = await fetch("https://api.sparebank1.no/personal/banking/accounts", { + headers: { Authorization: `Bearer ${await token()}` } + }) + if (!res.ok) throw new Error(`Failed to fetch accounts: ${await res.text()}`) + const json = await res.json() as { accounts: Sb1Account[] } + return json.accounts + }, + + async getTransactions(accountKey: string, fromDate?: string): Promise { + const params = new URLSearchParams({ accountKey }) + if (fromDate) { + params.set("fromDate", fromDate) + params.set("Transaction source", "ALL") + } + const res = await fetch(`https://api.sparebank1.no/personal/banking/transactions?${params}`, { + headers: { + Authorization: `Bearer ${await token()}`, + Accept: "application/vnd.sparebank1.v1+json;charset=utf-8" + } + }) + if (!res.ok) throw new Error(`Failed to fetch transactions: ${await res.text()}`) + const json = await res.json() + return json["transactions"] as Sb1Transaction[] + } + } +} -- cgit v1.3