@vouchfor/sdk 1.1.9 → 1.1.12
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.
- package/lib/services/baseService.js +3 -1
- package/lib/services/integration/base.js +3 -3
- package/lib/services/integration/integrationService.js +2 -2
- package/lib/services/rest/applicationService.js +4 -4
- package/lib/services/rest/applicationStorageService.js +3 -3
- package/lib/services/rest/base.js +2 -2
- package/lib/services/rest/campaignService.js +3 -3
- package/lib/services/rest/entityService.js +3 -3
- package/lib/services/rest/fileService.js +2 -2
- package/lib/services/rest/vouchService.js +2 -2
- package/lib/shared/utils.js +55 -5
- package/lib/vouch.js +3 -3
- package/package.json +1 -1
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
const utils = require('../shared/utils');
|
|
2
2
|
|
|
3
3
|
class BaseService {
|
|
4
|
-
constructor(client, config = {}) {
|
|
4
|
+
constructor(client, config = {}, logger) {
|
|
5
5
|
const basePath = config.basePath ? `/${config.basePath}` : '';
|
|
6
6
|
|
|
7
7
|
this._client = client;
|
|
8
8
|
this._apiKey = config.apiKey || '';
|
|
9
9
|
this._integrationKey = config.integrationKey || '';
|
|
10
10
|
this._baseUrl = `${config.baseUrl}${basePath}`;
|
|
11
|
+
this._logger = logger;
|
|
11
12
|
}
|
|
12
13
|
|
|
13
14
|
async _callApi({
|
|
@@ -34,6 +35,7 @@ class BaseService {
|
|
|
34
35
|
headers: apiHeaders,
|
|
35
36
|
method,
|
|
36
37
|
url,
|
|
38
|
+
logger: this._logger
|
|
37
39
|
});
|
|
38
40
|
}
|
|
39
41
|
}
|
|
@@ -2,13 +2,13 @@ const BaseService = require('../baseService');
|
|
|
2
2
|
const utils = require('../../shared/utils');
|
|
3
3
|
|
|
4
4
|
class BaseIntegrationService extends BaseService {
|
|
5
|
-
constructor(client, config = {}, basePath = '') {
|
|
5
|
+
constructor(client, config = {}, basePath = '', logger) {
|
|
6
6
|
super(client, {
|
|
7
7
|
apiKey: config.integrationKey,
|
|
8
8
|
baseUrl: `https://apps${utils.getApiSuffix(config.env)}.vouchfor.com/v1`,
|
|
9
9
|
basePath,
|
|
10
10
|
env: config.env,
|
|
11
|
-
});
|
|
11
|
+
}, logger);
|
|
12
12
|
}
|
|
13
13
|
|
|
14
14
|
_request({ body, method, path }) {
|
|
@@ -21,4 +21,4 @@ class BaseIntegrationService extends BaseService {
|
|
|
21
21
|
}
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
module.exports = BaseIntegrationService;
|
|
24
|
+
module.exports = BaseIntegrationService;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
const BaseIntegrationService = require('./base');
|
|
2
2
|
|
|
3
3
|
class IntegrationService extends BaseIntegrationService {
|
|
4
|
-
constructor(client, config) {
|
|
5
|
-
super(client, config, '');
|
|
4
|
+
constructor(client, config, logger) {
|
|
5
|
+
super(client, config, '', logger);
|
|
6
6
|
}
|
|
7
7
|
|
|
8
8
|
get applications() {
|
|
@@ -2,9 +2,9 @@ const BaseRestService = require('./base');
|
|
|
2
2
|
const ApplicationStorageService = require('./applicationStorageService');
|
|
3
3
|
|
|
4
4
|
class ApplicationService extends BaseRestService {
|
|
5
|
-
constructor(client, config) {
|
|
6
|
-
super(client, config, 'applications');
|
|
7
|
-
this.storage = new ApplicationStorageService(client, config);
|
|
5
|
+
constructor(client, config, logger) {
|
|
6
|
+
super(client, config, 'applications', logger);
|
|
7
|
+
this.storage = new ApplicationStorageService(client, config, logger);
|
|
8
8
|
}
|
|
9
9
|
|
|
10
10
|
create(application, apiKey) {
|
|
@@ -36,4 +36,4 @@ class ApplicationService extends BaseRestService {
|
|
|
36
36
|
}
|
|
37
37
|
}
|
|
38
38
|
|
|
39
|
-
module.exports = ApplicationService;
|
|
39
|
+
module.exports = ApplicationService;
|
|
@@ -5,8 +5,8 @@ function storagePath(id, key) {
|
|
|
5
5
|
}
|
|
6
6
|
|
|
7
7
|
class ApplicationStorageService extends BaseRestService {
|
|
8
|
-
constructor(client, config) {
|
|
9
|
-
super(client, config, 'applications');
|
|
8
|
+
constructor(client, config, logger) {
|
|
9
|
+
super(client, config, 'applications', logger);
|
|
10
10
|
}
|
|
11
11
|
|
|
12
12
|
set(id, key, data, apiKey) {
|
|
@@ -33,4 +33,4 @@ class ApplicationStorageService extends BaseRestService {
|
|
|
33
33
|
}
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
-
module.exports = ApplicationStorageService;
|
|
36
|
+
module.exports = ApplicationStorageService;
|
|
@@ -2,14 +2,14 @@ const BaseService = require('../baseService');
|
|
|
2
2
|
const utils = require('../../shared/utils');
|
|
3
3
|
|
|
4
4
|
class BaseRestService extends BaseService {
|
|
5
|
-
constructor(client, config = {}, basePath = '') {
|
|
5
|
+
constructor(client, config = {}, basePath = '', logger) {
|
|
6
6
|
super(client, {
|
|
7
7
|
apiKey: config.apiKey,
|
|
8
8
|
integrationKey: config.integrationKey,
|
|
9
9
|
baseUrl: `https://api${utils.getApiSuffix(config.env)}.vouchfor.com/v1`,
|
|
10
10
|
basePath,
|
|
11
11
|
env: config.env,
|
|
12
|
-
});
|
|
12
|
+
}, logger);
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
_request({ apiKey, body, method, path, headers }) {
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
const BaseRestService = require('./base');
|
|
2
2
|
|
|
3
3
|
class CampaignService extends BaseRestService {
|
|
4
|
-
constructor(client, config) {
|
|
5
|
-
super(client, config, 'campaigns');
|
|
4
|
+
constructor(client, config, logger) {
|
|
5
|
+
super(client, config, 'campaigns', logger);
|
|
6
6
|
}
|
|
7
7
|
|
|
8
8
|
create(campaign, apiKey) {
|
|
@@ -34,4 +34,4 @@ class CampaignService extends BaseRestService {
|
|
|
34
34
|
}
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
-
module.exports = CampaignService;
|
|
37
|
+
module.exports = CampaignService;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
const BaseRestService = require('./base');
|
|
2
2
|
|
|
3
3
|
class EntityService extends BaseRestService {
|
|
4
|
-
constructor(client, config) {
|
|
5
|
-
super(client, config, 'entities');
|
|
4
|
+
constructor(client, config, logger) {
|
|
5
|
+
super(client, config, 'entities', logger);
|
|
6
6
|
}
|
|
7
7
|
|
|
8
8
|
create(entity, apiKey) {
|
|
@@ -30,4 +30,4 @@ class EntityService extends BaseRestService {
|
|
|
30
30
|
}
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
-
module.exports = EntityService;
|
|
33
|
+
module.exports = EntityService;
|
|
@@ -5,8 +5,8 @@ const axios = require('axios');
|
|
|
5
5
|
const BaseRestService = require('./base');
|
|
6
6
|
|
|
7
7
|
class FileService extends BaseRestService {
|
|
8
|
-
constructor(client, config) {
|
|
9
|
-
super(client, config, 'files');
|
|
8
|
+
constructor(client, config, logger) {
|
|
9
|
+
super(client, config, 'files', logger);
|
|
10
10
|
}
|
|
11
11
|
|
|
12
12
|
upload(file, key, apiKey, { extract } = {}) {
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
const BaseRestService = require('./base');
|
|
2
2
|
|
|
3
3
|
class VouchService extends BaseRestService {
|
|
4
|
-
constructor(client, config) {
|
|
5
|
-
super(client, config, 'vouches');
|
|
4
|
+
constructor(client, config, logger) {
|
|
5
|
+
super(client, config, 'vouches', logger);
|
|
6
6
|
}
|
|
7
7
|
|
|
8
8
|
create(vouch, apiKey) {
|
package/lib/shared/utils.js
CHANGED
|
@@ -1,22 +1,65 @@
|
|
|
1
1
|
const followRedirects = require('follow-redirects');
|
|
2
|
-
const { https } = followRedirects;
|
|
2
|
+
const { http, https } = followRedirects;
|
|
3
3
|
|
|
4
4
|
followRedirects.maxRedirects = 10;
|
|
5
5
|
followRedirects.maxBodyLength = Infinity;
|
|
6
6
|
|
|
7
|
+
function logHttpResponse({ req, res, body, result, logger }) {
|
|
8
|
+
const { protocol, method, host, _header: header } = req._currentRequest;
|
|
9
|
+
const get = req.type === 'GET';
|
|
10
|
+
const type = res.headers['content-type'] ?? 'application/json';
|
|
11
|
+
const json = type.indexOf('application/json') !== -1;
|
|
12
|
+
const data = (json && body ? JSON.stringify(body) : body || '') + '\n';
|
|
13
|
+
const request = header + (get ? '' : data);
|
|
14
|
+
const { httpVersion, statusCode, statusMessage } = res;
|
|
15
|
+
const headers = [];
|
|
16
|
+
const n = res.rawHeaders.length / 2;
|
|
17
|
+
for (let i = 0; i < n; i += 2) {
|
|
18
|
+
headers.push(`${res.rawHeaders[i]}: ${res.rawHeaders[i+1]}`);
|
|
19
|
+
}
|
|
20
|
+
const content = res.headers['content-type'].split('/')[0];
|
|
21
|
+
const binary = [ 'image', 'audio', 'video' ].includes(content);
|
|
22
|
+
const payload = binary ? `<<< ${content} content} >>>` : result;
|
|
23
|
+
const response = `HTTP/${httpVersion} ${statusCode} ${statusMessage}\r\n`
|
|
24
|
+
+ headers.join(`\r\n`) + `\r\n\r\n` + payload + `\r\n`;
|
|
25
|
+
logger.info(`${(protocol + ' ' + method).toUpperCase()} request to host: ${host}\n`
|
|
26
|
+
+ `>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n`
|
|
27
|
+
+ request
|
|
28
|
+
+ `<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n`
|
|
29
|
+
+ response
|
|
30
|
+
+ `>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>`);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function logHttpError({ req, err, body, logger }) {
|
|
34
|
+
const { protocol, method, host, _header: header } = req._currentRequest;
|
|
35
|
+
const get = req.type === 'GET';
|
|
36
|
+
const type = res.headers['content-type'] ?? 'application/json';
|
|
37
|
+
const json = type.indexOf('application/json') !== -1;
|
|
38
|
+
const data = (json && body ? JSON.stringify(body) : body || '') + '\n';
|
|
39
|
+
const request = header + (get ? '' : data);
|
|
40
|
+
const error = err + `\r\n`;
|
|
41
|
+
logger.info(`${(protocol + ' ' + method).toUpperCase()} request to host: ${host}\n`
|
|
42
|
+
+ `>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n`
|
|
43
|
+
+ request
|
|
44
|
+
+ `<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n`
|
|
45
|
+
+ error
|
|
46
|
+
+ `>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>`);
|
|
47
|
+
}
|
|
48
|
+
|
|
7
49
|
module.exports = {
|
|
8
50
|
getApiSuffix(env = 'prod') {
|
|
9
51
|
const isProd = env === 'prod';
|
|
10
52
|
return isProd ? '' : `-${env}`;
|
|
11
53
|
},
|
|
12
|
-
request({ body, headers, method, url }) {
|
|
13
|
-
const {
|
|
54
|
+
request({ body, headers, method, url, logger }) {
|
|
55
|
+
const { protocol, hostname, port, pathname, search = '' } = new URL(url);
|
|
14
56
|
|
|
15
57
|
return new Promise((resolve, reject) => {
|
|
16
58
|
let result = '';
|
|
17
|
-
const req = https
|
|
59
|
+
const req = (protocol === 'https:' ? https : http)
|
|
18
60
|
.request({
|
|
19
|
-
|
|
61
|
+
hostname,
|
|
62
|
+
port,
|
|
20
63
|
path: `${pathname}${search}`,
|
|
21
64
|
headers,
|
|
22
65
|
method: method ? method : body ? 'POST' : 'GET',
|
|
@@ -27,6 +70,10 @@ module.exports = {
|
|
|
27
70
|
});
|
|
28
71
|
res.on('end', () => {
|
|
29
72
|
try {
|
|
73
|
+
if (logger) {
|
|
74
|
+
logHttpResponse({ req, res, body, result, logger });
|
|
75
|
+
}
|
|
76
|
+
|
|
30
77
|
if (res.statusCode >= 400) {
|
|
31
78
|
const error = res.statusMessage || 'error';
|
|
32
79
|
try {
|
|
@@ -47,6 +94,9 @@ module.exports = {
|
|
|
47
94
|
},
|
|
48
95
|
)
|
|
49
96
|
.on('error', (err) => {
|
|
97
|
+
if (logger) {
|
|
98
|
+
logHttpError({ req, err, body, logger });
|
|
99
|
+
}
|
|
50
100
|
reject(err);
|
|
51
101
|
});
|
|
52
102
|
if (typeof body?.pipe === 'function') {
|
package/lib/vouch.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
class Vouch {
|
|
2
|
-
constructor(config = {}) {
|
|
2
|
+
constructor(config = {}, logger) {
|
|
3
3
|
this.config = Object.freeze({
|
|
4
4
|
apiKey: config.apiKey,
|
|
5
5
|
integrationKey: config.integrationKey,
|
|
@@ -9,10 +9,10 @@ class Vouch {
|
|
|
9
9
|
const services = require('./services');
|
|
10
10
|
Object.keys(services).forEach(
|
|
11
11
|
(key) => {
|
|
12
|
-
this[key] = new services[key](this, this.config);
|
|
12
|
+
this[key] = new services[key](this, this.config, logger);
|
|
13
13
|
}
|
|
14
14
|
);
|
|
15
15
|
}
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
-
module.exports = Vouch;
|
|
18
|
+
module.exports = Vouch;
|