ibm-cloud-sdk-core 2.17.13 → 3.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/CHANGELOG.md CHANGED
@@ -1,3 +1,34 @@
1
+ # [3.0.0](https://github.com/IBM/node-sdk-core/compare/v2.17.15...v3.0.0) (2022-07-28)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * update file-type to resolve vulnerability ([#205](https://github.com/IBM/node-sdk-core/issues/205)) ([843e66d](https://github.com/IBM/node-sdk-core/commit/843e66d10aeb43dc5a0f7c9583f93ce6a5dbaa7e)), closes [#204](https://github.com/IBM/node-sdk-core/issues/204)
7
+
8
+
9
+ ### BREAKING CHANGES
10
+
11
+ * two synchronous public functions are now asynchronous
12
+
13
+ The function `getContentType` formerly returned a string but now returns a
14
+ Promise that resolves to a string. The function `buildRequestFileObject`
15
+ formerly returned a `FileObject` but now returns a Promise that resolves to
16
+ a `FileObject`.
17
+
18
+ ## [2.17.15](https://github.com/IBM/node-sdk-core/compare/v2.17.14...v2.17.15) (2022-07-05)
19
+
20
+
21
+ ### Bug Fixes
22
+
23
+ * ensure dist/docs is a directory during build ([#202](https://github.com/IBM/node-sdk-core/issues/202)) ([1b3ef62](https://github.com/IBM/node-sdk-core/commit/1b3ef621e3cbfe0586d433269f63acf77dfa7251))
24
+
25
+ ## [2.17.14](https://github.com/IBM/node-sdk-core/compare/v2.17.13...v2.17.14) (2022-05-02)
26
+
27
+
28
+ ### Bug Fixes
29
+
30
+ * do not retry on 501 ([#197](https://github.com/IBM/node-sdk-core/issues/197)) ([5cb9081](https://github.com/IBM/node-sdk-core/commit/5cb9081f581c73b3ba16e470eb4c03c9b8f5c266))
31
+
1
32
  ## [2.17.13](https://github.com/IBM/node-sdk-core/compare/v2.17.12...v2.17.13) (2022-04-04)
2
33
 
3
34
 
@@ -9,7 +9,7 @@ This function builds a `form-data` object for each file parameter.
9
9
  <b>Signature:</b>
10
10
 
11
11
  ```typescript
12
- export declare function buildRequestFileObject(fileParam: FileWithMetadata): FileObject;
12
+ export declare function buildRequestFileObject(fileParam: FileWithMetadata): Promise<FileObject>;
13
13
  ```
14
14
 
15
15
  ## Parameters
@@ -20,7 +20,7 @@ export declare function buildRequestFileObject(fileParam: FileWithMetadata): Fil
20
20
 
21
21
  <b>Returns:</b>
22
22
 
23
- [FileObject](./ibm-cloud-sdk-core.fileobject.md)
23
+ Promise&lt;[FileObject](./ibm-cloud-sdk-core.fileobject.md)<!-- -->&gt;
24
24
 
25
25
  {<!-- -->FileObject<!-- -->}
26
26
 
@@ -8,7 +8,7 @@
8
8
 
9
9
  ```typescript
10
10
  _default: {
11
- fromFilename: (file: String | File | Buffer | NodeJS.ReadableStream | FileObject) => string;
11
+ fromFilename: (file: String | File | FileObject | NodeJS.ReadableStream | Buffer) => string;
12
12
  fromHeader: (buffer: Buffer) => string;
13
13
  }
14
14
  ```
@@ -9,7 +9,7 @@ This function retrieves the content type of the input.
9
9
  <b>Signature:</b>
10
10
 
11
11
  ```typescript
12
- export declare function getContentType(inputData: NodeJS.ReadableStream | Buffer): string;
12
+ export declare function getContentType(inputData: NodeJS.ReadableStream | Buffer): Promise<string>;
13
13
  ```
14
14
 
15
15
  ## Parameters
@@ -20,7 +20,7 @@ export declare function getContentType(inputData: NodeJS.ReadableStream | Buffer
20
20
 
21
21
  <b>Returns:</b>
22
22
 
23
- string
23
+ Promise&lt;string&gt;
24
24
 
25
25
  {<!-- -->string<!-- -->} the content type of the input.
26
26
 
@@ -1639,11 +1639,24 @@
1639
1639
  "kind": "Content",
1640
1640
  "text": "): "
1641
1641
  },
1642
+ {
1643
+ "kind": "Reference",
1644
+ "text": "Promise",
1645
+ "canonicalReference": "!Promise:interface"
1646
+ },
1647
+ {
1648
+ "kind": "Content",
1649
+ "text": "<"
1650
+ },
1642
1651
  {
1643
1652
  "kind": "Reference",
1644
1653
  "text": "FileObject",
1645
1654
  "canonicalReference": "ibm-cloud-sdk-core!FileObject:interface"
1646
1655
  },
1656
+ {
1657
+ "kind": "Content",
1658
+ "text": ">"
1659
+ },
1647
1660
  {
1648
1661
  "kind": "Content",
1649
1662
  "text": ";"
@@ -1651,7 +1664,7 @@
1651
1664
  ],
1652
1665
  "returnTypeTokenRange": {
1653
1666
  "startIndex": 3,
1654
- "endIndex": 4
1667
+ "endIndex": 7
1655
1668
  },
1656
1669
  "releaseTag": "Public",
1657
1670
  "overloadIndex": 1,
@@ -2589,8 +2602,8 @@
2589
2602
  },
2590
2603
  {
2591
2604
  "kind": "Reference",
2592
- "text": "Buffer",
2593
- "canonicalReference": "!Buffer:interface"
2605
+ "text": "FileObject",
2606
+ "canonicalReference": "ibm-cloud-sdk-core!FileObject:interface"
2594
2607
  },
2595
2608
  {
2596
2609
  "kind": "Content",
@@ -2607,8 +2620,8 @@
2607
2620
  },
2608
2621
  {
2609
2622
  "kind": "Reference",
2610
- "text": "FileObject",
2611
- "canonicalReference": "ibm-cloud-sdk-core!FileObject:interface"
2623
+ "text": "Buffer",
2624
+ "canonicalReference": "!Buffer:interface"
2612
2625
  },
2613
2626
  {
2614
2627
  "kind": "Content",
@@ -3185,9 +3198,14 @@
3185
3198
  "kind": "Content",
3186
3199
  "text": "): "
3187
3200
  },
3201
+ {
3202
+ "kind": "Reference",
3203
+ "text": "Promise",
3204
+ "canonicalReference": "!Promise:interface"
3205
+ },
3188
3206
  {
3189
3207
  "kind": "Content",
3190
- "text": "string"
3208
+ "text": "<string>"
3191
3209
  },
3192
3210
  {
3193
3211
  "kind": "Content",
@@ -3196,7 +3214,7 @@
3196
3214
  ],
3197
3215
  "returnTypeTokenRange": {
3198
3216
  "startIndex": 5,
3199
- "endIndex": 6
3217
+ "endIndex": 7
3200
3218
  },
3201
3219
  "releaseTag": "Public",
3202
3220
  "overloadIndex": 1,
@@ -1,7 +1,7 @@
1
1
  /// <reference types="node" />
2
2
  import { FileObject } from "./helper";
3
3
  declare const _default: {
4
- fromFilename: (file: String | File | Buffer | NodeJS.ReadableStream | FileObject) => string;
4
+ fromFilename: (file: String | File | FileObject | NodeJS.ReadableStream | Buffer) => string;
5
5
  fromHeader: (buffer: Buffer) => string;
6
6
  };
7
7
  export default _default;
@@ -38,7 +38,7 @@ export declare function isEmptyObject(obj: any): boolean;
38
38
  * @param {NodeJS.ReadableStream|Buffer} inputData - The data to retrieve content type for.
39
39
  * @returns {string} the content type of the input.
40
40
  */
41
- export declare function getContentType(inputData: NodeJS.ReadableStream | Buffer): string;
41
+ export declare function getContentType(inputData: NodeJS.ReadableStream | Buffer): Promise<string>;
42
42
  /**
43
43
  *
44
44
  * @param {string} url - the url string.
@@ -104,7 +104,7 @@ export declare function getFormat(params: {
104
104
  * @param {string} fileParam.contentType The content type of the file.
105
105
  * @returns {FileObject}
106
106
  */
107
- export declare function buildRequestFileObject(fileParam: FileWithMetadata): FileObject;
107
+ export declare function buildRequestFileObject(fileParam: FileWithMetadata): Promise<FileObject>;
108
108
  /**
109
109
  * This function converts an object's keys to lower case.
110
110
  * note: does not convert nested keys
package/es/lib/helper.js CHANGED
@@ -13,11 +13,20 @@
13
13
  * See the License for the specific language governing permissions and
14
14
  * limitations under the License.
15
15
  */
16
- import fileType from 'file-type';
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
+ };
17
25
  import { isReadable } from 'isstream';
18
26
  import { lookup } from 'mime-types';
19
27
  import { basename } from 'path';
20
28
  import logger from './logger';
29
+ const FileType = require('file-type');
21
30
  // custom type guards
22
31
  function isFileObject(obj) {
23
32
  return Boolean(obj && obj.value);
@@ -40,17 +49,19 @@ export function isEmptyObject(obj) {
40
49
  * @returns {string} the content type of the input.
41
50
  */
42
51
  export function getContentType(inputData) {
43
- let contentType = null;
44
- if (isFileStream(inputData)) {
45
- // if the inputData is a NodeJS.ReadableStream
46
- const mimeType = lookup(inputData.path); // TODO: cleue quick hack, per type definition path could also be a Buffer
47
- contentType = { mime: mimeType || null };
48
- }
49
- else if (Buffer.isBuffer(inputData)) {
50
- // if the inputData is a Buffer
51
- contentType = fileType(inputData);
52
- }
53
- return contentType ? contentType.mime : null;
52
+ return __awaiter(this, void 0, void 0, function* () {
53
+ let contentType = null;
54
+ if (isFileStream(inputData)) {
55
+ // if the inputData is a NodeJS.ReadableStream
56
+ const mimeType = lookup(inputData.path); // TODO: cleue quick hack, per type definition path could also be a Buffer
57
+ contentType = { mime: mimeType || null };
58
+ }
59
+ else if (Buffer.isBuffer(inputData)) {
60
+ // if the inputData is a Buffer
61
+ contentType = yield FileType.fromBuffer(inputData);
62
+ }
63
+ return contentType ? contentType.mime : null;
64
+ });
54
65
  }
55
66
  /**
56
67
  *
@@ -198,44 +209,47 @@ export function getFormat(params, formats) {
198
209
  * @returns {FileObject}
199
210
  */
200
211
  export function buildRequestFileObject(fileParam) {
201
- let fileObj;
202
- if (isFileObject(fileParam.data)) {
203
- // For backward compatibility, we allow the data to be a FileObject.
204
- fileObj = { value: fileParam.data.value, options: {} };
205
- if (fileParam.data.options) {
206
- fileObj.options = {
207
- filename: fileParam.filename || fileParam.data.options.filename,
208
- contentType: fileParam.contentType || fileParam.data.options.contentType,
212
+ return __awaiter(this, void 0, void 0, function* () {
213
+ let fileObj;
214
+ if (isFileObject(fileParam.data)) {
215
+ // For backward compatibility, we allow the data to be a FileObject.
216
+ fileObj = { value: fileParam.data.value, options: {} };
217
+ if (fileParam.data.options) {
218
+ fileObj.options = {
219
+ filename: fileParam.filename || fileParam.data.options.filename,
220
+ contentType: fileParam.contentType || fileParam.data.options.contentType,
221
+ };
222
+ }
223
+ }
224
+ else {
225
+ fileObj = {
226
+ value: fileParam.data,
227
+ options: {
228
+ filename: fileParam.filename,
229
+ contentType: fileParam.contentType,
230
+ },
209
231
  };
210
232
  }
211
- }
212
- else {
213
- fileObj = {
214
- value: fileParam.data,
215
- options: {
216
- filename: fileParam.filename,
217
- contentType: fileParam.contentType,
218
- },
219
- };
220
- }
221
- // Also for backward compatibility, we allow data to be a string
222
- if (typeof fileObj.value === 'string') {
223
- fileObj.value = Buffer.from(fileObj.value);
224
- }
225
- // build filename
226
- // eslint-disable-next-line prefer-destructuring
227
- let filename = fileObj.options.filename;
228
- if (!filename && isFileStream(fileObj.value)) {
229
- // if readable stream with path property
230
- filename = fileObj.value.path;
231
- }
232
- // toString handles the case when path is a buffer
233
- fileObj.options.filename = filename ? basename(filename.toString()) : '_';
234
- // build contentType
235
- if (!fileObj.options.contentType && isFileData(fileObj.value)) {
236
- fileObj.options.contentType = getContentType(fileObj.value) || 'application/octet-stream';
237
- }
238
- return fileObj;
233
+ // Also for backward compatibility, we allow data to be a string
234
+ if (typeof fileObj.value === 'string') {
235
+ fileObj.value = Buffer.from(fileObj.value);
236
+ }
237
+ // build filename
238
+ // eslint-disable-next-line prefer-destructuring
239
+ let filename = fileObj.options.filename;
240
+ if (!filename && isFileStream(fileObj.value)) {
241
+ // if readable stream with path property
242
+ filename = fileObj.value.path;
243
+ }
244
+ // toString handles the case when path is a buffer
245
+ fileObj.options.filename = filename ? basename(filename.toString()) : '_';
246
+ // build contentType
247
+ if (!fileObj.options.contentType && isFileData(fileObj.value)) {
248
+ fileObj.options.contentType =
249
+ (yield getContentType(fileObj.value)) || 'application/octet-stream';
250
+ }
251
+ return fileObj;
252
+ });
239
253
  }
240
254
  /**
241
255
  * This function converts an object's keys to lower case.
@@ -135,14 +135,14 @@ export class RequestWrapper {
135
135
  // Skip keys with undefined/null values or empty object value
136
136
  values
137
137
  .filter((v) => v != null && !isEmptyObject(v))
138
- .forEach((value) => {
138
+ .forEach((value) => __awaiter(this, void 0, void 0, function* () {
139
139
  // Special case of empty file object
140
140
  if (Object.prototype.hasOwnProperty.call(value, 'contentType') &&
141
141
  !Object.prototype.hasOwnProperty.call(value, 'data')) {
142
142
  return;
143
143
  }
144
144
  if (isFileWithMetadata(value)) {
145
- const fileObj = buildRequestFileObject(value);
145
+ const fileObj = yield buildRequestFileObject(value);
146
146
  multipartForm.append(key, fileObj.value, fileObj.options);
147
147
  }
148
148
  else {
@@ -151,7 +151,7 @@ export class RequestWrapper {
151
151
  }
152
152
  multipartForm.append(key, value);
153
153
  }
154
- });
154
+ }));
155
155
  });
156
156
  }
157
157
  // Path params
@@ -290,6 +290,12 @@ export class RequestWrapper {
290
290
  retry: 4,
291
291
  retryDelay: 1000,
292
292
  httpMethodsToRetry: ['GET', 'HEAD', 'OPTIONS', 'DELETE', 'PUT', 'POST'],
293
+ // do not retry on 501
294
+ statusCodesToRetry: [
295
+ [429, 429],
296
+ [500, 500],
297
+ [502, 599],
298
+ ],
293
299
  instance: axiosInstance,
294
300
  backoffType: 'exponential',
295
301
  checkRetryAfter: true,
@@ -94,7 +94,7 @@ export class BearerTokenAuthenticator extends Authenticator {
94
94
  }
95
95
 
96
96
  // @public
97
- export function buildRequestFileObject(fileParam: FileWithMetadata): FileObject;
97
+ export function buildRequestFileObject(fileParam: FileWithMetadata): Promise<FileObject>;
98
98
 
99
99
  // @public
100
100
  export function checkCredentials(obj: any, credsToCheck: string[]): Error | null;
@@ -155,7 +155,7 @@ export class ContainerTokenManager extends IamRequestBasedTokenManager {
155
155
 
156
156
  // @public (undocumented)
157
157
  export const contentType: {
158
- fromFilename: (file: String | File | Buffer | NodeJS.ReadableStream | FileObject) => string;
158
+ fromFilename: (file: String | File | FileObject | NodeJS.ReadableStream | Buffer) => string;
159
159
  fromHeader: (buffer: Buffer) => string;
160
160
  };
161
161
 
@@ -211,7 +211,7 @@ export interface FileWithMetadata {
211
211
  export function getAuthenticatorFromEnvironment(serviceName: string): Authenticator;
212
212
 
213
213
  // @public
214
- export function getContentType(inputData: NodeJS.ReadableStream | Buffer): string;
214
+ export function getContentType(inputData: NodeJS.ReadableStream | Buffer): Promise<string>;
215
215
 
216
216
  // @public
217
217
  export function getCurrentTime(): number;
@@ -319,7 +319,7 @@ export declare class BaseService {
319
319
  * @param {string} fileParam.contentType The content type of the file.
320
320
  * @returns {FileObject}
321
321
  */
322
- export declare function buildRequestFileObject(fileParam: FileWithMetadata): FileObject;
322
+ export declare function buildRequestFileObject(fileParam: FileWithMetadata): Promise<FileObject>;
323
323
 
324
324
  /**
325
325
  * Checks credentials for common user mistakes of copying {, }, or " characters from the documentation
@@ -584,7 +584,7 @@ export declare class BaseService {
584
584
  }
585
585
 
586
586
  export declare const contentType: {
587
- fromFilename: (file: String | File | Buffer | NodeJS.ReadableStream | FileObject) => string;
587
+ fromFilename: (file: String | File | FileObject | NodeJS.ReadableStream | Buffer) => string;
588
588
  fromHeader: (buffer: Buffer) => string;
589
589
  };
590
590
 
@@ -683,7 +683,7 @@ export declare class BaseService {
683
683
  * @param {NodeJS.ReadableStream|Buffer} inputData - The data to retrieve content type for.
684
684
  * @returns {string} the content type of the input.
685
685
  */
686
- export declare function getContentType(inputData: NodeJS.ReadableStream | Buffer): string;
686
+ export declare function getContentType(inputData: NodeJS.ReadableStream | Buffer): Promise<string>;
687
687
 
688
688
  /**
689
689
  * Get the current time
@@ -1,7 +1,7 @@
1
1
  /// <reference types="node" />
2
2
  import { FileObject } from "./helper";
3
3
  declare const _default: {
4
- fromFilename: (file: String | File | Buffer | NodeJS.ReadableStream | FileObject) => string;
4
+ fromFilename: (file: String | File | FileObject | NodeJS.ReadableStream | Buffer) => string;
5
5
  fromHeader: (buffer: Buffer) => string;
6
6
  };
7
7
  export default _default;
package/lib/helper.d.ts CHANGED
@@ -38,7 +38,7 @@ export declare function isEmptyObject(obj: any): boolean;
38
38
  * @param {NodeJS.ReadableStream|Buffer} inputData - The data to retrieve content type for.
39
39
  * @returns {string} the content type of the input.
40
40
  */
41
- export declare function getContentType(inputData: NodeJS.ReadableStream | Buffer): string;
41
+ export declare function getContentType(inputData: NodeJS.ReadableStream | Buffer): Promise<string>;
42
42
  /**
43
43
  *
44
44
  * @param {string} url - the url string.
@@ -104,7 +104,7 @@ export declare function getFormat(params: {
104
104
  * @param {string} fileParam.contentType The content type of the file.
105
105
  * @returns {FileObject}
106
106
  */
107
- export declare function buildRequestFileObject(fileParam: FileWithMetadata): FileObject;
107
+ export declare function buildRequestFileObject(fileParam: FileWithMetadata): Promise<FileObject>;
108
108
  /**
109
109
  * This function converts an object's keys to lower case.
110
110
  * note: does not convert nested keys
package/lib/helper.js CHANGED
@@ -14,6 +14,42 @@
14
14
  * See the License for the specific language governing permissions and
15
15
  * limitations under the License.
16
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
+ };
17
53
  var __spreadArrays = (this && this.__spreadArrays) || function () {
18
54
  for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
19
55
  for (var r = Array(s), k = 0, i = 0; i < il; i++)
@@ -25,11 +61,11 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
25
61
  return (mod && mod.__esModule) ? mod : { "default": mod };
26
62
  };
27
63
  Object.defineProperty(exports, "__esModule", { value: true });
28
- var file_type_1 = __importDefault(require("file-type"));
29
64
  var isstream_1 = require("isstream");
30
65
  var mime_types_1 = require("mime-types");
31
66
  var path_1 = require("path");
32
67
  var logger_1 = __importDefault(require("./logger"));
68
+ var FileType = require('file-type');
33
69
  // custom type guards
34
70
  function isFileObject(obj) {
35
71
  return Boolean(obj && obj.value);
@@ -55,17 +91,27 @@ exports.isEmptyObject = isEmptyObject;
55
91
  * @returns {string} the content type of the input.
56
92
  */
57
93
  function getContentType(inputData) {
58
- var contentType = null;
59
- if (isFileStream(inputData)) {
60
- // if the inputData is a NodeJS.ReadableStream
61
- var mimeType = mime_types_1.lookup(inputData.path); // TODO: cleue quick hack, per type definition path could also be a Buffer
62
- contentType = { mime: mimeType || null };
63
- }
64
- else if (Buffer.isBuffer(inputData)) {
65
- // if the inputData is a Buffer
66
- contentType = file_type_1.default(inputData);
67
- }
68
- return contentType ? contentType.mime : null;
94
+ return __awaiter(this, void 0, void 0, function () {
95
+ var contentType, mimeType;
96
+ return __generator(this, function (_a) {
97
+ switch (_a.label) {
98
+ case 0:
99
+ contentType = null;
100
+ if (!isFileStream(inputData)) return [3 /*break*/, 1];
101
+ mimeType = mime_types_1.lookup(inputData.path);
102
+ contentType = { mime: mimeType || null };
103
+ return [3 /*break*/, 3];
104
+ case 1:
105
+ if (!Buffer.isBuffer(inputData)) return [3 /*break*/, 3];
106
+ return [4 /*yield*/, FileType.fromBuffer(inputData)];
107
+ case 2:
108
+ // if the inputData is a Buffer
109
+ contentType = _a.sent();
110
+ _a.label = 3;
111
+ case 3: return [2 /*return*/, contentType ? contentType.mime : null];
112
+ }
113
+ });
114
+ });
69
115
  }
70
116
  exports.getContentType = getContentType;
71
117
  /**
@@ -220,44 +266,52 @@ exports.getFormat = getFormat;
220
266
  * @returns {FileObject}
221
267
  */
222
268
  function buildRequestFileObject(fileParam) {
223
- var fileObj;
224
- if (isFileObject(fileParam.data)) {
225
- // For backward compatibility, we allow the data to be a FileObject.
226
- fileObj = { value: fileParam.data.value, options: {} };
227
- if (fileParam.data.options) {
228
- fileObj.options = {
229
- filename: fileParam.filename || fileParam.data.options.filename,
230
- contentType: fileParam.contentType || fileParam.data.options.contentType,
231
- };
232
- }
233
- }
234
- else {
235
- fileObj = {
236
- value: fileParam.data,
237
- options: {
238
- filename: fileParam.filename,
239
- contentType: fileParam.contentType,
240
- },
241
- };
242
- }
243
- // Also for backward compatibility, we allow data to be a string
244
- if (typeof fileObj.value === 'string') {
245
- fileObj.value = Buffer.from(fileObj.value);
246
- }
247
- // build filename
248
- // eslint-disable-next-line prefer-destructuring
249
- var filename = fileObj.options.filename;
250
- if (!filename && isFileStream(fileObj.value)) {
251
- // if readable stream with path property
252
- filename = fileObj.value.path;
253
- }
254
- // toString handles the case when path is a buffer
255
- fileObj.options.filename = filename ? path_1.basename(filename.toString()) : '_';
256
- // build contentType
257
- if (!fileObj.options.contentType && isFileData(fileObj.value)) {
258
- fileObj.options.contentType = getContentType(fileObj.value) || 'application/octet-stream';
259
- }
260
- return fileObj;
269
+ return __awaiter(this, void 0, void 0, function () {
270
+ var fileObj, filename, _a;
271
+ return __generator(this, function (_b) {
272
+ switch (_b.label) {
273
+ case 0:
274
+ if (isFileObject(fileParam.data)) {
275
+ // For backward compatibility, we allow the data to be a FileObject.
276
+ fileObj = { value: fileParam.data.value, options: {} };
277
+ if (fileParam.data.options) {
278
+ fileObj.options = {
279
+ filename: fileParam.filename || fileParam.data.options.filename,
280
+ contentType: fileParam.contentType || fileParam.data.options.contentType,
281
+ };
282
+ }
283
+ }
284
+ else {
285
+ fileObj = {
286
+ value: fileParam.data,
287
+ options: {
288
+ filename: fileParam.filename,
289
+ contentType: fileParam.contentType,
290
+ },
291
+ };
292
+ }
293
+ // Also for backward compatibility, we allow data to be a string
294
+ if (typeof fileObj.value === 'string') {
295
+ fileObj.value = Buffer.from(fileObj.value);
296
+ }
297
+ filename = fileObj.options.filename;
298
+ if (!filename && isFileStream(fileObj.value)) {
299
+ // if readable stream with path property
300
+ filename = fileObj.value.path;
301
+ }
302
+ // toString handles the case when path is a buffer
303
+ fileObj.options.filename = filename ? path_1.basename(filename.toString()) : '_';
304
+ if (!(!fileObj.options.contentType && isFileData(fileObj.value))) return [3 /*break*/, 2];
305
+ _a = fileObj.options;
306
+ return [4 /*yield*/, getContentType(fileObj.value)];
307
+ case 1:
308
+ _a.contentType =
309
+ (_b.sent()) || 'application/octet-stream';
310
+ _b.label = 2;
311
+ case 2: return [2 /*return*/, fileObj];
312
+ }
313
+ });
314
+ });
261
315
  }
262
316
  exports.buildRequestFileObject = buildRequestFileObject;
263
317
  /**
@@ -191,23 +191,32 @@ var RequestWrapper = /** @class */ (function () {
191
191
  // Skip keys with undefined/null values or empty object value
192
192
  values
193
193
  .filter(function (v) { return v != null && !helper_1.isEmptyObject(v); })
194
- .forEach(function (value) {
195
- // Special case of empty file object
196
- if (Object.prototype.hasOwnProperty.call(value, 'contentType') &&
197
- !Object.prototype.hasOwnProperty.call(value, 'data')) {
198
- return;
199
- }
200
- if (helper_1.isFileWithMetadata(value)) {
201
- var fileObj = helper_1.buildRequestFileObject(value);
202
- multipartForm.append(key, fileObj.value, fileObj.options);
203
- }
204
- else {
205
- if (typeof value === 'object' && !helper_1.isFileData(value)) {
206
- value = JSON.stringify(value);
194
+ .forEach(function (value) { return __awaiter(_this, void 0, void 0, function () {
195
+ var fileObj;
196
+ return __generator(this, function (_a) {
197
+ switch (_a.label) {
198
+ case 0:
199
+ // Special case of empty file object
200
+ if (Object.prototype.hasOwnProperty.call(value, 'contentType') &&
201
+ !Object.prototype.hasOwnProperty.call(value, 'data')) {
202
+ return [2 /*return*/];
203
+ }
204
+ if (!helper_1.isFileWithMetadata(value)) return [3 /*break*/, 2];
205
+ return [4 /*yield*/, helper_1.buildRequestFileObject(value)];
206
+ case 1:
207
+ fileObj = _a.sent();
208
+ multipartForm.append(key, fileObj.value, fileObj.options);
209
+ return [3 /*break*/, 3];
210
+ case 2:
211
+ if (typeof value === 'object' && !helper_1.isFileData(value)) {
212
+ value = JSON.stringify(value);
213
+ }
214
+ multipartForm.append(key, value);
215
+ _a.label = 3;
216
+ case 3: return [2 /*return*/];
207
217
  }
208
- multipartForm.append(key, value);
209
- }
210
- });
218
+ });
219
+ }); });
211
220
  });
212
221
  }
213
222
  // Path params
@@ -350,6 +359,12 @@ var RequestWrapper = /** @class */ (function () {
350
359
  retry: 4,
351
360
  retryDelay: 1000,
352
361
  httpMethodsToRetry: ['GET', 'HEAD', 'OPTIONS', 'DELETE', 'PUT', 'POST'],
362
+ // do not retry on 501
363
+ statusCodesToRetry: [
364
+ [429, 429],
365
+ [500, 500],
366
+ [502, 599],
367
+ ],
353
368
  instance: axiosInstance,
354
369
  backoffType: 'exponential',
355
370
  checkRetryAfter: true,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ibm-cloud-sdk-core",
3
- "version": "2.17.13",
3
+ "version": "3.0.0",
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",
@@ -49,7 +49,7 @@
49
49
  "dotenv": "^6.2.0",
50
50
  "expect": "^26.1.0",
51
51
  "extend": "^3.0.2",
52
- "file-type": "^7.7.1",
52
+ "file-type": "16.5.4",
53
53
  "form-data": "^2.3.3",
54
54
  "isstream": "~0.1.2",
55
55
  "jsonwebtoken": "^8.5.1",
@@ -87,7 +87,7 @@
87
87
  "build:api": "api-extractor run --local",
88
88
  "build:md": "api-documenter markdown -i temp --output-folder build/docs",
89
89
  "build:doc": "npm-run-all build:api build:md copy:doc",
90
- "copy:doc": "cp \"temp/ibm-cloud-sdk-core.api.json\" dist/docs",
90
+ "copy:doc": "mkdir -p dist/docs && cp \"temp/ibm-cloud-sdk-core.api.json\" dist/docs",
91
91
  "copy:pkg": "package-json-reducer -s \"config devDependencies directories scripts jestSonar jest\" -o ./dist/package.json package.json",
92
92
  "postversion": "publisher --no-checks --dry-run",
93
93
  "all": "npm-run-all build test lint"
@@ -1639,11 +1639,24 @@
1639
1639
  "kind": "Content",
1640
1640
  "text": "): "
1641
1641
  },
1642
+ {
1643
+ "kind": "Reference",
1644
+ "text": "Promise",
1645
+ "canonicalReference": "!Promise:interface"
1646
+ },
1647
+ {
1648
+ "kind": "Content",
1649
+ "text": "<"
1650
+ },
1642
1651
  {
1643
1652
  "kind": "Reference",
1644
1653
  "text": "FileObject",
1645
1654
  "canonicalReference": "ibm-cloud-sdk-core!FileObject:interface"
1646
1655
  },
1656
+ {
1657
+ "kind": "Content",
1658
+ "text": ">"
1659
+ },
1647
1660
  {
1648
1661
  "kind": "Content",
1649
1662
  "text": ";"
@@ -1651,7 +1664,7 @@
1651
1664
  ],
1652
1665
  "returnTypeTokenRange": {
1653
1666
  "startIndex": 3,
1654
- "endIndex": 4
1667
+ "endIndex": 7
1655
1668
  },
1656
1669
  "releaseTag": "Public",
1657
1670
  "overloadIndex": 1,
@@ -2589,8 +2602,8 @@
2589
2602
  },
2590
2603
  {
2591
2604
  "kind": "Reference",
2592
- "text": "Buffer",
2593
- "canonicalReference": "!Buffer:interface"
2605
+ "text": "FileObject",
2606
+ "canonicalReference": "ibm-cloud-sdk-core!FileObject:interface"
2594
2607
  },
2595
2608
  {
2596
2609
  "kind": "Content",
@@ -2607,8 +2620,8 @@
2607
2620
  },
2608
2621
  {
2609
2622
  "kind": "Reference",
2610
- "text": "FileObject",
2611
- "canonicalReference": "ibm-cloud-sdk-core!FileObject:interface"
2623
+ "text": "Buffer",
2624
+ "canonicalReference": "!Buffer:interface"
2612
2625
  },
2613
2626
  {
2614
2627
  "kind": "Content",
@@ -3185,9 +3198,14 @@
3185
3198
  "kind": "Content",
3186
3199
  "text": "): "
3187
3200
  },
3201
+ {
3202
+ "kind": "Reference",
3203
+ "text": "Promise",
3204
+ "canonicalReference": "!Promise:interface"
3205
+ },
3188
3206
  {
3189
3207
  "kind": "Content",
3190
- "text": "string"
3208
+ "text": "<string>"
3191
3209
  },
3192
3210
  {
3193
3211
  "kind": "Content",
@@ -3196,7 +3214,7 @@
3196
3214
  ],
3197
3215
  "returnTypeTokenRange": {
3198
3216
  "startIndex": 5,
3199
- "endIndex": 6
3217
+ "endIndex": 7
3200
3218
  },
3201
3219
  "releaseTag": "Public",
3202
3220
  "overloadIndex": 1,
@@ -94,7 +94,7 @@ export class BearerTokenAuthenticator extends Authenticator {
94
94
  }
95
95
 
96
96
  // @public
97
- export function buildRequestFileObject(fileParam: FileWithMetadata): FileObject;
97
+ export function buildRequestFileObject(fileParam: FileWithMetadata): Promise<FileObject>;
98
98
 
99
99
  // @public
100
100
  export function checkCredentials(obj: any, credsToCheck: string[]): Error | null;
@@ -155,7 +155,7 @@ export class ContainerTokenManager extends IamRequestBasedTokenManager {
155
155
 
156
156
  // @public (undocumented)
157
157
  export const contentType: {
158
- fromFilename: (file: String | File | Buffer | NodeJS.ReadableStream | FileObject) => string;
158
+ fromFilename: (file: String | File | FileObject | NodeJS.ReadableStream | Buffer) => string;
159
159
  fromHeader: (buffer: Buffer) => string;
160
160
  };
161
161
 
@@ -211,7 +211,7 @@ export interface FileWithMetadata {
211
211
  export function getAuthenticatorFromEnvironment(serviceName: string): Authenticator;
212
212
 
213
213
  // @public
214
- export function getContentType(inputData: NodeJS.ReadableStream | Buffer): string;
214
+ export function getContentType(inputData: NodeJS.ReadableStream | Buffer): Promise<string>;
215
215
 
216
216
  // @public
217
217
  export function getCurrentTime(): number;