@coderoftime/fints 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (254) hide show
  1. package/CHANGELOG.md +11 -0
  2. package/LICENSE +28 -0
  3. package/README.md +156 -2
  4. package/dist/core/account.d.ts +140 -0
  5. package/dist/core/account.d.ts.map +1 -0
  6. package/dist/core/account.js +174 -0
  7. package/dist/core/account.js.map +1 -0
  8. package/dist/core/amount.d.ts +62 -0
  9. package/dist/core/amount.d.ts.map +1 -0
  10. package/dist/core/amount.js +189 -0
  11. package/dist/core/amount.js.map +1 -0
  12. package/dist/core/balance.d.ts +54 -0
  13. package/dist/core/balance.d.ts.map +1 -0
  14. package/dist/core/balance.js +53 -0
  15. package/dist/core/balance.js.map +1 -0
  16. package/dist/core/bank-info.d.ts +49 -0
  17. package/dist/core/bank-info.d.ts.map +1 -0
  18. package/dist/core/bank-info.js +52 -0
  19. package/dist/core/bank-info.js.map +1 -0
  20. package/dist/core/client.d.ts +126 -0
  21. package/dist/core/client.d.ts.map +1 -0
  22. package/dist/core/client.js +2 -0
  23. package/dist/core/client.js.map +1 -0
  24. package/dist/core/config.d.ts +68 -0
  25. package/dist/core/config.d.ts.map +1 -0
  26. package/dist/core/config.js +113 -0
  27. package/dist/core/config.js.map +1 -0
  28. package/dist/core/errors.d.ts +91 -0
  29. package/dist/core/errors.d.ts.map +1 -0
  30. package/dist/core/errors.js +182 -0
  31. package/dist/core/errors.js.map +1 -0
  32. package/dist/core/feedback.d.ts +69 -0
  33. package/dist/core/feedback.d.ts.map +1 -0
  34. package/dist/core/feedback.js +124 -0
  35. package/dist/core/feedback.js.map +1 -0
  36. package/dist/core/transaction.d.ts +98 -0
  37. package/dist/core/transaction.d.ts.map +1 -0
  38. package/dist/core/transaction.js +91 -0
  39. package/dist/core/transaction.js.map +1 -0
  40. package/dist/core/transport.d.ts +37 -0
  41. package/dist/core/transport.d.ts.map +1 -0
  42. package/dist/core/transport.js +92 -0
  43. package/dist/core/transport.js.map +1 -0
  44. package/dist/formats/camt.d.ts +12 -0
  45. package/dist/formats/camt.d.ts.map +1 -0
  46. package/dist/formats/camt.js +159 -0
  47. package/dist/formats/camt.js.map +1 -0
  48. package/dist/formats/mt940.d.ts +42 -0
  49. package/dist/formats/mt940.d.ts.map +1 -0
  50. package/dist/formats/mt940.js +297 -0
  51. package/dist/formats/mt940.js.map +1 -0
  52. package/dist/formats/readers.d.ts +11 -0
  53. package/dist/formats/readers.d.ts.map +1 -0
  54. package/dist/formats/readers.js +47 -0
  55. package/dist/formats/readers.js.map +1 -0
  56. package/dist/formats/sepa-purpose.d.ts +22 -0
  57. package/dist/formats/sepa-purpose.d.ts.map +1 -0
  58. package/dist/formats/sepa-purpose.js +68 -0
  59. package/dist/formats/sepa-purpose.js.map +1 -0
  60. package/dist/index.d.ts +28 -0
  61. package/dist/index.d.ts.map +1 -0
  62. package/dist/index.js +27 -0
  63. package/dist/index.js.map +1 -0
  64. package/dist/session/app-approval.d.ts +60 -0
  65. package/dist/session/app-approval.d.ts.map +1 -0
  66. package/dist/session/app-approval.js +215 -0
  67. package/dist/session/app-approval.js.map +1 -0
  68. package/dist/session/bank-connection.d.ts +28 -0
  69. package/dist/session/bank-connection.d.ts.map +1 -0
  70. package/dist/session/bank-connection.js +48 -0
  71. package/dist/session/bank-connection.js.map +1 -0
  72. package/dist/session/dialog-runner.d.ts +34 -0
  73. package/dist/session/dialog-runner.d.ts.map +1 -0
  74. package/dist/session/dialog-runner.js +112 -0
  75. package/dist/session/dialog-runner.js.map +1 -0
  76. package/dist/session/login-guard.d.ts +73 -0
  77. package/dist/session/login-guard.d.ts.map +1 -0
  78. package/dist/session/login-guard.js +109 -0
  79. package/dist/session/login-guard.js.map +1 -0
  80. package/dist/session/parameter-data.d.ts +83 -0
  81. package/dist/session/parameter-data.d.ts.map +1 -0
  82. package/dist/session/parameter-data.js +68 -0
  83. package/dist/session/parameter-data.js.map +1 -0
  84. package/dist/session/pin-tan-client.d.ts +54 -0
  85. package/dist/session/pin-tan-client.d.ts.map +1 -0
  86. package/dist/session/pin-tan-client.js +153 -0
  87. package/dist/session/pin-tan-client.js.map +1 -0
  88. package/dist/session/pin-tan-dialog.d.ts +71 -0
  89. package/dist/session/pin-tan-dialog.d.ts.map +1 -0
  90. package/dist/session/pin-tan-dialog.js +124 -0
  91. package/dist/session/pin-tan-dialog.js.map +1 -0
  92. package/dist/session/protocol.d.ts +209 -0
  93. package/dist/session/protocol.d.ts.map +1 -0
  94. package/dist/session/protocol.js +75 -0
  95. package/dist/session/protocol.js.map +1 -0
  96. package/dist/session/response-handler.d.ts +48 -0
  97. package/dist/session/response-handler.d.ts.map +1 -0
  98. package/dist/session/response-handler.js +121 -0
  99. package/dist/session/response-handler.js.map +1 -0
  100. package/dist/session/sepa-accounts.d.ts +34 -0
  101. package/dist/session/sepa-accounts.d.ts.map +1 -0
  102. package/dist/session/sepa-accounts.js +96 -0
  103. package/dist/session/sepa-accounts.js.map +1 -0
  104. package/dist/session/session-setup.d.ts +72 -0
  105. package/dist/session/session-setup.d.ts.map +1 -0
  106. package/dist/session/session-setup.js +160 -0
  107. package/dist/session/session-setup.js.map +1 -0
  108. package/dist/session/session-state.d.ts +47 -0
  109. package/dist/session/session-state.d.ts.map +1 -0
  110. package/dist/session/session-state.js +147 -0
  111. package/dist/session/session-state.js.map +1 -0
  112. package/dist/session/tan-selection.d.ts +88 -0
  113. package/dist/session/tan-selection.d.ts.map +1 -0
  114. package/dist/session/tan-selection.js +178 -0
  115. package/dist/session/tan-selection.js.map +1 -0
  116. package/dist/session/user-prompts.d.ts +61 -0
  117. package/dist/session/user-prompts.d.ts.map +1 -0
  118. package/dist/session/user-prompts.js +164 -0
  119. package/dist/session/user-prompts.js.map +1 -0
  120. package/dist/tan/challenge.d.ts +114 -0
  121. package/dist/tan/challenge.d.ts.map +1 -0
  122. package/dist/tan/challenge.js +185 -0
  123. package/dist/tan/challenge.js.map +1 -0
  124. package/dist/tan/flicker.d.ts +36 -0
  125. package/dist/tan/flicker.d.ts.map +1 -0
  126. package/dist/tan/flicker.js +262 -0
  127. package/dist/tan/flicker.js.map +1 -0
  128. package/dist/tan/tan-image.d.ts +21 -0
  129. package/dist/tan/tan-image.d.ts.map +1 -0
  130. package/dist/tan/tan-image.js +41 -0
  131. package/dist/tan/tan-image.js.map +1 -0
  132. package/dist/tan/tan-models.d.ts +145 -0
  133. package/dist/tan/tan-models.d.ts.map +1 -0
  134. package/dist/tan/tan-models.js +148 -0
  135. package/dist/tan/tan-models.js.map +1 -0
  136. package/dist/util/async.d.ts +24 -0
  137. package/dist/util/async.d.ts.map +1 -0
  138. package/dist/util/async.js +49 -0
  139. package/dist/util/async.js.map +1 -0
  140. package/dist/util/bytes.d.ts +23 -0
  141. package/dist/util/bytes.d.ts.map +1 -0
  142. package/dist/util/bytes.js +136 -0
  143. package/dist/util/bytes.js.map +1 -0
  144. package/dist/util/call-context.d.ts +12 -0
  145. package/dist/util/call-context.d.ts.map +1 -0
  146. package/dist/util/call-context.js +29 -0
  147. package/dist/util/call-context.js.map +1 -0
  148. package/dist/util/dates.d.ts +62 -0
  149. package/dist/util/dates.d.ts.map +1 -0
  150. package/dist/util/dates.js +169 -0
  151. package/dist/util/dates.js.map +1 -0
  152. package/dist/util/errors.d.ts +9 -0
  153. package/dist/util/errors.d.ts.map +1 -0
  154. package/dist/util/errors.js +15 -0
  155. package/dist/util/errors.js.map +1 -0
  156. package/dist/util/immutable-map.d.ts +16 -0
  157. package/dist/util/immutable-map.d.ts.map +1 -0
  158. package/dist/util/immutable-map.js +38 -0
  159. package/dist/util/immutable-map.js.map +1 -0
  160. package/dist/util/json.d.ts +13 -0
  161. package/dist/util/json.d.ts.map +1 -0
  162. package/dist/util/json.js +44 -0
  163. package/dist/util/json.js.map +1 -0
  164. package/dist/util/text.d.ts +20 -0
  165. package/dist/util/text.d.ts.map +1 -0
  166. package/dist/util/text.js +78 -0
  167. package/dist/util/text.js.map +1 -0
  168. package/dist/util/xml.d.ts +35 -0
  169. package/dist/util/xml.d.ts.map +1 -0
  170. package/dist/util/xml.js +183 -0
  171. package/dist/util/xml.js.map +1 -0
  172. package/dist/v3/account-extension.d.ts +25 -0
  173. package/dist/v3/account-extension.d.ts.map +1 -0
  174. package/dist/v3/account-extension.js +176 -0
  175. package/dist/v3/account-extension.js.map +1 -0
  176. package/dist/v3/client.d.ts +24 -0
  177. package/dist/v3/client.d.ts.map +1 -0
  178. package/dist/v3/client.js +16 -0
  179. package/dist/v3/client.js.map +1 -0
  180. package/dist/v3/dialog.d.ts +78 -0
  181. package/dist/v3/dialog.d.ts.map +1 -0
  182. package/dist/v3/dialog.js +138 -0
  183. package/dist/v3/dialog.js.map +1 -0
  184. package/dist/v3/message.d.ts +72 -0
  185. package/dist/v3/message.d.ts.map +1 -0
  186. package/dist/v3/message.js +259 -0
  187. package/dist/v3/message.js.map +1 -0
  188. package/dist/v3/parameters.d.ts +79 -0
  189. package/dist/v3/parameters.d.ts.map +1 -0
  190. package/dist/v3/parameters.js +396 -0
  191. package/dist/v3/parameters.js.map +1 -0
  192. package/dist/v3/protocol.d.ts +68 -0
  193. package/dist/v3/protocol.d.ts.map +1 -0
  194. package/dist/v3/protocol.js +263 -0
  195. package/dist/v3/protocol.js.map +1 -0
  196. package/dist/v3/segments.d.ts +89 -0
  197. package/dist/v3/segments.d.ts.map +1 -0
  198. package/dist/v3/segments.js +283 -0
  199. package/dist/v3/segments.js.map +1 -0
  200. package/dist/v3/syntax.d.ts +76 -0
  201. package/dist/v3/syntax.d.ts.map +1 -0
  202. package/dist/v3/syntax.js +301 -0
  203. package/dist/v3/syntax.js.map +1 -0
  204. package/package.json +63 -4
  205. package/src/core/account.ts +250 -0
  206. package/src/core/amount.ts +209 -0
  207. package/src/core/balance.ts +84 -0
  208. package/src/core/bank-info.ts +66 -0
  209. package/src/core/client.ts +149 -0
  210. package/src/core/config.ts +141 -0
  211. package/src/core/errors.ts +203 -0
  212. package/src/core/feedback.ts +137 -0
  213. package/src/core/transaction.ts +156 -0
  214. package/src/core/transport.ts +114 -0
  215. package/src/formats/camt.ts +169 -0
  216. package/src/formats/mt940.ts +333 -0
  217. package/src/formats/readers.ts +56 -0
  218. package/src/formats/sepa-purpose.ts +75 -0
  219. package/src/index.ts +68 -0
  220. package/src/session/app-approval.ts +265 -0
  221. package/src/session/bank-connection.ts +51 -0
  222. package/src/session/dialog-runner.ts +119 -0
  223. package/src/session/login-guard.ts +135 -0
  224. package/src/session/parameter-data.ts +137 -0
  225. package/src/session/pin-tan-client.ts +186 -0
  226. package/src/session/pin-tan-dialog.ts +160 -0
  227. package/src/session/protocol.ts +311 -0
  228. package/src/session/response-handler.ts +130 -0
  229. package/src/session/sepa-accounts.ts +97 -0
  230. package/src/session/session-setup.ts +165 -0
  231. package/src/session/session-state.ts +170 -0
  232. package/src/session/tan-selection.ts +192 -0
  233. package/src/session/user-prompts.ts +174 -0
  234. package/src/tan/challenge.ts +252 -0
  235. package/src/tan/flicker.ts +270 -0
  236. package/src/tan/tan-image.ts +36 -0
  237. package/src/tan/tan-models.ts +247 -0
  238. package/src/util/async.ts +53 -0
  239. package/src/util/bytes.ts +135 -0
  240. package/src/util/call-context.ts +40 -0
  241. package/src/util/dates.ts +193 -0
  242. package/src/util/errors.ts +16 -0
  243. package/src/util/immutable-map.ts +47 -0
  244. package/src/util/json.ts +51 -0
  245. package/src/util/text.ts +82 -0
  246. package/src/util/xml.ts +172 -0
  247. package/src/v3/account-extension.ts +190 -0
  248. package/src/v3/client.ts +29 -0
  249. package/src/v3/dialog.ts +199 -0
  250. package/src/v3/message.ts +321 -0
  251. package/src/v3/parameters.ts +451 -0
  252. package/src/v3/protocol.ts +331 -0
  253. package/src/v3/segments.ts +331 -0
  254. package/src/v3/syntax.ts +307 -0
@@ -0,0 +1,250 @@
1
+ import { ImmutableMap } from '../util/immutable-map.js';
2
+ import { maskedId } from '../util/text.js';
3
+ import { defaultCountryCode } from './config.js';
4
+
5
+ /** Account kind derived from the FinTS account type code. */
6
+ export type AccountCategory =
7
+ /** Current account (type codes 1 to 9). */
8
+ | 'checking'
9
+ /** Savings account (10 to 19). */
10
+ | 'savings'
11
+ /** Fixed-term deposit (20 to 29). */
12
+ | 'fixedDeposit'
13
+ /** Securities account (30 to 39). */
14
+ | 'securities'
15
+ /** Loan account (40 to 49). */
16
+ | 'loan'
17
+ /** Credit card account (50 to 59). */
18
+ | 'creditCard'
19
+ /** Fund deposit (60 to 69). */
20
+ | 'fund'
21
+ /** Home savings contract (70 to 79). */
22
+ | 'homeSavings'
23
+ /** Insurance (80 to 89). */
24
+ | 'insurance'
25
+ /** Any other type code. */
26
+ | 'other'
27
+ /** The bank states no type code. */
28
+ | 'unknown';
29
+
30
+ const categoriesByTens: readonly AccountCategory[] = [
31
+ 'checking',
32
+ 'savings',
33
+ 'fixedDeposit',
34
+ 'securities',
35
+ 'loan',
36
+ 'creditCard',
37
+ 'fund',
38
+ 'homeSavings',
39
+ 'insurance',
40
+ ];
41
+
42
+ /** The category of the FinTS account type `code`, which is `undefined` if the bank states none. */
43
+ export function accountCategoryOf(code: number | undefined): AccountCategory {
44
+ if (code === undefined || code < 1) return 'unknown';
45
+ return categoriesByTens[Math.floor(code / 10)] ?? 'other';
46
+ }
47
+
48
+ /** What `Account` takes; the bank states the details it knows. */
49
+ export interface AccountInit {
50
+ accountNumber?: string | undefined;
51
+ subAccount?: string | undefined;
52
+ bankCode: string;
53
+ countryCode?: string | undefined;
54
+ iban?: string | undefined;
55
+ bic?: string | undefined;
56
+ customerId?: string | undefined;
57
+ currency?: string | undefined;
58
+ ownerName?: string | undefined;
59
+ productName?: string | undefined;
60
+ typeCode?: number | undefined;
61
+ allowedTransactions?: readonly string[] | undefined;
62
+ allowedTransactionsComplete?: boolean | undefined;
63
+ dataStatus?: AccountDataStatus | undefined;
64
+ }
65
+
66
+ /** A bank account as reported by the user parameter data or the SEPA account query. */
67
+ export class Account {
68
+ /** National account number. */
69
+ readonly accountNumber: string | undefined;
70
+
71
+ /** Sub-account characteristic (Unterkontomerkmal). */
72
+ readonly subAccount: string | undefined;
73
+
74
+ /** German bank code (BLZ) of the bank holding the account. */
75
+ readonly bankCode: string;
76
+
77
+ /** Numeric country code of the bank, `280` for Germany. */
78
+ readonly countryCode: string;
79
+
80
+ /** International bank account number. */
81
+ readonly iban: string | undefined;
82
+
83
+ /** Bank identifier code of the bank holding the account. */
84
+ readonly bic: string | undefined;
85
+
86
+ /** Customer ID the account is assigned to; may differ from the login name. */
87
+ readonly customerId: string | undefined;
88
+
89
+ /** Currency of the account, for example `EUR`. */
90
+ readonly currency: string | undefined;
91
+
92
+ /** Name of the account holder. */
93
+ readonly ownerName: string | undefined;
94
+
95
+ /** The bank's name for the account, for example `Girokonto`. */
96
+ readonly productName: string | undefined;
97
+
98
+ /** Raw FinTS account type (1–99); see `category`. */
99
+ readonly typeCode: number | undefined;
100
+
101
+ /**
102
+ * The transactions the bank lists as permitted for this account, by their segment IDs, such as `HKSAL`.
103
+ *
104
+ * The client applies them itself: `getBalance` and `getTransactions` throw `FinTsUnsupportedError`, without asking
105
+ * the bank, where the parameter data rule the query out.
106
+ */
107
+ readonly allowedTransactions: readonly string[];
108
+
109
+ /**
110
+ * Whether `allowedTransactions` is complete: the bank permits nothing it does not list (FinTS "UPD-Verwendung" 0).
111
+ * Otherwise the bank's lists say nothing about a transaction they leave out, and only the bank can tell.
112
+ */
113
+ readonly allowedTransactionsComplete: boolean;
114
+
115
+ /** What the bank tells about the data it holds for the account, where it does. */
116
+ readonly dataStatus: AccountDataStatus | undefined;
117
+
118
+ constructor(init: AccountInit) {
119
+ this.accountNumber = init.accountNumber;
120
+ this.subAccount = init.subAccount;
121
+ this.bankCode = init.bankCode;
122
+ this.countryCode = init.countryCode ?? defaultCountryCode;
123
+ this.iban = init.iban;
124
+ this.bic = init.bic;
125
+ this.customerId = init.customerId;
126
+ this.currency = init.currency;
127
+ this.ownerName = init.ownerName;
128
+ this.productName = init.productName;
129
+ this.typeCode = init.typeCode;
130
+ this.allowedTransactions = Object.freeze([...(init.allowedTransactions ?? [])]);
131
+ this.allowedTransactionsComplete = init.allowedTransactionsComplete ?? false;
132
+ this.dataStatus = init.dataStatus;
133
+ }
134
+
135
+ /** The kind of account, derived from `typeCode`. */
136
+ get category(): AccountCategory {
137
+ return accountCategoryOf(this.typeCode);
138
+ }
139
+
140
+ /**
141
+ * Whether `other` is the same account: by IBAN when both have one, otherwise by account number, sub-account and
142
+ * bank.
143
+ *
144
+ * Account numbers are compared without leading zeros, since banks pad them differently. An empty bank or country
145
+ * code, as of a balance record without bank identification, matches any.
146
+ */
147
+ matches(other: Account): boolean {
148
+ if (this.iban !== undefined && other.iban !== undefined) return this.iban === other.iban;
149
+ const number = this.accountNumber?.replace(/^0+/, '');
150
+ return (
151
+ number !== undefined &&
152
+ number === other.accountNumber?.replace(/^0+/, '') &&
153
+ (this.subAccount ?? '') === (other.subAccount ?? '') &&
154
+ sameOrUnstated(this.bankCode, other.bankCode) &&
155
+ sameOrUnstated(this.countryCode, other.countryCode)
156
+ );
157
+ }
158
+
159
+ /** This account with the given details replaced; a detail that is `undefined` stays as it is. */
160
+ copyWith(changes: Omit<Partial<AccountInit>, 'bankCode' | 'countryCode'>): Account {
161
+ return new Account({
162
+ accountNumber: changes.accountNumber ?? this.accountNumber,
163
+ subAccount: changes.subAccount ?? this.subAccount,
164
+ bankCode: this.bankCode,
165
+ countryCode: this.countryCode,
166
+ iban: changes.iban ?? this.iban,
167
+ bic: changes.bic ?? this.bic,
168
+ customerId: changes.customerId ?? this.customerId,
169
+ currency: changes.currency ?? this.currency,
170
+ ownerName: changes.ownerName ?? this.ownerName,
171
+ productName: changes.productName ?? this.productName,
172
+ typeCode: changes.typeCode ?? this.typeCode,
173
+ allowedTransactions: changes.allowedTransactions ?? this.allowedTransactions,
174
+ allowedTransactionsComplete: changes.allowedTransactionsComplete ?? this.allowedTransactionsComplete,
175
+ dataStatus: changes.dataStatus ?? this.dataStatus,
176
+ });
177
+ }
178
+
179
+ /**
180
+ * The product name and the last digits of the IBAN or account number only, so that logs and crash reports do not
181
+ * collect account data.
182
+ */
183
+ toString(): string {
184
+ const id = this.iban ?? this.accountNumber;
185
+ const parts = [
186
+ ...(id === undefined ? [] : [maskedId(id)]),
187
+ ...(this.productName === undefined ? [] : [this.productName]),
188
+ ];
189
+ return `Account(${parts.join(', ')})`;
190
+ }
191
+ }
192
+
193
+ function sameOrUnstated(a: string, b: string): boolean {
194
+ return a === '' || b === '' || a === b;
195
+ }
196
+
197
+ /**
198
+ * What the bank tells about the data it holds for an account, so that an app asks only for what has changed (FinTS 3.0
199
+ * user parameter data, "Erweiterung, kontobezogen", Formals E.3.1 and F).
200
+ *
201
+ * The rules are binding for an app that stores transactions; banks may refuse an app that ignores them:
202
+ * - Do not fetch the transactions again while the bank names the ID they were stored with (`mt940TransactionsId` or
203
+ * `camtTransactionsId`, for the format the app asked for, so pass `format` to `getTransactions` explicitly), or, in
204
+ * the older format, the timestamp `lastTransactions`.
205
+ * - Do not fetch them before `nextTransactionsAt`.
206
+ * - Do not ask for the balance separately where `transactionBalancesCurrent` says that the transactions carry the
207
+ * current one.
208
+ */
209
+ export class AccountDataStatus {
210
+ /**
211
+ * IDs of the data the bank holds for the user, keyed by the segment ID of the query that fetches them, for example
212
+ * `HKKAZ` (transactions as MT940) or `HKCAZ` (as camt): the data have changed when the ID changes. An empty ID means
213
+ * that the user has no such data; a query the map leaves out, that the bank tells nothing about it.
214
+ */
215
+ readonly inventory: ReadonlyMap<string, string>;
216
+
217
+ /**
218
+ * The timestamp of the last transactions the bank provided, in the older format of the extension (`umsltzt`);
219
+ * compared like an ID in `inventory`.
220
+ */
221
+ readonly lastTransactions: string | undefined;
222
+
223
+ /** The moment before which the bank provides no new transactions. */
224
+ readonly nextTransactionsAt: Date | undefined;
225
+
226
+ /** Whether the balances the transactions carry are the current ones. */
227
+ readonly transactionBalancesCurrent: boolean;
228
+
229
+ constructor(init: {
230
+ inventory?: ReadonlyMap<string, string>;
231
+ lastTransactions?: string | undefined;
232
+ nextTransactionsAt?: Date | undefined;
233
+ transactionBalancesCurrent?: boolean;
234
+ }) {
235
+ this.inventory = new ImmutableMap(init.inventory ?? []);
236
+ this.lastTransactions = init.lastTransactions;
237
+ this.nextTransactionsAt = init.nextTransactionsAt;
238
+ this.transactionBalancesCurrent = init.transactionBalancesCurrent ?? false;
239
+ }
240
+
241
+ /** The ID of the transactions the bank holds as MT940, its entry for `HKKAZ` in `inventory`. */
242
+ get mt940TransactionsId(): string | undefined {
243
+ return this.inventory.get('HKKAZ');
244
+ }
245
+
246
+ /** The ID of the transactions the bank holds as camt, its entry for `HKCAZ` in `inventory`. */
247
+ get camtTransactionsId(): string | undefined {
248
+ return this.inventory.get('HKCAZ');
249
+ }
250
+ }
@@ -0,0 +1,209 @@
1
+ import { FormatError } from '../util/errors.js';
2
+
3
+ /** Most decimal places an amount has: ISO 20022, the widest of the bank formats, allows five. */
4
+ const maxScale = 5;
5
+
6
+ /** Largest `minorUnits` an amount holds: 2^53 - 1, which JavaScript numbers represent exactly. */
7
+ const maxMinorUnits = Number.MAX_SAFE_INTEGER;
8
+ const powersOfTen = [1, 10, 100, 1000, 10000, 100000];
9
+
10
+ /** Most digits a parsed amount has: few enough that the sum of two parsed amounts still fits. */
11
+ const maxDigits = 15;
12
+
13
+ const decimal = /^([+-]?)(\d*)(?:[.,](\d*))?$/;
14
+
15
+ /** Decimal places of the minor unit of `currency` (ISO 4217): two, except for the currencies listed here. */
16
+ function minorUnitDigits(currency: string): number {
17
+ switch (currency) {
18
+ case 'BIF':
19
+ case 'CLP':
20
+ case 'DJF':
21
+ case 'GNF':
22
+ case 'ISK':
23
+ case 'JPY':
24
+ case 'KMF':
25
+ case 'KRW':
26
+ case 'PYG':
27
+ case 'RWF':
28
+ case 'UGX':
29
+ case 'UYI':
30
+ case 'VND':
31
+ case 'VUV':
32
+ case 'XAF':
33
+ case 'XOF':
34
+ case 'XPF':
35
+ return 0;
36
+ case 'BHD':
37
+ case 'IQD':
38
+ case 'JOD':
39
+ case 'KWD':
40
+ case 'LYD':
41
+ case 'OMR':
42
+ case 'TND':
43
+ return 3;
44
+ case 'CLF':
45
+ case 'UYW':
46
+ return 4;
47
+ default:
48
+ return 2;
49
+ }
50
+ }
51
+
52
+ /** A money amount stored as an integer number of `scale`-digit minor units. */
53
+ export class Amount {
54
+ /** The amount in units of 10^-`scale` of the currency, negative for a debit: 1234 with scale 2 is 12.34. */
55
+ readonly minorUnits: number;
56
+
57
+ /** Number of decimal places `minorUnits` has, from 0 to 5. */
58
+ readonly scale: number;
59
+
60
+ /** ISO 4217 currency code, for example `EUR`. */
61
+ readonly currency: string;
62
+
63
+ /**
64
+ * An amount of `minorUnits` with `scale` decimal places; throws `RangeError` for a `scale` outside 0 to 5, and for
65
+ * `minorUnits` that are no integer or beyond 2^53 - 1 in either direction, about 90 trillion at two decimal places.
66
+ */
67
+ constructor(minorUnits: number, scale: number, currency: string) {
68
+ if (!Number.isInteger(scale) || scale < 0 || scale > maxScale) {
69
+ throw new RangeError(`scale must be an integer from 0 to ${maxScale}: ${scale}`);
70
+ }
71
+ if (!Number.isSafeInteger(minorUnits)) throw new RangeError(`minorUnits out of range: ${minorUnits}`);
72
+ // No negative zero, which would format as `-0.00`.
73
+ this.minorUnits = minorUnits === 0 ? 0 : minorUnits;
74
+ this.scale = scale;
75
+ this.currency = currency;
76
+ }
77
+
78
+ /**
79
+ * Parses a number as banks send it: digits with at most one decimal separator, `,` (FinTS 3.0, MT940) or `.` (camt),
80
+ * and no thousands separators.
81
+ *
82
+ * The amount has the decimal places of the `currency`'s minor unit (ISO 4217): two for most currencies, none for
83
+ * `JPY`, three for `KWD`. It has more where the value has more significant digits. So `100,` in `EUR` is `100.00`, in
84
+ * `JPY` `100`.
85
+ *
86
+ * Throws `FormatError` for anything else, so that `1.234,56` in bank data is an error, not a guess, and for more than
87
+ * five decimal places (trailing zeros aside) or more than 15 digits.
88
+ */
89
+ static parseDecimal(value: string, currency: string): Amount {
90
+ const match = decimal.exec(value.trim());
91
+ const whole = match?.[2] ?? '';
92
+ let fraction = match?.[3] ?? '';
93
+ if (!match || (whole === '' && fraction === '')) throw new FormatError('Invalid amount', value);
94
+ const minorDigits = minorUnitDigits(currency);
95
+ while (fraction.length > minorDigits && fraction.endsWith('0')) {
96
+ fraction = fraction.substring(0, fraction.length - 1);
97
+ }
98
+ fraction = fraction.padEnd(minorDigits, '0');
99
+ const digits = `${whole.replace(/^0+/, '')}${fraction}`;
100
+ if (fraction.length > maxScale || digits.length > maxDigits) {
101
+ throw new FormatError(`Amount with more than ${maxScale} decimal places or ${maxDigits} digits`, value);
102
+ }
103
+ // Nothing is left of a zero without decimal places, such as `0` in `JPY`.
104
+ const units = digits === '' ? 0 : Number(digits);
105
+ return new Amount(match[1] === '-' ? -units : units, fraction.length, currency);
106
+ }
107
+
108
+ /** The amount as a floating-point number, which cannot hold every decimal fraction exactly; do not calculate with it. */
109
+ get value(): number {
110
+ return this.minorUnits / powersOfTen[this.scale]!;
111
+ }
112
+
113
+ /** Whether the amount is below zero. */
114
+ get isNegative(): boolean {
115
+ return this.minorUnits < 0;
116
+ }
117
+
118
+ /** Whether the amount is zero. */
119
+ get isZero(): boolean {
120
+ return this.minorUnits === 0;
121
+ }
122
+
123
+ /** This amount with the opposite sign. */
124
+ negate(): Amount {
125
+ return new Amount(-this.minorUnits, this.scale, this.currency);
126
+ }
127
+
128
+ /** This amount without its sign. */
129
+ abs(): Amount {
130
+ return this.isNegative ? this.negate() : this;
131
+ }
132
+
133
+ /**
134
+ * The sum with `other`, at the larger of the two scales; throws `TypeError` for another currency and `RangeError` for
135
+ * a sum beyond what an amount holds.
136
+ */
137
+ add(other: Amount): Amount {
138
+ this.checkCurrency(other);
139
+ const [mine, theirs] = this.atCommonScale(other);
140
+ const sum = mine + theirs;
141
+ const limit = BigInt(maxMinorUnits);
142
+ if (sum > limit || sum < -limit)
143
+ throw new RangeError(`The sum of ${this.toString()} and ${other.toString()} is too large for an Amount`);
144
+ return new Amount(Number(sum), Math.max(this.scale, other.scale), this.currency);
145
+ }
146
+
147
+ /** The difference to `other`; throws `TypeError` for another currency. */
148
+ subtract(other: Amount): Amount {
149
+ return this.add(other.negate());
150
+ }
151
+
152
+ /** Compares by value, whatever the scales; throws `TypeError` for another currency. */
153
+ compareTo(other: Amount): number {
154
+ this.checkCurrency(other);
155
+ return this.compareValue(other);
156
+ }
157
+
158
+ /** Whether `other` is the same amount in the same currency, whatever the `scale`: 1.50 equals 1.500. */
159
+ equals(other: unknown): boolean {
160
+ return other instanceof Amount && other.currency === this.currency && this.compareValue(other) === 0;
161
+ }
162
+
163
+ /** Formats the amount without currency, for example `-12.50`. */
164
+ toDecimalString(decimalSeparator = '.'): string {
165
+ const digits = Math.abs(this.minorUnits)
166
+ .toString()
167
+ .padStart(this.scale + 1, '0');
168
+ const whole = digits.substring(0, digits.length - this.scale);
169
+ const fraction = digits.substring(digits.length - this.scale);
170
+ const sign = this.isNegative ? '-' : '';
171
+ return this.scale === 0 ? `${sign}${whole}` : `${sign}${whole}${decimalSeparator}${fraction}`;
172
+ }
173
+
174
+ toString(): string {
175
+ return `${this.toDecimalString()} ${this.currency}`;
176
+ }
177
+
178
+ /** The minor units of both amounts at the larger scale, as `bigint`s, since the factor could leave the safe range. */
179
+ private atCommonScale(other: Amount): [bigint, bigint] {
180
+ const target = Math.max(this.scale, other.scale);
181
+ return [this.rescale(target), other.rescale(target)];
182
+ }
183
+
184
+ private rescale(target: number): bigint {
185
+ return BigInt(this.minorUnits) * BigInt(powersOfTen[target - this.scale]!);
186
+ }
187
+
188
+ private compareValue(other: Amount): number {
189
+ if (this.scale === other.scale) return Math.sign(this.minorUnits - other.minorUnits);
190
+ const [mine, theirs] = this.atCommonScale(other);
191
+ return mine === theirs ? 0 : mine < theirs ? -1 : 1;
192
+ }
193
+
194
+ private checkCurrency(other: Amount): void {
195
+ if (other.currency !== this.currency)
196
+ throw new TypeError(`Currency mismatch: ${this.currency} vs ${other.currency}`);
197
+ }
198
+ }
199
+
200
+ /**
201
+ * `amount` as a credit (unchanged) or a debit (negated), by the bank's credit/debit `mark`.
202
+ *
203
+ * Throws `FormatError` for any other `mark`, `undefined` included: a missing mark must not be read as a credit.
204
+ */
205
+ export function withMark(amount: Amount, mark: string | undefined, credit: string, debit: string): Amount {
206
+ if (mark === credit) return amount;
207
+ if (mark === debit) return amount.negate();
208
+ throw new FormatError(`Credit/debit mark is neither "${credit}" nor "${debit}"`, mark);
209
+ }
@@ -0,0 +1,84 @@
1
+ import { formatIsoDate } from '../util/dates.js';
2
+ import type { Account } from './account.js';
3
+ import type { Amount } from './amount.js';
4
+
5
+ /** What `Balance` takes; the bank states the amounts it knows. */
6
+ export interface BalanceInit {
7
+ account: Account;
8
+ booked: Amount;
9
+ bookedAsOf: Date;
10
+ bookedAsOfTime?: Date | undefined;
11
+ pending?: Amount | undefined;
12
+ creditLine?: Amount | undefined;
13
+ available?: Amount | undefined;
14
+ used?: Amount | undefined;
15
+ overdraft?: Amount | undefined;
16
+ bookingDate?: Date | undefined;
17
+ bookingTime?: Date | undefined;
18
+ productName?: string | undefined;
19
+ }
20
+
21
+ /** Account balance as reported by the bank. */
22
+ export class Balance {
23
+ /** The account the balance belongs to. */
24
+ readonly account: Account;
25
+
26
+ /** Booked balance; negative when overdrawn. */
27
+ readonly booked: Amount;
28
+
29
+ /**
30
+ * The calendar day the bank states with the booked balance: the day it transmitted the balance, which need not be the
31
+ * day the balance was booked (`bookingDate`). A local `Date` at midnight.
32
+ */
33
+ readonly bookedAsOf: Date;
34
+
35
+ /** The exact moment of `bookedAsOf`, where the bank states the time of day as well; `undefined` otherwise. */
36
+ readonly bookedAsOfTime: Date | undefined;
37
+
38
+ /** Balance of pending (not yet booked) transactions, if the bank reports it. */
39
+ readonly pending: Amount | undefined;
40
+
41
+ /** Arranged overdraft limit. */
42
+ readonly creditLine: Amount | undefined;
43
+
44
+ /** Amount currently available for payments. */
45
+ readonly available: Amount | undefined;
46
+
47
+ /** Amount already drawn on the account's limit. */
48
+ readonly used: Amount | undefined;
49
+
50
+ /** Amount by which the account exceeds its limit. */
51
+ readonly overdraft: Amount | undefined;
52
+
53
+ /** The calendar day the booked balance was booked (Buchungszeitpunkt), where the bank states it. */
54
+ readonly bookingDate: Date | undefined;
55
+
56
+ /** The exact moment of `bookingDate`, where the bank states the time of day as well; `undefined` otherwise. */
57
+ readonly bookingTime: Date | undefined;
58
+
59
+ /** The bank's name for the account, for example `Girokonto`. */
60
+ readonly productName: string | undefined;
61
+
62
+ constructor(init: BalanceInit) {
63
+ this.account = init.account;
64
+ this.booked = init.booked;
65
+ this.bookedAsOf = init.bookedAsOf;
66
+ this.bookedAsOfTime = init.bookedAsOfTime;
67
+ this.pending = init.pending;
68
+ this.creditLine = init.creditLine;
69
+ this.available = init.available;
70
+ this.used = init.used;
71
+ this.overdraft = init.overdraft;
72
+ this.bookingDate = init.bookingDate;
73
+ this.bookingTime = init.bookingTime;
74
+ this.productName = init.productName;
75
+ }
76
+
77
+ /**
78
+ * The account, as `Account.toString` shows it, and the date; no amount, so that logs and crash reports do not collect
79
+ * account data.
80
+ */
81
+ toString(): string {
82
+ return `Balance(${this.account.toString()}, ${formatIsoDate(this.bookedAsOf)})`;
83
+ }
84
+ }
@@ -0,0 +1,66 @@
1
+ /** General bank information from the bank parameter data (BPD). */
2
+ export class BankInfo {
3
+ /** German bank code (BLZ). */
4
+ readonly bankCode: string;
5
+
6
+ /** The bank's name. */
7
+ readonly name: string | undefined;
8
+
9
+ /** Version of the bank parameter data this information comes from. */
10
+ readonly bpdVersion: number;
11
+
12
+ /** FinTS versions the bank supports, as it names them. */
13
+ readonly supportedVersions: readonly string[];
14
+
15
+ /** Languages the bank's messages come in, as codes the bank defines. */
16
+ readonly languages: readonly string[];
17
+
18
+ /** Largest message in kilobytes (1024 bytes) the bank accepts; the client sends nothing larger. */
19
+ readonly maxMessageSize: number | undefined;
20
+
21
+ /**
22
+ * Time in seconds before which a client must not send a life-indicator message to keep an idle dialog open, `0` for
23
+ * none (HIBPA, Formals C.9). This client sends no such messages.
24
+ */
25
+ readonly minTimeout: number | undefined;
26
+
27
+ /**
28
+ * Time in seconds after which the bank will likely end a dialog without messages, `0` for no limit (HIBPA, Formals
29
+ * C.9). The client starts a new dialog for a call after that time.
30
+ */
31
+ readonly maxTimeout: number | undefined;
32
+
33
+ constructor(init: {
34
+ bankCode: string;
35
+ name?: string | undefined;
36
+ bpdVersion: number;
37
+ supportedVersions?: readonly string[];
38
+ languages?: readonly string[];
39
+ maxMessageSize?: number | undefined;
40
+ minTimeout?: number | undefined;
41
+ maxTimeout?: number | undefined;
42
+ }) {
43
+ this.bankCode = init.bankCode;
44
+ this.name = init.name;
45
+ this.bpdVersion = init.bpdVersion;
46
+ this.supportedVersions = Object.freeze([...(init.supportedVersions ?? [])]);
47
+ this.languages = Object.freeze([...(init.languages ?? [])]);
48
+ this.maxMessageSize = init.maxMessageSize;
49
+ this.minTimeout = init.minTimeout;
50
+ this.maxTimeout = init.maxTimeout;
51
+ }
52
+ }
53
+
54
+ /** Free-text message sent by the bank during login. */
55
+ export class BankMessage {
56
+ constructor(
57
+ /** What the message is about. */
58
+ readonly subject: string,
59
+ /** The message itself. */
60
+ readonly text: string,
61
+ ) {}
62
+
63
+ toString(): string {
64
+ return `${this.subject}: ${this.text}`;
65
+ }
66
+ }