@dynatrace-sdk/http-client 1.0.9 → 1.1.1
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 +18 -0
- package/cjs/index.js +88 -2
- package/docs/DOCS.md +599 -314
- package/esm/index.js +88 -2
- package/package.json +1 -1
- package/types/packages/http-client/src/index.d.ts +1 -1
- package/types/packages/http-client/src/lib/platform/index.d.ts +1 -0
- package/types/packages/http-client/src/lib/platform/oauth-http-client.d.ts +41 -0
package/esm/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @license
|
|
3
|
-
* Copyright 2023 Dynatrace LLC
|
|
3
|
+
* Copyright 2023-2024 Dynatrace LLC
|
|
4
4
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
5
|
* you may not use this file except in compliance with the License.
|
|
6
6
|
* You may obtain a copy of the License at
|
|
@@ -144,7 +144,7 @@ async function encodeFormDataRequestBody(body) {
|
|
|
144
144
|
);
|
|
145
145
|
break;
|
|
146
146
|
case "binary": {
|
|
147
|
-
if (field.value instanceof File) {
|
|
147
|
+
if (File && field.value instanceof File) {
|
|
148
148
|
const file = field.value;
|
|
149
149
|
formData.append(
|
|
150
150
|
field.name,
|
|
@@ -526,6 +526,91 @@ var HttpClient = class {
|
|
|
526
526
|
}
|
|
527
527
|
};
|
|
528
528
|
var httpClient = new HttpClient();
|
|
529
|
+
|
|
530
|
+
// packages/http-client/src/lib/platform/oauth-http-client.ts
|
|
531
|
+
var OAuthToken = class {
|
|
532
|
+
_scope;
|
|
533
|
+
_tokenType;
|
|
534
|
+
_expirationDate;
|
|
535
|
+
_accessToken;
|
|
536
|
+
constructor(oauthTokenResponse) {
|
|
537
|
+
this._scope = oauthTokenResponse.scope;
|
|
538
|
+
this._tokenType = oauthTokenResponse.token_type;
|
|
539
|
+
this._expirationDate = ((expiresIn) => {
|
|
540
|
+
const date = new Date();
|
|
541
|
+
date.setSeconds(date.getSeconds() + expiresIn);
|
|
542
|
+
return date;
|
|
543
|
+
})(oauthTokenResponse.expires_in);
|
|
544
|
+
this._accessToken = oauthTokenResponse.access_token;
|
|
545
|
+
}
|
|
546
|
+
get scope() {
|
|
547
|
+
return this._scope;
|
|
548
|
+
}
|
|
549
|
+
get tokenType() {
|
|
550
|
+
return this._tokenType;
|
|
551
|
+
}
|
|
552
|
+
isExpired() {
|
|
553
|
+
return this._expirationDate <= new Date();
|
|
554
|
+
}
|
|
555
|
+
get accessToken() {
|
|
556
|
+
return this._accessToken;
|
|
557
|
+
}
|
|
558
|
+
};
|
|
559
|
+
var send2 = async (info, init) => {
|
|
560
|
+
try {
|
|
561
|
+
return await fetch(info, init);
|
|
562
|
+
} catch (e) {
|
|
563
|
+
throwIfAbortError(e);
|
|
564
|
+
throw new HttpClientRequestError(e);
|
|
565
|
+
}
|
|
566
|
+
};
|
|
567
|
+
var OAuthHttpClient = class {
|
|
568
|
+
constructor(config) {
|
|
569
|
+
this.config = config;
|
|
570
|
+
}
|
|
571
|
+
oauthToken;
|
|
572
|
+
async send(options) {
|
|
573
|
+
const { statusValidator = defaultStatusValidator } = options;
|
|
574
|
+
if (!this.oauthToken || this.oauthToken.isExpired()) {
|
|
575
|
+
this.oauthToken = await this.requestToken();
|
|
576
|
+
}
|
|
577
|
+
const requestBodyType = options.requestBodyType ?? "json";
|
|
578
|
+
const url = new URL(options.url, this.config.environmentUrl);
|
|
579
|
+
const body = options.body !== void 0 ? await encodeRequestBody(options.body, requestBodyType) : void 0;
|
|
580
|
+
const headers = {
|
|
581
|
+
...applyContentTypeHeader(options.headers, requestBodyType),
|
|
582
|
+
Authorization: `${this.oauthToken.tokenType} ${this.oauthToken.accessToken}`
|
|
583
|
+
};
|
|
584
|
+
const response = await send2(url.toString(), {
|
|
585
|
+
method: options.method,
|
|
586
|
+
signal: options.abortSignal,
|
|
587
|
+
body,
|
|
588
|
+
headers
|
|
589
|
+
});
|
|
590
|
+
const ok = statusValidator(response.status);
|
|
591
|
+
if (ok) {
|
|
592
|
+
return new HttpClientResponse(response);
|
|
593
|
+
} else {
|
|
594
|
+
throw new HttpClientResponseError(response, { requestMethod: options.method });
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
async requestToken() {
|
|
598
|
+
const res = await send2(this.config.authUrl, {
|
|
599
|
+
method: "POST",
|
|
600
|
+
headers: {
|
|
601
|
+
"Content-Type": "application/x-www-form-urlencoded"
|
|
602
|
+
},
|
|
603
|
+
body: new URLSearchParams({
|
|
604
|
+
grant_type: "client_credentials",
|
|
605
|
+
client_id: this.config.clientId,
|
|
606
|
+
client_secret: this.config.secret,
|
|
607
|
+
scope: this.config.scopes.join(" ")
|
|
608
|
+
})
|
|
609
|
+
});
|
|
610
|
+
const data = await res.json();
|
|
611
|
+
return new OAuthToken(data);
|
|
612
|
+
}
|
|
613
|
+
};
|
|
529
614
|
export {
|
|
530
615
|
AbortController as PlatformAbortController,
|
|
531
616
|
AbortSignal as PlatformAbortSignal,
|
|
@@ -538,6 +623,7 @@ export {
|
|
|
538
623
|
HttpClientResponse as PlatformHttpClientResponse,
|
|
539
624
|
HttpClientResponseError as PlatformHttpClientResponseError,
|
|
540
625
|
UnsupportedOperationError as PlatformUnsupportedOperationError,
|
|
626
|
+
OAuthHttpClient as _OAuthHttpClient,
|
|
541
627
|
httpClient,
|
|
542
628
|
isHttpClientAbortError,
|
|
543
629
|
isHttpClientNetworkError,
|
package/package.json
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export { AbortController, AbortSignal, BaseError, Binary, BinaryFormDataRequestField, BinaryFormDataResponseField, DataTypeError, DataTypes, FormDataRequestBody, FormDataRequestField, FormDataResponseBody, FormDataResponseField, Headers, HttpClient, HttpClientAbortError, HttpClientNetworkError, HttpClientRequestError, HttpClientRequestOptions, HttpClientResponse, HttpClientResponseError, JsonFormDataRequestField, RequestBodyTypes, ResponseBodyTypes, StatusValidator, TextFormDataRequestField, TextFormDataResponseField, UnsupportedOperationError, isHttpClientResponseError, isHttpClientAbortError, isHttpClientNetworkError, isHttpClientRequestError, } from './lib/types';
|
|
2
|
-
export { AbortController as PlatformAbortController, AbortSignal as PlatformAbortSignal, BaseError as PlatformBaseError, Binary as PlatformBinary, DataTypeError as PlatformDataTypeError, HttpClient as PlatformHttpClient, HttpClientAbortError as PlatformHttpClientAbortError, HttpClientRequestError as PlatformHttpClientRequestError, HttpClientResponse as PlatformHttpClientResponse, HttpClientResponseError as PlatformHttpClientResponseError, UnsupportedOperationError as PlatformUnsupportedOperationError, httpClient, } from './lib/platform';
|
|
2
|
+
export { AbortController as PlatformAbortController, AbortSignal as PlatformAbortSignal, BaseError as PlatformBaseError, Binary as PlatformBinary, DataTypeError as PlatformDataTypeError, HttpClient as PlatformHttpClient, HttpClientAbortError as PlatformHttpClientAbortError, HttpClientRequestError as PlatformHttpClientRequestError, HttpClientResponse as PlatformHttpClientResponse, HttpClientResponseError as PlatformHttpClientResponseError, OAuthHttpClient as _OAuthHttpClient, OAuthHttpClientConfig as _OAuthHttpClientConfig, UnsupportedOperationError as PlatformUnsupportedOperationError, httpClient, } from './lib/platform';
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import * as types from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* DO NOT USE - internal use only.
|
|
4
|
+
*/
|
|
5
|
+
export interface OAuthHttpClientConfig {
|
|
6
|
+
clientId: string;
|
|
7
|
+
secret: string;
|
|
8
|
+
scopes: string[];
|
|
9
|
+
environmentUrl: string;
|
|
10
|
+
authUrl: string;
|
|
11
|
+
}
|
|
12
|
+
export interface OAuthTokenResponse {
|
|
13
|
+
scope: string;
|
|
14
|
+
token_type: string;
|
|
15
|
+
expires_in: number;
|
|
16
|
+
access_token: string;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* DO NOT USE - internal use only.
|
|
20
|
+
*/
|
|
21
|
+
export declare class OAuthToken {
|
|
22
|
+
private readonly _scope;
|
|
23
|
+
private readonly _tokenType;
|
|
24
|
+
private readonly _expirationDate;
|
|
25
|
+
private readonly _accessToken;
|
|
26
|
+
constructor(oauthTokenResponse: OAuthTokenResponse);
|
|
27
|
+
get scope(): string;
|
|
28
|
+
get tokenType(): string;
|
|
29
|
+
isExpired(): boolean;
|
|
30
|
+
get accessToken(): string;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* DO NOT USE - internal use only.
|
|
34
|
+
*/
|
|
35
|
+
export declare class OAuthHttpClient implements types.HttpClient {
|
|
36
|
+
private readonly config;
|
|
37
|
+
private oauthToken?;
|
|
38
|
+
constructor(config: OAuthHttpClientConfig);
|
|
39
|
+
send<T extends keyof types.RequestBodyTypes = 'json'>(options: types.HttpClientRequestOptions<T>): Promise<types.HttpClientResponse>;
|
|
40
|
+
private requestToken;
|
|
41
|
+
}
|