From 27f120b608f77997d9220871818133f769887a6e Mon Sep 17 00:00:00 2001 From: Phil Kunz Date: Thu, 3 Oct 2019 14:04:15 +0200 Subject: [PATCH] fix(core): update --- test/test.ts | 8 ++++++-- ts/bunq.classes.account.ts | 25 +++++++++++++++++++++---- ts/bunq.classes.monetaryaccount.ts | 1 + ts/bunq.paths.ts | 3 ++- ts/bunq.plugins.ts | 2 +- 5 files changed, 31 insertions(+), 8 deletions(-) diff --git a/test/test.ts b/test/test.ts index 632770f..98256b8 100644 --- a/test/test.ts +++ b/test/test.ts @@ -27,15 +27,19 @@ tap.test('should init the client', async () => { tap.test('should get accounts', async () => { const accounts = await testBunqAccount.getAccounts(); - console.log(accounts[2].alias); + console.log(accounts); }); tap.test('should get transactions', async () => { const accounts = await testBunqAccount.getAccounts(); for (const account of accounts) { const transactions = await account.getTransactions(); - // console.log(transactions); + console.log(transactions); } }); +tap.test('should stop the instance', async () => { + await testBunqAccount.stop(); +}) + tap.start(); diff --git a/ts/bunq.classes.account.ts b/ts/bunq.classes.account.ts index 4050cae..a716b29 100644 --- a/ts/bunq.classes.account.ts +++ b/ts/bunq.classes.account.ts @@ -34,13 +34,22 @@ export class BunqAccount { // lets setup bunq client await plugins.smartfile.fs.ensureDir(paths.nogitDir); - await plugins.smartfile.fs.ensureFile(paths.bunqJsonFile, '{}'); - const storageInstance = plugins.JSONFileStore(paths.bunqJsonFile); - this.bunqJSClient = new plugins.bunqCommunityClient.default(storageInstance); + await plugins.smartfile.fs.ensureFile(paths.bunqJsonProductionFile, '{}'); + await plugins.smartfile.fs.ensureFile(paths.bunqJsonSandboxFile, '{}'); + let apiKey: string; + + if (this.options.environment === 'SANDBOX') { + this.bunqJSClient = new plugins.bunqCommunityClient.default(plugins.JSONFileStore(paths.bunqJsonSandboxFile)); + apiKey = await this.bunqJSClient.api.sandboxUser.post(); + console.log(apiKey); + } else { + this.bunqJSClient = new plugins.bunqCommunityClient.default(plugins.JSONFileStore(paths.bunqJsonProductionFile)); + apiKey = this.options.apiKey; + } // run the bunq application with our API key await this.bunqJSClient.run( - this.options.apiKey, + apiKey, this.permittedIps, this.options.environment, this.encryptionKey @@ -79,4 +88,12 @@ export class BunqAccount { } return accountsArray; } + + public async stop() { + if (this.bunqJSClient) { + this.bunqJSClient.setKeepAlive(false); + this.bunqJSClient.destroyApiSession(); + this.bunqJSClient = null; + } + } } diff --git a/ts/bunq.classes.monetaryaccount.ts b/ts/bunq.classes.monetaryaccount.ts index 00f9c02..7c951c3 100644 --- a/ts/bunq.classes.monetaryaccount.ts +++ b/ts/bunq.classes.monetaryaccount.ts @@ -97,5 +97,6 @@ export class MonetaryAccount { for (const apiTransaction of apiTransactions) { transactionsArray.push(Transaction.fromApiObject(this, apiTransaction)); } + return transactionsArray; } } diff --git a/ts/bunq.paths.ts b/ts/bunq.paths.ts index 95b3ab1..319c016 100644 --- a/ts/bunq.paths.ts +++ b/ts/bunq.paths.ts @@ -3,4 +3,5 @@ import * as plugins from './bunq.plugins'; export const packageDir = plugins.path.join(__dirname, '../'); export const nogitDir = plugins.path.join(packageDir, './.nogit/'); -export const bunqJsonFile = plugins.path.join(nogitDir, 'bunq.json'); \ No newline at end of file +export const bunqJsonProductionFile = plugins.path.join(nogitDir, 'bunqproduction.json'); +export const bunqJsonSandboxFile = plugins.path.join(nogitDir, 'bunqsandbox.json'); \ No newline at end of file diff --git a/ts/bunq.plugins.ts b/ts/bunq.plugins.ts index 93e7266..44f53ff 100644 --- a/ts/bunq.plugins.ts +++ b/ts/bunq.plugins.ts @@ -17,7 +17,7 @@ export { }; // third party -import JSONFileStore from "@bunq-community/bunq-js-client/dist/Stores/JSONFileStore"; +import JSONFileStore from "@bunq-community/bunq-js-client/dist/Stores/JSONFileStore"; import * as bunqCommunityClient from '@bunq-community/bunq-js-client'; export { JSONFileStore, bunqCommunityClient };