-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
484 lines (416 loc) · 18 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
const got = require('got')
const validateSearchParameters = (pagingParameters) => {
const searchParams = {}
const { limit = 100, before, after } = pagingParameters || {}
if (typeof limit !== 'number') throw new TypeError('limit should be a number')
if (limit < 0 && limit > 100) throw new RangeError('limit should be more than 0 or no more than 100')
searchParams.limit = limit
if (before) {
if (typeof before !== 'string') throw new TypeError('before should be a uuid of type string')
searchParams.before = before
} else if (after) {
if (typeof after !== 'string') throw new TypeError('after should be a uuid of type string')
searchParams.after = after
}
return searchParams
}
/** Class representing a Ponto client. */
class Ponto {
#hostName = 'api.myponto.com'
#url
#auth
#accessToken
#acccessTokenExpirationTimestamp
/**
* Create a Ponto client.
*
* @param {string} clientId - The ponto client id.
* @param {string} clientSecret - The ponto client secret.
* @param {boolean} [sandboxMode=false] - Use sandbox mode.
*/
constructor (clientId, clientSecret, sandboxMode = false) {
this.#url = (sandboxMode) ? `https://${this.#hostName}/sandbox/` : `https://${this.#hostName}/`
this.sandboxMode = sandboxMode
this.#auth = 'Basic ' + Buffer.from(`${clientId}:${clientSecret}`).toString('base64')
this.#accessToken = undefined
this.#acccessTokenExpirationTimestamp = 0
}
/**
* Get's the AccessToken
*
* @private
* @returns {Promise} Promise object represents the response with accessToken
*/
async #getAccessToken () {
return got.post(`https://${this.#hostName}/oauth2/token`, {
searchParams: {
grant_type: 'client_credentials'
},
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
Accept: 'application/json',
Authorization: this.#auth
},
responseType: 'json',
resolveBodyOnly: true
})
}
/**
* Check if we have an AccessToken and if it is not expired and get one if necessary
*
* @private
* @returns {Promise} Promise
*/
async #checkAccessToken () {
if (!this.#accessToken || this.#acccessTokenExpirationTimestamp < Date.now()) {
const token = await this.#getAccessToken()
this.#accessToken = token.access_token
this.#acccessTokenExpirationTimestamp = Date.now() + ((token.expires_in - 20) * 1000) // expires_in = Amount of time the access token is valid, in seconds
}
}
/**
* Get a list of Financial Institutions
*
* @param {object} pagingParameters - paging parameters object
* @param {number} pagingParameters.limit - amount of Financial Institutions to return
* @param {string} pagingParameters.before - cursor to get Financial Institutions before this Financial Institution
* @param {string} pagingParameters.after cursor to get Financial Institutions after this Financial Institution
* @returns {Promise} Promise object represents a list of Financial Institutions
*/
async listFinancialInstitutions (pagingParameters) {
const searchParams = validateSearchParameters(pagingParameters)
await this.#checkAccessToken()
const body = await got.get(`${this.#url}financial-institutions`, {
searchParams,
headers: {
Accept: 'application/json',
Authorization: `Bearer ${this.#accessToken}`
},
responseType: 'json',
resolveBodyOnly: true
})
const { meta: { paging: { limit, before, after } } } = body
if (before) {
body.previous = () => {
return this.listFinancialInstitutions({ limit, before })
}
}
if (after) {
body.next = () => {
return this.listFinancialInstitutions({ limit, after })
}
}
return body
}
/**
* Get a Financial Institution
*
* @param {string} financialInstitutionId - id of a Financial Institution
* @returns {Promise} Promise object represents a Financial Institution
*/
async getFinancialInstitution (financialInstitutionId) {
if (!financialInstitutionId) throw new Error('financialInstitutionId is required')
if (typeof financialInstitutionId !== 'string') throw new TypeError('financialInstitutionId should be of type string')
await this.#checkAccessToken()
return got(`${this.#url}financial-institutions/${financialInstitutionId}`, {
headers: {
Accept: 'application/json',
Authorization: `Bearer ${this.#accessToken}`
},
responseType: 'json',
resolveBodyOnly: true
})
}
/**
* Get a list of Accounts for a Financial Institution
*
* @param {string} financialInstitutionId id of a Financial Institution
* @param {object} pagingParameters - paging parameters object
* @param {number} pagingParameters.limit - amount of Financial Institutions to return
* @param {string} pagingParameters.before - cursor to get Financial Institutions before this Financial Institution
* @param {string} pagingParameters.after cursor to get Financial Institutions after this Financial Institution
* @returns {Promise} Promise object represents a list of your accounts
*/
async listFinancialInstitutionAccounts (financialInstitutionId, pagingParameters) {
if (!this.sandboxMode) throw new Error('listFinancialInstitutionAccounts is Sandbox only, use listAccounts instead')
if (!financialInstitutionId) throw new Error('financialInstitutionId is required')
if (typeof financialInstitutionId !== 'string') throw new TypeError('financialInstitutionId should be of type string')
const searchParams = validateSearchParameters(pagingParameters)
await this.#checkAccessToken()
const body = await got.get(`${this.#url}financial-institutions/${financialInstitutionId}/financial-institution-accounts`, {
searchParams,
headers: {
Accept: 'application/json',
Authorization: `Bearer ${this.#accessToken}`
},
responseType: 'json',
resolveBodyOnly: true
})
const { meta: { paging: { limit, before, after } } } = body
if (before) {
body.previous = () => {
return this.listFinancialInstitutionAccounts(financialInstitutionId, { limit, before })
}
}
if (after) {
body.next = () => {
return this.listFinancialInstitutionAccounts(financialInstitutionId, { limit, after })
}
}
return body
}
/**
* Get an Account for a Financial Institution
*
* @param {string} financialInstitutionId - id of a Financial Institution
* @param {string} financialInstitutionAccountId - id of an Account
* @returns {Promise} Promise object represents an Account
*/
async getFinancialInstitutionAccount (financialInstitutionId, financialInstitutionAccountId) {
if (!this.sandboxMode) throw new Error('getFinancialInstitutionAccount is Sandbox only, use getAccount instead')
if (!financialInstitutionId) throw new Error('financialInstitutionId is required')
if (typeof financialInstitutionId !== 'string') throw new TypeError('financialInstitutionId should be of type string')
if (!financialInstitutionAccountId) throw new Error('financialInstitutionAccountId is required')
if (typeof financialInstitutionAccountId !== 'string') throw new TypeError('financialInstitutionAccountId should be of type string')
await this.#checkAccessToken()
return got.get(`${this.#url}financial-institutions/${financialInstitutionId}/financial-institution-accounts/${financialInstitutionAccountId}`, {
headers: {
Accept: 'application/json',
Authorization: `Bearer ${this.#accessToken}`
},
responseType: 'json',
resolveBodyOnly: true
})
}
/**
* Get a list of Transactions for a Financial Institution
*
* @param {string} financialInstitutionId - id of a Financial Institution
* @param {string} financialInstitutionAccountId - id of a Financial Institution Account
* @param {object} pagingParameters - paging parameters object
* @param {number} pagingParameters.limit - amount of Financial Institutions to return
* @param {string} pagingParameters.before - cursor to get Financial Institutions before this Financial Institution
* @param {string} pagingParameters.after cursor to get Financial Institutions after this Financial Institution
* @returns {Promise} Promise object represents a list of your transactions
*/
async listFinancialInstitutionTransactions (financialInstitutionId, financialInstitutionAccountId, pagingParameters) {
if (!this.sandboxMode) throw new Error('listFinancialInstitutionTransactions is Sandbox only, use listAccounts instead')
if (!financialInstitutionId) throw new Error('financialInstitutionId is required')
if (typeof financialInstitutionId !== 'string') throw new TypeError('financialInstitutionId should be of type string')
if (!financialInstitutionAccountId) throw new Error('financialInstitutionAccountId is required')
if (typeof financialInstitutionAccountId !== 'string') throw new TypeError('financialInstitutionAccountId should be of type string')
const searchParams = validateSearchParameters(pagingParameters)
await this.#checkAccessToken()
const body = await got.get(`${this.#url}financial-institutions/${financialInstitutionId}/financial-institution-accounts/${financialInstitutionAccountId}/financial-institution-transactions`, {
searchParams,
headers: {
Accept: 'application/json',
Authorization: `Bearer ${this.#accessToken}`
},
responseType: 'json',
resolveBodyOnly: true
})
const { meta: { paging: { limit, before, after } } } = body
if (before) {
body.previous = () => {
return this.listFinancialInstitutionTransactions(financialInstitutionId, financialInstitutionAccountId, { limit, before })
}
}
if (after) {
body.next = () => {
return this.listFinancialInstitutionTransactions(financialInstitutionId, financialInstitutionAccountId, { limit, after })
}
}
return body
}
/**
* Get a Transaction for a Financial Institution
*
* @param {string} financialInstitutionId - id of a Financial Institution
* @param {string} financialInstitutionAccountId - id of a Financial Institution Account
* @param {string}financialInstitutionTransactionId - id of a Financial Institution Account Transaction
* @returns {Promise} Promise object represents a transaction
*/
async getFinancialInstitutionTransaction (financialInstitutionId, financialInstitutionAccountId, financialInstitutionTransactionId) {
if (!this.sandboxMode) throw new Error('getFinancialInstitutionAccount is Sandbox only, use getAccount instead')
if (!financialInstitutionId) throw new Error('financialInstitutionId is required')
if (typeof financialInstitutionId !== 'string') throw new TypeError('financialInstitutionId should be of type string')
if (!financialInstitutionAccountId) throw new Error('financialInstitutionAccountId is required')
if (typeof financialInstitutionAccountId !== 'string') throw new TypeError('financialInstitutionAccountId should be of type string')
if (!financialInstitutionTransactionId) throw new Error('financialInstitutionTransactionId is required')
if (typeof financialInstitutionTransactionId !== 'string') throw new TypeError('financialInstitutionTransactionId should be of type string')
await this.#checkAccessToken()
return got.get(`${this.#url}financial-institutions/${financialInstitutionId}/financial-institution-accounts/${financialInstitutionAccountId}/financial-institution-transactions/${financialInstitutionTransactionId}`, {
headers: {
Accept: 'application/json',
Authorization: `Bearer ${this.#accessToken}`
},
responseType: 'json',
resolveBodyOnly: true
})
}
/**
* Get a list of Accounts
*
* @param {object} pagingParameters - paging parameters object
* @param {number} pagingParameters.limit - amount of Financial Institutions to return
* @param {string} pagingParameters.before - cursor to get Financial Institutions before this Financial Institution
* @param {string} pagingParameters.after cursor to get Financial Institutions after this Financial Institution
* @returns {Promise} Promise object represents a list of your accounts
*/
async listAccounts (pagingParameters) {
if (this.sandboxMode) throw new Error('listAccounts has no sandbox, use listFinancialInstitutionAccounts instead')
const searchParams = validateSearchParameters(pagingParameters)
await this.#checkAccessToken()
const body = await got.get(`${this.#url}accounts`, {
searchParams,
headers: {
Accept: 'application/json',
Authorization: `Bearer ${this.#accessToken}`
},
responseType: 'json',
resolveBodyOnly: true
})
const { meta: { paging: { limit, before, after } } } = body
if (before) {
body.previous = () => {
return this.listAccounts({ limit, before })
}
}
if (after) {
body.next = () => {
return this.listAccounts({ limit, after })
}
}
return body
}
/**
* Get an Account
*
* @param {string} accountId id of an Account
* @returns {Promise} Promise object represents an Account
*/
async getAccount (accountId) {
if (this.sandboxMode) throw new Error('getAccount has no Sandbox, use getFinancialInstitutionAccount instead')
if (!accountId) throw new Error('accountId is required')
if (typeof accountId !== 'string') throw new TypeError('accountId should be of type string')
await this.#checkAccessToken()
return got.get(`${this.#url}accounts/${accountId}`, {
headers: {
Accept: 'application/json',
Authorization: `Bearer ${this.#accessToken}`
},
responseType: 'json',
resolveBodyOnly: true
})
}
/**
* Sync accountDetails or accountTransactions for an Account
*
* @param {string} accountId id of an Account
* @param {string} [subtype=accountDetails] - two options 'accountDetails' || 'accountTransactions'
* @returns {Promise} Promise object represents a new sync object with id
*/
async syncAccount (accountId, subtype = 'accountDetails') {
if (this.sandboxMode) throw new Error('syncAccount has no sandbox')
if (!accountId) throw new Error('accountId is required')
if (typeof accountId !== 'string') throw new TypeError('accountId should be of type string')
if (!['accountDetails', 'accountTransactions'].includes(subtype)) throw new Error('subType should: "accountDetails" or "accountTransactions"')
await this.#checkAccessToken()
const body = await got.post(`${this.#url}synchronizations`, {
headers: {
Accept: 'application/json',
Authorization: `Bearer ${this.#accessToken}`,
'Content-Type': 'application/json'
},
responseType: 'json',
resolveBodyOnly: true,
json: {
data: {
type: 'synchronization',
attributes: {
resourceType: 'account',
resourceId: accountId,
subtype
}
}
}
})
return body
}
/**
* Get a synchronisation
*
* @param {string} synchronizationId
* @returns {Promise} Promise object represents a sync object
*/
async getSynchronization (synchronizationId) {
if (this.sandboxMode) throw new Error('getSynchronization has no sandbox')
if (!synchronizationId) throw new Error('synchronizationId is required')
if (typeof synchronizationId !== 'string') throw new TypeError('synchronizationId should be of type string')
await this.#checkAccessToken()
return got.post(`${this.#url}synchronizations/'${synchronizationId}`, {
headers: {
Accept: 'application/json',
Authorization: `Bearer ${this.#accessToken}`
},
responseType: 'json',
resolveBodyOnly: true
})
}
/**
* Get a list of Transactions for an Account
*
* @param {string} accountId - Id of the account
* @param {object} pagingParameters - paging parameters object
* @param {number} pagingParameters.limit - amount of Transactions to return
* @param {string} pagingParameters.before - cursor to get Transactions before this Transaction
* @param {string} pagingParameters.after cursor to get Transactions after this Transaction
* @returns {Promise} Promise object represents a list of your Transactions for the account
*/
async listTransactions (accountId, pagingParameters) {
if (this.sandboxMode) throw new Error('getSynchronization has no sandbox')
if (!accountId) throw new Error('accountId is required')
if (typeof accountId !== 'string') throw new TypeError('accountId should be of type string')
const searchParams = validateSearchParameters(pagingParameters)
await this.#checkAccessToken()
const body = await got.get(`${this.#url}accounts/${accountId}/transactions`, {
searchParams,
headers: {
Accept: 'application/json',
Authorization: `Bearer ${this.#accessToken}`
},
responseType: 'json',
resolveBodyOnly: true
})
const { meta: { paging: { limit, before, after } } } = body
if (before) {
body.previous = () => {
return this.listTransactions(accountId, { limit, before })
}
}
if (after) {
body.next = () => {
return this.listTransactions(accountId, { limit, after })
}
}
return body
}
async getTransaction (accountId, transactionId) {
if (this.sandboxMode) throw new Error('getSynchronization has no sandbox')
if (!accountId) throw new Error('accountId is required')
if (typeof accountId !== 'string') throw new TypeError('accountId should be of type string')
if (!transactionId) throw new Error('transactionId is required')
if (typeof transactionId !== 'string') throw new TypeError('transactionId should be of type string')
await this.#checkAccessToken()
return got.get(`${this.#url}accounts/${accountId}/transactions/${transactionId}`, {
headers: {
Accept: 'application/json',
Authorization: `Bearer ${this.#accessToken}`
},
responseType: 'json',
resolveBodyOnly: true
})
}
}
module.exports = Ponto