@dynatrace-sdk/http-client 1.0.10 → 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 CHANGED
@@ -2,6 +2,18 @@
2
2
 
3
3
  @dynatrace-sdk/http-client
4
4
 
5
+ ## 1.1.1
6
+
7
+ ### Patch Changes
8
+
9
+ - Rename `OAuthHttpClient` to `_OAuthHttpClient` to mark it for internal use only.
10
+
11
+ ## 1.1.0
12
+
13
+ ### Minor Changes
14
+
15
+ - Add `OAuthHttpClient`
16
+
5
17
  ## 1.0.10
6
18
 
7
19
  ### Patch Changes
package/cjs/index.js CHANGED
@@ -46,6 +46,7 @@ __export(src_exports, {
46
46
  PlatformHttpClientResponse: () => HttpClientResponse,
47
47
  PlatformHttpClientResponseError: () => HttpClientResponseError,
48
48
  PlatformUnsupportedOperationError: () => UnsupportedOperationError,
49
+ _OAuthHttpClient: () => OAuthHttpClient,
49
50
  httpClient: () => httpClient,
50
51
  isHttpClientAbortError: () => isHttpClientAbortError,
51
52
  isHttpClientNetworkError: () => isHttpClientNetworkError,
@@ -562,3 +563,88 @@ var HttpClient = class {
562
563
  }
563
564
  };
564
565
  var httpClient = new HttpClient();
566
+
567
+ // packages/http-client/src/lib/platform/oauth-http-client.ts
568
+ var OAuthToken = class {
569
+ _scope;
570
+ _tokenType;
571
+ _expirationDate;
572
+ _accessToken;
573
+ constructor(oauthTokenResponse) {
574
+ this._scope = oauthTokenResponse.scope;
575
+ this._tokenType = oauthTokenResponse.token_type;
576
+ this._expirationDate = ((expiresIn) => {
577
+ const date = new Date();
578
+ date.setSeconds(date.getSeconds() + expiresIn);
579
+ return date;
580
+ })(oauthTokenResponse.expires_in);
581
+ this._accessToken = oauthTokenResponse.access_token;
582
+ }
583
+ get scope() {
584
+ return this._scope;
585
+ }
586
+ get tokenType() {
587
+ return this._tokenType;
588
+ }
589
+ isExpired() {
590
+ return this._expirationDate <= new Date();
591
+ }
592
+ get accessToken() {
593
+ return this._accessToken;
594
+ }
595
+ };
596
+ var send2 = async (info, init) => {
597
+ try {
598
+ return await fetch(info, init);
599
+ } catch (e) {
600
+ throwIfAbortError(e);
601
+ throw new HttpClientRequestError(e);
602
+ }
603
+ };
604
+ var OAuthHttpClient = class {
605
+ constructor(config) {
606
+ this.config = config;
607
+ }
608
+ oauthToken;
609
+ async send(options) {
610
+ const { statusValidator = defaultStatusValidator } = options;
611
+ if (!this.oauthToken || this.oauthToken.isExpired()) {
612
+ this.oauthToken = await this.requestToken();
613
+ }
614
+ const requestBodyType = options.requestBodyType ?? "json";
615
+ const url = new URL(options.url, this.config.environmentUrl);
616
+ const body = options.body !== void 0 ? await encodeRequestBody(options.body, requestBodyType) : void 0;
617
+ const headers = {
618
+ ...applyContentTypeHeader(options.headers, requestBodyType),
619
+ Authorization: `${this.oauthToken.tokenType} ${this.oauthToken.accessToken}`
620
+ };
621
+ const response = await send2(url.toString(), {
622
+ method: options.method,
623
+ signal: options.abortSignal,
624
+ body,
625
+ headers
626
+ });
627
+ const ok = statusValidator(response.status);
628
+ if (ok) {
629
+ return new HttpClientResponse(response);
630
+ } else {
631
+ throw new HttpClientResponseError(response, { requestMethod: options.method });
632
+ }
633
+ }
634
+ async requestToken() {
635
+ const res = await send2(this.config.authUrl, {
636
+ method: "POST",
637
+ headers: {
638
+ "Content-Type": "application/x-www-form-urlencoded"
639
+ },
640
+ body: new URLSearchParams({
641
+ grant_type: "client_credentials",
642
+ client_id: this.config.clientId,
643
+ client_secret: this.config.secret,
644
+ scope: this.config.scopes.join(" ")
645
+ })
646
+ });
647
+ const data = await res.json();
648
+ return new OAuthToken(data);
649
+ }
650
+ };