ibm-cloud-sdk-core 2.17.15 → 3.1.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.
Files changed (36) hide show
  1. package/CHANGELOG.md +31 -0
  2. package/auth/authenticators/authenticator-interface.d.ts +1 -1
  3. package/auth/authenticators/authenticator.d.ts +1 -1
  4. package/auth/authenticators/basic-authenticator.d.ts +1 -1
  5. package/auth/authenticators/token-request-based-authenticator.d.ts +1 -1
  6. package/build/docs/ibm-cloud-sdk-core.authenticator.authenticate.md +2 -2
  7. package/build/docs/ibm-cloud-sdk-core.authenticatorinterface.authenticate.md +2 -2
  8. package/build/docs/ibm-cloud-sdk-core.baseservice.md +1 -0
  9. package/build/docs/ibm-cloud-sdk-core.baseservice.setdefaultheaders.md +24 -0
  10. package/build/docs/ibm-cloud-sdk-core.basicauthenticator.authenticate.md +2 -2
  11. package/build/docs/ibm-cloud-sdk-core.buildrequestfileobject.md +2 -2
  12. package/build/docs/ibm-cloud-sdk-core.contenttype.md +1 -1
  13. package/build/docs/ibm-cloud-sdk-core.getcontenttype.md +2 -2
  14. package/build/docs/ibm-cloud-sdk-core.tokenrequestbasedauthenticator.authenticate.md +2 -2
  15. package/docs/ibm-cloud-sdk-core.api.json +80 -51
  16. package/es/auth/authenticators/authenticator-interface.d.ts +1 -1
  17. package/es/auth/authenticators/authenticator.d.ts +1 -1
  18. package/es/auth/authenticators/basic-authenticator.d.ts +1 -1
  19. package/es/auth/authenticators/token-request-based-authenticator.d.ts +1 -1
  20. package/es/lib/base-service.d.ts +6 -0
  21. package/es/lib/base-service.js +12 -0
  22. package/es/lib/content-type.d.ts +1 -1
  23. package/es/lib/helper.d.ts +2 -2
  24. package/es/lib/helper.js +62 -48
  25. package/es/lib/request-wrapper.js +3 -3
  26. package/etc/ibm-cloud-sdk-core.api.md +8 -7
  27. package/ibm-cloud-sdk-core.d.ts +13 -7
  28. package/lib/base-service.d.ts +6 -0
  29. package/lib/base-service.js +12 -0
  30. package/lib/content-type.d.ts +1 -1
  31. package/lib/helper.d.ts +2 -2
  32. package/lib/helper.js +104 -50
  33. package/lib/request-wrapper.js +25 -16
  34. package/package.json +2 -2
  35. package/temp/ibm-cloud-sdk-core.api.json +80 -51
  36. package/temp/ibm-cloud-sdk-core.api.md +8 -7
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
@@ -21,7 +21,7 @@ export function atMostOne(a: any, b: any): boolean;
21
21
  export class Authenticator implements AuthenticatorInterface {
22
22
  constructor();
23
23
  // Warning: (ae-forgotten-export) The symbol "AuthenticateOptions" needs to be exported by the entry point index.d.ts
24
- authenticate(requestOptions: AuthenticateOptions): Promise<void | Error>;
24
+ authenticate(requestOptions: AuthenticateOptions): Promise<void>;
25
25
  authenticationType(): string;
26
26
  static AUTHTYPE_BASIC: string;
27
27
  // (undocumented)
@@ -42,7 +42,7 @@ export class Authenticator implements AuthenticatorInterface {
42
42
 
43
43
  // @public
44
44
  export interface AuthenticatorInterface {
45
- authenticate(requestOptions: AuthenticateOptions): Promise<void | Error>;
45
+ authenticate(requestOptions: AuthenticateOptions): Promise<void>;
46
46
  authenticationType(): string;
47
47
  }
48
48
 
@@ -64,6 +64,7 @@ export class BaseService {
64
64
  enableRetries(retryOptions?: RetryOptions): void;
65
65
  getAuthenticator(): any;
66
66
  getHttpClient(): AxiosInstance;
67
+ setDefaultHeaders(headers: OutgoingHttpHeaders): void;
67
68
  setEnableGzipCompression(setting: boolean): void;
68
69
  setServiceUrl(url: string): void;
69
70
  }
@@ -72,7 +73,7 @@ export class BaseService {
72
73
  export class BasicAuthenticator extends Authenticator {
73
74
  // Warning: (ae-forgotten-export) The symbol "Options" needs to be exported by the entry point index.d.ts
74
75
  constructor(options: Options);
75
- authenticate(requestOptions: AuthenticateOptions): Promise<void | Error>;
76
+ authenticate(requestOptions: AuthenticateOptions): Promise<void>;
76
77
  authenticationType(): string;
77
78
  // (undocumented)
78
79
  protected authHeader: {
@@ -94,7 +95,7 @@ export class BearerTokenAuthenticator extends Authenticator {
94
95
  }
95
96
 
96
97
  // @public
97
- export function buildRequestFileObject(fileParam: FileWithMetadata): FileObject;
98
+ export function buildRequestFileObject(fileParam: FileWithMetadata): Promise<FileObject>;
98
99
 
99
100
  // @public
100
101
  export function checkCredentials(obj: any, credsToCheck: string[]): Error | null;
@@ -155,7 +156,7 @@ export class ContainerTokenManager extends IamRequestBasedTokenManager {
155
156
 
156
157
  // @public (undocumented)
157
158
  export const contentType: {
158
- fromFilename: (file: String | File | Buffer | NodeJS.ReadableStream | FileObject) => string;
159
+ fromFilename: (file: String | File | FileObject | NodeJS.ReadableStream | Buffer) => string;
159
160
  fromHeader: (buffer: Buffer) => string;
160
161
  };
161
162
 
@@ -211,7 +212,7 @@ export interface FileWithMetadata {
211
212
  export function getAuthenticatorFromEnvironment(serviceName: string): Authenticator;
212
213
 
213
214
  // @public
214
- export function getContentType(inputData: NodeJS.ReadableStream | Buffer): string;
215
+ export function getContentType(inputData: NodeJS.ReadableStream | Buffer): Promise<string>;
215
216
 
216
217
  // @public
217
218
  export function getCurrentTime(): number;
@@ -390,7 +391,7 @@ export type TokenManagerOptions = {
390
391
  export class TokenRequestBasedAuthenticator extends Authenticator {
391
392
  // Warning: (ae-forgotten-export) The symbol "BaseOptions" needs to be exported by the entry point index.d.ts
392
393
  constructor(options: BaseOptions);
393
- authenticate(requestOptions: AuthenticateOptions): Promise<void | Error>;
394
+ authenticate(requestOptions: AuthenticateOptions): Promise<void>;
394
395
  // (undocumented)
395
396
  protected disableSslVerification: boolean;
396
397
  // (undocumented)
@@ -71,7 +71,7 @@ export declare class Authenticator implements AuthenticatorInterface {
71
71
  * @throws {Error} - The authenticate method was not implemented by a
72
72
  * subclass.
73
73
  */
74
- authenticate(requestOptions: AuthenticateOptions): Promise<void | Error>;
74
+ authenticate(requestOptions: AuthenticateOptions): Promise<void>;
75
75
  /**
76
76
  * Retrieves the authenticator's type.
77
77
  * The returned value will be the same string that is used
@@ -98,7 +98,7 @@ export declare interface AuthenticatorInterface {
98
98
  * @param {object.<string, string>} requestOptions.headers The headers the
99
99
  * authentication information will be added to.
100
100
  */
101
- authenticate(requestOptions: AuthenticateOptions): Promise<void | Error>;
101
+ authenticate(requestOptions: AuthenticateOptions): Promise<void>;
102
102
  /**
103
103
  * Returns a string that indicates the authentication type.
104
104
  */
@@ -158,6 +158,12 @@ export declare class BaseService {
158
158
  * @param {string} url The base URL for the service.
159
159
  */
160
160
  setServiceUrl(url: string): void;
161
+ /**
162
+ * Set the HTTP headers to be sent in every request.
163
+ *
164
+ * @param {OutgoingHttpHeaders} headers The map of headers to include in requests.
165
+ */
166
+ setDefaultHeaders(headers: OutgoingHttpHeaders): void;
161
167
  /**
162
168
  * Turn request body compression on or off.
163
169
  *
@@ -255,7 +261,7 @@ export declare class BaseService {
255
261
  * @param {object.<string, string>} requestOptions.headers - The headers the
256
262
  * authentication information will be added too.
257
263
  */
258
- authenticate(requestOptions: AuthenticateOptions): Promise<void | Error>;
264
+ authenticate(requestOptions: AuthenticateOptions): Promise<void>;
259
265
  /**
260
266
  * Returns the authenticator's type ('basic').
261
267
  *
@@ -319,7 +325,7 @@ export declare class BaseService {
319
325
  * @param {string} fileParam.contentType The content type of the file.
320
326
  * @returns {FileObject}
321
327
  */
322
- export declare function buildRequestFileObject(fileParam: FileWithMetadata): FileObject;
328
+ export declare function buildRequestFileObject(fileParam: FileWithMetadata): Promise<FileObject>;
323
329
 
324
330
  /**
325
331
  * Checks credentials for common user mistakes of copying {, }, or " characters from the documentation
@@ -584,7 +590,7 @@ export declare class BaseService {
584
590
  }
585
591
 
586
592
  export declare const contentType: {
587
- fromFilename: (file: String | File | Buffer | NodeJS.ReadableStream | FileObject) => string;
593
+ fromFilename: (file: String | File | FileObject | NodeJS.ReadableStream | Buffer) => string;
588
594
  fromHeader: (buffer: Buffer) => string;
589
595
  };
590
596
 
@@ -683,7 +689,7 @@ export declare class BaseService {
683
689
  * @param {NodeJS.ReadableStream|Buffer} inputData - The data to retrieve content type for.
684
690
  * @returns {string} the content type of the input.
685
691
  */
686
- export declare function getContentType(inputData: NodeJS.ReadableStream | Buffer): string;
692
+ export declare function getContentType(inputData: NodeJS.ReadableStream | Buffer): Promise<string>;
687
693
 
688
694
  /**
689
695
  * Get the current time
@@ -1411,7 +1417,7 @@ export declare class BaseService {
1411
1417
  * authentication information will be added too. Overrides default headers
1412
1418
  * where there's conflict.
1413
1419
  */
1414
- authenticate(requestOptions: AuthenticateOptions): Promise<void | Error>;
1420
+ authenticate(requestOptions: AuthenticateOptions): Promise<void>;
1415
1421
  }
1416
1422
 
1417
1423
  /**
@@ -87,6 +87,12 @@ export declare class BaseService {
87
87
  * @param {string} url The base URL for the service.
88
88
  */
89
89
  setServiceUrl(url: string): void;
90
+ /**
91
+ * Set the HTTP headers to be sent in every request.
92
+ *
93
+ * @param {OutgoingHttpHeaders} headers The map of headers to include in requests.
94
+ */
95
+ setDefaultHeaders(headers: OutgoingHttpHeaders): void;
90
96
  /**
91
97
  * Turn request body compression on or off.
92
98
  *
@@ -106,6 +106,18 @@ var BaseService = /** @class */ (function () {
106
106
  this.baseOptions.serviceUrl = helper_1.stripTrailingSlash(url);
107
107
  }
108
108
  };
109
+ /**
110
+ * Set the HTTP headers to be sent in every request.
111
+ *
112
+ * @param {OutgoingHttpHeaders} headers The map of headers to include in requests.
113
+ */
114
+ BaseService.prototype.setDefaultHeaders = function (headers) {
115
+ if (typeof headers !== 'object') {
116
+ // do nothing, for now
117
+ return;
118
+ }
119
+ this.baseOptions.headers = headers;
120
+ };
109
121
  /**
110
122
  * Turn request body compression on or off.
111
123
  *
@@ -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
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ibm-cloud-sdk-core",
3
- "version": "2.17.15",
3
+ "version": "3.1.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",