@dynatrace-sdk/http-client 1.1.1 → 1.3.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 +19 -0
- package/cjs/index.js +269 -26
- package/docs/DOCS.md +206 -180
- package/esm/index.js +269 -26
- package/package.json +1 -1
- package/types/packages/http-client/src/lib/platform/abort-controller.d.ts +1 -1
- package/types/packages/http-client/src/lib/platform/abort-signal.d.ts +1 -1
- package/types/packages/http-client/src/lib/platform/decode-form-data.d.ts +48 -0
- package/types/packages/http-client/src/lib/platform/http-client.d.ts +19 -0
- package/types/packages/http-client/src/lib/types/abort-signal.d.ts +4 -1
- package/types/packages/http-client/src/lib/types/data-types.d.ts +1 -1
- package/types/packages/http-client/src/lib/types/form-data-request-body.d.ts +2 -2
- package/types/packages/http-client/src/lib/types/form-data-response-body.d.ts +2 -2
- package/types/packages/http-client/src/lib/types/request-body-types.d.ts +1 -1
- package/types/packages/http-client/src/lib/types/response-body-types.d.ts +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,25 @@
|
|
|
2
2
|
|
|
3
3
|
@dynatrace-sdk/http-client
|
|
4
4
|
|
|
5
|
+
## 1.3.0
|
|
6
|
+
|
|
7
|
+
### Minor Changes
|
|
8
|
+
|
|
9
|
+
- Added support for native AbortSignal, existing implementation marked as deprecated. APPDEV-8680
|
|
10
|
+
|
|
11
|
+
## 1.2.1
|
|
12
|
+
|
|
13
|
+
### Patch Changes
|
|
14
|
+
|
|
15
|
+
- Fix multipart/form-data deserialization in Safari. APPDEV-8496
|
|
16
|
+
|
|
17
|
+
## 1.2.0
|
|
18
|
+
|
|
19
|
+
### Minor Changes
|
|
20
|
+
|
|
21
|
+
- Allow configuring the base URL of the http-client. APPDEV-8174
|
|
22
|
+
- Allow setting the default headers for the http-client. APPDEV-8175
|
|
23
|
+
|
|
5
24
|
## 1.1.1
|
|
6
25
|
|
|
7
26
|
### Patch Changes
|
package/cjs/index.js
CHANGED
|
@@ -114,19 +114,19 @@ function getImpl(data, type) {
|
|
|
114
114
|
}
|
|
115
115
|
throw new UnsupportedOperationError(`Unsupported binary type: ${type}.`);
|
|
116
116
|
}
|
|
117
|
-
var Binary = class {
|
|
117
|
+
var Binary = class _Binary {
|
|
118
118
|
constructor(data) {
|
|
119
119
|
this.data = data;
|
|
120
120
|
}
|
|
121
121
|
static fromText(text) {
|
|
122
|
-
return new
|
|
122
|
+
return new _Binary(new Blob([text]));
|
|
123
123
|
}
|
|
124
124
|
static fromJson(json) {
|
|
125
|
-
return new
|
|
125
|
+
return new _Binary(new Blob([JSON.stringify(json)]));
|
|
126
126
|
}
|
|
127
127
|
static from(data) {
|
|
128
128
|
if (data instanceof Blob || data instanceof ArrayBuffer || data instanceof ReadableStream) {
|
|
129
|
-
return new
|
|
129
|
+
return new _Binary(data);
|
|
130
130
|
} else {
|
|
131
131
|
throw new UnsupportedOperationError("Unsupported binary type.");
|
|
132
132
|
}
|
|
@@ -298,6 +298,190 @@ var HttpClientRequestError = class extends BaseError {
|
|
|
298
298
|
name = "RequestError";
|
|
299
299
|
};
|
|
300
300
|
|
|
301
|
+
// packages/http-client/src/lib/platform/decode-form-data.ts
|
|
302
|
+
function byteToChar(byte) {
|
|
303
|
+
if (byte === void 0)
|
|
304
|
+
return void 0;
|
|
305
|
+
return String.fromCharCode(byte);
|
|
306
|
+
}
|
|
307
|
+
function isTextContentType(contentType) {
|
|
308
|
+
return ["application/json", "text/plain"].includes(String(contentType).toLowerCase());
|
|
309
|
+
}
|
|
310
|
+
function removeEscaping(value) {
|
|
311
|
+
if (!value)
|
|
312
|
+
return value;
|
|
313
|
+
return JSON.parse(`"${value}"`);
|
|
314
|
+
}
|
|
315
|
+
function parseContentDisposition(contentDisposition) {
|
|
316
|
+
const fileName = removeEscaping(
|
|
317
|
+
/form-data;.*filename="((?:[^"\\]|\\.)*)"/gm.exec(contentDisposition)?.[1]
|
|
318
|
+
);
|
|
319
|
+
const name = removeEscaping(
|
|
320
|
+
/form-data;.*(?<!file)name="((?:[^"\\]|\\.)*)"/gm.exec(contentDisposition)?.[1]
|
|
321
|
+
);
|
|
322
|
+
return { name, fileName };
|
|
323
|
+
}
|
|
324
|
+
function parseFormDataBoundary(contentType) {
|
|
325
|
+
return /multipart\/form-data;boundary=(.*)/gm.exec(contentType)?.[1];
|
|
326
|
+
}
|
|
327
|
+
var FormDataParser = class {
|
|
328
|
+
constructor(sourceArray, boundary) {
|
|
329
|
+
this.sourceArray = sourceArray;
|
|
330
|
+
this.boundary = boundary;
|
|
331
|
+
}
|
|
332
|
+
currentChar = null;
|
|
333
|
+
currentByte = null;
|
|
334
|
+
offset = 0;
|
|
335
|
+
utf8Decoder = new TextDecoder("utf-8");
|
|
336
|
+
get previousByte() {
|
|
337
|
+
return this.sourceArray[this.offset - 2];
|
|
338
|
+
}
|
|
339
|
+
get previousChar() {
|
|
340
|
+
return byteToChar(this.previousByte);
|
|
341
|
+
}
|
|
342
|
+
next() {
|
|
343
|
+
if (this.offset >= this.sourceArray.byteLength) {
|
|
344
|
+
this.currentChar = null;
|
|
345
|
+
this.currentByte = null;
|
|
346
|
+
return false;
|
|
347
|
+
}
|
|
348
|
+
this.currentByte = this.sourceArray[this.offset];
|
|
349
|
+
this.currentChar = String.fromCharCode(this.currentByte);
|
|
350
|
+
this.offset++;
|
|
351
|
+
return true;
|
|
352
|
+
}
|
|
353
|
+
charAt(offset) {
|
|
354
|
+
return byteToChar(this.sourceArray[offset]) ?? null;
|
|
355
|
+
}
|
|
356
|
+
nextChar() {
|
|
357
|
+
this.next();
|
|
358
|
+
return this.currentChar;
|
|
359
|
+
}
|
|
360
|
+
nextByte() {
|
|
361
|
+
this.next();
|
|
362
|
+
return this.currentByte;
|
|
363
|
+
}
|
|
364
|
+
get nextTwoChars() {
|
|
365
|
+
return String.fromCharCode(this.sourceArray[this.offset], this.sourceArray[this.offset + 1]);
|
|
366
|
+
}
|
|
367
|
+
findNextBoundaryOffset(startOffset = this.offset) {
|
|
368
|
+
let boundaryIndex = 0;
|
|
369
|
+
let boundaryMatch = false;
|
|
370
|
+
let offset = startOffset;
|
|
371
|
+
let currentChar = this.charAt(offset);
|
|
372
|
+
while (!boundaryMatch) {
|
|
373
|
+
if (currentChar === null) {
|
|
374
|
+
return null;
|
|
375
|
+
}
|
|
376
|
+
if (currentChar === this.boundary[boundaryIndex]) {
|
|
377
|
+
boundaryIndex++;
|
|
378
|
+
if (boundaryIndex === this.boundary.length) {
|
|
379
|
+
boundaryMatch = true;
|
|
380
|
+
}
|
|
381
|
+
} else {
|
|
382
|
+
boundaryIndex = 0;
|
|
383
|
+
}
|
|
384
|
+
offset++;
|
|
385
|
+
currentChar = this.charAt(offset);
|
|
386
|
+
}
|
|
387
|
+
return offset - this.boundary.length;
|
|
388
|
+
}
|
|
389
|
+
skipPastNextBoundary() {
|
|
390
|
+
const nextBoundaryOffset = this.findNextBoundaryOffset();
|
|
391
|
+
if (nextBoundaryOffset === null) {
|
|
392
|
+
return false;
|
|
393
|
+
}
|
|
394
|
+
this.offset = nextBoundaryOffset + this.boundary.length;
|
|
395
|
+
this.currentByte = this.sourceArray[this.offset];
|
|
396
|
+
this.currentChar = String.fromCharCode(this.currentByte);
|
|
397
|
+
const twoChars = this.nextTwoChars;
|
|
398
|
+
if (twoChars == "--") {
|
|
399
|
+
return false;
|
|
400
|
+
} else if (twoChars == "\r\n") {
|
|
401
|
+
this.offset++;
|
|
402
|
+
this.next();
|
|
403
|
+
}
|
|
404
|
+
return true;
|
|
405
|
+
}
|
|
406
|
+
readLine() {
|
|
407
|
+
const lineBuffer = [];
|
|
408
|
+
do {
|
|
409
|
+
const byte = this.nextByte();
|
|
410
|
+
if (byte === null) {
|
|
411
|
+
return null;
|
|
412
|
+
}
|
|
413
|
+
lineBuffer.push(byte);
|
|
414
|
+
} while (this.currentChar !== null && this.currentChar !== "\n" && this.previousChar !== "\r");
|
|
415
|
+
return Uint8Array.from(lineBuffer);
|
|
416
|
+
}
|
|
417
|
+
readSectionHeaders() {
|
|
418
|
+
const headers = {};
|
|
419
|
+
const headerStartOffset = this.offset;
|
|
420
|
+
let isEndOfHeaders = false;
|
|
421
|
+
while (!isEndOfHeaders) {
|
|
422
|
+
const line = this.readLine();
|
|
423
|
+
if (line === null) {
|
|
424
|
+
throw TypeError("Unexpected end of response while parsing headers");
|
|
425
|
+
}
|
|
426
|
+
const headerString = this.utf8Decoder.decode(line);
|
|
427
|
+
if (headerString === "\r\n") {
|
|
428
|
+
isEndOfHeaders = true;
|
|
429
|
+
break;
|
|
430
|
+
}
|
|
431
|
+
const [header, value] = headerString.split(":").map((part) => (part ?? "").trim());
|
|
432
|
+
headers[header.toLowerCase()] = value;
|
|
433
|
+
}
|
|
434
|
+
const headersLength = this.offset - headerStartOffset;
|
|
435
|
+
return {
|
|
436
|
+
headersLength,
|
|
437
|
+
headers
|
|
438
|
+
};
|
|
439
|
+
}
|
|
440
|
+
extractData() {
|
|
441
|
+
const nextBoundaryOffset = this.findNextBoundaryOffset() ?? this.sourceArray.length;
|
|
442
|
+
const lastTwoBytes = String.fromCharCode(
|
|
443
|
+
this.sourceArray[nextBoundaryOffset - 2],
|
|
444
|
+
this.sourceArray[nextBoundaryOffset - 1]
|
|
445
|
+
);
|
|
446
|
+
const endOfDataSection = lastTwoBytes === "\r\n" ? nextBoundaryOffset - 2 : nextBoundaryOffset;
|
|
447
|
+
const data = this.sourceArray.slice(this.offset, endOfDataSection);
|
|
448
|
+
return {
|
|
449
|
+
payloadStartOffset: this.offset,
|
|
450
|
+
payloadEndOffset: endOfDataSection,
|
|
451
|
+
data
|
|
452
|
+
};
|
|
453
|
+
}
|
|
454
|
+
parse() {
|
|
455
|
+
const sections = [];
|
|
456
|
+
while (this.skipPastNextBoundary()) {
|
|
457
|
+
const headersStartOffset = this.offset;
|
|
458
|
+
const { headers } = this.readSectionHeaders();
|
|
459
|
+
const { data, payloadStartOffset, payloadEndOffset } = this.extractData();
|
|
460
|
+
const isText = isTextContentType(headers["content-type"]);
|
|
461
|
+
const { name, fileName } = parseContentDisposition(headers["content-disposition"]);
|
|
462
|
+
sections.push({
|
|
463
|
+
name,
|
|
464
|
+
text: isText ? this.utf8Decoder.decode(data) : void 0,
|
|
465
|
+
file: !isText ? new File([data], fileName ?? "", {
|
|
466
|
+
type: headers["content-type"] ?? "application/octet-stream"
|
|
467
|
+
}) : void 0,
|
|
468
|
+
rawData: data,
|
|
469
|
+
headers,
|
|
470
|
+
headersStartOffset,
|
|
471
|
+
payloadStartOffset,
|
|
472
|
+
payloadEndOffset
|
|
473
|
+
});
|
|
474
|
+
}
|
|
475
|
+
return sections;
|
|
476
|
+
}
|
|
477
|
+
};
|
|
478
|
+
function isAppleBrowser() {
|
|
479
|
+
return /apple/i.test(navigator.vendor);
|
|
480
|
+
}
|
|
481
|
+
function parseMultipartFormDataResponse(response, boundary) {
|
|
482
|
+
return new FormDataParser(response, boundary).parse();
|
|
483
|
+
}
|
|
484
|
+
|
|
301
485
|
// packages/http-client/src/lib/platform/decode-response-body.ts
|
|
302
486
|
function emptyReadableStream() {
|
|
303
487
|
return new ReadableStream({
|
|
@@ -319,25 +503,62 @@ async function decodeFormDataResponseBody(response) {
|
|
|
319
503
|
if (response.body === null) {
|
|
320
504
|
return [];
|
|
321
505
|
} else {
|
|
322
|
-
const formData = await response.formData();
|
|
323
506
|
const result = [];
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
507
|
+
if (isAppleBrowser()) {
|
|
508
|
+
const responseClone = await response.clone();
|
|
509
|
+
const responseBinaryData = new Uint8Array(await responseClone.arrayBuffer());
|
|
510
|
+
const boundary = parseFormDataBoundary(responseClone.headers.get("content-type"));
|
|
511
|
+
const manuallyParsedFormData = parseMultipartFormDataResponse(
|
|
512
|
+
responseBinaryData,
|
|
513
|
+
`--${boundary}`
|
|
514
|
+
);
|
|
515
|
+
const automaticallyParsedFormData = await response.formData();
|
|
516
|
+
automaticallyParsedFormData.forEach((value, key) => {
|
|
517
|
+
const manuallyParsedSection = manuallyParsedFormData.find(({ name }) => name === key);
|
|
518
|
+
if (typeof value === "string" && manuallyParsedSection?.file !== void 0) {
|
|
519
|
+
result.push({
|
|
520
|
+
type: "binary",
|
|
521
|
+
name: key,
|
|
522
|
+
contentType: manuallyParsedSection.headers["content-type"],
|
|
523
|
+
filename: manuallyParsedSection.file.name,
|
|
524
|
+
value: Binary.from(manuallyParsedSection.file)
|
|
525
|
+
});
|
|
526
|
+
} else if (value instanceof File) {
|
|
527
|
+
result.push({
|
|
528
|
+
type: "binary",
|
|
529
|
+
name: key,
|
|
530
|
+
contentType: value.type,
|
|
531
|
+
filename: value.name,
|
|
532
|
+
value: Binary.from(value)
|
|
533
|
+
});
|
|
534
|
+
} else {
|
|
535
|
+
result.push({
|
|
536
|
+
type: "text",
|
|
537
|
+
name: key,
|
|
538
|
+
value
|
|
539
|
+
});
|
|
540
|
+
}
|
|
541
|
+
});
|
|
542
|
+
} else {
|
|
543
|
+
const formData = await response.formData();
|
|
544
|
+
formData.forEach((value, key) => {
|
|
545
|
+
if (value instanceof File) {
|
|
546
|
+
result.push({
|
|
547
|
+
type: "binary",
|
|
548
|
+
name: key,
|
|
549
|
+
contentType: value.type,
|
|
550
|
+
filename: value.name,
|
|
551
|
+
value: Binary.from(value)
|
|
552
|
+
});
|
|
553
|
+
} else {
|
|
554
|
+
result.push({
|
|
555
|
+
type: "text",
|
|
556
|
+
name: key,
|
|
557
|
+
value
|
|
558
|
+
});
|
|
559
|
+
}
|
|
560
|
+
});
|
|
561
|
+
}
|
|
341
562
|
return result;
|
|
342
563
|
}
|
|
343
564
|
}
|
|
@@ -544,13 +765,35 @@ var send = async (info, init) => {
|
|
|
544
765
|
}
|
|
545
766
|
};
|
|
546
767
|
var HttpClient = class {
|
|
768
|
+
_baseUrl;
|
|
769
|
+
_defaultHeaders;
|
|
770
|
+
constructor(options) {
|
|
771
|
+
this._baseUrl = options?.baseUrl;
|
|
772
|
+
this._defaultHeaders = options?.defaultHeaders;
|
|
773
|
+
}
|
|
774
|
+
/**
|
|
775
|
+
* Sets HTTP Client Instance baseUrl param to the incoming value `baseUrl`.
|
|
776
|
+
* @param baseUrl Base URL used in the request path.
|
|
777
|
+
*/
|
|
778
|
+
_setBaseURL(baseUrl) {
|
|
779
|
+
this._baseUrl = baseUrl;
|
|
780
|
+
}
|
|
781
|
+
/**
|
|
782
|
+
* Sets HTTP Client Instance defaultHeaders param to the incoming value `defaultHeaders`.
|
|
783
|
+
* @param defaultHeaders HTTP Headers to be used in outgoing requests.
|
|
784
|
+
*/
|
|
785
|
+
_setDefaultHeaders(defaultHeaders) {
|
|
786
|
+
this._defaultHeaders = defaultHeaders;
|
|
787
|
+
}
|
|
547
788
|
async send(options) {
|
|
548
789
|
const { statusValidator = defaultStatusValidator } = options;
|
|
549
790
|
const requestBodyType = options.requestBodyType ?? "json";
|
|
550
791
|
const body = options.body !== void 0 ? await encodeRequestBody(options.body, requestBodyType) : void 0;
|
|
551
|
-
const
|
|
792
|
+
const url = this._baseUrl ? new URL(options.url, this._baseUrl).toString() : options.url;
|
|
793
|
+
const requestHeaders = { ...this._defaultHeaders, ...options?.headers };
|
|
794
|
+
const response = await send(url, {
|
|
552
795
|
method: options.method,
|
|
553
|
-
headers: applyContentTypeHeader(
|
|
796
|
+
headers: applyContentTypeHeader(requestHeaders, requestBodyType),
|
|
554
797
|
signal: options.abortSignal,
|
|
555
798
|
body
|
|
556
799
|
});
|
|
@@ -574,7 +817,7 @@ var OAuthToken = class {
|
|
|
574
817
|
this._scope = oauthTokenResponse.scope;
|
|
575
818
|
this._tokenType = oauthTokenResponse.token_type;
|
|
576
819
|
this._expirationDate = ((expiresIn) => {
|
|
577
|
-
const date = new Date();
|
|
820
|
+
const date = /* @__PURE__ */ new Date();
|
|
578
821
|
date.setSeconds(date.getSeconds() + expiresIn);
|
|
579
822
|
return date;
|
|
580
823
|
})(oauthTokenResponse.expires_in);
|
|
@@ -587,7 +830,7 @@ var OAuthToken = class {
|
|
|
587
830
|
return this._tokenType;
|
|
588
831
|
}
|
|
589
832
|
isExpired() {
|
|
590
|
-
return this._expirationDate <= new Date();
|
|
833
|
+
return this._expirationDate <= /* @__PURE__ */ new Date();
|
|
591
834
|
}
|
|
592
835
|
get accessToken() {
|
|
593
836
|
return this._accessToken;
|