@vouchfor/sdk 1.1.5 → 1.1.8

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.
@@ -13,27 +13,29 @@ class BaseService {
13
13
  async _callApi({
14
14
  apiKey = '',
15
15
  integrationKey = '',
16
+ headers,
16
17
  method,
17
18
  body,
18
19
  url,
19
20
  }) {
20
- const headers = {
21
+ const apiHeaders = {
21
22
  Accept: 'application/json',
22
23
  'Content-Type': 'application/json',
23
24
  'x-api-key': apiKey,
25
+ ...headers
24
26
  };
25
27
 
26
28
  if (integrationKey) {
27
- headers['x-integration-key'] = integrationKey;
29
+ apiHeaders['x-integration-key'] = integrationKey;
28
30
  }
29
31
 
30
32
  return utils.request({
31
33
  body,
32
- headers,
34
+ headers: apiHeaders,
33
35
  method,
34
36
  url,
35
37
  });
36
38
  }
37
39
  }
38
40
 
39
- module.exports = BaseService;
41
+ module.exports = BaseService;
@@ -4,4 +4,5 @@ module.exports = Object.freeze({
4
4
  entities: require('./rest/entityService'),
5
5
  integrations: require('./integration/integrationService'),
6
6
  vouches: require('./rest/vouchService'),
7
- });
7
+ files: require('./rest/fileService'),
8
+ });
@@ -12,15 +12,16 @@ class BaseRestService extends BaseService {
12
12
  });
13
13
  }
14
14
 
15
- _request({ apiKey, body, method, path }) {
15
+ _request({ apiKey, body, method, path, headers }) {
16
16
  return this._callApi({
17
17
  apiKey: apiKey || this._apiKey,
18
18
  integrationKey: this._integrationKey,
19
19
  body,
20
+ headers,
20
21
  method,
21
22
  url: `${this._baseUrl}/${path}`,
22
23
  });
23
24
  }
24
25
  }
25
26
 
26
- module.exports = BaseRestService;
27
+ module.exports = BaseRestService;
@@ -0,0 +1,66 @@
1
+ const fs = require('fs');
2
+ const FormData = require('form-data');
3
+ const axios = require('axios');
4
+
5
+ const BaseRestService = require('./base');
6
+
7
+ class FileService extends BaseRestService {
8
+ constructor(client, config) {
9
+ super(client, config, 'files');
10
+ }
11
+
12
+ upload(file, key, apiKey, { extract } = {}) {
13
+ const input = fs.createReadStream(file);
14
+ const form = new FormData();
15
+ form.append('title', key);
16
+ form.append('file', input);
17
+ form.append('extract', !!extract);
18
+ return this._request({
19
+ apiKey,
20
+ body: form,
21
+ path: key,
22
+ headers: form.getHeaders(),
23
+ });
24
+ }
25
+
26
+ /*///////////////////////////////////////////
27
+ // this is what the function should look like
28
+ // but there is an error proxying the data at
29
+ // the moment, I have added a work around
30
+ proxy(url, key, apiKey, { extract, headers } = {}) {
31
+ return axios({
32
+ method: 'GET',
33
+ url,
34
+ headers,
35
+ responseType: 'stream',
36
+ maxContentLength: Infinity,
37
+ maxBodyLength: Infinity,
38
+ }).then(resp => {
39
+ const form = new FormData();
40
+ form.append('title', key);
41
+ form.append('file', resp.data);
42
+ form.append('extract', !!extract);
43
+ return this._request({
44
+ apiKey,
45
+ body: form,
46
+ path: key,
47
+ headers: form.getHeaders(),
48
+ });
49
+ })
50
+ }
51
+ ///////////////////////////////////////////*/
52
+
53
+ proxy(url, key, apiKey, { extract, headers } = {}) {
54
+ return this._request({
55
+ apiKey,
56
+ body: {
57
+ url,
58
+ extract,
59
+ headers
60
+ },
61
+ path: key
62
+ });
63
+ }
64
+ }
65
+
66
+ module.exports = FileService;
@@ -25,6 +25,14 @@ class VouchService extends BaseRestService {
25
25
  return this._request({ apiKey, path: `${id}` });
26
26
  }
27
27
 
28
+ counts(id, apiKey) {
29
+ return this._request({ apiKey, path: `${id}/counts` });
30
+ }
31
+
32
+ status(id, apiKey) {
33
+ return this._request({ apiKey, path: `${id}/status` });
34
+ }
35
+
28
36
  list(apiKey) {
29
37
  return this._request({ apiKey, path: '' });
30
38
  }
@@ -41,7 +49,7 @@ class VouchService extends BaseRestService {
41
49
  function getPath(id, ordinality) {
42
50
  return ordinality
43
51
  ? `${id}/questions/${ordinality}/clips`
44
- : `${id}clips`;
52
+ : `${id}/clips`;
45
53
  }
46
54
 
47
55
  return {
@@ -71,4 +79,4 @@ class VouchService extends BaseRestService {
71
79
  }
72
80
  }
73
81
 
74
- module.exports = VouchService;
82
+ module.exports = VouchService;
@@ -1,4 +1,8 @@
1
- const https = require('https');
1
+ const followRedirects = require('follow-redirects');
2
+ const { https } = followRedirects;
3
+
4
+ followRedirects.maxRedirects = 10;
5
+ followRedirects.maxBodyLength = Infinity;
2
6
 
3
7
  module.exports = {
4
8
  getApiSuffix(env = 'prod') {
@@ -32,7 +36,9 @@ module.exports = {
32
36
  reject(error);
33
37
  }
34
38
 
35
- const data = !!result ? JSON.parse(result) : result;
39
+ const type = res.headers['content-type'] ?? 'application/json';
40
+ const json = type.indexOf('application/json') !== -1;
41
+ const data = !!result && json ? JSON.parse(result) : result;
36
42
  resolve(data);
37
43
  } catch (err) {
38
44
  reject(err);
@@ -43,10 +49,14 @@ module.exports = {
43
49
  .on('error', (err) => {
44
50
  reject(err);
45
51
  });
46
- if (body) {
47
- req.write(JSON.stringify(body));
52
+ if (typeof body?.pipe === 'function') {
53
+ body.pipe(req);
54
+ } else {
55
+ if (body) {
56
+ req.write(JSON.stringify(body));
57
+ }
58
+ req.end();
48
59
  }
49
- req.end();
50
- })
60
+ });
51
61
  }
52
- }
62
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vouchfor/sdk",
3
- "version": "1.1.5",
3
+ "version": "1.1.8",
4
4
  "description": "Vouch API SDK",
5
5
  "main": "lib/index.js",
6
6
  "repository": {
@@ -16,8 +16,11 @@
16
16
  "directories": {
17
17
  "lib": "lib"
18
18
  },
19
- "dependencies": {},
20
- "devDependencies": {},
19
+ "dependencies": {
20
+ "axios": "^1.6.2",
21
+ "follow-redirects": "^1.15.3",
22
+ "form-data": "^4.0.0"
23
+ },
21
24
  "scripts": {
22
25
  "test": "echo \"Error: no test specified\" && exit 1"
23
26
  }
package/test.js CHANGED
@@ -7,6 +7,7 @@ const vouchClient = new Vouch({
7
7
 
8
8
  (async () => {
9
9
  const response = await vouchClient.integrations.applications.list();
10
+ console.log(typeof response);
10
11
  const items = await response.applications.reduce(async (ac, application) => {
11
12
  const arr = await ac;
12
13
  console.log(`id => ${application.id}`);