🧹 Moved Sb1 API to separate workspace and created common workspace

- Created common workspace
- Create Sparebank1Api workspace
- Moved logger to common
- Moved SB1 types to types.ts
- Logger will avoid duplicating first line when capturing console.logs
- Updated imports and added type keyword
- Added nonUniqueId type
This commit is contained in:
Martin Berg Alstad 2025-02-09 12:35:08 +01:00
parent efa9e785f2
commit c5b1ec20d6
Signed by: martials
GPG Key ID: A3824877B269F2E2
23 changed files with 202 additions and 151 deletions

View File

@ -1,10 +1,8 @@
# Sparebank1 ActualBudget Integration
🔧 WIP!
### Setting up the environment
In order to start the application, a `.env.local` file must be present at the root level. The possible and required
In order to start the application, an `.env.local` file must be present at the root level. The possible and required
fields
can be found in the [.env.example](.env.example) file and `config.ts`.

View File

@ -22,6 +22,7 @@ export const BANK_ACCOUNT_IDS = getArrayOrThrow("BANK_ACCOUNT_IDS")
export const DB_DIRECTORY = getOrDefault("DB_DIRECTORY", "data")
export const DB_FILENAME = getOrDefault("DB_FILENAME", "default")
export const LOG_LEVEL = getOrDefault("LOG_LEVEL", "info")
// TODO default to fetch 1 day
// Relative number of days in the past to start fetching transactions from
export const TRANSACTION_RELATIVE_FROM_DATE = getNumberOrDefault(
"TRANSACTION_RELATIVE_FROM_DATE",

View File

@ -4,10 +4,10 @@
"description": "",
"main": "index.js",
"scripts": {
"start": "dotenvx run --env-file=.env.local -- node --import=tsx ./src/main.ts | pino-pretty",
"start": "dotenvx run --env-file=.env.local -- node --import=tsx ./src/main.ts | ./packages/common/node_modules/pino-pretty/bin.js",
"start-prod": "node --import=tsx ./src/main.ts",
"run-once": "ONCE=true dotenvx run --env-file=.env.local -- node --import=tsx ./src/main.ts | pino-pretty",
"test": "dotenvx run --env-file=.env.test.local -- node --experimental-vm-modules node_modules/jest/bin/jest.js | pino-pretty",
"run-once": "ONCE=true dotenvx run --env-file=.env.local -- node --import=tsx ./src/main.ts | ./packages/common/node_modules/pino-pretty/bin.js",
"test": "dotenvx run --env-file=.env.test.local -- node --experimental-vm-modules node_modules/jest/bin/jest.js | ./packages/common/node_modules/pino-pretty/bin.js",
"docker-build": "DB_DIRECTORY=data docker compose --env-file .env.local up -d --build",
"format": "prettier --write \"./**/*.{js,mjs,ts,md,json}\""
},
@ -21,7 +21,6 @@
"cron": "^3.5.0",
"dayjs": "^1.11.13",
"dotenv": "^16.4.7",
"pino": "^9.6.0",
"prettier": "^3.4.2",
"tsx": "^4.19.2"
},
@ -31,7 +30,6 @@
"@types/jest": "^29.5.14",
"@types/node": "^22.10.7",
"jest": "^29.7.0",
"pino-pretty": "^13.0.0",
"ts-jest": "^29.2.5",
"ts-node": "^10.9.2",
"typescript": "^5.7.3"

View File

@ -1,5 +1,5 @@
import pino from "pino"
import { LOG_LEVEL } from "../config.ts"
import { LOG_LEVEL } from "@/../config.ts"
/**
* / Returns a logging instance with the default log-level "info"
@ -11,7 +11,11 @@ const logger = pino(
)
console.log = function (...args): void {
logger.info(args, args?.[0])
if (args.length > 1) {
logger.info(args?.slice(1), args?.[0])
} else {
logger.info(args?.[0])
}
}
export default logger

View File

@ -0,0 +1,12 @@
{
"name": "common",
"version": "1.0.0",
"description": "",
"license": "ISC",
"dependencies": {
"pino": "^9.6.0"
},
"devDependencies": {
"pino-pretty": "^13.0.0"
}
}

View File

@ -0,0 +1,12 @@
import type { Failure, Success } from "./types"
export const baseUrl = "https://api.sparebank1.no"
export const success = <T>(data: T): Success<T> => ({
status: "success",
data: data,
})
export const failure = <T>(data: T): Failure<T> => ({
status: "failure",
data: data,
})

View File

@ -0,0 +1,30 @@
import { OAuthTokenResponse, Result } from "./types"
import * as querystring from "node:querystring"
import { baseUrl, failure, success } from "./common"
import logger from "@common/logger"
export async function refreshToken(
clientId: string,
clientSecret: string,
refreshToken: string,
): Promise<Result<OAuthTokenResponse, string>> {
const queries = querystring.stringify({
client_id: clientId,
client_secret: clientSecret,
refresh_token: refreshToken,
grant_type: "refresh_token",
})
const url = `${baseUrl}/oauth/token?${queries}`
logger.debug(`Sending POST request to url: '${url}'`)
const response = await fetch(url, {
method: "post",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
})
logger.debug(`Received response with status '${response.status}'`)
if (!response.ok) {
return failure(await response.text())
}
return success(await response.json())
}

View File

@ -0,0 +1,6 @@
{
"name": "packages",
"version": "1.0.0",
"description": "",
"license": "ISC"
}

View File

@ -0,0 +1,35 @@
import type { Interval, TransactionResponse } from "./types"
import * as querystring from "node:querystring"
import { toISODateString } from "@common/date"
import logger from "@common/logger"
import { baseUrl } from "./common"
export async function list(
accessToken: string,
accountKeys: string | ReadonlyArray<string>,
interval?: Interval,
): Promise<TransactionResponse> {
const queryString = querystring.stringify({
accountKey: accountKeys,
...(interval && {
fromDate: toISODateString(interval.fromDate),
toDate: toISODateString(interval.toDate),
}),
})
const url = `${baseUrl}/personal/banking/transactions?${queryString}`
logger.debug(`Sending GET request to '${url}'`)
const response = await fetch(url, {
headers: {
Authorization: `Bearer ${accessToken}`,
Accept: "application/vnd.sparebank1.v1+json;charset=utf-8",
},
})
logger.debug(`Received response with status '${response.status}'`)
if (response.ok) {
return response.json()
} else {
logger.warn(await response.json())
return { transactions: [] }
}
}

View File

@ -0,0 +1,43 @@
import type { Dayjs } from "dayjs"
export type Success<T> = { status: "success"; data: T }
export type Failure<T> = { status: "failure"; data: T }
export type Result<OK, Err> = Success<OK> | Failure<Err>
export interface Interval {
fromDate: Dayjs
toDate: Dayjs
}
export interface OAuthTokenResponse {
access_token: string
expires_in: number
refresh_token_expires_in: number
refresh_token_absolute_expires_in: number
token_type: "Bearer"
refresh_token: string
}
export type BookingStatus = "PENDING" | "BOOKED"
export type NonUniqueId = "000000000000000000" | `${number}`
export interface SB1Transaction {
id: string
nonUniqueId: NonUniqueId
// The Id of the account
accountKey: string
// Unix time
date: number
// Amount in NOK
amount: number
cleanedDescription: string
remoteAccountName: string
bookingStatus: BookingStatus
[key: string]: string | number | boolean | unknown
}
export interface TransactionResponse {
transactions: ReadonlyArray<SB1Transaction>
}

18
pnpm-lock.yaml generated
View File

@ -26,9 +26,6 @@ importers:
dotenv:
specifier: ^16.4.7
version: 16.4.7
pino:
specifier: ^9.6.0
version: 9.6.0
prettier:
specifier: ^3.4.2
version: 3.4.2
@ -51,9 +48,6 @@ importers:
jest:
specifier: ^29.7.0
version: 29.7.0(@types/node@22.10.7)(ts-node@10.9.2(@types/node@22.10.7)(typescript@5.7.3))
pino-pretty:
specifier: ^13.0.0
version: 13.0.0
ts-jest:
specifier: ^29.2.5
version: 29.2.5(@babel/core@7.26.0)(@jest/transform@29.7.0)(@jest/types@29.6.3)(babel-jest@29.7.0(@babel/core@7.26.0))(jest@29.7.0(@types/node@22.10.7)(ts-node@10.9.2(@types/node@22.10.7)(typescript@5.7.3)))(typescript@5.7.3)
@ -64,6 +58,18 @@ importers:
specifier: ^5.7.3
version: 5.7.3
packages/common:
dependencies:
pino:
specifier: ^9.6.0
version: 9.6.0
devDependencies:
pino-pretty:
specifier: ^13.0.0
version: 13.0.0
packages/sparebank1Api: {}
packages:
'@actual-app/api@25.1.0':

2
pnpm-workspace.yaml Normal file
View File

@ -0,0 +1,2 @@
packages:
- "packages/*"

View File

@ -7,7 +7,7 @@ import {
} from "../config.ts"
import type { TransactionEntity } from "@actual-app/api/@types/loot-core/types/models"
import { type UUID } from "node:crypto"
import logger from "@/logger.ts"
import logger from "@common/logger.ts"
export interface Actual {
importTransactions: (

View File

@ -1,7 +1,7 @@
import Database from "better-sqlite3"
import { type OAuthTokenResponse } from "@/bank/sparebank1.ts"
import dayjs, { type Dayjs } from "dayjs"
import type { OAuthTokenResponse } from "@sb1/types.ts"
export type TokenResponse = {
key: TokenKey
@ -9,10 +9,8 @@ export type TokenResponse = {
expires_at: Dayjs
}
export type TokenResponseRaw = {
key: TokenResponse["key"]
token: TokenResponse["token"]
expires_at: string
type TokenResponseRaw = {
[K in keyof TokenResponse]: K extends "expires_at" ? string : TokenResponse[K]
}
export type TokenKey = "access-token" | "refresh-token"

View File

@ -1,47 +1,22 @@
import { BANK_INITIAL_REFRESH_TOKEN } from "@/../config.ts"
import logger from "@/logger.ts"
import dayjs, { Dayjs } from "dayjs"
import { Database } from "better-sqlite3"
import {
BANK_INITIAL_REFRESH_TOKEN,
BANK_OAUTH_CLIENT_ID,
BANK_OAUTH_CLIENT_SECRET,
} from "@/../config.ts"
import logger from "@common/logger.ts"
import dayjs, { type Dayjs } from "dayjs"
import type { Database } from "better-sqlite3"
import {
clearTokens,
fetchToken,
insertTokens,
type TokenResponse,
} from "@/bank/db/queries.ts"
import * as Api from "./sparebank1Api.ts"
import { ActualTransaction } from "@/actual.ts"
import * as Oauth from "@sb1/oauth.ts"
import * as Transactions from "@sb1/transactions.ts"
import type { ActualTransaction } from "@/actual.ts"
import { bankTransactionIntoActualTransaction } from "@/mappings.ts"
export interface OAuthTokenResponse {
access_token: string
expires_in: number
refresh_token_expires_in: number
refresh_token_absolute_expires_in: number
token_type: "Bearer"
refresh_token: string
}
export type BookingStatus = "PENDING" | "BOOKED"
export interface Transaction {
id: string
nonUniqueId: string
// The Id of the account
accountKey: string
// Unix time
date: number
// Amount in NOK
amount: number
cleanedDescription: string
remoteAccountName: string
bookingStatus: BookingStatus
[key: string]: string | number | boolean | unknown
}
export interface TransactionResponse {
transactions: ReadonlyArray<Transaction>
}
import type { OAuthTokenResponse } from "@sb1/types.ts"
export interface Bank {
fetchTransactions: (
@ -66,7 +41,7 @@ export class Sparebank1Impl implements Bank {
interval: Interval,
...accountKeys: ReadonlyArray<string>
): Promise<ReadonlyArray<ActualTransaction>> {
const response = await Api.transactions(
const response = await Transactions.list(
await this.getAccessToken(),
accountKeys,
interval,
@ -105,7 +80,11 @@ export class Sparebank1Impl implements Bank {
private async fetchNewTokens(): Promise<OAuthTokenResponse> {
const refreshToken = await this.getRefreshToken()
const result = await Api.refreshToken(refreshToken)
const result = await Oauth.refreshToken(
BANK_OAUTH_CLIENT_ID,
BANK_OAUTH_CLIENT_SECRET,
refreshToken,
)
if (result.status === "failure") {
throw new Error(`Failed to fetch refresh token: '${result.data}'`)

View File

@ -1,72 +0,0 @@
import { BANK_OAUTH_CLIENT_ID, BANK_OAUTH_CLIENT_SECRET } from "@/../config.ts"
import type {
Interval,
OAuthTokenResponse,
TransactionResponse,
} from "@/bank/sparebank1.ts"
import logger from "@/logger.ts"
import { toISODateString } from "@/date.ts"
import * as querystring from "node:querystring"
const baseUrl = "https://api.sparebank1.no"
type Success<T> = { status: "success"; data: T }
type Failure<T> = { status: "failure"; data: T }
type Result<OK, Err> = Success<OK> | Failure<Err>
const success = <T>(data: T): Success<T> => ({ status: "success", data: data })
const failure = <T>(data: T): Failure<T> => ({ status: "failure", data: data })
export async function transactions(
accessToken: string,
accountKeys: string | ReadonlyArray<string>,
interval?: Interval,
): Promise<TransactionResponse> {
const queryString = querystring.stringify({
accountKey: accountKeys,
...(interval && {
fromDate: toISODateString(interval.fromDate),
toDate: toISODateString(interval.toDate),
}),
})
const url = `${baseUrl}/personal/banking/transactions?${queryString}`
logger.debug(`Sending GET request to '${url}'`)
const response = await fetch(url, {
headers: {
Authorization: `Bearer ${accessToken}`,
Accept: "application/vnd.sparebank1.v1+json;charset=utf-8",
},
})
logger.debug(`Received response with status '${response.status}'`)
if (response.ok) {
return response.json()
} else {
logger.warn(await response.json())
return { transactions: [] }
}
}
export async function refreshToken(
refreshToken: string,
): Promise<Result<OAuthTokenResponse, string>> {
const queries = querystring.stringify({
client_id: BANK_OAUTH_CLIENT_ID,
client_secret: BANK_OAUTH_CLIENT_SECRET,
refresh_token: refreshToken,
grant_type: "refresh_token",
})
const url = `${baseUrl}/oauth/token?${queries}`
logger.debug(`Sending POST request to url: '${url}'`)
const response = await fetch(url, {
method: "post",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
})
logger.debug(`Received response with status '${response.status}'`)
if (!response.ok) {
return failure(await response.text())
}
return success(await response.json())
}

View File

@ -1,5 +1,5 @@
import { CronJob } from "cron"
import logger from "@/logger.ts"
import logger from "@common/logger.ts"
/**
* Run a function every day at 1 AM, Oslo time.

View File

@ -1,5 +1,5 @@
import * as fs from "node:fs"
import logger from "./logger"
import logger from "@common/logger"
export function createDirsIfMissing(...directories: string[]): void {
directories.forEach(createDirIfMissing)

View File

@ -9,7 +9,7 @@ import {
TRANSACTION_RELATIVE_FROM_DATE,
TRANSACTION_RELATIVE_TO_DATE,
} from "../config.ts"
import logger from "@/logger.ts"
import logger from "@common/logger.ts"
import type { UUID } from "node:crypto"
import { createDb } from "@/bank/db/queries.ts"
import { CronJob } from "cron"

View File

@ -1,13 +1,13 @@
import type { Transaction } from "@/bank/sparebank1.ts"
import type { UUID } from "node:crypto"
import dayjs from "dayjs"
import { toISODateString } from "@/date.ts"
import { type ActualTransaction } from "@/actual.ts"
import { ACTUAL_ACCOUNT_IDS, BANK_ACCOUNT_IDS } from "../config.ts"
import logger from "@/logger.ts"
import logger from "@common/logger.ts"
import { toISODateString } from "@common/date.ts"
import type { SB1Transaction } from "@sb1/types.ts"
export function bankTransactionIntoActualTransaction(
transaction: Transaction,
transaction: SB1Transaction,
): ActualTransaction {
return {
id: transaction.id,
@ -23,12 +23,12 @@ export function bankTransactionIntoActualTransaction(
}
}
export function isCleared(transaction: Transaction): boolean {
export function isCleared(transaction: SB1Transaction): boolean {
const id = Number(transaction.nonUniqueId)
return transaction.bookingStatus === "BOOKED" && !Number.isNaN(id) && id > 0
}
function getActualAccountId(transcation: Transaction): UUID {
function getActualAccountId(transcation: SB1Transaction): UUID {
for (
let i = 0;
i < Math.min(ACTUAL_ACCOUNT_IDS.length, BANK_ACCOUNT_IDS.length);

View File

@ -1,12 +1,8 @@
import type {
Bank,
BookingStatus,
Interval,
Transaction,
} from "@/bank/sparebank1.ts"
import type { Bank, Interval } from "@/bank/sparebank1.ts"
import dayjs from "dayjs"
import { ActualTransaction } from "@/actual.ts"
import type { ActualTransaction } from "@/actual.ts"
import { bankTransactionIntoActualTransaction } from "@/mappings.ts"
import type { BookingStatus, SB1Transaction } from "@sb1/types.ts"
export class BankStub implements Bank {
async fetchTransactions(
@ -20,7 +16,7 @@ export class BankStub implements Bank {
bookingStatus: "BOOKED" as BookingStatus,
accountKey: "1",
}
const bankTransactions: ReadonlyArray<Transaction> = [
const bankTransactions: ReadonlyArray<SB1Transaction> = [
{
id: "1",
nonUniqueId: "1",

View File

@ -9,8 +9,11 @@
"strict": true,
"skipLibCheck": true,
"allowImportingTsExtensions": true,
"noEmit": true,
"paths": {
"@/*": ["./src/*"]
"@/*": ["./src/*"],
"@common/*": ["./packages/common/*"],
"@sb1/*": ["./packages/sparebank1Api/*"]
}
},
"exclude": ["node_modules", "./*.ts", "__test__"]