diff --git a/examples/config.js b/examples/config.js deleted file mode 100644 index b7a9634..0000000 --- a/examples/config.js +++ /dev/null @@ -1,25 +0,0 @@ -const _ = require('lodash'); - -const ebics = require('../index'); -const config = require('./config.json'); -var myArgs = process.argv.slice(2); -//We can run this script with an argument. I.e. "testing" or "production" and it will load the corresponding configuration values for the corresponding environment from config.json -const whichEnvironment = myArgs[0].trim().toLowerCase(); -const defaultConfig = config.testing; -//We default back to test in case whichEnvironment argument is not passed while running the script, so we don't accidentally end up making changes in production. -const environment = whichEnvironment || 'testing'; -const environmentConfig = config[environment]; -if (!environmentConfig) { - console.log("ERROR! Could not find configuration for \""+whichEnvironment+"\" using configuration for \"testing\" instead"); -} - -global.gFinalConfig = _.merge(defaultConfig, environmentConfig); - -global.gClient = new ebics.Client({ - url: gFinalConfig.serverAddress, - partnerId: gFinalConfig.partnerId, - userId: gFinalConfig.userId, - hostId: gFinalConfig.hostId, - passphrase: gFinalConfig.passphrase, // keys-test will be encrypted with this passphrase - keyStorage: ebics.fsKeysStorage(gFinalConfig.keyStorage), -}); diff --git a/examples/config.json b/examples/config.json deleted file mode 100644 index 88c38b6..0000000 --- a/examples/config.json +++ /dev/null @@ -1,42 +0,0 @@ -{ - "testing": { - "serverAddress": "https://ebics-test.server", - "partnerId": "EBICS ParnerID Test", - "userId": "MyUserIdTest", - "hostId": "MyHostIdTest", - "passphrase": "MyPasswordTest", - "keyStorage": "./keys-test", - "bankName":"Test Bank", - "languageCode":"en" - }, - "development": { - "serverAddress": "https://ebics-development.server", - "partnerId": "EBICS ParnerID Development", - "userId": "MyUserIdDevelopment", - "hostId": "MyHostIdDevelopment", - "passphrase": "MyPasswordDevelopment", - "keyStorage": "./keys-development", - "bankName":"Development Bank", - "languageCode":"en" - }, - "staging": { - "serverAddress": "https://ebics-staging.server", - "partnerId": "EBICS ParnerID Staging", - "userId": "MyUserIdStaging", - "hostId": "MyHostIdStaging", - "passphrase": "MyPasswordStaging", - "keyStorage": "./keys-staging", - "bankName":"Staging Bank", - "languageCode":"en" - }, - "production": { - "serverAddress": "https://ebics.server", - "partnerId": "EBICS ParnerID Production", - "userId": "MyUserIdProduction", - "hostId": "MyHostIdProduction", - "passphrase": "MyPasswordProduction", - "keyStorage": "./keys-prod", - "bankName":"Production Bank", - "languageCode":"en" - } -} diff --git a/examples/config/config.json b/examples/config/config.json new file mode 100644 index 0000000..a1b6815 --- /dev/null +++ b/examples/config/config.json @@ -0,0 +1,10 @@ +{ + "serverAddress": "https://ebics-test.server", + "partnerId": "EBICS ParnerID Test", + "userId": "MyUserIdTest", + "hostId": "MyHostIdTest", + "passphrase": "MyPasswordTest", + "keyStorage": "./keys-test", + "bankName":"Test Bank", + "languageCode":"en" +} diff --git a/examples/config/config.production.json b/examples/config/config.production.json new file mode 100644 index 0000000..7432101 --- /dev/null +++ b/examples/config/config.production.json @@ -0,0 +1,10 @@ +{ + "serverAddress": "https://ebics.server", + "partnerId": "EBICS ParnerID Production", + "userId": "MyUserIdProduction", + "hostId": "MyHostIdProduction", + "passphrase": "MyPasswordProduction", + "keyStorage": "./keys-prod", + "bankName":"Production Bank", + "languageCode":"en" +} diff --git a/examples/getClient.js b/examples/getClient.js new file mode 100644 index 0000000..a473f42 --- /dev/null +++ b/examples/getClient.js @@ -0,0 +1,21 @@ +'use strict'; + +const { Client, fsKeysStorage } = require('../index'); + +const loadConfig = require('./loadConfig'); + +module.exports = ({ + serverAddress, + partnerId, + userId, + hostId, + passphrase, + keyStoragePath, +} = loadConfig()) => new Client({ + serverAddress, + partnerId, + userId, + hostId, + passphrase, + keyStorage: fsKeysStorage(keyStoragePath), +}); diff --git a/examples/loadConfig.js b/examples/loadConfig.js new file mode 100644 index 0000000..5442c9e --- /dev/null +++ b/examples/loadConfig.js @@ -0,0 +1,35 @@ +'use strict'; + +const fs = require('fs'); +const path = require('path'); + +const safeLoadJson = (file) => { + if (!file) + return {}; + + try { + return JSON.parse(fs.readFileSync(file)); + } catch (e) { + console.warn(`Couldn't load ${file} config file.`); + return {}; + } +} + +const getDefaultEnv = () => { + const [,,parArg] = process.argv; + return parArg || process.env.NODE_ENV; +} + +const loadConfig = (configDirectory = path.join(__dirname, './config'), env = getDefaultEnv()) => { + console.log(`Loading config form ${configDirectory} with env set to ${env}.`); + + const baseConfigFile = path.join(configDirectory, 'config.json'); + const envConfigFile = env ? path.join(configDirectory, `config.${env}.json`) : null; + + return { + ...safeLoadJson(baseConfigFile), + ...safeLoadJson(envConfigFile), + } +} + +module.exports = loadConfig(); diff --git a/examples/save-bank-keys.js b/examples/save-bank-keys.js index 0b0261e..7ac3ee1 100644 --- a/examples/save-bank-keys.js +++ b/examples/save-bank-keys.js @@ -2,10 +2,7 @@ 'use strict'; -const ebics = require('../index'); -const configjs = require('./config.js'); - -const client = gClient; +const client = require('./getClient')(); // Client keys must be already generated and send by letter. // The bank should have enabled the user diff --git a/examples/send-c52-order.js b/examples/send-c52-order.js index 2a8f95d..862379d 100644 --- a/examples/send-c52-order.js +++ b/examples/send-c52-order.js @@ -2,10 +2,7 @@ 'use strict'; -const ebics = require('../index'); -const configjs = require('./config.js'); - -const client = gClient; +const client = require('./getClient')(); // The bank keys must have been already saved client.send(ebics.Orders.C52(null, null)) // startDate 'YYYY-MM-DD', endDate 'YYYY-MM-DD' diff --git a/examples/send-c53-order.js b/examples/send-c53-order.js index 40d004f..a366fb2 100644 --- a/examples/send-c53-order.js +++ b/examples/send-c53-order.js @@ -2,10 +2,7 @@ 'use strict'; -const ebics = require('../index'); -const configjs = require('./config.js'); - -const client = gClient; +const client = require('./getClient')(); // The bank keys must have been already saved client.send(ebics.Orders.C53(null, null)) // startDate 'YYYY-MM-DD', endDate 'YYYY-MM-DD' diff --git a/examples/send-dki-order.js b/examples/send-dki-order.js index 9a44bf3..7796926 100644 --- a/examples/send-dki-order.js +++ b/examples/send-dki-order.js @@ -2,10 +2,7 @@ 'use strict'; -const ebics = require('../index'); -const configjs = require('./config.js'); - -const client = gClient; +const client = require('./getClient')(); // The bank keys must have been already saved client.send(ebics.Orders.DKI(null, null)) // startDate 'YYYY-MM-DD', endDate 'YYYY-MM-DD' diff --git a/examples/send-hkd-order.js b/examples/send-hkd-order.js index fc05f17..3e8eafc 100644 --- a/examples/send-hkd-order.js +++ b/examples/send-hkd-order.js @@ -2,10 +2,7 @@ 'use strict'; -const ebics = require('../index'); -const configjs = require('./config.js'); - -const client = gClient; +const client = require('./getClient')(); // The bank keys must have been already saved client.send(ebics.Orders.HKD) diff --git a/examples/send-htd-order.js b/examples/send-htd-order.js index 6a06122..4eb8488 100644 --- a/examples/send-htd-order.js +++ b/examples/send-htd-order.js @@ -2,10 +2,7 @@ 'use strict'; -const ebics = require('../index'); -const configjs = require('./config.js'); - -const client = gClient; +const client = require('./getClient')(); // The bank keys must have been already saved client.send(ebics.Orders.HTD) diff --git a/examples/send-sta-order.js b/examples/send-sta-order.js index db1d6f9..6dd19db 100644 --- a/examples/send-sta-order.js +++ b/examples/send-sta-order.js @@ -2,10 +2,7 @@ 'use strict'; -const ebics = require('../index'); -const configjs = require('./config.js'); - -const client = gClient; +const client = require('./getClient')(); // The bank keys must have been already saved client.send(ebics.Orders.STA(null, null)) // startDate 'YYYY-MM-DD', endDate 'YYYY-MM-DD' diff --git a/examples/send-vmk-order.js b/examples/send-vmk-order.js index cba6bfc..d35e818 100644 --- a/examples/send-vmk-order.js +++ b/examples/send-vmk-order.js @@ -2,10 +2,7 @@ 'use strict'; -const ebics = require('../index'); -const configjs = require('./config.js'); - -const client = gClient; +const client = require('./getClient')(); // The bank keys must have been already saved client.send(ebics.Orders.VMK(null, null)) // startDate 'YYYY-MM-DD', endDate 'YYYY-MM-DD' diff --git a/examples/send-xg1-order.js b/examples/send-xg1-order.js index 6596aff..4b93c97 100644 --- a/examples/send-xg1-order.js +++ b/examples/send-xg1-order.js @@ -5,14 +5,7 @@ const ebics = require('../index'); const fs = require('fs'); -const client = new ebics.Client({ - url: 'https://ebics.server', - partnerId: '', - userId: '', - hostId: '', - passphrase: 'test', // keys-test will be decrypted with this passphrase - keyStorage: ebics.fsKeysStorage('./keys-test'), -}); +const client = require('./getClient')(); // The bank keys must have been already saved const paymentFile = fs.readFileSync('mytestfile.xml').toString();