@dynatrace-sdk/http-client 0.4.4

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 (47) hide show
  1. package/CHANGELOG.md +77 -0
  2. package/README.md +3 -0
  3. package/cjs/index.js +455 -0
  4. package/docs/DOCS.md +1813 -0
  5. package/dynatrace-metadata.json +5 -0
  6. package/esm/index.js +435 -0
  7. package/package.json +10 -0
  8. package/types/index.d.ts +1 -0
  9. package/types/packages/http-client/src/index.d.ts +2 -0
  10. package/types/packages/http-client/src/lib/platform/abort-controller.d.ts +6 -0
  11. package/types/packages/http-client/src/lib/platform/abort-signal.d.ts +6 -0
  12. package/types/packages/http-client/src/lib/platform/apply-content-type-header.d.ts +2 -0
  13. package/types/packages/http-client/src/lib/platform/base-error.d.ts +5 -0
  14. package/types/packages/http-client/src/lib/platform/binary.d.ts +13 -0
  15. package/types/packages/http-client/src/lib/platform/data-type-error.d.ts +6 -0
  16. package/types/packages/http-client/src/lib/platform/decode-response-body.d.ts +3 -0
  17. package/types/packages/http-client/src/lib/platform/default-status-validator.d.ts +2 -0
  18. package/types/packages/http-client/src/lib/platform/encode-request-body.d.ts +2 -0
  19. package/types/packages/http-client/src/lib/platform/http-client-abort-error.d.ts +7 -0
  20. package/types/packages/http-client/src/lib/platform/http-client-request-error.d.ts +7 -0
  21. package/types/packages/http-client/src/lib/platform/http-client-response-error-serializer.d.ts +1 -0
  22. package/types/packages/http-client/src/lib/platform/http-client-response-error.d.ts +8 -0
  23. package/types/packages/http-client/src/lib/platform/http-client-response.d.ts +10 -0
  24. package/types/packages/http-client/src/lib/platform/http-client.d.ts +5 -0
  25. package/types/packages/http-client/src/lib/platform/index.d.ts +11 -0
  26. package/types/packages/http-client/src/lib/platform/unsupported-operation-error.d.ts +6 -0
  27. package/types/packages/http-client/src/lib/types/abort-controller.d.ts +14 -0
  28. package/types/packages/http-client/src/lib/types/abort-signal.d.ts +15 -0
  29. package/types/packages/http-client/src/lib/types/base-error.d.ts +4 -0
  30. package/types/packages/http-client/src/lib/types/binary.d.ts +15 -0
  31. package/types/packages/http-client/src/lib/types/data-type-error.d.ts +14 -0
  32. package/types/packages/http-client/src/lib/types/data-types.d.ts +36 -0
  33. package/types/packages/http-client/src/lib/types/form-data-request-body.d.ts +83 -0
  34. package/types/packages/http-client/src/lib/types/form-data-response-body.d.ts +15 -0
  35. package/types/packages/http-client/src/lib/types/headers.d.ts +6 -0
  36. package/types/packages/http-client/src/lib/types/http-client-abort-error.d.ts +14 -0
  37. package/types/packages/http-client/src/lib/types/http-client-network-error.d.ts +14 -0
  38. package/types/packages/http-client/src/lib/types/http-client-request-error.d.ts +14 -0
  39. package/types/packages/http-client/src/lib/types/http-client-request-options.d.ts +42 -0
  40. package/types/packages/http-client/src/lib/types/http-client-response-error.d.ts +19 -0
  41. package/types/packages/http-client/src/lib/types/http-client-response.d.ts +33 -0
  42. package/types/packages/http-client/src/lib/types/http-client.d.ts +24 -0
  43. package/types/packages/http-client/src/lib/types/index.d.ts +20 -0
  44. package/types/packages/http-client/src/lib/types/request-body-types.d.ts +26 -0
  45. package/types/packages/http-client/src/lib/types/response-body-types.d.ts +46 -0
  46. package/types/packages/http-client/src/lib/types/status-validator.d.ts +7 -0
  47. package/types/packages/http-client/src/lib/types/unsupported-operation-error.d.ts +14 -0
package/CHANGELOG.md ADDED
@@ -0,0 +1,77 @@
1
+ # http-client
2
+
3
+ @dynatrace-sdk/http-client
4
+
5
+ ## 0.4.4
6
+
7
+ ### Patch Changes
8
+
9
+ - Convey Blob.type to Content-Type header for form field.
10
+
11
+ ## 0.4.3
12
+
13
+ ### Patch Changes
14
+
15
+ - Adjust autogenerated docs to the latest template view.
16
+
17
+ ## 0.4.2
18
+
19
+ ### Patch Changes
20
+
21
+ - Fix aborted fetch causing throwing "DataTypeError".
22
+
23
+ ## 0.4.1
24
+
25
+ ### Patch Changes
26
+
27
+ - Add File type support to FormData request body.
28
+ - Fixed typings of PlatformAbortController and PlatformAbortSignal
29
+
30
+ ## 0.4.0
31
+
32
+ ### Minor Changes
33
+
34
+ - Adjust AbortSignal typing to current TypeScript version (4.7.4).
35
+
36
+ ## 0.3.1
37
+
38
+ ### Patch Changes
39
+
40
+ - Add markdown documentation to each package inside `/docs` folder
41
+
42
+ ## 0.3.0
43
+
44
+ ### Minor Changes
45
+
46
+ - Expose both ESM and CJS modules.
47
+
48
+ ## 0.2.1
49
+
50
+ ### Patch Changes
51
+
52
+ - Change namespace of http-client to @dynatrace-sdk.
53
+
54
+ ## 0.2.0
55
+
56
+ ### Minor Changes
57
+
58
+ - Fix for sending proper payload contents when Binary datatype is passed.
59
+
60
+ ## 0.1.2
61
+
62
+ ### Patch Changes
63
+
64
+ - Bump sdk-web to version 0.5.12 to support http-client errors in app-shell.
65
+
66
+ ## 0.1.1
67
+
68
+ ### Patch Changes
69
+
70
+ - Add content-type header based on request body type.
71
+ - Add global serializer for http errors.
72
+
73
+ ## 0.1.0
74
+
75
+ ### Minor Changes
76
+
77
+ - Added HttpClient implementation.
package/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # @dynatrace-sdk/http-client
2
+
3
+ An environment agnostic (browser/deno vs nodejs) abstraction layer for making HTTP calls.
package/cjs/index.js ADDED
@@ -0,0 +1,455 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // packages/http-client/src/index.ts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ PlatformAbortController: () => AbortController,
24
+ PlatformAbortSignal: () => AbortSignal,
25
+ PlatformBaseError: () => BaseError,
26
+ PlatformBinary: () => Binary,
27
+ PlatformDataTypeError: () => DataTypeError,
28
+ PlatformHttpClient: () => HttpClient,
29
+ PlatformHttpClientAbortError: () => HttpClientAbortError,
30
+ PlatformHttpClientRequestError: () => HttpClientRequestError,
31
+ PlatformHttpClientResponse: () => HttpClientResponse,
32
+ PlatformHttpClientResponseError: () => HttpClientResponseError,
33
+ PlatformUnsupportedOperationError: () => UnsupportedOperationError,
34
+ httpClient: () => httpClient
35
+ });
36
+ module.exports = __toCommonJS(src_exports);
37
+
38
+ // packages/http-client/src/lib/platform/abort-controller.ts
39
+ var AbortController = globalThis.AbortController;
40
+
41
+ // packages/http-client/src/lib/platform/abort-signal.ts
42
+ var AbortSignal = globalThis.AbortSignal;
43
+
44
+ // packages/http-client/src/lib/platform/base-error.ts
45
+ var BaseError = class extends Error {
46
+ constructor(message, cause) {
47
+ super(message);
48
+ this.cause = cause;
49
+ }
50
+ };
51
+
52
+ // packages/http-client/src/lib/platform/unsupported-operation-error.ts
53
+ var UnsupportedOperationError = class extends BaseError {
54
+ name = "UnsupportedOperationError";
55
+ constructor(message, cause) {
56
+ super(message, cause);
57
+ }
58
+ };
59
+
60
+ // packages/http-client/src/lib/platform/binary.ts
61
+ function getImpl(data, type) {
62
+ const response = new Response(data);
63
+ switch (type) {
64
+ case "text":
65
+ return response.text();
66
+ case "json":
67
+ return response.json();
68
+ case "array-buffer":
69
+ return response.arrayBuffer();
70
+ case "blob":
71
+ return response.blob();
72
+ case "readable-stream":
73
+ return response.body;
74
+ }
75
+ throw new UnsupportedOperationError(`Unsupported binary type: ${type}.`);
76
+ }
77
+ var Binary = class {
78
+ constructor(data) {
79
+ this.data = data;
80
+ }
81
+ static fromText(text) {
82
+ return new Binary(new Blob([text]));
83
+ }
84
+ static fromJson(json) {
85
+ return new Binary(new Blob([JSON.stringify(json)]));
86
+ }
87
+ static from(data) {
88
+ if (data instanceof Blob || data instanceof ArrayBuffer || data instanceof ReadableStream) {
89
+ return new Binary(data);
90
+ } else {
91
+ throw new UnsupportedOperationError("Unsupported binary type.");
92
+ }
93
+ }
94
+ get(type) {
95
+ return getImpl(this.data, type);
96
+ }
97
+ };
98
+ function isBinary(body) {
99
+ return typeof body.get === "function";
100
+ }
101
+
102
+ // packages/http-client/src/lib/platform/data-type-error.ts
103
+ var DataTypeError = class extends BaseError {
104
+ name = "DataTypeError";
105
+ constructor(message, cause) {
106
+ super(message, cause);
107
+ }
108
+ };
109
+
110
+ // packages/http-client/src/lib/platform/default-status-validator.ts
111
+ var defaultStatusValidator = (status) => 200 <= status && status < 300;
112
+
113
+ // packages/http-client/src/lib/platform/encode-request-body.ts
114
+ function encodeTextRequestBody(body) {
115
+ return String(body);
116
+ }
117
+ function encodeJsonRequestBody(body) {
118
+ return JSON.stringify(body);
119
+ }
120
+ async function encodeBinaryFormDataField(field) {
121
+ if (field instanceof ArrayBuffer || field instanceof Blob) {
122
+ return field;
123
+ } else if (field instanceof ReadableStream) {
124
+ return new Response(field).arrayBuffer();
125
+ } else if (isBinary(field)) {
126
+ return field.get("array-buffer");
127
+ } else {
128
+ throw new DataTypeError("Cannot encode form data field as binary type.");
129
+ }
130
+ }
131
+ async function encodeFormDataRequestBody(body) {
132
+ const formData = new FormData();
133
+ for (const field of body) {
134
+ switch (field.type) {
135
+ case "text":
136
+ formData.append(field.name, encodeTextRequestBody(field.value));
137
+ break;
138
+ case "json":
139
+ formData.append(
140
+ field.name,
141
+ new Blob([encodeJsonRequestBody(field.value)], {
142
+ type: field.contentType ?? "application/json"
143
+ }),
144
+ field.filename ?? "json"
145
+ );
146
+ break;
147
+ case "binary": {
148
+ if (field.value instanceof File) {
149
+ const file = field.value;
150
+ formData.append(
151
+ field.name,
152
+ new File([file], field.filename ?? file.name, {
153
+ type: field.contentType ?? file.type ?? "application/octet-stream",
154
+ lastModified: file.lastModified
155
+ }),
156
+ field.filename ?? file.name
157
+ );
158
+ } else {
159
+ const blobType = "type" in field.value ? field.value.type : void 0;
160
+ formData.append(
161
+ field.name,
162
+ new Blob([await encodeBinaryFormDataField(field.value)], {
163
+ type: field.contentType ?? blobType ?? "application/octet-stream"
164
+ }),
165
+ field.filename ?? "binary"
166
+ );
167
+ }
168
+ break;
169
+ }
170
+ }
171
+ }
172
+ return formData;
173
+ }
174
+ async function encodeBinaryRequestBody(body) {
175
+ if (body instanceof Blob || body instanceof ArrayBuffer || body instanceof ReadableStream) {
176
+ return body;
177
+ } else if (isBinary(body)) {
178
+ try {
179
+ return await body.get("array-buffer");
180
+ } catch (e) {
181
+ throw new DataTypeError("Cannot encode body as binary type.", e);
182
+ }
183
+ } else {
184
+ throw new DataTypeError("Cannot encode body as binary type.");
185
+ }
186
+ }
187
+ async function encodeRequestBody(body, requestBodyType) {
188
+ switch (requestBodyType) {
189
+ case "text":
190
+ return encodeTextRequestBody(body);
191
+ case "json":
192
+ return encodeJsonRequestBody(body);
193
+ case "form-data":
194
+ return await encodeFormDataRequestBody(body);
195
+ case "binary":
196
+ return await encodeBinaryRequestBody(body);
197
+ }
198
+ throw new DataTypeError(`Invalid body type: '${requestBodyType}'`);
199
+ }
200
+
201
+ // packages/http-client/src/lib/platform/apply-content-type-header.ts
202
+ function getContentTypeBy(bodyType) {
203
+ switch (bodyType) {
204
+ case "json":
205
+ return "application/json";
206
+ case "text":
207
+ return "text/plain";
208
+ case "binary":
209
+ return "application/octet-stream";
210
+ }
211
+ throw new DataTypeError(`Invalid body type: '${bodyType}'`);
212
+ }
213
+ function applyContentTypeHeader(headers, requestBodyType) {
214
+ const isContentTypePresent = Object.keys(headers ?? {}).some(
215
+ (header) => header.toLowerCase() === "content-type"
216
+ );
217
+ if (!isContentTypePresent && requestBodyType !== "form-data") {
218
+ return {
219
+ "content-type": getContentTypeBy(requestBodyType),
220
+ ...headers
221
+ };
222
+ }
223
+ return headers;
224
+ }
225
+
226
+ // packages/http-client/src/lib/platform/http-client-abort-error.ts
227
+ var HttpClientAbortError = class extends Error {
228
+ constructor(cause) {
229
+ super(
230
+ typeof cause?.message === "string" ? `Request aborted: ${cause.message}` : "Request aborted."
231
+ );
232
+ this.cause = cause;
233
+ }
234
+ name = "AbortError";
235
+ };
236
+ var throwIfAbortError = (e) => {
237
+ if (e instanceof DOMException && e.name === "AbortError") {
238
+ throw new HttpClientAbortError(e);
239
+ }
240
+ };
241
+
242
+ // packages/http-client/src/lib/platform/http-client-request-error.ts
243
+ var HttpClientRequestError = class extends BaseError {
244
+ constructor(cause) {
245
+ super("Request failed.");
246
+ this.cause = cause;
247
+ }
248
+ name = "RequestError";
249
+ };
250
+
251
+ // packages/http-client/src/lib/platform/decode-response-body.ts
252
+ function emptyReadableStream() {
253
+ return new ReadableStream({
254
+ start(controller) {
255
+ controller.close();
256
+ }
257
+ });
258
+ }
259
+ async function decodeTextResponseBody(response) {
260
+ return response.body !== null ? response.text() : "";
261
+ }
262
+ async function decodeJsonResponseBody(response) {
263
+ return response.body !== null ? response.json() : null;
264
+ }
265
+ async function decodeBinaryResponseBody(response) {
266
+ return Binary.from(decodeReadableStreamResponseBody(response));
267
+ }
268
+ async function decodeFormDataResponseBody(response) {
269
+ if (response.body === null) {
270
+ return [];
271
+ } else {
272
+ const formData = await response.formData();
273
+ const result = [];
274
+ formData.forEach((value, key) => {
275
+ if (value instanceof File) {
276
+ result.push({
277
+ type: "binary",
278
+ name: key,
279
+ contentType: value.type,
280
+ filename: value.name,
281
+ value: Binary.from(value)
282
+ });
283
+ } else {
284
+ result.push({
285
+ type: "text",
286
+ name: key,
287
+ value
288
+ });
289
+ }
290
+ });
291
+ return result;
292
+ }
293
+ }
294
+ async function decodeArrayBufferResponseBody(response) {
295
+ return response !== null ? response.arrayBuffer() : new ArrayBuffer(0);
296
+ }
297
+ async function decodeBlobResponseBody(response) {
298
+ return response !== null ? response.blob() : new Blob();
299
+ }
300
+ function decodeReadableStreamResponseBody(response) {
301
+ return response.body !== null ? response.body : emptyReadableStream();
302
+ }
303
+ async function decodeAsyncResponseBody(response, responseBodyType) {
304
+ try {
305
+ switch (responseBodyType) {
306
+ case "text":
307
+ return await decodeTextResponseBody(response);
308
+ case "json":
309
+ return await decodeJsonResponseBody(response);
310
+ case "binary":
311
+ return await decodeBinaryResponseBody(response);
312
+ case "form-data":
313
+ return await decodeFormDataResponseBody(response);
314
+ case "array-buffer":
315
+ return await decodeArrayBufferResponseBody(response);
316
+ case "blob":
317
+ return await decodeBlobResponseBody(response);
318
+ }
319
+ } catch (e) {
320
+ throwIfAbortError(e);
321
+ throw new DataTypeError(
322
+ `Response body does not conform to the specified response body type: '${responseBodyType}'.`,
323
+ e
324
+ );
325
+ }
326
+ }
327
+ function decodeSyncResponseBody(response, responseBodyType) {
328
+ try {
329
+ return decodeReadableStreamResponseBody(response);
330
+ } catch (e) {
331
+ throwIfAbortError(e);
332
+ throw new DataTypeError(
333
+ `Response body does not conform to the specified response body type: '${responseBodyType}'.`,
334
+ e
335
+ );
336
+ }
337
+ }
338
+ function decodeResponseBodyImpl(response, responseBodyType) {
339
+ switch (responseBodyType) {
340
+ case "text":
341
+ case "json":
342
+ case "binary":
343
+ case "form-data":
344
+ case "array-buffer":
345
+ case "blob":
346
+ return decodeAsyncResponseBody(response, responseBodyType);
347
+ case "readable-stream":
348
+ return decodeSyncResponseBody(response, responseBodyType);
349
+ }
350
+ switch (responseBodyType) {
351
+ case "buffer":
352
+ case "stream":
353
+ throw new UnsupportedOperationError(
354
+ `Unsupported response body type: '${responseBodyType}'`
355
+ );
356
+ default:
357
+ throw new DataTypeError(`Invalid body type: '${responseBodyType}'`);
358
+ }
359
+ }
360
+ function decodeResponseBody(response, responseBodyType) {
361
+ return decodeResponseBodyImpl(
362
+ response,
363
+ responseBodyType
364
+ );
365
+ }
366
+
367
+ // packages/http-client/src/lib/platform/http-client-response.ts
368
+ var HttpClientResponse = class {
369
+ constructor(response) {
370
+ this.response = response;
371
+ }
372
+ get url() {
373
+ return this.response.url;
374
+ }
375
+ get status() {
376
+ return this.response.status;
377
+ }
378
+ get statusText() {
379
+ return this.response.statusText;
380
+ }
381
+ get headers() {
382
+ const headers = {};
383
+ this.response.headers.forEach((value, key) => {
384
+ headers[key] = value;
385
+ });
386
+ return headers;
387
+ }
388
+ body(responseBodyType = "json") {
389
+ return decodeResponseBody(this.response, responseBodyType);
390
+ }
391
+ };
392
+
393
+ // packages/http-client/src/lib/platform/http-client-response-error-serializer.ts
394
+ var import_sdk_web = require("@dynatrace/sdk-web");
395
+ var errorSerializerAdded = false;
396
+ function addHttpClientResponseErrorSerializer() {
397
+ if (!errorSerializerAdded && typeof window.dtSdk !== "undefined") {
398
+ (0, import_sdk_web.addGlobalErrorSerializer)(httpClientResponseErrorSerializer);
399
+ errorSerializerAdded = true;
400
+ }
401
+ }
402
+ var httpClientResponseErrorSerializer = async (error) => {
403
+ if (error instanceof HttpClientResponseError) {
404
+ const status = error.response.status;
405
+ const message = await error.response.body("text");
406
+ return {
407
+ name: error.name,
408
+ status,
409
+ message,
410
+ stack: error.stack,
411
+ type: import_sdk_web.ErrorType.HTTP
412
+ };
413
+ }
414
+ };
415
+
416
+ // packages/http-client/src/lib/platform/http-client-response-error.ts
417
+ var HttpClientResponseError = class extends BaseError {
418
+ name = "ResponseError";
419
+ response;
420
+ constructor(response) {
421
+ addHttpClientResponseErrorSerializer();
422
+ super(`HTTP Error ${response.status}: ${response.statusText}`);
423
+ this.response = new HttpClientResponse(response);
424
+ }
425
+ };
426
+
427
+ // packages/http-client/src/lib/platform/http-client.ts
428
+ var send = async (info, init) => {
429
+ try {
430
+ return await fetch(info, init);
431
+ } catch (e) {
432
+ throwIfAbortError(e);
433
+ throw new HttpClientRequestError(e);
434
+ }
435
+ };
436
+ var HttpClient = class {
437
+ async send(options) {
438
+ const { statusValidator = defaultStatusValidator } = options;
439
+ const requestBodyType = options.requestBodyType ?? "json";
440
+ const body = options.body !== void 0 ? await encodeRequestBody(options.body, requestBodyType) : void 0;
441
+ const response = await send(options.url, {
442
+ method: options.method,
443
+ headers: applyContentTypeHeader(options.headers, requestBodyType),
444
+ signal: options.abortSignal,
445
+ body
446
+ });
447
+ const ok = statusValidator(response.status);
448
+ if (ok) {
449
+ return new HttpClientResponse(response);
450
+ } else {
451
+ throw new HttpClientResponseError(response);
452
+ }
453
+ }
454
+ };
455
+ var httpClient = new HttpClient();