Martin Berg Alstad 4367c24fb0
🌟 Refactor
- Moved mappings into Sb1 impl
- Moved Actual types to @common
- Moved createDirIfMissing to respective functions
- Refactored main into multiple functions
- Moved create db into Sb1impl and close
- Log requests on info
2025-02-13 21:07:30 +01:00

37 lines
1.1 KiB
TypeScript

import type { TransactionResponse } from "./types"
import * as querystring from "node:querystring"
import { toISODateString } from "@common/date"
import logger from "@common/logger"
import { baseUrl } from "./common"
import type { Interval } from "@common/types.ts"
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.info(`GET '${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: [] }
}
}