@justworkflowit/cdk-constructs 0.0.177 → 0.0.179

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.
@@ -60707,12 +60707,141 @@ var require_models = __commonJS({
60707
60707
  }
60708
60708
  });
60709
60709
 
60710
+ // ../JustWorkflowItApiClient/dist-cjs/createAuthenticatedClient.js
60711
+ var require_createAuthenticatedClient = __commonJS({
60712
+ "../JustWorkflowItApiClient/dist-cjs/createAuthenticatedClient.js"(exports2) {
60713
+ "use strict";
60714
+ Object.defineProperty(exports2, "__esModule", { value: true });
60715
+ exports2.createAuthenticatedClient = void 0;
60716
+ var JustWorkflowIt_1 = require_JustWorkflowIt();
60717
+ var TokenBucket = class {
60718
+ tokens;
60719
+ lastRefill;
60720
+ maxTokens;
60721
+ refillRate;
60722
+ constructor(config) {
60723
+ this.maxTokens = config.maxTokens;
60724
+ this.refillRate = config.refillRate;
60725
+ this.tokens = config.maxTokens;
60726
+ this.lastRefill = Date.now();
60727
+ }
60728
+ async acquire() {
60729
+ while (true) {
60730
+ this.refill();
60731
+ if (this.tokens >= 1) {
60732
+ this.tokens -= 1;
60733
+ return;
60734
+ }
60735
+ const waitMs = 1 / this.refillRate * 1e3;
60736
+ await new Promise((resolve) => setTimeout(resolve, waitMs));
60737
+ }
60738
+ }
60739
+ refill() {
60740
+ const now = Date.now();
60741
+ const elapsedSeconds = (now - this.lastRefill) / 1e3;
60742
+ const tokensToAdd = elapsedSeconds * this.refillRate;
60743
+ this.tokens = Math.min(this.maxTokens, this.tokens + tokensToAdd);
60744
+ this.lastRefill = now;
60745
+ }
60746
+ };
60747
+ function calculateBackoffDelay(attempt, baseDelayMs, maxDelayMs, enableJitter) {
60748
+ const exponentialDelay = baseDelayMs * Math.pow(2, attempt);
60749
+ const cappedDelay = Math.min(exponentialDelay, maxDelayMs);
60750
+ if (!enableJitter) {
60751
+ return cappedDelay;
60752
+ }
60753
+ return Math.floor(Math.random() * cappedDelay);
60754
+ }
60755
+ function isRetryableError(error2, retryableStatusCodes) {
60756
+ if (!error2 || typeof error2 !== "object") {
60757
+ return false;
60758
+ }
60759
+ const err = error2;
60760
+ const statusCode = err.$metadata?.httpStatusCode;
60761
+ if (statusCode && retryableStatusCodes.includes(statusCode)) {
60762
+ return true;
60763
+ }
60764
+ const errorName = err.name;
60765
+ if (errorName === "NetworkError" || errorName === "TimeoutError" || errorName === "ConnectTimeoutError") {
60766
+ return true;
60767
+ }
60768
+ return false;
60769
+ }
60770
+ var DEFAULT_RETRY_CONFIG = {
60771
+ maxAttempts: 3,
60772
+ baseDelayMs: 100,
60773
+ maxDelayMs: 5e3,
60774
+ enableJitter: true,
60775
+ retryableStatusCodes: [429, 500, 502, 503, 504]
60776
+ };
60777
+ function createAuthenticatedClient(config) {
60778
+ const endpoint2 = config.endpoint ?? "https://api.justworkflowit.com";
60779
+ const client = new JustWorkflowIt_1.JustWorkflowIt({ endpoint: endpoint2 });
60780
+ const getToken = typeof config.accessToken === "function" ? config.accessToken : () => Promise.resolve(config.accessToken);
60781
+ const retryConfig = config.retry === false ? false : {
60782
+ ...DEFAULT_RETRY_CONFIG,
60783
+ ...config.retry ?? {}
60784
+ };
60785
+ const rateLimiter = config.rateLimit ? new TokenBucket(config.rateLimit) : null;
60786
+ client.middlewareStack.add((next) => async (args) => {
60787
+ const request = args.request;
60788
+ const token = await getToken();
60789
+ request.headers["Authorization"] = `Bearer ${token}`;
60790
+ return next(args);
60791
+ }, {
60792
+ step: "build",
60793
+ name: "attachBearerToken",
60794
+ priority: "high"
60795
+ });
60796
+ if (rateLimiter) {
60797
+ client.middlewareStack.add((next) => async (args) => {
60798
+ await rateLimiter.acquire();
60799
+ return next(args);
60800
+ }, {
60801
+ step: "finalizeRequest",
60802
+ name: "rateLimiter",
60803
+ priority: "high"
60804
+ });
60805
+ }
60806
+ if (retryConfig) {
60807
+ client.middlewareStack.add((next) => async (args) => {
60808
+ let attempt = 0;
60809
+ let lastError;
60810
+ while (attempt <= retryConfig.maxAttempts) {
60811
+ try {
60812
+ return await next(args);
60813
+ } catch (error2) {
60814
+ lastError = error2;
60815
+ if (attempt >= retryConfig.maxAttempts) {
60816
+ throw error2;
60817
+ }
60818
+ if (!isRetryableError(error2, retryConfig.retryableStatusCodes)) {
60819
+ throw error2;
60820
+ }
60821
+ const delayMs = calculateBackoffDelay(attempt, retryConfig.baseDelayMs, retryConfig.maxDelayMs, retryConfig.enableJitter);
60822
+ await new Promise((resolve) => setTimeout(resolve, delayMs));
60823
+ attempt++;
60824
+ }
60825
+ }
60826
+ throw lastError;
60827
+ }, {
60828
+ step: "finalizeRequest",
60829
+ name: "retryWithBackoff",
60830
+ priority: "low"
60831
+ });
60832
+ }
60833
+ return client;
60834
+ }
60835
+ exports2.createAuthenticatedClient = createAuthenticatedClient;
60836
+ }
60837
+ });
60838
+
60710
60839
  // ../JustWorkflowItApiClient/dist-cjs/index.js
60711
60840
  var require_dist_cjs91 = __commonJS({
60712
60841
  "../JustWorkflowItApiClient/dist-cjs/index.js"(exports2) {
60713
60842
  "use strict";
60714
60843
  Object.defineProperty(exports2, "__esModule", { value: true });
60715
- exports2.JustWorkflowItServiceException = void 0;
60844
+ exports2.createAuthenticatedClient = exports2.JustWorkflowItServiceException = void 0;
60716
60845
  var tslib_1 = (init_tslib_es6(), __toCommonJS(tslib_es6_exports));
60717
60846
  tslib_1.__exportStar(require_JustWorkflowItClient(), exports2);
60718
60847
  tslib_1.__exportStar(require_JustWorkflowIt(), exports2);
@@ -60722,6 +60851,10 @@ var require_dist_cjs91 = __commonJS({
60722
60851
  Object.defineProperty(exports2, "JustWorkflowItServiceException", { enumerable: true, get: function() {
60723
60852
  return JustWorkflowItServiceException_1.JustWorkflowItServiceException;
60724
60853
  } });
60854
+ var createAuthenticatedClient_1 = require_createAuthenticatedClient();
60855
+ Object.defineProperty(exports2, "createAuthenticatedClient", { enumerable: true, get: function() {
60856
+ return createAuthenticatedClient_1.createAuthenticatedClient;
60857
+ } });
60725
60858
  }
60726
60859
  });
60727
60860
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@justworkflowit/cdk-constructs",
3
3
  "description": "",
4
- "version": "0.0.177",
4
+ "version": "0.0.179",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "publishConfig": {