@vouchfor/sdk 1.1.9 → 1.1.11

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.
@@ -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) {
@@ -4,12 +4,54 @@ const { https } = followRedirects;
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 }) {
54
+ request({ body, headers, method, url, logger }) {
13
55
  const { host, pathname, search = '' } = new URL(url);
14
56
 
15
57
  return new Promise((resolve, reject) => {
@@ -27,6 +69,10 @@ module.exports = {
27
69
  });
28
70
  res.on('end', () => {
29
71
  try {
72
+ if (logger) {
73
+ logHttpResponse({ req, res, body, result, logger });
74
+ }
75
+
30
76
  if (res.statusCode >= 400) {
31
77
  const error = res.statusMessage || 'error';
32
78
  try {
@@ -47,6 +93,9 @@ module.exports = {
47
93
  },
48
94
  )
49
95
  .on('error', (err) => {
96
+ if (logger) {
97
+ logHttpError({ req, err, body, logger });
98
+ }
50
99
  reject(err);
51
100
  });
52
101
  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;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vouchfor/sdk",
3
- "version": "1.1.9",
3
+ "version": "1.1.11",
4
4
  "description": "Vouch API SDK",
5
5
  "main": "lib/index.js",
6
6
  "repository": {
package/test.js DELETED
@@ -1,23 +0,0 @@
1
- const Vouch = require("./lib/vouch");
2
-
3
- const vouchClient = new Vouch({
4
- env: 'dev',
5
- integrationKey: 'b0883f06-84af-4108-8543-31874c335c36-kkr3Wy4CLY8C2g65UT79sgUfOIR2ZzkA9uQam3bTs4ZNdlSs7M'
6
- });
7
-
8
- (async () => {
9
- const response = await vouchClient.integrations.applications.list();
10
- console.log(typeof response);
11
- const items = await response.applications.reduce(async (ac, application) => {
12
- const arr = await ac;
13
- console.log(`id => ${application.id}`);
14
- try {
15
- const credentials = await vouchClient.integrations.authenticate({ entityId: application.entity.id });
16
- arr.push(credentials);
17
- } catch (err) {
18
- console.error(`failed to get credentials ${err} - ${application.id}`);
19
- }
20
-
21
- return arr;
22
- }, Promise.resolve([]));
23
- })();