Martin Berg Alstad c5b1ec20d6
🧹 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
2025-02-09 12:35:08 +01:00

36 lines
1.1 KiB
TypeScript

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: [] }
}
}