diff options
| author | ivar <i@oiee.no> | 2026-03-09 23:05:38 +0100 |
|---|---|---|
| committer | ivar <i@oiee.no> | 2026-03-09 23:05:38 +0100 |
| commit | 69448e29a85cad3a94b3be3ad33efbc52764528f (patch) | |
| tree | c32b8c817322fdf26edbbb3fa75b9505a7020ae8 /cli/src/sb1.ts | |
| parent | b35302fa020ec82a9d67a6cb34379d42983d3cfc (diff) | |
| download | sparebank1-actualbudget-69448e29a85cad3a94b3be3ad33efbc52764528f.tar.xz sparebank1-actualbudget-69448e29a85cad3a94b3be3ad33efbc52764528f.zip | |
Diffstat (limited to 'cli/src/sb1.ts')
| -rw-r--r-- | cli/src/sb1.ts | 37 |
1 files changed, 37 insertions, 0 deletions
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<Sb1Account[]> { + 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<Sb1Transaction[]> { + 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[] + } + } +} |
