ibm-cloud-sdk-core 3.2.2-beta-1 → 3.2.2-beta-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.
@@ -0,0 +1,23 @@
1
+ /**
2
+ * (C) Copyright IBM Corp. 2022.
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 { AxiosRequestConfig, AxiosResponse } from 'axios';
17
+ import { CookieJar } from 'tough-cookie';
18
+ export declare class CookieInterceptor {
19
+ private readonly cookieJar;
20
+ constructor(cookieJar: CookieJar | boolean);
21
+ requestInterceptor(config: AxiosRequestConfig): Promise<AxiosRequestConfig<any>>;
22
+ responseInterceptor(response: AxiosResponse): Promise<AxiosResponse<any, any>>;
23
+ }
@@ -0,0 +1,87 @@
1
+ /**
2
+ * (C) Copyright IBM Corp. 2022.
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
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
17
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
18
+ return new (P || (P = Promise))(function (resolve, reject) {
19
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
20
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
21
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
22
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
23
+ });
24
+ };
25
+ import extend from 'extend';
26
+ import { CookieJar } from 'tough-cookie';
27
+ import logger from './logger';
28
+ export class CookieInterceptor {
29
+ constructor(cookieJar) {
30
+ if (cookieJar) {
31
+ if (cookieJar === true) {
32
+ logger.debug('CookieInterceptor: creating new CookieJar');
33
+ this.cookieJar = new CookieJar();
34
+ }
35
+ else {
36
+ logger.debug('CookieInterceptor: using supplied CookieJar');
37
+ this.cookieJar = cookieJar;
38
+ }
39
+ }
40
+ else {
41
+ throw new Error('Must supply a cookie jar or true.');
42
+ }
43
+ }
44
+ requestInterceptor(config) {
45
+ return __awaiter(this, void 0, void 0, function* () {
46
+ logger.debug('CookieInterceptor: intercepting request');
47
+ if (config && config.url) {
48
+ logger.debug(`CookieInterceptor: getting cookies for: ${config.url}`);
49
+ const cookieHeaderValue = yield this.cookieJar.getCookieString(config.url);
50
+ if (cookieHeaderValue) {
51
+ logger.debug('CookieInterceptor: setting cookie header');
52
+ const cookieHeader = { cookie: cookieHeaderValue };
53
+ config.headers = extend(true, {}, config.headers, cookieHeader);
54
+ }
55
+ else {
56
+ logger.debug(`CookieInterceptor: no cookies for: ${config.url}`);
57
+ }
58
+ }
59
+ else {
60
+ logger.debug('CookieInterceptor: no request URL.');
61
+ }
62
+ return config;
63
+ });
64
+ }
65
+ responseInterceptor(response) {
66
+ return __awaiter(this, void 0, void 0, function* () {
67
+ logger.debug('CookieInterceptor: intercepting response.');
68
+ if (response && response.headers) {
69
+ logger.debug('CookieInterceptor: checking for set-cookie headers.');
70
+ const cookies = response.headers['set-cookie'];
71
+ if (cookies) {
72
+ logger.debug('CookieInterceptor: setting cookies in jar.');
73
+ cookies.forEach((cookie) => __awaiter(this, void 0, void 0, function* () {
74
+ yield this.cookieJar.setCookie(cookie, response.request.url);
75
+ }));
76
+ }
77
+ else {
78
+ logger.debug('CookieInterceptor: no set-cookie headers.');
79
+ }
80
+ }
81
+ else {
82
+ logger.debug('CookieInterceptor: no response headers.');
83
+ }
84
+ return response;
85
+ });
86
+ }
87
+ }
@@ -1,5 +1,5 @@
1
1
  /**
2
- * (C) Copyright IBM Corp. 2014, 2021.
2
+ * (C) Copyright IBM Corp. 2014, 2022.
3
3
  *
4
4
  * Licensed under the Apache License, Version 2.0 (the "License");
5
5
  * you may not use this file except in compliance with the License.
@@ -9,7 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  });
10
10
  };
11
11
  /**
12
- * (C) Copyright IBM Corp. 2014, 2021.
12
+ * (C) Copyright IBM Corp. 2014, 2022.
13
13
  *
14
14
  * Licensed under the Apache License, Version 2.0 (the "License");
15
15
  * you may not use this file except in compliance with the License.
@@ -25,17 +25,16 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
25
25
  */
26
26
  import axios from 'axios';
27
27
  import * as rax from 'retry-axios';
28
- import { wrapper as axiosCookieJarSupport } from 'axios-cookiejar-support';
29
28
  import extend from 'extend';
30
29
  import FormData from 'form-data';
31
30
  import { Agent } from 'https';
32
31
  import isStream from 'isstream';
33
32
  import { stringify } from 'querystring';
34
33
  import { gzipSync } from 'zlib';
35
- import { CookieJar } from 'tough-cookie';
36
34
  import { buildRequestFileObject, isEmptyObject, isFileData, isFileWithMetadata, stripTrailingSlash, } from './helper';
37
35
  import logger from './logger';
38
36
  import { streamToPromise } from './stream-to-promise';
37
+ import { CookieInterceptor } from './cookie-support';
39
38
  export class RequestWrapper {
40
39
  constructor(axiosOptions) {
41
40
  axiosOptions = axiosOptions || {};
@@ -73,16 +72,9 @@ export class RequestWrapper {
73
72
  });
74
73
  // if a cookie jar is provided, wrap the axios instance and update defaults
75
74
  if (axiosOptions.jar) {
76
- axiosCookieJarSupport(this.axiosInstance);
77
- // axios-cookiejar-support < 2.x accepted a boolean
78
- // some SDKs might be using that, so create a default
79
- // CookieJar if detecting true.
80
- if (axiosOptions.jar === true) {
81
- this.axiosInstance.defaults.jar = new CookieJar();
82
- }
83
- else {
84
- this.axiosInstance.defaults.jar = axiosOptions.jar;
85
- }
75
+ const cookieInterceptor = new CookieInterceptor(axiosOptions.jar);
76
+ this.axiosInstance.interceptors.request.use((config) => cookieInterceptor.requestInterceptor(config));
77
+ this.axiosInstance.interceptors.response.use((response) => cookieInterceptor.responseInterceptor(response));
86
78
  }
87
79
  // get retry config properties and conditionally enable retries
88
80
  if (axiosOptions.enableRetries) {
@@ -0,0 +1,23 @@
1
+ /**
2
+ * (C) Copyright IBM Corp. 2022.
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 { AxiosRequestConfig, AxiosResponse } from 'axios';
17
+ import { CookieJar } from 'tough-cookie';
18
+ export declare class CookieInterceptor {
19
+ private readonly cookieJar;
20
+ constructor(cookieJar: CookieJar | boolean);
21
+ requestInterceptor(config: AxiosRequestConfig): Promise<AxiosRequestConfig<any>>;
22
+ responseInterceptor(response: AxiosResponse): Promise<AxiosResponse<any, any>>;
23
+ }
@@ -0,0 +1,141 @@
1
+ "use strict";
2
+ /**
3
+ * (C) Copyright IBM Corp. 2022.
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
+ */
17
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
18
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
19
+ return new (P || (P = Promise))(function (resolve, reject) {
20
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
21
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
22
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
23
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
24
+ });
25
+ };
26
+ var __generator = (this && this.__generator) || function (thisArg, body) {
27
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
28
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
29
+ function verb(n) { return function (v) { return step([n, v]); }; }
30
+ function step(op) {
31
+ if (f) throw new TypeError("Generator is already executing.");
32
+ while (_) try {
33
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
34
+ if (y = 0, t) op = [op[0] & 2, t.value];
35
+ switch (op[0]) {
36
+ case 0: case 1: t = op; break;
37
+ case 4: _.label++; return { value: op[1], done: false };
38
+ case 5: _.label++; y = op[1]; op = [0]; continue;
39
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
40
+ default:
41
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
42
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
43
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
44
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
45
+ if (t[2]) _.ops.pop();
46
+ _.trys.pop(); continue;
47
+ }
48
+ op = body.call(thisArg, _);
49
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
50
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
51
+ }
52
+ };
53
+ var __importDefault = (this && this.__importDefault) || function (mod) {
54
+ return (mod && mod.__esModule) ? mod : { "default": mod };
55
+ };
56
+ Object.defineProperty(exports, "__esModule", { value: true });
57
+ exports.CookieInterceptor = void 0;
58
+ var extend_1 = __importDefault(require("extend"));
59
+ var tough_cookie_1 = require("tough-cookie");
60
+ var logger_1 = __importDefault(require("./logger"));
61
+ var CookieInterceptor = /** @class */ (function () {
62
+ function CookieInterceptor(cookieJar) {
63
+ if (cookieJar) {
64
+ if (cookieJar === true) {
65
+ logger_1.default.debug('CookieInterceptor: creating new CookieJar');
66
+ this.cookieJar = new tough_cookie_1.CookieJar();
67
+ }
68
+ else {
69
+ logger_1.default.debug('CookieInterceptor: using supplied CookieJar');
70
+ this.cookieJar = cookieJar;
71
+ }
72
+ }
73
+ else {
74
+ throw new Error('Must supply a cookie jar or true.');
75
+ }
76
+ }
77
+ CookieInterceptor.prototype.requestInterceptor = function (config) {
78
+ return __awaiter(this, void 0, void 0, function () {
79
+ var cookieHeaderValue, cookieHeader;
80
+ return __generator(this, function (_a) {
81
+ switch (_a.label) {
82
+ case 0:
83
+ logger_1.default.debug('CookieInterceptor: intercepting request');
84
+ if (!(config && config.url)) return [3 /*break*/, 2];
85
+ logger_1.default.debug("CookieInterceptor: getting cookies for: ".concat(config.url));
86
+ return [4 /*yield*/, this.cookieJar.getCookieString(config.url)];
87
+ case 1:
88
+ cookieHeaderValue = _a.sent();
89
+ if (cookieHeaderValue) {
90
+ logger_1.default.debug('CookieInterceptor: setting cookie header');
91
+ cookieHeader = { cookie: cookieHeaderValue };
92
+ config.headers = (0, extend_1.default)(true, {}, config.headers, cookieHeader);
93
+ }
94
+ else {
95
+ logger_1.default.debug("CookieInterceptor: no cookies for: ".concat(config.url));
96
+ }
97
+ return [3 /*break*/, 3];
98
+ case 2:
99
+ logger_1.default.debug('CookieInterceptor: no request URL.');
100
+ _a.label = 3;
101
+ case 3: return [2 /*return*/, config];
102
+ }
103
+ });
104
+ });
105
+ };
106
+ CookieInterceptor.prototype.responseInterceptor = function (response) {
107
+ return __awaiter(this, void 0, void 0, function () {
108
+ var cookies;
109
+ var _this = this;
110
+ return __generator(this, function (_a) {
111
+ logger_1.default.debug('CookieInterceptor: intercepting response.');
112
+ if (response && response.headers) {
113
+ logger_1.default.debug('CookieInterceptor: checking for set-cookie headers.');
114
+ cookies = response.headers['set-cookie'];
115
+ if (cookies) {
116
+ logger_1.default.debug('CookieInterceptor: setting cookies in jar.');
117
+ cookies.forEach(function (cookie) { return __awaiter(_this, void 0, void 0, function () {
118
+ return __generator(this, function (_a) {
119
+ switch (_a.label) {
120
+ case 0: return [4 /*yield*/, this.cookieJar.setCookie(cookie, response.request.url)];
121
+ case 1:
122
+ _a.sent();
123
+ return [2 /*return*/];
124
+ }
125
+ });
126
+ }); });
127
+ }
128
+ else {
129
+ logger_1.default.debug('CookieInterceptor: no set-cookie headers.');
130
+ }
131
+ }
132
+ else {
133
+ logger_1.default.debug('CookieInterceptor: no response headers.');
134
+ }
135
+ return [2 /*return*/, response];
136
+ });
137
+ });
138
+ };
139
+ return CookieInterceptor;
140
+ }());
141
+ exports.CookieInterceptor = CookieInterceptor;
@@ -1,5 +1,5 @@
1
1
  /**
2
- * (C) Copyright IBM Corp. 2014, 2021.
2
+ * (C) Copyright IBM Corp. 2014, 2022.
3
3
  *
4
4
  * Licensed under the Apache License, Version 2.0 (the "License");
5
5
  * you may not use this file except in compliance with the License.
@@ -76,7 +76,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
76
76
  Object.defineProperty(exports, "__esModule", { value: true });
77
77
  exports.RequestWrapper = void 0;
78
78
  /**
79
- * (C) Copyright IBM Corp. 2014, 2021.
79
+ * (C) Copyright IBM Corp. 2014, 2022.
80
80
  *
81
81
  * Licensed under the Apache License, Version 2.0 (the "License");
82
82
  * you may not use this file except in compliance with the License.
@@ -92,17 +92,16 @@ exports.RequestWrapper = void 0;
92
92
  */
93
93
  var axios_1 = __importDefault(require("axios"));
94
94
  var rax = __importStar(require("retry-axios"));
95
- var axios_cookiejar_support_1 = require("axios-cookiejar-support");
96
95
  var extend_1 = __importDefault(require("extend"));
97
96
  var form_data_1 = __importDefault(require("form-data"));
98
97
  var https_1 = require("https");
99
98
  var isstream_1 = __importDefault(require("isstream"));
100
99
  var querystring_1 = require("querystring");
101
100
  var zlib_1 = require("zlib");
102
- var tough_cookie_1 = require("tough-cookie");
103
101
  var helper_1 = require("./helper");
104
102
  var logger_1 = __importDefault(require("./logger"));
105
103
  var stream_to_promise_1 = require("./stream-to-promise");
104
+ var cookie_support_1 = require("./cookie-support");
106
105
  var RequestWrapper = /** @class */ (function () {
107
106
  function RequestWrapper(axiosOptions) {
108
107
  var _this = this;
@@ -141,16 +140,13 @@ var RequestWrapper = /** @class */ (function () {
141
140
  });
142
141
  // if a cookie jar is provided, wrap the axios instance and update defaults
143
142
  if (axiosOptions.jar) {
144
- (0, axios_cookiejar_support_1.wrapper)(this.axiosInstance);
145
- // axios-cookiejar-support < 2.x accepted a boolean
146
- // some SDKs might be using that, so create a default
147
- // CookieJar if detecting true.
148
- if (axiosOptions.jar === true) {
149
- this.axiosInstance.defaults.jar = new tough_cookie_1.CookieJar();
150
- }
151
- else {
152
- this.axiosInstance.defaults.jar = axiosOptions.jar;
153
- }
143
+ var cookieInterceptor_1 = new cookie_support_1.CookieInterceptor(axiosOptions.jar);
144
+ this.axiosInstance.interceptors.request.use(function (config) {
145
+ return cookieInterceptor_1.requestInterceptor(config);
146
+ });
147
+ this.axiosInstance.interceptors.response.use(function (response) {
148
+ return cookieInterceptor_1.responseInterceptor(response);
149
+ });
154
150
  }
155
151
  // get retry config properties and conditionally enable retries
156
152
  if (axiosOptions.enableRetries) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ibm-cloud-sdk-core",
3
- "version": "3.2.2-beta-1",
3
+ "version": "3.2.2-beta-3",
4
4
  "description": "Core functionality to support SDKs generated with IBM's OpenAPI SDK Generator.",
5
5
  "main": "./index.js",
6
6
  "typings": "./es/index.d.ts",
@@ -77,7 +77,6 @@
77
77
  "@types/node": "~10.14.19",
78
78
  "@types/tough-cookie": "^4.0.0",
79
79
  "axios": "1.2.0",
80
- "axios-cookiejar-support": "4.0.3",
81
80
  "camelcase": "^5.3.1",
82
81
  "debug": "^4.1.1",
83
82
  "dotenv": "^6.2.0",