@adobe-commerce/aio-toolkit 1.0.0 → 1.0.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/dist/index.js CHANGED
@@ -76,13 +76,13 @@ var HttpStatus = /* @__PURE__ */ ((HttpStatus2) => {
76
76
  return HttpStatus2;
77
77
  })(HttpStatus || {});
78
78
  var HttpMethod = /* @__PURE__ */ ((HttpMethod3) => {
79
- HttpMethod3["GET"] = "get";
80
- HttpMethod3["POST"] = "post";
81
- HttpMethod3["PUT"] = "put";
82
- HttpMethod3["DELETE"] = "delete";
83
- HttpMethod3["PATCH"] = "patch";
84
- HttpMethod3["HEAD"] = "head";
85
- HttpMethod3["OPTIONS"] = "options";
79
+ HttpMethod3["GET"] = "GET";
80
+ HttpMethod3["POST"] = "POST";
81
+ HttpMethod3["PUT"] = "PUT";
82
+ HttpMethod3["DELETE"] = "DELETE";
83
+ HttpMethod3["PATCH"] = "PATCH";
84
+ HttpMethod3["HEAD"] = "HEAD";
85
+ HttpMethod3["OPTIONS"] = "OPTIONS";
86
86
  return HttpMethod3;
87
87
  })(HttpMethod || {});
88
88
 
@@ -256,9 +256,9 @@ var _RuntimeAction = class _RuntimeAction {
256
256
  if (errorMessage) {
257
257
  return response_default.error(400 /* BAD_REQUEST */, errorMessage);
258
258
  }
259
- const requestMethod = params.__ow_method;
259
+ const requestMethod = params.__ow_method?.toUpperCase();
260
260
  if (httpMethods.length > 0 && !httpMethods.includes(requestMethod)) {
261
- const errorMessage2 = `Invalid HTTP method: ${requestMethod}. Allowed methods are: ${httpMethods.join(", ")}`;
261
+ const errorMessage2 = `Invalid HTTP method: ${params.__ow_method}. Allowed methods are: ${httpMethods.join(", ")}`;
262
262
  logger.error(errorMessage2);
263
263
  return response_default.error(405 /* METHOD_NOT_ALLOWED */, errorMessage2);
264
264
  }
@@ -319,7 +319,7 @@ var _GraphQlAction = class _GraphQlAction {
319
319
  }, name = "main", disableIntrospection = false) {
320
320
  return runtime_action_default.execute(
321
321
  `graphql-${name}`,
322
- ["get" /* GET */, "post" /* POST */],
322
+ ["GET" /* GET */, "POST" /* POST */],
323
323
  ["query"],
324
324
  [],
325
325
  async (params, ctx) => {
@@ -549,21 +549,31 @@ var file_repository_default = FileRepository;
549
549
  // src/integration/bearer-token/index.ts
550
550
  var _BearerToken = class _BearerToken {
551
551
  /**
552
- * Extracts the Bearer token from OpenWhisk action parameters and returns detailed token information.
553
- * Looks for the authorization header in __ow_headers and extracts the token value
554
- * after the "Bearer " prefix.
552
+ * Extracts the Bearer token from HTTP request headers and returns detailed token information.
553
+ * Supports both standard HTTP headers and OpenWhisk action parameter formats.
555
554
  *
556
- * @param params - OpenWhisk action input parameters containing headers
555
+ * @param headersOrParams - Either a standard headers object or OpenWhisk action parameters
557
556
  * @returns Detailed token information object
558
557
  *
559
558
  * @example
559
+ * // Standard HTTP headers approach
560
+ * const headers = {
561
+ * authorization: 'Bearer abc123token'
562
+ * };
563
+ * const tokenInfo = BearerToken.extract(headers);
564
+ *
565
+ * @example
566
+ * // OpenWhisk action parameters (backward compatibility)
560
567
  * const params = {
561
568
  * __ow_headers: {
562
569
  * authorization: 'Bearer abc123token'
563
570
  * }
564
571
  * };
565
572
  * const tokenInfo = BearerToken.extract(params);
566
- * // returns: {
573
+ *
574
+ * @example
575
+ * // Both return the same result:
576
+ * // {
567
577
  * // token: 'abc123token',
568
578
  * // tokenLength: 11,
569
579
  * // isValid: true,
@@ -571,26 +581,54 @@ var _BearerToken = class _BearerToken {
571
581
  * // timeUntilExpiry: 3600000
572
582
  * // }
573
583
  */
574
- static extract(params) {
584
+ static extract(headersOrParams) {
575
585
  let token = null;
576
- if (params.__ow_headers?.authorization?.startsWith("Bearer ")) {
577
- token = params.__ow_headers.authorization.substring("Bearer ".length);
586
+ if (headersOrParams.authorization?.startsWith("Bearer ")) {
587
+ token = headersOrParams.authorization.substring("Bearer ".length);
588
+ } else if (headersOrParams.__ow_headers?.authorization?.startsWith("Bearer ")) {
589
+ token = headersOrParams.__ow_headers.authorization.substring("Bearer ".length);
578
590
  }
579
591
  return _BearerToken.info(token);
580
592
  }
581
593
  /**
582
- * Gets detailed information about a Bearer token
583
- * @param token - The Bearer token string (or null)
584
- * @returns {BearerTokenInfo} Detailed token information including validity and expiry
594
+ * Analyzes a Bearer token and returns detailed information including validity and expiry.
595
+ * Supports both JWT tokens (with automatic expiry detection) and plain tokens (24h default expiry).
596
+ *
597
+ * @param token - The Bearer token string (or null). Can be JWT or plain token.
598
+ * @returns Detailed token information object
585
599
  *
586
600
  * @example
587
- * const tokenInfo = BearerToken.info('abc123token');
601
+ * // Plain token (gets 24h default expiry)
602
+ * const plainTokenInfo = BearerToken.info('abc123token');
588
603
  * // returns: {
589
604
  * // token: 'abc123token',
590
605
  * // tokenLength: 11,
591
606
  * // isValid: true,
592
- * // expiry: '2024-01-01T12:00:00.000Z',
593
- * // timeUntilExpiry: 3600000
607
+ * // expiry: '2024-01-02T12:00:00.000Z', // 24h from now
608
+ * // timeUntilExpiry: 86400000 // milliseconds until expiry
609
+ * // }
610
+ *
611
+ * @example
612
+ * // JWT token (automatic expiry detection from 'exp' or 'expires_in' claims)
613
+ * const jwtToken = 'eyJhbGciOiJIUzI1NiJ9.eyJleHAiOjE3MDQ0Njc2MDB9.signature';
614
+ * const jwtTokenInfo = BearerToken.info(jwtToken);
615
+ * // returns: {
616
+ * // token: 'eyJhbGciOiJIUzI1NiJ9...',
617
+ * // tokenLength: 45,
618
+ * // isValid: true, // false if expired
619
+ * // expiry: '2024-01-05T12:00:00.000Z', // from JWT exp claim
620
+ * // timeUntilExpiry: 172800000 // actual time until expiry
621
+ * // }
622
+ *
623
+ * @example
624
+ * // Null or invalid token
625
+ * const nullTokenInfo = BearerToken.info(null);
626
+ * // returns: {
627
+ * // token: null,
628
+ * // tokenLength: 0,
629
+ * // isValid: false,
630
+ * // expiry: null,
631
+ * // timeUntilExpiry: null
594
632
  * // }
595
633
  */
596
634
  static info(token) {
@@ -656,14 +694,67 @@ var bearer_token_default = BearerToken;
656
694
  var import_node_fetch = __toESM(require("node-fetch"));
657
695
  var _RestClient = class _RestClient {
658
696
  /**
659
- * A generic method to make GET rest call
697
+ * A completely raw method to make HTTP requests
660
698
  *
661
699
  * @param endpoint
700
+ * @param method
662
701
  * @param headers
702
+ * @param payload
703
+ * @returns {Promise<Response>}
704
+ */
705
+ async makeRequest(endpoint, method = "GET", headers = {}, payload = null) {
706
+ let options = {
707
+ method,
708
+ headers
709
+ };
710
+ if (payload !== null) {
711
+ options = {
712
+ ...options,
713
+ body: JSON.stringify(payload),
714
+ headers: {
715
+ ...headers,
716
+ "Content-Type": "application/json"
717
+ }
718
+ };
719
+ }
720
+ return await (0, import_node_fetch.default)(endpoint, options);
721
+ }
722
+ /**
723
+ * A method to parse HTTP response
724
+ *
725
+ * @param response
663
726
  * @returns {Promise<any>}
664
727
  */
665
- async get(endpoint, headers = {}) {
666
- return await this.apiCall(endpoint, "GET", headers);
728
+ async parseResponse(response) {
729
+ if (!response.ok) {
730
+ throw new Error(`HTTP error! status: ${response.status}`);
731
+ }
732
+ if (response.status === 204 || response.headers?.get("content-length") === "0") {
733
+ return null;
734
+ }
735
+ if (typeof response.json === "function") {
736
+ const contentType = response.headers?.get("content-type");
737
+ if (!contentType || contentType.includes("application/json") || contentType.includes("application/hal+json")) {
738
+ return await response.json();
739
+ }
740
+ }
741
+ if (typeof response.text === "function") {
742
+ const text = await response.text();
743
+ return text;
744
+ }
745
+ return null;
746
+ }
747
+ /**
748
+ * A generic method to make GET rest call
749
+ *
750
+ * @param endpoint
751
+ * @param headers
752
+ * @param parsed
753
+ * @returns {Promise<Response | any>}
754
+ */
755
+ async get(endpoint, headers = {}, parsed = true) {
756
+ const response = await this.makeRequest(endpoint, "GET", headers);
757
+ return parsed ? await this.parseResponse(response) : response;
667
758
  }
668
759
  /**
669
760
  * A generic method to make POST rest call
@@ -671,10 +762,12 @@ var _RestClient = class _RestClient {
671
762
  * @param endpoint
672
763
  * @param headers
673
764
  * @param payload
674
- * @returns {Promise<any>}
765
+ * @param parsed
766
+ * @returns {Promise<Response | any>}
675
767
  */
676
- async post(endpoint, headers = {}, payload = null) {
677
- return await this.apiCall(endpoint, "POST", headers, payload);
768
+ async post(endpoint, headers = {}, payload = null, parsed = true) {
769
+ const response = await this.makeRequest(endpoint, "POST", headers, payload);
770
+ return parsed ? await this.parseResponse(response) : response;
678
771
  }
679
772
  /**
680
773
  * A generic method to make PUT rest call
@@ -682,20 +775,24 @@ var _RestClient = class _RestClient {
682
775
  * @param endpoint
683
776
  * @param headers
684
777
  * @param payload
685
- * @returns {Promise<any>}
778
+ * @param parsed
779
+ * @returns {Promise<Response | any>}
686
780
  */
687
- async put(endpoint, headers = {}, payload = null) {
688
- return await this.apiCall(endpoint, "PUT", headers, payload);
781
+ async put(endpoint, headers = {}, payload = null, parsed = true) {
782
+ const response = await this.makeRequest(endpoint, "PUT", headers, payload);
783
+ return parsed ? await this.parseResponse(response) : response;
689
784
  }
690
785
  /**
691
786
  * A generic method to make DELETE rest call
692
787
  *
693
788
  * @param endpoint
694
789
  * @param headers
695
- * @returns {Promise<any>}
790
+ * @param parsed
791
+ * @returns {Promise<Response | any>}
696
792
  */
697
- async delete(endpoint, headers = {}) {
698
- return await this.apiCall(endpoint, "DELETE", headers);
793
+ async delete(endpoint, headers = {}, parsed = true) {
794
+ const response = await this.makeRequest(endpoint, "DELETE", headers);
795
+ return parsed ? await this.parseResponse(response) : response;
699
796
  }
700
797
  /**
701
798
  * A generic method to make rest call
@@ -705,6 +802,7 @@ var _RestClient = class _RestClient {
705
802
  * @param headers
706
803
  * @param payload
707
804
  * @returns {Promise<any>}
805
+ * @deprecated Use makeRequest() and parseResponse() methods instead
708
806
  */
709
807
  async apiCall(endpoint, method = "POST", headers = {}, payload = null) {
710
808
  let options = {