@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.mjs CHANGED
@@ -15,13 +15,13 @@ var HttpStatus = /* @__PURE__ */ ((HttpStatus2) => {
15
15
  return HttpStatus2;
16
16
  })(HttpStatus || {});
17
17
  var HttpMethod = /* @__PURE__ */ ((HttpMethod3) => {
18
- HttpMethod3["GET"] = "get";
19
- HttpMethod3["POST"] = "post";
20
- HttpMethod3["PUT"] = "put";
21
- HttpMethod3["DELETE"] = "delete";
22
- HttpMethod3["PATCH"] = "patch";
23
- HttpMethod3["HEAD"] = "head";
24
- HttpMethod3["OPTIONS"] = "options";
18
+ HttpMethod3["GET"] = "GET";
19
+ HttpMethod3["POST"] = "POST";
20
+ HttpMethod3["PUT"] = "PUT";
21
+ HttpMethod3["DELETE"] = "DELETE";
22
+ HttpMethod3["PATCH"] = "PATCH";
23
+ HttpMethod3["HEAD"] = "HEAD";
24
+ HttpMethod3["OPTIONS"] = "OPTIONS";
25
25
  return HttpMethod3;
26
26
  })(HttpMethod || {});
27
27
 
@@ -195,9 +195,9 @@ var _RuntimeAction = class _RuntimeAction {
195
195
  if (errorMessage) {
196
196
  return response_default.error(400 /* BAD_REQUEST */, errorMessage);
197
197
  }
198
- const requestMethod = params.__ow_method;
198
+ const requestMethod = params.__ow_method?.toUpperCase();
199
199
  if (httpMethods.length > 0 && !httpMethods.includes(requestMethod)) {
200
- const errorMessage2 = `Invalid HTTP method: ${requestMethod}. Allowed methods are: ${httpMethods.join(", ")}`;
200
+ const errorMessage2 = `Invalid HTTP method: ${params.__ow_method}. Allowed methods are: ${httpMethods.join(", ")}`;
201
201
  logger.error(errorMessage2);
202
202
  return response_default.error(405 /* METHOD_NOT_ALLOWED */, errorMessage2);
203
203
  }
@@ -258,7 +258,7 @@ var _GraphQlAction = class _GraphQlAction {
258
258
  }, name = "main", disableIntrospection = false) {
259
259
  return runtime_action_default.execute(
260
260
  `graphql-${name}`,
261
- ["get" /* GET */, "post" /* POST */],
261
+ ["GET" /* GET */, "POST" /* POST */],
262
262
  ["query"],
263
263
  [],
264
264
  async (params, ctx) => {
@@ -488,21 +488,31 @@ var file_repository_default = FileRepository;
488
488
  // src/integration/bearer-token/index.ts
489
489
  var _BearerToken = class _BearerToken {
490
490
  /**
491
- * Extracts the Bearer token from OpenWhisk action parameters and returns detailed token information.
492
- * Looks for the authorization header in __ow_headers and extracts the token value
493
- * after the "Bearer " prefix.
491
+ * Extracts the Bearer token from HTTP request headers and returns detailed token information.
492
+ * Supports both standard HTTP headers and OpenWhisk action parameter formats.
494
493
  *
495
- * @param params - OpenWhisk action input parameters containing headers
494
+ * @param headersOrParams - Either a standard headers object or OpenWhisk action parameters
496
495
  * @returns Detailed token information object
497
496
  *
498
497
  * @example
498
+ * // Standard HTTP headers approach
499
+ * const headers = {
500
+ * authorization: 'Bearer abc123token'
501
+ * };
502
+ * const tokenInfo = BearerToken.extract(headers);
503
+ *
504
+ * @example
505
+ * // OpenWhisk action parameters (backward compatibility)
499
506
  * const params = {
500
507
  * __ow_headers: {
501
508
  * authorization: 'Bearer abc123token'
502
509
  * }
503
510
  * };
504
511
  * const tokenInfo = BearerToken.extract(params);
505
- * // returns: {
512
+ *
513
+ * @example
514
+ * // Both return the same result:
515
+ * // {
506
516
  * // token: 'abc123token',
507
517
  * // tokenLength: 11,
508
518
  * // isValid: true,
@@ -510,26 +520,54 @@ var _BearerToken = class _BearerToken {
510
520
  * // timeUntilExpiry: 3600000
511
521
  * // }
512
522
  */
513
- static extract(params) {
523
+ static extract(headersOrParams) {
514
524
  let token = null;
515
- if (params.__ow_headers?.authorization?.startsWith("Bearer ")) {
516
- token = params.__ow_headers.authorization.substring("Bearer ".length);
525
+ if (headersOrParams.authorization?.startsWith("Bearer ")) {
526
+ token = headersOrParams.authorization.substring("Bearer ".length);
527
+ } else if (headersOrParams.__ow_headers?.authorization?.startsWith("Bearer ")) {
528
+ token = headersOrParams.__ow_headers.authorization.substring("Bearer ".length);
517
529
  }
518
530
  return _BearerToken.info(token);
519
531
  }
520
532
  /**
521
- * Gets detailed information about a Bearer token
522
- * @param token - The Bearer token string (or null)
523
- * @returns {BearerTokenInfo} Detailed token information including validity and expiry
533
+ * Analyzes a Bearer token and returns detailed information including validity and expiry.
534
+ * Supports both JWT tokens (with automatic expiry detection) and plain tokens (24h default expiry).
535
+ *
536
+ * @param token - The Bearer token string (or null). Can be JWT or plain token.
537
+ * @returns Detailed token information object
524
538
  *
525
539
  * @example
526
- * const tokenInfo = BearerToken.info('abc123token');
540
+ * // Plain token (gets 24h default expiry)
541
+ * const plainTokenInfo = BearerToken.info('abc123token');
527
542
  * // returns: {
528
543
  * // token: 'abc123token',
529
544
  * // tokenLength: 11,
530
545
  * // isValid: true,
531
- * // expiry: '2024-01-01T12:00:00.000Z',
532
- * // timeUntilExpiry: 3600000
546
+ * // expiry: '2024-01-02T12:00:00.000Z', // 24h from now
547
+ * // timeUntilExpiry: 86400000 // milliseconds until expiry
548
+ * // }
549
+ *
550
+ * @example
551
+ * // JWT token (automatic expiry detection from 'exp' or 'expires_in' claims)
552
+ * const jwtToken = 'eyJhbGciOiJIUzI1NiJ9.eyJleHAiOjE3MDQ0Njc2MDB9.signature';
553
+ * const jwtTokenInfo = BearerToken.info(jwtToken);
554
+ * // returns: {
555
+ * // token: 'eyJhbGciOiJIUzI1NiJ9...',
556
+ * // tokenLength: 45,
557
+ * // isValid: true, // false if expired
558
+ * // expiry: '2024-01-05T12:00:00.000Z', // from JWT exp claim
559
+ * // timeUntilExpiry: 172800000 // actual time until expiry
560
+ * // }
561
+ *
562
+ * @example
563
+ * // Null or invalid token
564
+ * const nullTokenInfo = BearerToken.info(null);
565
+ * // returns: {
566
+ * // token: null,
567
+ * // tokenLength: 0,
568
+ * // isValid: false,
569
+ * // expiry: null,
570
+ * // timeUntilExpiry: null
533
571
  * // }
534
572
  */
535
573
  static info(token) {
@@ -595,14 +633,67 @@ var bearer_token_default = BearerToken;
595
633
  import fetch from "node-fetch";
596
634
  var _RestClient = class _RestClient {
597
635
  /**
598
- * A generic method to make GET rest call
636
+ * A completely raw method to make HTTP requests
599
637
  *
600
638
  * @param endpoint
639
+ * @param method
601
640
  * @param headers
641
+ * @param payload
642
+ * @returns {Promise<Response>}
643
+ */
644
+ async makeRequest(endpoint, method = "GET", headers = {}, payload = null) {
645
+ let options = {
646
+ method,
647
+ headers
648
+ };
649
+ if (payload !== null) {
650
+ options = {
651
+ ...options,
652
+ body: JSON.stringify(payload),
653
+ headers: {
654
+ ...headers,
655
+ "Content-Type": "application/json"
656
+ }
657
+ };
658
+ }
659
+ return await fetch(endpoint, options);
660
+ }
661
+ /**
662
+ * A method to parse HTTP response
663
+ *
664
+ * @param response
602
665
  * @returns {Promise<any>}
603
666
  */
604
- async get(endpoint, headers = {}) {
605
- return await this.apiCall(endpoint, "GET", headers);
667
+ async parseResponse(response) {
668
+ if (!response.ok) {
669
+ throw new Error(`HTTP error! status: ${response.status}`);
670
+ }
671
+ if (response.status === 204 || response.headers?.get("content-length") === "0") {
672
+ return null;
673
+ }
674
+ if (typeof response.json === "function") {
675
+ const contentType = response.headers?.get("content-type");
676
+ if (!contentType || contentType.includes("application/json") || contentType.includes("application/hal+json")) {
677
+ return await response.json();
678
+ }
679
+ }
680
+ if (typeof response.text === "function") {
681
+ const text = await response.text();
682
+ return text;
683
+ }
684
+ return null;
685
+ }
686
+ /**
687
+ * A generic method to make GET rest call
688
+ *
689
+ * @param endpoint
690
+ * @param headers
691
+ * @param parsed
692
+ * @returns {Promise<Response | any>}
693
+ */
694
+ async get(endpoint, headers = {}, parsed = true) {
695
+ const response = await this.makeRequest(endpoint, "GET", headers);
696
+ return parsed ? await this.parseResponse(response) : response;
606
697
  }
607
698
  /**
608
699
  * A generic method to make POST rest call
@@ -610,10 +701,12 @@ var _RestClient = class _RestClient {
610
701
  * @param endpoint
611
702
  * @param headers
612
703
  * @param payload
613
- * @returns {Promise<any>}
704
+ * @param parsed
705
+ * @returns {Promise<Response | any>}
614
706
  */
615
- async post(endpoint, headers = {}, payload = null) {
616
- return await this.apiCall(endpoint, "POST", headers, payload);
707
+ async post(endpoint, headers = {}, payload = null, parsed = true) {
708
+ const response = await this.makeRequest(endpoint, "POST", headers, payload);
709
+ return parsed ? await this.parseResponse(response) : response;
617
710
  }
618
711
  /**
619
712
  * A generic method to make PUT rest call
@@ -621,20 +714,24 @@ var _RestClient = class _RestClient {
621
714
  * @param endpoint
622
715
  * @param headers
623
716
  * @param payload
624
- * @returns {Promise<any>}
717
+ * @param parsed
718
+ * @returns {Promise<Response | any>}
625
719
  */
626
- async put(endpoint, headers = {}, payload = null) {
627
- return await this.apiCall(endpoint, "PUT", headers, payload);
720
+ async put(endpoint, headers = {}, payload = null, parsed = true) {
721
+ const response = await this.makeRequest(endpoint, "PUT", headers, payload);
722
+ return parsed ? await this.parseResponse(response) : response;
628
723
  }
629
724
  /**
630
725
  * A generic method to make DELETE rest call
631
726
  *
632
727
  * @param endpoint
633
728
  * @param headers
634
- * @returns {Promise<any>}
729
+ * @param parsed
730
+ * @returns {Promise<Response | any>}
635
731
  */
636
- async delete(endpoint, headers = {}) {
637
- return await this.apiCall(endpoint, "DELETE", headers);
732
+ async delete(endpoint, headers = {}, parsed = true) {
733
+ const response = await this.makeRequest(endpoint, "DELETE", headers);
734
+ return parsed ? await this.parseResponse(response) : response;
638
735
  }
639
736
  /**
640
737
  * A generic method to make rest call
@@ -644,6 +741,7 @@ var _RestClient = class _RestClient {
644
741
  * @param headers
645
742
  * @param payload
646
743
  * @returns {Promise<any>}
744
+ * @deprecated Use makeRequest() and parseResponse() methods instead
647
745
  */
648
746
  async apiCall(endpoint, method = "POST", headers = {}, payload = null) {
649
747
  let options = {