ibm-cloud-sdk-core 2.10.4 → 2.11.3
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/CHANGELOG.md +28 -0
- package/auth/authenticators/authenticator.js +1 -15
- package/auth/authenticators/no-auth-authenticator.d.ts +0 -1
- package/auth/authenticators/no-auth-authenticator.js +16 -15
- package/auth/authenticators/token-request-based-authenticator.js +0 -1
- package/auth/token-managers/cp4d-token-manager.js +2 -2
- package/auth/token-managers/iam-token-manager.js +2 -2
- package/auth/token-managers/jwt-token-manager.d.ts +1 -16
- package/auth/token-managers/jwt-token-manager.js +28 -16
- package/auth/token-managers/token-manager.d.ts +1 -1
- package/auth/token-managers/token-manager.js +27 -46
- package/auth/utils/get-authenticator-from-environment.js +1 -1
- package/auth/utils/helpers.js +4 -5
- package/auth/utils/read-credentials-file.browser.js +0 -1
- package/auth/utils/read-credentials-file.js +1 -1
- package/auth/utils/read-external-sources.d.ts +0 -15
- package/auth/utils/read-external-sources.js +10 -16
- package/index.d.ts +1 -1
- package/index.js +2 -2
- package/lib/base-service.js +16 -4
- package/lib/content-type.d.ts +1 -1
- package/lib/content-type.js +4 -1
- package/lib/helper.d.ts +12 -0
- package/lib/helper.js +43 -11
- package/lib/logger.js +1 -1
- package/lib/querystring.js +4 -5
- package/lib/request-wrapper.d.ts +0 -15
- package/lib/request-wrapper.js +53 -38
- package/lib/sdk-test-helpers.browser.js +7 -24
- package/lib/sdk-test-helpers.js +0 -6
- package/lib/stream-to-promise.js +1 -3
- package/package.json +3 -6
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,31 @@
|
|
|
1
|
+
## [2.11.3](https://github.com/IBM/node-sdk-core/compare/v2.11.2...v2.11.3) (2021-06-14)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* **build:** revert inadvertently-committed directories ([#147](https://github.com/IBM/node-sdk-core/issues/147)) ([1fb6295](https://github.com/IBM/node-sdk-core/commit/1fb62955e74e73c805b6221ca85e22517335f273))
|
|
7
|
+
|
|
8
|
+
## [2.11.2](https://github.com/IBM/node-sdk-core/compare/v2.11.1...v2.11.2) (2021-06-10)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Bug Fixes
|
|
12
|
+
|
|
13
|
+
* remove default limit on request body length ([#145](https://github.com/IBM/node-sdk-core/issues/145)) ([abf604a](https://github.com/IBM/node-sdk-core/commit/abf604a10483194d0e46e3a1d2e4c27c20331867))
|
|
14
|
+
|
|
15
|
+
## [2.11.1](https://github.com/IBM/node-sdk-core/compare/v2.11.0...v2.11.1) (2021-06-07)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
### Bug Fixes
|
|
19
|
+
|
|
20
|
+
* change constructServiceURL from method to function ([#141](https://github.com/IBM/node-sdk-core/issues/141)) ([cd2d28c](https://github.com/IBM/node-sdk-core/commit/cd2d28ce12b5189ab9de240d6b6d86aa77d33a73))
|
|
21
|
+
|
|
22
|
+
# [2.11.0](https://github.com/IBM/node-sdk-core/compare/v2.10.4...v2.11.0) (2021-05-27)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
### Features
|
|
26
|
+
|
|
27
|
+
* add `BaseService.constructServiceURL` method ([#138](https://github.com/IBM/node-sdk-core/issues/138)) ([0e73bde](https://github.com/IBM/node-sdk-core/commit/0e73bde12df3cfab0b9243d0064ef40d93c1072a))
|
|
28
|
+
|
|
1
29
|
## [2.10.4](https://github.com/IBM/node-sdk-core/compare/v2.10.3...v2.10.4) (2021-05-10)
|
|
2
30
|
|
|
3
31
|
|
|
@@ -1,19 +1,5 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
* Copyright 2019 IBM Corp. All Rights Reserved.
|
|
4
|
-
*
|
|
5
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
-
* you may not use this file except in compliance with the License.
|
|
7
|
-
* You may obtain a copy of the License at
|
|
8
|
-
*
|
|
9
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
-
*
|
|
11
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
-
* See the License for the specific language governing permissions and
|
|
15
|
-
* limitations under the License.
|
|
16
|
-
*/
|
|
2
|
+
/* eslint-disable @typescript-eslint/no-unused-vars, class-methods-use-this */
|
|
17
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
4
|
/**
|
|
19
5
|
* Base Authenticator class for other Authenticators to extend. Not intended
|
|
@@ -21,6 +21,5 @@ import { AuthenticateOptions } from './authenticator-interface';
|
|
|
21
21
|
* useful during development and testing.
|
|
22
22
|
*/
|
|
23
23
|
export declare class NoAuthAuthenticator extends Authenticator {
|
|
24
|
-
constructor();
|
|
25
24
|
authenticate(requestOptions: AuthenticateOptions): Promise<void>;
|
|
26
25
|
}
|
|
@@ -1,4 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
/* eslint-disable @typescript-eslint/no-unused-vars, class-methods-use-this */
|
|
3
|
+
var __extends = (this && this.__extends) || (function () {
|
|
4
|
+
var extendStatics = function (d, b) {
|
|
5
|
+
extendStatics = Object.setPrototypeOf ||
|
|
6
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
7
|
+
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
|
8
|
+
return extendStatics(d, b);
|
|
9
|
+
};
|
|
10
|
+
return function (d, b) {
|
|
11
|
+
extendStatics(d, b);
|
|
12
|
+
function __() { this.constructor = d; }
|
|
13
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
14
|
+
};
|
|
15
|
+
})();
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
2
17
|
/**
|
|
3
18
|
* Copyright 2019 IBM Corp. All Rights Reserved.
|
|
4
19
|
*
|
|
@@ -14,20 +29,6 @@
|
|
|
14
29
|
* See the License for the specific language governing permissions and
|
|
15
30
|
* limitations under the License.
|
|
16
31
|
*/
|
|
17
|
-
var __extends = (this && this.__extends) || (function () {
|
|
18
|
-
var extendStatics = function (d, b) {
|
|
19
|
-
extendStatics = Object.setPrototypeOf ||
|
|
20
|
-
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
21
|
-
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
|
22
|
-
return extendStatics(d, b);
|
|
23
|
-
};
|
|
24
|
-
return function (d, b) {
|
|
25
|
-
extendStatics(d, b);
|
|
26
|
-
function __() { this.constructor = d; }
|
|
27
|
-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
28
|
-
};
|
|
29
|
-
})();
|
|
30
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
31
32
|
var authenticator_1 = require("./authenticator");
|
|
32
33
|
/**
|
|
33
34
|
* NoAuthAuthenticator is a placeholder authenticator implementation which
|
|
@@ -37,7 +38,7 @@ var authenticator_1 = require("./authenticator");
|
|
|
37
38
|
var NoAuthAuthenticator = /** @class */ (function (_super) {
|
|
38
39
|
__extends(NoAuthAuthenticator, _super);
|
|
39
40
|
function NoAuthAuthenticator() {
|
|
40
|
-
return _super.
|
|
41
|
+
return _super !== null && _super.apply(this, arguments) || this;
|
|
41
42
|
}
|
|
42
43
|
NoAuthAuthenticator.prototype.authenticate = function (requestOptions) {
|
|
43
44
|
// immediately proceed to request. it will probably fail
|
|
@@ -107,7 +107,6 @@ var TokenRequestBasedAuthenticator = /** @class */ (function (_super) {
|
|
|
107
107
|
return this.tokenManager.getToken().then(function (token) {
|
|
108
108
|
var authHeader = { Authorization: "Bearer " + token };
|
|
109
109
|
requestOptions.headers = extend(true, {}, requestOptions.headers, authHeader);
|
|
110
|
-
return;
|
|
111
110
|
});
|
|
112
111
|
};
|
|
113
112
|
return TokenRequestBasedAuthenticator;
|
|
@@ -65,7 +65,7 @@ var Cp4dTokenManager = /** @class */ (function (_super) {
|
|
|
65
65
|
var tokenApiPath = '/v1/authorize';
|
|
66
66
|
// do not append the path if user already has
|
|
67
67
|
if (_this.url && !_this.url.endsWith(tokenApiPath)) {
|
|
68
|
-
_this.url
|
|
68
|
+
_this.url += tokenApiPath;
|
|
69
69
|
}
|
|
70
70
|
_this.username = options.username;
|
|
71
71
|
_this.password = options.password;
|
|
@@ -88,7 +88,7 @@ var Cp4dTokenManager = /** @class */ (function (_super) {
|
|
|
88
88
|
method: 'POST',
|
|
89
89
|
headers: extend(true, {}, this.headers, requiredHeaders),
|
|
90
90
|
rejectUnauthorized: !this.disableSslVerification,
|
|
91
|
-
}
|
|
91
|
+
},
|
|
92
92
|
};
|
|
93
93
|
return this.requestWrapperInstance.sendRequest(parameters);
|
|
94
94
|
};
|
|
@@ -185,10 +185,10 @@ var IamTokenManager = /** @class */ (function (_super) {
|
|
|
185
185
|
form: {
|
|
186
186
|
grant_type: 'urn:ibm:params:oauth:grant-type:apikey',
|
|
187
187
|
apikey: this.apikey,
|
|
188
|
-
response_type: 'cloud_iam'
|
|
188
|
+
response_type: 'cloud_iam',
|
|
189
189
|
},
|
|
190
190
|
rejectUnauthorized: !this.disableSslVerification,
|
|
191
|
-
}
|
|
191
|
+
},
|
|
192
192
|
};
|
|
193
193
|
if (this.scope) {
|
|
194
194
|
parameters.options.form[SCOPE] = this.scope;
|
|
@@ -1,19 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
* Copyright 2019 IBM Corp. All Rights Reserved.
|
|
3
|
-
*
|
|
4
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
-
* you may not use this file except in compliance with the License.
|
|
6
|
-
* You may obtain a copy of the License at
|
|
7
|
-
*
|
|
8
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
-
*
|
|
10
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
-
* See the License for the specific language governing permissions and
|
|
14
|
-
* limitations under the License.
|
|
15
|
-
*/
|
|
16
|
-
import { TokenManager, TokenManagerOptions } from "./token-manager";
|
|
1
|
+
import { TokenManager, TokenManagerOptions } from './token-manager';
|
|
17
2
|
/** Configuration options for JWT token retrieval. */
|
|
18
3
|
export declare type JwtTokenManagerOptions = TokenManagerOptions;
|
|
19
4
|
/**
|
|
@@ -1,4 +1,30 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
/* eslint-disable class-methods-use-this */
|
|
3
|
+
var __extends = (this && this.__extends) || (function () {
|
|
4
|
+
var extendStatics = function (d, b) {
|
|
5
|
+
extendStatics = Object.setPrototypeOf ||
|
|
6
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
7
|
+
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
|
8
|
+
return extendStatics(d, b);
|
|
9
|
+
};
|
|
10
|
+
return function (d, b) {
|
|
11
|
+
extendStatics(d, b);
|
|
12
|
+
function __() { this.constructor = d; }
|
|
13
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
14
|
+
};
|
|
15
|
+
})();
|
|
16
|
+
var __assign = (this && this.__assign) || function () {
|
|
17
|
+
__assign = Object.assign || function(t) {
|
|
18
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
19
|
+
s = arguments[i];
|
|
20
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
21
|
+
t[p] = s[p];
|
|
22
|
+
}
|
|
23
|
+
return t;
|
|
24
|
+
};
|
|
25
|
+
return __assign.apply(this, arguments);
|
|
26
|
+
};
|
|
27
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
2
28
|
/**
|
|
3
29
|
* Copyright 2019 IBM Corp. All Rights Reserved.
|
|
4
30
|
*
|
|
@@ -14,20 +40,6 @@
|
|
|
14
40
|
* See the License for the specific language governing permissions and
|
|
15
41
|
* limitations under the License.
|
|
16
42
|
*/
|
|
17
|
-
var __extends = (this && this.__extends) || (function () {
|
|
18
|
-
var extendStatics = function (d, b) {
|
|
19
|
-
extendStatics = Object.setPrototypeOf ||
|
|
20
|
-
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
21
|
-
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
|
22
|
-
return extendStatics(d, b);
|
|
23
|
-
};
|
|
24
|
-
return function (d, b) {
|
|
25
|
-
extendStatics(d, b);
|
|
26
|
-
function __() { this.constructor = d; }
|
|
27
|
-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
28
|
-
};
|
|
29
|
-
})();
|
|
30
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
31
43
|
var jwt = require("jsonwebtoken");
|
|
32
44
|
var logger_1 = require("../../lib/logger");
|
|
33
45
|
var token_manager_1 = require("./token-manager");
|
|
@@ -103,9 +115,9 @@ var JwtTokenManager = /** @class */ (function (_super) {
|
|
|
103
115
|
var fractionOfTtl = 0.8;
|
|
104
116
|
var timeToLive = exp - iat;
|
|
105
117
|
this.expireTime = exp;
|
|
106
|
-
this.refreshTime = exp -
|
|
118
|
+
this.refreshTime = exp - timeToLive * (1.0 - fractionOfTtl);
|
|
107
119
|
}
|
|
108
|
-
this.tokenInfo =
|
|
120
|
+
this.tokenInfo = __assign({}, responseBody);
|
|
109
121
|
};
|
|
110
122
|
return JwtTokenManager;
|
|
111
123
|
}(token_manager_1.TokenManager));
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
1
2
|
/**
|
|
2
3
|
* Copyright 2020 IBM Corp. All Rights Reserved.
|
|
3
4
|
*
|
|
@@ -13,7 +14,6 @@
|
|
|
13
14
|
* See the License for the specific language governing permissions and
|
|
14
15
|
* limitations under the License.
|
|
15
16
|
*/
|
|
16
|
-
/// <reference types="node" />
|
|
17
17
|
import { OutgoingHttpHeaders } from 'http';
|
|
18
18
|
import { RequestWrapper } from '../../lib/request-wrapper';
|
|
19
19
|
/** Configuration options for token retrieval. */
|
|
@@ -1,19 +1,5 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
* Copyright 2020 IBM Corp. All Rights Reserved.
|
|
4
|
-
*
|
|
5
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
-
* you may not use this file except in compliance with the License.
|
|
7
|
-
* You may obtain a copy of the License at
|
|
8
|
-
*
|
|
9
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
-
*
|
|
11
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
-
* See the License for the specific language governing permissions and
|
|
15
|
-
* limitations under the License.
|
|
16
|
-
*/
|
|
2
|
+
/* eslint-disable @typescript-eslint/no-unused-vars, class-methods-use-this */
|
|
17
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
4
|
var helper_1 = require("../../lib/helper");
|
|
19
5
|
var logger_1 = require("../../lib/logger");
|
|
@@ -61,20 +47,16 @@ var TokenManager = /** @class */ (function () {
|
|
|
61
47
|
var _this = this;
|
|
62
48
|
if (!this.accessToken || this.isTokenExpired()) {
|
|
63
49
|
// 1. request a new token
|
|
64
|
-
return this.pacedRequestToken().then(function () {
|
|
65
|
-
return _this.accessToken;
|
|
66
|
-
});
|
|
50
|
+
return this.pacedRequestToken().then(function () { return _this.accessToken; });
|
|
67
51
|
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
// 2. use valid, managed token
|
|
76
|
-
return Promise.resolve(this.accessToken);
|
|
52
|
+
// If refresh needed, kick one off
|
|
53
|
+
if (this.tokenNeedsRefresh()) {
|
|
54
|
+
this.requestToken().then(function (tokenResponse) {
|
|
55
|
+
_this.saveTokenInfo(tokenResponse);
|
|
56
|
+
});
|
|
77
57
|
}
|
|
58
|
+
// 2. use valid, managed token
|
|
59
|
+
return Promise.resolve(this.accessToken);
|
|
78
60
|
};
|
|
79
61
|
/**
|
|
80
62
|
* Setter for the disableSslVerification property.
|
|
@@ -115,30 +97,30 @@ var TokenManager = /** @class */ (function () {
|
|
|
115
97
|
TokenManager.prototype.pacedRequestToken = function () {
|
|
116
98
|
var _this = this;
|
|
117
99
|
var currentTime = utils_1.getCurrentTime();
|
|
118
|
-
if (this.requestTime >
|
|
100
|
+
if (this.requestTime > currentTime - 60) {
|
|
119
101
|
// token request is active -- queue the promise for this request
|
|
120
102
|
return new Promise(function (resolve, reject) {
|
|
121
103
|
_this.pendingRequests.push({ resolve: resolve, reject: reject });
|
|
122
104
|
});
|
|
123
105
|
}
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
});
|
|
132
|
-
_this.pendingRequests = [];
|
|
133
|
-
_this.requestTime = 0;
|
|
134
|
-
}).catch(function (err) {
|
|
135
|
-
_this.pendingRequests.forEach(function (_a) {
|
|
136
|
-
var reject = _a.reject;
|
|
137
|
-
reject(err);
|
|
138
|
-
});
|
|
139
|
-
throw (err);
|
|
106
|
+
this.requestTime = currentTime;
|
|
107
|
+
return this.requestToken()
|
|
108
|
+
.then(function (tokenResponse) {
|
|
109
|
+
_this.saveTokenInfo(tokenResponse);
|
|
110
|
+
_this.pendingRequests.forEach(function (_a) {
|
|
111
|
+
var resolve = _a.resolve;
|
|
112
|
+
resolve();
|
|
140
113
|
});
|
|
141
|
-
|
|
114
|
+
_this.pendingRequests = [];
|
|
115
|
+
_this.requestTime = 0;
|
|
116
|
+
})
|
|
117
|
+
.catch(function (err) {
|
|
118
|
+
_this.pendingRequests.forEach(function (_a) {
|
|
119
|
+
var reject = _a.reject;
|
|
120
|
+
reject(err);
|
|
121
|
+
});
|
|
122
|
+
throw err;
|
|
123
|
+
});
|
|
142
124
|
};
|
|
143
125
|
/**
|
|
144
126
|
* Request a token using an API endpoint.
|
|
@@ -163,7 +145,6 @@ var TokenManager = /** @class */ (function () {
|
|
|
163
145
|
*/
|
|
164
146
|
TokenManager.prototype.saveTokenInfo = function (tokenResponse) {
|
|
165
147
|
var errMsg = '`saveTokenInfo` MUST be overridden by a subclass of TokenManager.';
|
|
166
|
-
var err = new Error(errMsg);
|
|
167
148
|
logger_1.default.error(errMsg);
|
|
168
149
|
};
|
|
169
150
|
/**
|
|
@@ -74,7 +74,7 @@ function getAuthenticatorFromEnvironment(serviceName) {
|
|
|
74
74
|
authenticator = new authenticators_1.IamAuthenticator(credentials);
|
|
75
75
|
break;
|
|
76
76
|
default:
|
|
77
|
-
throw new Error(
|
|
77
|
+
throw new Error("Invalid value for AUTH_TYPE: " + authType);
|
|
78
78
|
}
|
|
79
79
|
return authenticator;
|
|
80
80
|
}
|
package/auth/utils/helpers.js
CHANGED
|
@@ -31,7 +31,7 @@ exports.computeBasicAuthHeader = computeBasicAuthHeader;
|
|
|
31
31
|
// returns true if the string has a curly bracket or quote as the first or last character
|
|
32
32
|
// these are common user-issues that we should handle before they get a network error
|
|
33
33
|
function badCharAtAnEnd(value) {
|
|
34
|
-
return value.startsWith('{') || value.startsWith('"') || value.endsWith('}') || value.endsWith('"');
|
|
34
|
+
return (value.startsWith('{') || value.startsWith('"') || value.endsWith('}') || value.endsWith('"'));
|
|
35
35
|
}
|
|
36
36
|
/**
|
|
37
37
|
* Checks credentials for common user mistakes of copying {, }, or " characters from the documentation
|
|
@@ -48,12 +48,11 @@ function checkCredentials(obj, credsToCheck) {
|
|
|
48
48
|
}
|
|
49
49
|
});
|
|
50
50
|
if (errorMessage.length) {
|
|
51
|
-
errorMessage +=
|
|
51
|
+
errorMessage +=
|
|
52
|
+
'Revise these credentials - they should not start or end with curly brackets or quotes.';
|
|
52
53
|
return new Error(errorMessage);
|
|
53
54
|
}
|
|
54
|
-
|
|
55
|
-
return null;
|
|
56
|
-
}
|
|
55
|
+
return null;
|
|
57
56
|
}
|
|
58
57
|
exports.checkCredentials = checkCredentials;
|
|
59
58
|
/**
|
|
@@ -18,7 +18,7 @@ function readCredentialsFile() {
|
|
|
18
18
|
// it should be the path to the file
|
|
19
19
|
// then look at the current working directory
|
|
20
20
|
// then at the os-dependent home directory
|
|
21
|
-
var givenFilepath = process.env
|
|
21
|
+
var givenFilepath = process.env.IBM_CREDENTIALS_FILE || '';
|
|
22
22
|
var workingDir = constructFilepath(process.cwd());
|
|
23
23
|
var homeDir = constructFilepath(os.homedir());
|
|
24
24
|
var filepathToUse;
|
|
@@ -1,18 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright 2019 IBM Corp. All Rights Reserved.
|
|
3
|
-
*
|
|
4
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
-
* you may not use this file except in compliance with the License.
|
|
6
|
-
* You may obtain a copy of the License at
|
|
7
|
-
*
|
|
8
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
-
*
|
|
10
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
-
* See the License for the specific language governing permissions and
|
|
14
|
-
* limitations under the License.
|
|
15
|
-
*/
|
|
16
1
|
/**
|
|
17
2
|
* Read properties stored in external sources like Environment Variables,
|
|
18
3
|
* the credentials file, VCAP services, etc. and return them as an
|
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
/* eslint-disable no-restricted-syntax */
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
2
4
|
/**
|
|
3
5
|
* Copyright 2019 IBM Corp. All Rights Reserved.
|
|
4
6
|
*
|
|
@@ -14,7 +16,6 @@
|
|
|
14
16
|
* See the License for the specific language governing permissions and
|
|
15
17
|
* limitations under the License.
|
|
16
18
|
*/
|
|
17
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
19
|
var camelcase = require("camelcase");
|
|
19
20
|
var isEmpty = require("lodash.isempty");
|
|
20
21
|
var logger_1 = require("../../lib/logger");
|
|
@@ -64,7 +65,7 @@ function getProperties(serviceName) {
|
|
|
64
65
|
*/
|
|
65
66
|
function filterPropertiesByServiceName(envObj, serviceName) {
|
|
66
67
|
var credentials = {};
|
|
67
|
-
var name = serviceName.toUpperCase().replace(/-/g, '_') +
|
|
68
|
+
var name = serviceName.toUpperCase().replace(/-/g, '_') + "_"; // append the underscore that must follow the service name
|
|
68
69
|
// filter out properties that don't begin with the service name
|
|
69
70
|
Object.keys(envObj).forEach(function (key) {
|
|
70
71
|
if (key.startsWith(name)) {
|
|
@@ -101,14 +102,12 @@ function getVCAPCredentialsForService(name) {
|
|
|
101
102
|
var serviceName = _a[_i];
|
|
102
103
|
for (var _b = 0, _c = services[serviceName]; _b < _c.length; _b++) {
|
|
103
104
|
var instance = _c[_b];
|
|
104
|
-
if (instance
|
|
105
|
-
if (
|
|
105
|
+
if (instance.name === name) {
|
|
106
|
+
if (Object.prototype.hasOwnProperty.call(instance, 'credentials')) {
|
|
106
107
|
return instance.credentials;
|
|
107
108
|
}
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
return {};
|
|
111
|
-
}
|
|
109
|
+
logger_1.default.debug('no data read from VCAP_SERVICES');
|
|
110
|
+
return {};
|
|
112
111
|
}
|
|
113
112
|
}
|
|
114
113
|
}
|
|
@@ -116,19 +115,14 @@ function getVCAPCredentialsForService(name) {
|
|
|
116
115
|
var serviceName = _e[_d];
|
|
117
116
|
if (serviceName === name) {
|
|
118
117
|
if (services[serviceName].length > 0) {
|
|
119
|
-
if (services[serviceName][0]
|
|
118
|
+
if (Object.prototype.hasOwnProperty.call(services[serviceName][0], 'credentials')) {
|
|
120
119
|
return services[serviceName][0].credentials;
|
|
121
120
|
}
|
|
122
|
-
else {
|
|
123
|
-
logger_1.default.debug('no data read from VCAP_SERVICES');
|
|
124
|
-
return {};
|
|
125
|
-
}
|
|
126
|
-
return services[serviceName][0].credentials || {};
|
|
127
|
-
}
|
|
128
|
-
else {
|
|
129
121
|
logger_1.default.debug('no data read from VCAP_SERVICES');
|
|
130
122
|
return {};
|
|
131
123
|
}
|
|
124
|
+
logger_1.default.debug('no data read from VCAP_SERVICES');
|
|
125
|
+
return {};
|
|
132
126
|
}
|
|
133
127
|
}
|
|
134
128
|
}
|
package/index.d.ts
CHANGED
|
@@ -16,11 +16,11 @@
|
|
|
16
16
|
/**
|
|
17
17
|
* @module ibm-cloud-sdk-core
|
|
18
18
|
*/
|
|
19
|
+
import * as unitTestUtils from './lib/sdk-test-helpers';
|
|
19
20
|
export { BaseService, UserOptions } from './lib/base-service';
|
|
20
21
|
export * from './auth';
|
|
21
22
|
export * from './lib/helper';
|
|
22
23
|
export { default as qs } from './lib/querystring';
|
|
23
24
|
export { default as contentType } from './lib/content-type';
|
|
24
25
|
export * from './lib/stream-to-promise';
|
|
25
|
-
import * as unitTestUtils from './lib/sdk-test-helpers';
|
|
26
26
|
export { unitTestUtils };
|
package/index.js
CHANGED
|
@@ -21,6 +21,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
21
21
|
/**
|
|
22
22
|
* @module ibm-cloud-sdk-core
|
|
23
23
|
*/
|
|
24
|
+
var unitTestUtils = require("./lib/sdk-test-helpers");
|
|
25
|
+
exports.unitTestUtils = unitTestUtils;
|
|
24
26
|
var base_service_1 = require("./lib/base-service");
|
|
25
27
|
exports.BaseService = base_service_1.BaseService;
|
|
26
28
|
__export(require("./auth"));
|
|
@@ -30,5 +32,3 @@ exports.qs = querystring_1.default;
|
|
|
30
32
|
var content_type_1 = require("./lib/content-type");
|
|
31
33
|
exports.contentType = content_type_1.default;
|
|
32
34
|
__export(require("./lib/stream-to-promise"));
|
|
33
|
-
var unitTestUtils = require("./lib/sdk-test-helpers");
|
|
34
|
-
exports.unitTestUtils = unitTestUtils;
|
package/lib/base-service.js
CHANGED
|
@@ -14,6 +14,17 @@
|
|
|
14
14
|
* See the License for the specific language governing permissions and
|
|
15
15
|
* limitations under the License.
|
|
16
16
|
*/
|
|
17
|
+
var __assign = (this && this.__assign) || function () {
|
|
18
|
+
__assign = Object.assign || function(t) {
|
|
19
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
20
|
+
s = arguments[i];
|
|
21
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
22
|
+
t[p] = s[p];
|
|
23
|
+
}
|
|
24
|
+
return t;
|
|
25
|
+
};
|
|
26
|
+
return __assign.apply(this, arguments);
|
|
27
|
+
};
|
|
17
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
29
|
var auth_1 = require("../auth");
|
|
19
30
|
var helper_1 = require("./helper");
|
|
@@ -45,14 +56,14 @@ var BaseService = /** @class */ (function () {
|
|
|
45
56
|
logger_1.default.error("Error creating an instance of BaseService: " + err);
|
|
46
57
|
throw new Error(err);
|
|
47
58
|
}
|
|
48
|
-
var
|
|
49
|
-
var options =
|
|
59
|
+
var baseServiceOptions = {};
|
|
60
|
+
var options = __assign({}, userOptions);
|
|
50
61
|
// for compatibility
|
|
51
62
|
if (options.url && !options.serviceUrl) {
|
|
52
63
|
options.serviceUrl = options.url;
|
|
53
64
|
}
|
|
54
65
|
if (options.serviceUrl) {
|
|
55
|
-
|
|
66
|
+
baseServiceOptions.serviceUrl = helper_1.stripTrailingSlash(options.serviceUrl);
|
|
56
67
|
}
|
|
57
68
|
// check serviceUrl for common user errors
|
|
58
69
|
var credentialProblems = auth_1.checkCredentials(options, ['serviceUrl']);
|
|
@@ -66,7 +77,7 @@ var BaseService = /** @class */ (function () {
|
|
|
66
77
|
options.disableSslVerification = false;
|
|
67
78
|
}
|
|
68
79
|
var serviceClass = this.constructor;
|
|
69
|
-
this.baseOptions =
|
|
80
|
+
this.baseOptions = __assign(__assign({ qs: {}, serviceUrl: serviceClass.DEFAULT_SERVICE_URL }, options), baseServiceOptions);
|
|
70
81
|
this.requestWrapperInstance = new request_wrapper_1.RequestWrapper(this.baseOptions);
|
|
71
82
|
// enforce that an authenticator is set
|
|
72
83
|
if (!options.authenticator) {
|
|
@@ -151,6 +162,7 @@ var BaseService = /** @class */ (function () {
|
|
|
151
162
|
return _this.requestWrapperInstance.sendRequest(parameters);
|
|
152
163
|
});
|
|
153
164
|
};
|
|
165
|
+
// eslint-disable-next-line class-methods-use-this
|
|
154
166
|
BaseService.prototype.readOptionsFromExternalConfig = function (serviceName) {
|
|
155
167
|
var results = {};
|
|
156
168
|
var properties = auth_1.readExternalSources(serviceName);
|
package/lib/content-type.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
-
import { FileObject } from
|
|
2
|
+
import { FileObject } from "./helper";
|
|
3
3
|
declare const _default: {
|
|
4
4
|
fromFilename: (file: String | File | Buffer | NodeJS.ReadableStream | FileObject) => string;
|
|
5
5
|
fromHeader: (buffer: Buffer) => string;
|
package/lib/content-type.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
/* eslint-disable prettier/prettier */
|
|
2
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
4
|
var path_1 = require("path");
|
|
4
5
|
// This module attempts to identify common content-types based on the filename or header
|
|
@@ -46,7 +47,9 @@ var fromHeader = function (buffer) {
|
|
|
46
47
|
* @return {String|undefined}
|
|
47
48
|
*/
|
|
48
49
|
var fromFilename = function (file) {
|
|
49
|
-
var ext = path_1.extname(
|
|
50
|
+
var ext = path_1.extname(
|
|
51
|
+
// eslint-disable-next-line @typescript-eslint/dot-notation
|
|
52
|
+
(typeof file === 'string' && file) || file['name'] || '');
|
|
50
53
|
return filenameContentTypes[ext];
|
|
51
54
|
};
|
|
52
55
|
exports.default = {
|
package/lib/helper.d.ts
CHANGED
|
@@ -96,3 +96,15 @@ export declare function buildRequestFileObject(fileParam: FileWithMetadata): Fil
|
|
|
96
96
|
* @returns {Object}
|
|
97
97
|
*/
|
|
98
98
|
export declare function toLowerKeys(obj: Object): Object;
|
|
99
|
+
/**
|
|
100
|
+
* Constructs a service URL by formatting a parameterized URL.
|
|
101
|
+
*
|
|
102
|
+
* @param {string} parameterizedUrl URL that contains variable placeholders, e.g. '{scheme}://ibm.com'.
|
|
103
|
+
* @param {Map<string, string>} defaultUrlVariables Map from variable names to default values.
|
|
104
|
+
* Each variable in the parameterized URL must have a default value specified in this map.
|
|
105
|
+
* @param {Map<string, string>} providedUrlVariables Map from variable names to desired values.
|
|
106
|
+
* If a variable is not provided in this map,
|
|
107
|
+
* the default variable value will be used instead.
|
|
108
|
+
* @returns {string} The formatted URL with all variable placeholders replaced by values.
|
|
109
|
+
*/
|
|
110
|
+
export declare function constructServiceUrl(parameterizedUrl: string, defaultUrlVariables: Map<string, string>, providedUrlVariables: Map<string, string> | null): string;
|
package/lib/helper.js
CHANGED
|
@@ -113,7 +113,7 @@ function getMissingParams(params, requires) {
|
|
|
113
113
|
});
|
|
114
114
|
}
|
|
115
115
|
return missing.length > 0
|
|
116
|
-
? new Error(
|
|
116
|
+
? new Error("Missing required parameters: " + missing.join(', '))
|
|
117
117
|
: null;
|
|
118
118
|
}
|
|
119
119
|
exports.getMissingParams = getMissingParams;
|
|
@@ -149,12 +149,9 @@ function getFormat(params, formats) {
|
|
|
149
149
|
logger_1.default.debug("No formats to parse in getFormat. Returning null");
|
|
150
150
|
return null;
|
|
151
151
|
}
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
return item;
|
|
156
|
-
}
|
|
157
|
-
}
|
|
152
|
+
var validFormats = formats.filter(function (item) { return item in params; });
|
|
153
|
+
if (validFormats.length)
|
|
154
|
+
return validFormats[0];
|
|
158
155
|
logger_1.default.debug("No formats to parse in getFormat. Returning null");
|
|
159
156
|
return null;
|
|
160
157
|
}
|
|
@@ -185,7 +182,7 @@ function buildRequestFileObject(fileParam) {
|
|
|
185
182
|
options: {
|
|
186
183
|
filename: fileParam.filename,
|
|
187
184
|
contentType: fileParam.contentType,
|
|
188
|
-
}
|
|
185
|
+
},
|
|
189
186
|
};
|
|
190
187
|
}
|
|
191
188
|
// Also for backward compatibility, we allow data to be a string
|
|
@@ -193,6 +190,7 @@ function buildRequestFileObject(fileParam) {
|
|
|
193
190
|
fileObj.value = Buffer.from(fileObj.value);
|
|
194
191
|
}
|
|
195
192
|
// build filename
|
|
193
|
+
// eslint-disable-next-line prefer-destructuring
|
|
196
194
|
var filename = fileObj.options.filename;
|
|
197
195
|
if (!filename && isFileStream(fileObj.value)) {
|
|
198
196
|
// if readable stream with path property
|
|
@@ -214,15 +212,49 @@ exports.buildRequestFileObject = buildRequestFileObject;
|
|
|
214
212
|
* @returns {Object}
|
|
215
213
|
*/
|
|
216
214
|
function toLowerKeys(obj) {
|
|
217
|
-
var
|
|
215
|
+
var lowerCaseObj = {};
|
|
218
216
|
if (obj) {
|
|
219
|
-
|
|
217
|
+
lowerCaseObj = Object.assign.apply(Object, __spreadArrays([{}], Object.keys(obj).map(function (key) {
|
|
220
218
|
var _a;
|
|
221
219
|
return (_a = {},
|
|
222
220
|
_a[key.toLowerCase()] = obj[key],
|
|
223
221
|
_a);
|
|
224
222
|
})));
|
|
225
223
|
}
|
|
226
|
-
return
|
|
224
|
+
return lowerCaseObj;
|
|
227
225
|
}
|
|
228
226
|
exports.toLowerKeys = toLowerKeys;
|
|
227
|
+
/**
|
|
228
|
+
* Constructs a service URL by formatting a parameterized URL.
|
|
229
|
+
*
|
|
230
|
+
* @param {string} parameterizedUrl URL that contains variable placeholders, e.g. '{scheme}://ibm.com'.
|
|
231
|
+
* @param {Map<string, string>} defaultUrlVariables Map from variable names to default values.
|
|
232
|
+
* Each variable in the parameterized URL must have a default value specified in this map.
|
|
233
|
+
* @param {Map<string, string>} providedUrlVariables Map from variable names to desired values.
|
|
234
|
+
* If a variable is not provided in this map,
|
|
235
|
+
* the default variable value will be used instead.
|
|
236
|
+
* @returns {string} The formatted URL with all variable placeholders replaced by values.
|
|
237
|
+
*/
|
|
238
|
+
function constructServiceUrl(parameterizedUrl, defaultUrlVariables, providedUrlVariables) {
|
|
239
|
+
// If null was passed, we set the variables to an empty map.
|
|
240
|
+
// This results in all default variable values being used.
|
|
241
|
+
if (providedUrlVariables === null) {
|
|
242
|
+
providedUrlVariables = new Map();
|
|
243
|
+
}
|
|
244
|
+
// Verify the provided variable names.
|
|
245
|
+
providedUrlVariables.forEach(function (_, name) {
|
|
246
|
+
if (!defaultUrlVariables.has(name)) {
|
|
247
|
+
throw new Error("'" + name + "' is an invalid variable name.\n Valid variable names: [" + Array.from(defaultUrlVariables.keys()).sort() + "].");
|
|
248
|
+
}
|
|
249
|
+
});
|
|
250
|
+
// Format the URL with provided or default variable values.
|
|
251
|
+
var formattedUrl = parameterizedUrl;
|
|
252
|
+
defaultUrlVariables.forEach(function (defaultValue, name) {
|
|
253
|
+
// Use the default variable value if none was provided.
|
|
254
|
+
var providedValue = providedUrlVariables.get(name);
|
|
255
|
+
var formatValue = providedValue !== undefined ? providedValue : defaultValue;
|
|
256
|
+
formattedUrl = formattedUrl.replace("{" + name + "}", formatValue);
|
|
257
|
+
});
|
|
258
|
+
return formattedUrl;
|
|
259
|
+
}
|
|
260
|
+
exports.constructServiceUrl = constructServiceUrl;
|
package/lib/logger.js
CHANGED
package/lib/querystring.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
/* eslint-disable prettier/prettier, arrow-body-style */
|
|
2
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
4
|
/**
|
|
4
5
|
* Stringify query params, Watson-style
|
|
@@ -13,11 +14,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
13
14
|
var stringify = function (queryParams) {
|
|
14
15
|
return Object.keys(queryParams)
|
|
15
16
|
.map(function (key) {
|
|
16
|
-
return (key +
|
|
17
|
-
|
|
18
|
-
(key
|
|
19
|
-
? queryParams[key]
|
|
20
|
-
: encodeURIComponent(queryParams[key]))); // the server chokes if the token is correctly url-encoded
|
|
17
|
+
return (key + "=" + (key === 'watson-token'
|
|
18
|
+
? queryParams[key]
|
|
19
|
+
: encodeURIComponent(queryParams[key]))); // the server chokes if the token is correctly url-encoded
|
|
21
20
|
})
|
|
22
21
|
.join('&');
|
|
23
22
|
};
|
package/lib/request-wrapper.d.ts
CHANGED
|
@@ -1,18 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright 2014 IBM Corp. All Rights Reserved.
|
|
3
|
-
*
|
|
4
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
-
* you may not use this file except in compliance with the License.
|
|
6
|
-
* You may obtain a copy of the License at
|
|
7
|
-
*
|
|
8
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
-
*
|
|
10
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
-
* See the License for the specific language governing permissions and
|
|
14
|
-
* limitations under the License.
|
|
15
|
-
*/
|
|
16
1
|
export declare class RequestWrapper {
|
|
17
2
|
private axiosInstance;
|
|
18
3
|
private compressRequestData;
|
package/lib/request-wrapper.js
CHANGED
|
@@ -1,19 +1,16 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
* See the License for the specific language governing permissions and
|
|
15
|
-
* limitations under the License.
|
|
16
|
-
*/
|
|
2
|
+
/* eslint-disable class-methods-use-this */
|
|
3
|
+
var __assign = (this && this.__assign) || function () {
|
|
4
|
+
__assign = Object.assign || function(t) {
|
|
5
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
6
|
+
s = arguments[i];
|
|
7
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
8
|
+
t[p] = s[p];
|
|
9
|
+
}
|
|
10
|
+
return t;
|
|
11
|
+
};
|
|
12
|
+
return __assign.apply(this, arguments);
|
|
13
|
+
};
|
|
17
14
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
18
15
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
19
16
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -51,6 +48,21 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
51
48
|
}
|
|
52
49
|
};
|
|
53
50
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
51
|
+
/**
|
|
52
|
+
* Copyright 2014 IBM Corp. All Rights Reserved.
|
|
53
|
+
*
|
|
54
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
55
|
+
* you may not use this file except in compliance with the License.
|
|
56
|
+
* You may obtain a copy of the License at
|
|
57
|
+
*
|
|
58
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
59
|
+
*
|
|
60
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
61
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
62
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
63
|
+
* See the License for the specific language governing permissions and
|
|
64
|
+
* limitations under the License.
|
|
65
|
+
*/
|
|
54
66
|
var axios_1 = require("axios");
|
|
55
67
|
var axios_cookiejar_support_1 = require("axios-cookiejar-support");
|
|
56
68
|
var extend = require("extend");
|
|
@@ -62,8 +74,6 @@ var zlib = require("zlib");
|
|
|
62
74
|
var helper_1 = require("./helper");
|
|
63
75
|
var logger_1 = require("./logger");
|
|
64
76
|
var stream_to_promise_1 = require("./stream-to-promise");
|
|
65
|
-
var isBrowser = typeof window === 'object';
|
|
66
|
-
var globalTransactionId = 'x-global-transaction-id';
|
|
67
77
|
var RequestWrapper = /** @class */ (function () {
|
|
68
78
|
function RequestWrapper(axiosOptions) {
|
|
69
79
|
axiosOptions = axiosOptions || {};
|
|
@@ -74,17 +84,18 @@ var RequestWrapper = /** @class */ (function () {
|
|
|
74
84
|
// defaults here
|
|
75
85
|
var axiosConfig = {
|
|
76
86
|
maxContentLength: Infinity,
|
|
87
|
+
maxBodyLength: Infinity,
|
|
77
88
|
headers: {
|
|
78
89
|
post: {
|
|
79
|
-
'Content-Type': 'application/json'
|
|
90
|
+
'Content-Type': 'application/json',
|
|
80
91
|
},
|
|
81
92
|
put: {
|
|
82
|
-
'Content-Type': 'application/json'
|
|
93
|
+
'Content-Type': 'application/json',
|
|
83
94
|
},
|
|
84
95
|
patch: {
|
|
85
|
-
'Content-Type': 'application/json'
|
|
96
|
+
'Content-Type': 'application/json',
|
|
86
97
|
},
|
|
87
|
-
}
|
|
98
|
+
},
|
|
88
99
|
};
|
|
89
100
|
// merge axios config into default
|
|
90
101
|
extend(true, axiosConfig, axiosOptions);
|
|
@@ -180,9 +191,12 @@ var RequestWrapper = /** @class */ (function () {
|
|
|
180
191
|
Object.keys(formData).forEach(function (key) {
|
|
181
192
|
var values = Array.isArray(formData[key]) ? formData[key] : [formData[key]];
|
|
182
193
|
// Skip keys with undefined/null values or empty object value
|
|
183
|
-
values
|
|
194
|
+
values
|
|
195
|
+
.filter(function (v) { return v != null && !helper_1.isEmptyObject(v); })
|
|
196
|
+
.forEach(function (value) {
|
|
184
197
|
// Special case of empty file object
|
|
185
|
-
if (
|
|
198
|
+
if (Object.prototype.hasOwnProperty.call(value, 'contentType') &&
|
|
199
|
+
!Object.prototype.hasOwnProperty.call(value, 'data')) {
|
|
186
200
|
return;
|
|
187
201
|
}
|
|
188
202
|
if (helper_1.isFileWithMetadata(value)) {
|
|
@@ -201,10 +215,14 @@ var RequestWrapper = /** @class */ (function () {
|
|
|
201
215
|
// Path params
|
|
202
216
|
url = parsePath(url, path);
|
|
203
217
|
// Headers
|
|
204
|
-
options.headers =
|
|
218
|
+
options.headers = __assign({}, options.headers);
|
|
205
219
|
// Convert array-valued query params to strings
|
|
206
220
|
if (qs && Object.keys(qs).length > 0) {
|
|
207
|
-
Object.keys(qs).forEach(function (key) {
|
|
221
|
+
Object.keys(qs).forEach(function (key) {
|
|
222
|
+
if (Array.isArray(qs[key])) {
|
|
223
|
+
qs[key] = qs[key].join(',');
|
|
224
|
+
}
|
|
225
|
+
});
|
|
208
226
|
}
|
|
209
227
|
// Add service default endpoint if options.url start with /
|
|
210
228
|
if (url && url.charAt(0) === '/') {
|
|
@@ -236,16 +254,13 @@ var RequestWrapper = /** @class */ (function () {
|
|
|
236
254
|
params: qs,
|
|
237
255
|
data: data,
|
|
238
256
|
responseType: options.responseType || 'json',
|
|
239
|
-
paramsSerializer: function (params) {
|
|
240
|
-
return querystring.stringify(params);
|
|
241
|
-
},
|
|
257
|
+
paramsSerializer: function (params) { return querystring.stringify(params); },
|
|
242
258
|
};
|
|
243
259
|
return [2 /*return*/, this.axiosInstance(requestParams).then(function (res) {
|
|
244
260
|
// sometimes error responses will still trigger the `then` block - escape that behavior here
|
|
245
261
|
if (!res) {
|
|
246
|
-
return;
|
|
262
|
+
return undefined;
|
|
247
263
|
}
|
|
248
|
-
;
|
|
249
264
|
// these objects contain circular json structures and are not always relevant to the user
|
|
250
265
|
// if the user wants them, they can be accessed through the debug properties
|
|
251
266
|
delete res.config;
|
|
@@ -316,8 +331,9 @@ var RequestWrapper = /** @class */ (function () {
|
|
|
316
331
|
error.body = 'Response not received - no connection was made to the service.';
|
|
317
332
|
// when a request to a private cloud instance has an ssl problem, it never connects and follows this branch of the error handling
|
|
318
333
|
if (isSelfSignedCertificateError(axiosError)) {
|
|
319
|
-
error.message =
|
|
320
|
-
"
|
|
334
|
+
error.message =
|
|
335
|
+
"The connection failed because the SSL certificate is not valid. " +
|
|
336
|
+
"To use a self-signed certificate, set the `disableSslVerification` parameter in the constructor options.";
|
|
321
337
|
}
|
|
322
338
|
}
|
|
323
339
|
else {
|
|
@@ -332,8 +348,7 @@ var RequestWrapper = /** @class */ (function () {
|
|
|
332
348
|
return __generator(this, function (_c) {
|
|
333
349
|
switch (_c.label) {
|
|
334
350
|
case 0:
|
|
335
|
-
contentSetToGzip = headers['Content-Encoding'] &&
|
|
336
|
-
headers['Content-Encoding'].toString().includes('gzip');
|
|
351
|
+
contentSetToGzip = headers['Content-Encoding'] && headers['Content-Encoding'].toString().includes('gzip');
|
|
337
352
|
if (!data || contentSetToGzip) {
|
|
338
353
|
return [2 /*return*/, data];
|
|
339
354
|
}
|
|
@@ -407,9 +422,7 @@ function isAuthenticationError(error) {
|
|
|
407
422
|
var code = error.status || null;
|
|
408
423
|
var body = error.data || {};
|
|
409
424
|
// handle specific error from iam service, should be relevant across platforms
|
|
410
|
-
var isIamServiceError = body.context &&
|
|
411
|
-
body.context.url &&
|
|
412
|
-
body.context.url.indexOf('iam') > -1;
|
|
425
|
+
var isIamServiceError = body.context && body.context.url && body.context.url.indexOf('iam') > -1;
|
|
413
426
|
if (code === 401 || code === 403 || isIamServiceError) {
|
|
414
427
|
isAuthErr = true;
|
|
415
428
|
}
|
|
@@ -452,7 +465,9 @@ function hasStringProperty(obj, property) {
|
|
|
452
465
|
*/
|
|
453
466
|
function parseServiceErrorMessage(response) {
|
|
454
467
|
var message;
|
|
455
|
-
if (Array.isArray(response.errors) &&
|
|
468
|
+
if (Array.isArray(response.errors) &&
|
|
469
|
+
response.errors.length > 0 &&
|
|
470
|
+
hasStringProperty(response.errors[0], 'message')) {
|
|
456
471
|
message = response.errors[0].message;
|
|
457
472
|
}
|
|
458
473
|
else if (hasStringProperty(response, 'error')) {
|
|
@@ -1,33 +1,16 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
2
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
4
|
// Dummy for browser
|
|
4
|
-
function checkUrlAndMethod(options, url, method) {
|
|
5
|
-
return;
|
|
6
|
-
}
|
|
5
|
+
function checkUrlAndMethod(options, url, method) { }
|
|
7
6
|
exports.checkUrlAndMethod = checkUrlAndMethod;
|
|
8
|
-
|
|
9
|
-
function checkMediaHeaders(createRequestMock, accept, contentType) {
|
|
10
|
-
return;
|
|
11
|
-
}
|
|
7
|
+
function checkMediaHeaders(createRequestMock, accept, contentType) { }
|
|
12
8
|
exports.checkMediaHeaders = checkMediaHeaders;
|
|
13
|
-
|
|
14
|
-
function checkUserHeader(createRequestMock, userHeaderName, userHeaderValue) {
|
|
15
|
-
return;
|
|
16
|
-
}
|
|
9
|
+
function checkUserHeader(createRequestMock, userHeaderName, userHeaderValue) { }
|
|
17
10
|
exports.checkUserHeader = checkUserHeader;
|
|
18
|
-
|
|
19
|
-
function checkForSuccessfulExecution(createRequestMock) {
|
|
20
|
-
return;
|
|
21
|
-
}
|
|
11
|
+
function checkForSuccessfulExecution(createRequestMock) { }
|
|
22
12
|
exports.checkForSuccessfulExecution = checkForSuccessfulExecution;
|
|
23
|
-
|
|
24
|
-
function getOptions(createRequestMock) {
|
|
25
|
-
return;
|
|
26
|
-
}
|
|
13
|
+
function getOptions(createRequestMock) { }
|
|
27
14
|
exports.getOptions = getOptions;
|
|
28
|
-
|
|
29
|
-
function expectToBePromise(sdkPromise) {
|
|
30
|
-
return;
|
|
31
|
-
}
|
|
15
|
+
function expectToBePromise(sdkPromise) { }
|
|
32
16
|
exports.expectToBePromise = expectToBePromise;
|
|
33
|
-
;
|
package/lib/sdk-test-helpers.js
CHANGED
|
@@ -35,7 +35,6 @@ function checkUrlAndMethod(options, url, method) {
|
|
|
35
35
|
expect(options.method).toEqual(method);
|
|
36
36
|
}
|
|
37
37
|
exports.checkUrlAndMethod = checkUrlAndMethod;
|
|
38
|
-
;
|
|
39
38
|
/**
|
|
40
39
|
* Takes the mock object for the `createRequest` method, extracts the headers that were sent with the call,
|
|
41
40
|
* and checks for the expected values for `Accept` and `Content-Type`. This to verify that the SDK sets
|
|
@@ -52,7 +51,6 @@ function checkMediaHeaders(createRequestMock, accept, contentType) {
|
|
|
52
51
|
expect(headers['Content-Type']).toEqual(contentType);
|
|
53
52
|
}
|
|
54
53
|
exports.checkMediaHeaders = checkMediaHeaders;
|
|
55
|
-
;
|
|
56
54
|
/**
|
|
57
55
|
* Takes the mock object for the `createRequest` method, extracts the headers that were sent with the call,
|
|
58
56
|
* and checks for the expected value for a user-defined header. This is verify that the SDK accepts header
|
|
@@ -68,7 +66,6 @@ function checkUserHeader(createRequestMock, userHeaderName, userHeaderValue) {
|
|
|
68
66
|
expect(headers[userHeaderName]).toEqual(userHeaderValue);
|
|
69
67
|
}
|
|
70
68
|
exports.checkUserHeader = checkUserHeader;
|
|
71
|
-
;
|
|
72
69
|
/**
|
|
73
70
|
* This method simply ensures that the method executed without any issues by extracting
|
|
74
71
|
* the argument from the mock object for the `createRequest` method and verifying that it is an object.
|
|
@@ -81,7 +78,6 @@ function checkForSuccessfulExecution(createRequestMock) {
|
|
|
81
78
|
expect(typeof sdkParams).toEqual('object');
|
|
82
79
|
}
|
|
83
80
|
exports.checkForSuccessfulExecution = checkForSuccessfulExecution;
|
|
84
|
-
;
|
|
85
81
|
/**
|
|
86
82
|
* This method extracts the `options` property from the object passed into `createRequest`. This property is
|
|
87
83
|
* an object containing all of the SDK method-specific information (like `path` and `body`) used to build a request.
|
|
@@ -94,7 +90,6 @@ function getOptions(createRequestMock) {
|
|
|
94
90
|
return createRequestMock.mock.calls[0][0].options;
|
|
95
91
|
}
|
|
96
92
|
exports.getOptions = getOptions;
|
|
97
|
-
;
|
|
98
93
|
/**
|
|
99
94
|
* This method simply ensures that the SDK methods return Promises by checking for
|
|
100
95
|
* the `then` function - common way to assess whether or not an object is a Promise.
|
|
@@ -106,4 +101,3 @@ function expectToBePromise(sdkPromise) {
|
|
|
106
101
|
expect(typeof sdkPromise.then).toBe('function');
|
|
107
102
|
}
|
|
108
103
|
exports.expectToBePromise = expectToBePromise;
|
|
109
|
-
;
|
package/lib/stream-to-promise.js
CHANGED
|
@@ -16,9 +16,7 @@ function streamToPromise(stream) {
|
|
|
16
16
|
results.push(result);
|
|
17
17
|
})
|
|
18
18
|
.on('end', function () {
|
|
19
|
-
resolve(Buffer.isBuffer(results[0])
|
|
20
|
-
? Buffer.concat(results).toString()
|
|
21
|
-
: results);
|
|
19
|
+
resolve(Buffer.isBuffer(results[0]) ? Buffer.concat(results).toString() : results);
|
|
22
20
|
})
|
|
23
21
|
.on('error', reject);
|
|
24
22
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ibm-cloud-sdk-core",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.11.3",
|
|
4
4
|
"description": "Core functionality to support SDKs generated with IBM's OpenAPI SDK Generator.",
|
|
5
5
|
"main": "index",
|
|
6
6
|
"repository": {
|
|
@@ -64,14 +64,11 @@
|
|
|
64
64
|
},
|
|
65
65
|
"scripts": {
|
|
66
66
|
"commitmsg": "commitlint -E GIT_PARAMS",
|
|
67
|
-
"tslint:config": "tslint-config-prettier-check ./tslint.json",
|
|
68
|
-
"tslint:fix": "tslint --fix -p . -c tslint.json",
|
|
69
|
-
"tslint:check": "tslint -p . -c ./tslint.json",
|
|
70
67
|
"eslint:config": "eslint --print-config .eslintrc.js | eslint-config-prettier-check",
|
|
71
68
|
"eslint:fix": "eslint . --fix",
|
|
72
69
|
"eslint:check": "eslint . --cache",
|
|
73
|
-
"lint": "npm run eslint:check
|
|
74
|
-
"lint:fix": "npm run eslint:fix
|
|
70
|
+
"lint": "npm run eslint:check",
|
|
71
|
+
"lint:fix": "npm run eslint:fix",
|
|
75
72
|
"jest": "jest",
|
|
76
73
|
"test": "jest test/unit/",
|
|
77
74
|
"test-travis": "jest --runInBand test/unit/",
|