astro-tokenkit 1.0.13 → 1.0.15

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/README.md CHANGED
@@ -116,6 +116,7 @@ const specializedClient = createClient({
116
116
  | `interceptors`| `InterceptorsConfig` | Request/Response/Error interceptors. |
117
117
  | `context` | `AsyncLocalStorage` | External AsyncLocalStorage instance. |
118
118
  | `getContextStore`| `() => TokenKitContext`| Custom method to retrieve the context store. |
119
+ | `setContextStore`| `(ctx) => void`| Custom method to set the context store. |
119
120
  | `runWithContext`| `Function`| Custom runner to bind context. |
120
121
 
121
122
  ### Auth Configuration
package/dist/index.cjs CHANGED
@@ -826,14 +826,30 @@ function sleep(ms) {
826
826
  }
827
827
 
828
828
  // packages/astro-tokenkit/src/middleware.ts
829
+ const LOGGED_KEY = Symbol.for('astro-tokenkit.middleware.logged');
829
830
  /**
830
831
  * Create middleware for context binding and automatic token rotation
831
832
  */
832
833
  function createMiddleware() {
833
834
  return (ctx, next) => __awaiter(this, void 0, void 0, function* () {
834
- var _a;
835
835
  const tokenManager = getTokenManager();
836
836
  const config = getConfig();
837
+ const globalStorage = globalThis;
838
+ if (!globalStorage[LOGGED_KEY]) {
839
+ const authStatus = tokenManager ? 'enabled' : 'disabled';
840
+ let contextStrategy = 'default';
841
+ if (config.runWithContext) {
842
+ contextStrategy = 'custom (runWithContext)';
843
+ }
844
+ else if (config.setContextStore) {
845
+ contextStrategy = 'custom (getter/setter)';
846
+ }
847
+ else if (config.context) {
848
+ contextStrategy = 'custom (external AsyncLocalStorage)';
849
+ }
850
+ console.log(`[TokenKit] Middleware initialized (auth: ${authStatus}, context: ${contextStrategy})`);
851
+ globalStorage[LOGGED_KEY] = true;
852
+ }
837
853
  const runLogic = () => __awaiter(this, void 0, void 0, function* () {
838
854
  // Proactively ensure a valid session if auth is configured
839
855
  if (tokenManager) {
@@ -848,22 +864,19 @@ function createMiddleware() {
848
864
  }
849
865
  return next();
850
866
  });
851
- // If getContextStore is defined, it means the context is managed externally (e.g., by a superior ALS)
852
- // We skip runWithContext to avoid nesting ALS.run() unnecessarily,
853
- // UNLESS a custom runWithContext is provided.
854
- if (config.getContextStore && !config.runWithContext) {
855
- let storage = config.getContextStore();
856
- if (storage)
857
- // Update existing reference
858
- storage.cookies = ctx.cookies;
859
- else if (config.setContextStore)
860
- config.setContextStore({ cookies: ctx.cookies });
861
- else
862
- console.error("[TokenKit] getContextStore returned null or undefined and no setter was found");
867
+ const setupAndRun = () => __awaiter(this, void 0, void 0, function* () {
868
+ if (config.setContextStore) {
869
+ config.setContextStore(ctx);
870
+ }
863
871
  return runLogic();
872
+ });
873
+ if (config.runWithContext) {
874
+ return config.runWithContext(ctx, setupAndRun);
875
+ }
876
+ if (config.setContextStore) {
877
+ return setupAndRun();
864
878
  }
865
- const runner = (_a = config.runWithContext) !== null && _a !== void 0 ? _a : runWithContext;
866
- return runner(ctx, runLogic);
879
+ return runWithContext(ctx, runLogic);
867
880
  });
868
881
  }
869
882