@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 CHANGED
@@ -2,6 +2,25 @@
2
2
 
3
3
  @dynatrace-sdk/http-client
4
4
 
5
+ ## 1.2.0
6
+
7
+ ### Minor Changes
8
+
9
+ - Allow configuring the base URL of the http-client. APPDEV-8174
10
+ - Allow setting the default headers for the http-client. APPDEV-8175
11
+
12
+ ## 1.1.1
13
+
14
+ ### Patch Changes
15
+
16
+ - Rename `OAuthHttpClient` to `_OAuthHttpClient` to mark it for internal use only.
17
+
18
+ ## 1.1.0
19
+
20
+ ### Minor Changes
21
+
22
+ - Add `OAuthHttpClient`
23
+
5
24
  ## 1.0.10
6
25
 
7
26
  ### 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,
@@ -543,13 +544,27 @@ var send = async (info, init) => {
543
544
  }
544
545
  };
545
546
  var HttpClient = class {
547
+ _baseUrl;
548
+ _defaultHeaders;
549
+ constructor(options) {
550
+ this._baseUrl = options?.baseUrl;
551
+ this._defaultHeaders = options?.defaultHeaders;
552
+ }
553
+ _setBaseURL(baseUrl) {
554
+ this._baseUrl = baseUrl;
555
+ }
556
+ _setDefaultHeaders(defaultHeaders) {
557
+ this._defaultHeaders = defaultHeaders;
558
+ }
546
559
  async send(options) {
547
560
  const { statusValidator = defaultStatusValidator } = options;
548
561
  const requestBodyType = options.requestBodyType ?? "json";
549
562
  const body = options.body !== void 0 ? await encodeRequestBody(options.body, requestBodyType) : void 0;
550
- const response = await send(options.url, {
563
+ const url = this._baseUrl ? new URL(options.url, this._baseUrl).toString() : options.url;
564
+ const requestHeaders = { ...this._defaultHeaders, ...options?.headers };
565
+ const response = await send(url, {
551
566
  method: options.method,
552
- headers: applyContentTypeHeader(options.headers, requestBodyType),
567
+ headers: applyContentTypeHeader(requestHeaders, requestBodyType),
553
568
  signal: options.abortSignal,
554
569
  body
555
570
  });
@@ -562,3 +577,88 @@ var HttpClient = class {
562
577
  }
563
578
  };
564
579
  var httpClient = new HttpClient();
580
+
581
+ // packages/http-client/src/lib/platform/oauth-http-client.ts
582
+ var OAuthToken = class {
583
+ _scope;
584
+ _tokenType;
585
+ _expirationDate;
586
+ _accessToken;
587
+ constructor(oauthTokenResponse) {
588
+ this._scope = oauthTokenResponse.scope;
589
+ this._tokenType = oauthTokenResponse.token_type;
590
+ this._expirationDate = ((expiresIn) => {
591
+ const date = new Date();
592
+ date.setSeconds(date.getSeconds() + expiresIn);
593
+ return date;
594
+ })(oauthTokenResponse.expires_in);
595
+ this._accessToken = oauthTokenResponse.access_token;
596
+ }
597
+ get scope() {
598
+ return this._scope;
599
+ }
600
+ get tokenType() {
601
+ return this._tokenType;
602
+ }
603
+ isExpired() {
604
+ return this._expirationDate <= new Date();
605
+ }
606
+ get accessToken() {
607
+ return this._accessToken;
608
+ }
609
+ };
610
+ var send2 = async (info, init) => {
611
+ try {
612
+ return await fetch(info, init);
613
+ } catch (e) {
614
+ throwIfAbortError(e);
615
+ throw new HttpClientRequestError(e);
616
+ }
617
+ };
618
+ var OAuthHttpClient = class {
619
+ constructor(config) {
620
+ this.config = config;
621
+ }
622
+ oauthToken;
623
+ async send(options) {
624
+ const { statusValidator = defaultStatusValidator } = options;
625
+ if (!this.oauthToken || this.oauthToken.isExpired()) {
626
+ this.oauthToken = await this.requestToken();
627
+ }
628
+ const requestBodyType = options.requestBodyType ?? "json";
629
+ const url = new URL(options.url, this.config.environmentUrl);
630
+ const body = options.body !== void 0 ? await encodeRequestBody(options.body, requestBodyType) : void 0;
631
+ const headers = {
632
+ ...applyContentTypeHeader(options.headers, requestBodyType),
633
+ Authorization: `${this.oauthToken.tokenType} ${this.oauthToken.accessToken}`
634
+ };
635
+ const response = await send2(url.toString(), {
636
+ method: options.method,
637
+ signal: options.abortSignal,
638
+ body,
639
+ headers
640
+ });
641
+ const ok = statusValidator(response.status);
642
+ if (ok) {
643
+ return new HttpClientResponse(response);
644
+ } else {
645
+ throw new HttpClientResponseError(response, { requestMethod: options.method });
646
+ }
647
+ }
648
+ async requestToken() {
649
+ const res = await send2(this.config.authUrl, {
650
+ method: "POST",
651
+ headers: {
652
+ "Content-Type": "application/x-www-form-urlencoded"
653
+ },
654
+ body: new URLSearchParams({
655
+ grant_type: "client_credentials",
656
+ client_id: this.config.clientId,
657
+ client_secret: this.config.secret,
658
+ scope: this.config.scopes.join(" ")
659
+ })
660
+ });
661
+ const data = await res.json();
662
+ return new OAuthToken(data);
663
+ }
664
+ };