feat: generalize examples

This commit is contained in:
nanov 2019-12-18 16:48:05 +02:00
parent fcbf4ca165
commit f6dfdf4c40
15 changed files with 85 additions and 107 deletions

View File

@ -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),
});

View File

@ -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"
}
}

View File

@ -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"
}

View File

@ -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"
}

21
examples/getClient.js Normal file
View File

@ -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),
});

35
examples/loadConfig.js Normal file
View File

@ -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();

View File

@ -2,10 +2,7 @@
'use strict'; 'use strict';
const ebics = require('../index'); const client = require('./getClient')();
const configjs = require('./config.js');
const client = gClient;
// Client keys must be already generated and send by letter. // Client keys must be already generated and send by letter.
// The bank should have enabled the user // The bank should have enabled the user

View File

@ -2,10 +2,7 @@
'use strict'; 'use strict';
const ebics = require('../index'); const client = require('./getClient')();
const configjs = require('./config.js');
const client = gClient;
// The bank keys must have been already saved // The bank keys must have been already saved
client.send(ebics.Orders.C52(null, null)) // startDate 'YYYY-MM-DD', endDate 'YYYY-MM-DD' client.send(ebics.Orders.C52(null, null)) // startDate 'YYYY-MM-DD', endDate 'YYYY-MM-DD'

View File

@ -2,10 +2,7 @@
'use strict'; 'use strict';
const ebics = require('../index'); const client = require('./getClient')();
const configjs = require('./config.js');
const client = gClient;
// The bank keys must have been already saved // The bank keys must have been already saved
client.send(ebics.Orders.C53(null, null)) // startDate 'YYYY-MM-DD', endDate 'YYYY-MM-DD' client.send(ebics.Orders.C53(null, null)) // startDate 'YYYY-MM-DD', endDate 'YYYY-MM-DD'

View File

@ -2,10 +2,7 @@
'use strict'; 'use strict';
const ebics = require('../index'); const client = require('./getClient')();
const configjs = require('./config.js');
const client = gClient;
// The bank keys must have been already saved // The bank keys must have been already saved
client.send(ebics.Orders.DKI(null, null)) // startDate 'YYYY-MM-DD', endDate 'YYYY-MM-DD' client.send(ebics.Orders.DKI(null, null)) // startDate 'YYYY-MM-DD', endDate 'YYYY-MM-DD'

View File

@ -2,10 +2,7 @@
'use strict'; 'use strict';
const ebics = require('../index'); const client = require('./getClient')();
const configjs = require('./config.js');
const client = gClient;
// The bank keys must have been already saved // The bank keys must have been already saved
client.send(ebics.Orders.HKD) client.send(ebics.Orders.HKD)

View File

@ -2,10 +2,7 @@
'use strict'; 'use strict';
const ebics = require('../index'); const client = require('./getClient')();
const configjs = require('./config.js');
const client = gClient;
// The bank keys must have been already saved // The bank keys must have been already saved
client.send(ebics.Orders.HTD) client.send(ebics.Orders.HTD)

View File

@ -2,10 +2,7 @@
'use strict'; 'use strict';
const ebics = require('../index'); const client = require('./getClient')();
const configjs = require('./config.js');
const client = gClient;
// The bank keys must have been already saved // The bank keys must have been already saved
client.send(ebics.Orders.STA(null, null)) // startDate 'YYYY-MM-DD', endDate 'YYYY-MM-DD' client.send(ebics.Orders.STA(null, null)) // startDate 'YYYY-MM-DD', endDate 'YYYY-MM-DD'

View File

@ -2,10 +2,7 @@
'use strict'; 'use strict';
const ebics = require('../index'); const client = require('./getClient')();
const configjs = require('./config.js');
const client = gClient;
// The bank keys must have been already saved // The bank keys must have been already saved
client.send(ebics.Orders.VMK(null, null)) // startDate 'YYYY-MM-DD', endDate 'YYYY-MM-DD' client.send(ebics.Orders.VMK(null, null)) // startDate 'YYYY-MM-DD', endDate 'YYYY-MM-DD'

View File

@ -5,14 +5,7 @@
const ebics = require('../index'); const ebics = require('../index');
const fs = require('fs'); const fs = require('fs');
const client = new ebics.Client({ const client = require('./getClient')();
url: 'https://ebics.server',
partnerId: '',
userId: '',
hostId: '',
passphrase: 'test', // keys-test will be decrypted with this passphrase
keyStorage: ebics.fsKeysStorage('./keys-test'),
});
// The bank keys must have been already saved // The bank keys must have been already saved
const paymentFile = fs.readFileSync('mytestfile.xml').toString(); const paymentFile = fs.readFileSync('mytestfile.xml').toString();