node-vault-client 1.0.1 → 2.0.0

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/src/Lease.js CHANGED
@@ -1,57 +1,57 @@
1
- 'use strict';
2
-
3
- const _ = require('lodash');
4
-
5
- class Lease {
6
- constructor(
7
- requestId,
8
- leaseId,
9
- leaseDuration,
10
- renewable,
11
- data
12
- ) {
13
- this.__requestId = requestId;
14
- this.__leaseId = leaseId;
15
- this.__leaseDuration = leaseDuration;
16
- this.__renewable = renewable;
17
- this.__data = data === undefined ? {} : data;
18
- }
19
-
20
- static fromResponse(response) {
21
- return new Lease(
22
- response.request_id,
23
- response.lease_id,
24
- response.lease_duration,
25
- response.renewable,
26
- response.data
27
- );
28
- }
29
-
30
- /**
31
- * @param {String} key
32
- * @returns {String}
33
- */
34
- getValue(key) {
35
- if (this.__data[key] === undefined) {
36
- throw new Error('Requested key does not exist');
37
- }
38
-
39
- return this.__data[key];
40
- }
41
-
42
- /**
43
- * @returns {Object}
44
- */
45
- getData() {
46
- return _.cloneDeep(this.__data);
47
- }
48
-
49
- /**
50
- * @returns {bool}
51
- */
52
- isRenewable() {
53
- return this.__renewable;
54
- }
55
- }
56
-
57
- module.exports = Lease;
1
+ 'use strict';
2
+
3
+ const _ = require('lodash');
4
+
5
+ class Lease {
6
+ constructor(
7
+ requestId,
8
+ leaseId,
9
+ leaseDuration,
10
+ renewable,
11
+ data
12
+ ) {
13
+ this.__requestId = requestId;
14
+ this.__leaseId = leaseId;
15
+ this.__leaseDuration = leaseDuration;
16
+ this.__renewable = renewable;
17
+ this.__data = data === undefined ? {} : data;
18
+ }
19
+
20
+ static fromResponse(response) {
21
+ return new Lease(
22
+ response.request_id,
23
+ response.lease_id,
24
+ response.lease_duration,
25
+ response.renewable,
26
+ response.data
27
+ );
28
+ }
29
+
30
+ /**
31
+ * @param {String} key
32
+ * @returns {String}
33
+ */
34
+ getValue(key) {
35
+ if (this.__data[key] === undefined) {
36
+ throw new Error('Requested key does not exist');
37
+ }
38
+
39
+ return this.__data[key];
40
+ }
41
+
42
+ /**
43
+ * @returns {Object}
44
+ */
45
+ getData() {
46
+ return _.cloneDeep(this.__data);
47
+ }
48
+
49
+ /**
50
+ * @returns {bool}
51
+ */
52
+ isRenewable() {
53
+ return this.__renewable;
54
+ }
55
+ }
56
+
57
+ module.exports = Lease;
@@ -1,55 +1,94 @@
1
- 'use strict';
2
-
3
- const rp = require('request-promise');
4
- const urljoin = require('url-join');
5
- const _ = require('lodash');
6
-
7
- class VaultApiClient {
8
-
9
- /**
10
- * @param {Object} config
11
- * @param {String} config.url - the url of the vault server
12
- * @param {String} [config.apiVersion='v1']
13
- * @param {Object} logger
14
- */
15
- constructor(config, logger) {
16
- this.__config = _.defaultsDeep(_.cloneDeep(config), {
17
- apiVersion: 'v1',
18
- });
19
-
20
- this._logger = logger;
21
- }
22
-
23
- makeRequest(method, path, data, headers) {
24
- data = data === undefined ? null : data;
25
- headers = headers === undefined ? {} : headers;
26
-
27
- const requestOptions = {
28
- method: method,
29
- body: data === null ? undefined : data,
30
- uri: urljoin(this.__config.url, this.__config.apiVersion, path),
31
- followRedirects: true,
32
- followAllRedirects: true,
33
- headers,
34
- json: true,
35
- };
36
-
37
- this._logger.debug(
38
- 'making request: %s %s',
39
- requestOptions.method,
40
- requestOptions.uri
41
- );
42
-
43
- return rp(requestOptions)
44
- .then((response) => {
45
- this._logger.debug('%s %s response body:\n%s',
46
- requestOptions.method,
47
- requestOptions.uri,
48
- JSON.stringify(response, null, ' ')
49
- );
50
- return response;
51
- });
52
- }
53
- }
54
-
55
- module.exports = VaultApiClient;
1
+ 'use strict';
2
+
3
+ const urljoin = require('url-join');
4
+ const _ = require('lodash');
5
+
6
+ class VaultApiClient {
7
+
8
+ /**
9
+ * @param {Object} config
10
+ * @param {String} config.url - the url of the vault server
11
+ * @param {String} [config.apiVersion='v1']
12
+ * @param {Object} [config.requestOptions] - extra options shallow-merged into every
13
+ * `fetch()` call. Use it to inject an undici `dispatcher` for a proxy/SOCKS agent
14
+ * or for custom TLS trust (self-signed / internal CA). Request-specific fields
15
+ * (`method`, `body`) always take precedence; `headers` are merged with the
16
+ * per-request headers winning. Stored by reference (not deep-cloned) so live
17
+ * objects such as a Dispatcher keep their prototype and remain usable.
18
+ * @param {Object} logger
19
+ */
20
+ constructor(config, logger) {
21
+ const requestOptions = config && config.requestOptions;
22
+
23
+ this.__config = _.defaultsDeep(_.cloneDeep(_.omit(config, ['requestOptions'])), {
24
+ apiVersion: 'v1',
25
+ });
26
+
27
+ if (requestOptions !== undefined) {
28
+ this.__config.requestOptions = requestOptions;
29
+ }
30
+
31
+ this._logger = logger;
32
+ }
33
+
34
+ makeRequest(method, path, data, headers) {
35
+ data = data === undefined ? null : data;
36
+ headers = headers === undefined ? {} : headers;
37
+
38
+ const uri = urljoin(this.__config.url, this.__config.apiVersion, path);
39
+
40
+ const requestOptions = this.__config.requestOptions || {};
41
+
42
+ const options = Object.assign(
43
+ { redirect: 'follow' },
44
+ requestOptions,
45
+ {
46
+ method: method,
47
+ headers: Object.assign(
48
+ { Accept: 'application/json' },
49
+ requestOptions.headers,
50
+ headers
51
+ ),
52
+ }
53
+ );
54
+ if (data !== null) {
55
+ options.body = JSON.stringify(data);
56
+ options.headers['Content-Type'] = 'application/json';
57
+ }
58
+
59
+ this._logger.debug(
60
+ 'making request: %s %s',
61
+ method,
62
+ uri
63
+ );
64
+
65
+ return fetch(uri, options).then((response) => {
66
+ return response.text().then((text) => {
67
+ let body;
68
+ if (text) {
69
+ try {
70
+ body = JSON.parse(text);
71
+ } catch {
72
+ body = text;
73
+ }
74
+ }
75
+
76
+ if (!response.ok) {
77
+ const error = new Error(`${response.status} - ${text}`);
78
+ error.statusCode = response.status;
79
+ error.error = body;
80
+ throw error;
81
+ }
82
+
83
+ this._logger.debug('%s %s response body:\n%s',
84
+ method,
85
+ uri,
86
+ JSON.stringify(body, null, ' ')
87
+ );
88
+ return body;
89
+ });
90
+ });
91
+ }
92
+ }
93
+
94
+ module.exports = VaultApiClient;