@dynatrace-sdk/http-client 1.0.10 → 1.2.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 +102 -2
- package/docs/DOCS.md +679 -315
- package/esm/index.js +102 -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/http-client.d.ts +19 -0
- 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
|
@@ -507,13 +507,27 @@ var send = async (info, init) => {
|
|
|
507
507
|
}
|
|
508
508
|
};
|
|
509
509
|
var HttpClient = class {
|
|
510
|
+
_baseUrl;
|
|
511
|
+
_defaultHeaders;
|
|
512
|
+
constructor(options) {
|
|
513
|
+
this._baseUrl = options?.baseUrl;
|
|
514
|
+
this._defaultHeaders = options?.defaultHeaders;
|
|
515
|
+
}
|
|
516
|
+
_setBaseURL(baseUrl) {
|
|
517
|
+
this._baseUrl = baseUrl;
|
|
518
|
+
}
|
|
519
|
+
_setDefaultHeaders(defaultHeaders) {
|
|
520
|
+
this._defaultHeaders = defaultHeaders;
|
|
521
|
+
}
|
|
510
522
|
async send(options) {
|
|
511
523
|
const { statusValidator = defaultStatusValidator } = options;
|
|
512
524
|
const requestBodyType = options.requestBodyType ?? "json";
|
|
513
525
|
const body = options.body !== void 0 ? await encodeRequestBody(options.body, requestBodyType) : void 0;
|
|
514
|
-
const
|
|
526
|
+
const url = this._baseUrl ? new URL(options.url, this._baseUrl).toString() : options.url;
|
|
527
|
+
const requestHeaders = { ...this._defaultHeaders, ...options?.headers };
|
|
528
|
+
const response = await send(url, {
|
|
515
529
|
method: options.method,
|
|
516
|
-
headers: applyContentTypeHeader(
|
|
530
|
+
headers: applyContentTypeHeader(requestHeaders, requestBodyType),
|
|
517
531
|
signal: options.abortSignal,
|
|
518
532
|
body
|
|
519
533
|
});
|
|
@@ -526,6 +540,91 @@ var HttpClient = class {
|
|
|
526
540
|
}
|
|
527
541
|
};
|
|
528
542
|
var httpClient = new HttpClient();
|
|
543
|
+
|
|
544
|
+
// packages/http-client/src/lib/platform/oauth-http-client.ts
|
|
545
|
+
var OAuthToken = class {
|
|
546
|
+
_scope;
|
|
547
|
+
_tokenType;
|
|
548
|
+
_expirationDate;
|
|
549
|
+
_accessToken;
|
|
550
|
+
constructor(oauthTokenResponse) {
|
|
551
|
+
this._scope = oauthTokenResponse.scope;
|
|
552
|
+
this._tokenType = oauthTokenResponse.token_type;
|
|
553
|
+
this._expirationDate = ((expiresIn) => {
|
|
554
|
+
const date = new Date();
|
|
555
|
+
date.setSeconds(date.getSeconds() + expiresIn);
|
|
556
|
+
return date;
|
|
557
|
+
})(oauthTokenResponse.expires_in);
|
|
558
|
+
this._accessToken = oauthTokenResponse.access_token;
|
|
559
|
+
}
|
|
560
|
+
get scope() {
|
|
561
|
+
return this._scope;
|
|
562
|
+
}
|
|
563
|
+
get tokenType() {
|
|
564
|
+
return this._tokenType;
|
|
565
|
+
}
|
|
566
|
+
isExpired() {
|
|
567
|
+
return this._expirationDate <= new Date();
|
|
568
|
+
}
|
|
569
|
+
get accessToken() {
|
|
570
|
+
return this._accessToken;
|
|
571
|
+
}
|
|
572
|
+
};
|
|
573
|
+
var send2 = async (info, init) => {
|
|
574
|
+
try {
|
|
575
|
+
return await fetch(info, init);
|
|
576
|
+
} catch (e) {
|
|
577
|
+
throwIfAbortError(e);
|
|
578
|
+
throw new HttpClientRequestError(e);
|
|
579
|
+
}
|
|
580
|
+
};
|
|
581
|
+
var OAuthHttpClient = class {
|
|
582
|
+
constructor(config) {
|
|
583
|
+
this.config = config;
|
|
584
|
+
}
|
|
585
|
+
oauthToken;
|
|
586
|
+
async send(options) {
|
|
587
|
+
const { statusValidator = defaultStatusValidator } = options;
|
|
588
|
+
if (!this.oauthToken || this.oauthToken.isExpired()) {
|
|
589
|
+
this.oauthToken = await this.requestToken();
|
|
590
|
+
}
|
|
591
|
+
const requestBodyType = options.requestBodyType ?? "json";
|
|
592
|
+
const url = new URL(options.url, this.config.environmentUrl);
|
|
593
|
+
const body = options.body !== void 0 ? await encodeRequestBody(options.body, requestBodyType) : void 0;
|
|
594
|
+
const headers = {
|
|
595
|
+
...applyContentTypeHeader(options.headers, requestBodyType),
|
|
596
|
+
Authorization: `${this.oauthToken.tokenType} ${this.oauthToken.accessToken}`
|
|
597
|
+
};
|
|
598
|
+
const response = await send2(url.toString(), {
|
|
599
|
+
method: options.method,
|
|
600
|
+
signal: options.abortSignal,
|
|
601
|
+
body,
|
|
602
|
+
headers
|
|
603
|
+
});
|
|
604
|
+
const ok = statusValidator(response.status);
|
|
605
|
+
if (ok) {
|
|
606
|
+
return new HttpClientResponse(response);
|
|
607
|
+
} else {
|
|
608
|
+
throw new HttpClientResponseError(response, { requestMethod: options.method });
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
async requestToken() {
|
|
612
|
+
const res = await send2(this.config.authUrl, {
|
|
613
|
+
method: "POST",
|
|
614
|
+
headers: {
|
|
615
|
+
"Content-Type": "application/x-www-form-urlencoded"
|
|
616
|
+
},
|
|
617
|
+
body: new URLSearchParams({
|
|
618
|
+
grant_type: "client_credentials",
|
|
619
|
+
client_id: this.config.clientId,
|
|
620
|
+
client_secret: this.config.secret,
|
|
621
|
+
scope: this.config.scopes.join(" ")
|
|
622
|
+
})
|
|
623
|
+
});
|
|
624
|
+
const data = await res.json();
|
|
625
|
+
return new OAuthToken(data);
|
|
626
|
+
}
|
|
627
|
+
};
|
|
529
628
|
export {
|
|
530
629
|
AbortController as PlatformAbortController,
|
|
531
630
|
AbortSignal as PlatformAbortSignal,
|
|
@@ -538,6 +637,7 @@ export {
|
|
|
538
637
|
HttpClientResponse as PlatformHttpClientResponse,
|
|
539
638
|
HttpClientResponseError as PlatformHttpClientResponseError,
|
|
540
639
|
UnsupportedOperationError as PlatformUnsupportedOperationError,
|
|
640
|
+
OAuthHttpClient as _OAuthHttpClient,
|
|
541
641
|
httpClient,
|
|
542
642
|
isHttpClientAbortError,
|
|
543
643
|
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';
|
|
@@ -1,5 +1,24 @@
|
|
|
1
1
|
import * as types from '../types';
|
|
2
|
+
import { Headers } from '../types';
|
|
3
|
+
interface HttpClientConfiguration {
|
|
4
|
+
baseUrl?: string;
|
|
5
|
+
defaultHeaders?: Headers;
|
|
6
|
+
}
|
|
2
7
|
export declare class HttpClient implements types.HttpClient {
|
|
8
|
+
private _baseUrl;
|
|
9
|
+
private _defaultHeaders;
|
|
10
|
+
constructor(options?: HttpClientConfiguration);
|
|
11
|
+
/**
|
|
12
|
+
* Sets HTTP Client Instance baseUrl param to the incoming value `baseUrl`.
|
|
13
|
+
* @param baseUrl Base URL used in the request path.
|
|
14
|
+
*/
|
|
15
|
+
_setBaseURL(baseUrl: string): void;
|
|
16
|
+
/**
|
|
17
|
+
* Sets HTTP Client Instance defaultHeaders param to the incoming value `defaultHeaders`.
|
|
18
|
+
* @param defaultHeaders HTTP Headers to be used in outgoing requests.
|
|
19
|
+
*/
|
|
20
|
+
_setDefaultHeaders(defaultHeaders: Headers): void;
|
|
3
21
|
send<T extends keyof types.RequestBodyTypes = 'json'>(options: types.HttpClientRequestOptions<T>): Promise<types.HttpClientResponse>;
|
|
4
22
|
}
|
|
5
23
|
export declare const httpClient: HttpClient;
|
|
24
|
+
export {};
|
|
@@ -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
|
+
}
|