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.
@@ -10,14 +10,30 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  import { runWithContext as defaultRunWithContext } from './client/context';
12
12
  import { getConfig, getTokenManager } from './config';
13
+ const LOGGED_KEY = Symbol.for('astro-tokenkit.middleware.logged');
13
14
  /**
14
15
  * Create middleware for context binding and automatic token rotation
15
16
  */
16
17
  export function createMiddleware() {
17
18
  return (ctx, next) => __awaiter(this, void 0, void 0, function* () {
18
- var _a;
19
19
  const tokenManager = getTokenManager();
20
20
  const config = getConfig();
21
+ const globalStorage = globalThis;
22
+ if (!globalStorage[LOGGED_KEY]) {
23
+ const authStatus = tokenManager ? 'enabled' : 'disabled';
24
+ let contextStrategy = 'default';
25
+ if (config.runWithContext) {
26
+ contextStrategy = 'custom (runWithContext)';
27
+ }
28
+ else if (config.setContextStore) {
29
+ contextStrategy = 'custom (getter/setter)';
30
+ }
31
+ else if (config.context) {
32
+ contextStrategy = 'custom (external AsyncLocalStorage)';
33
+ }
34
+ console.log(`[TokenKit] Middleware initialized (auth: ${authStatus}, context: ${contextStrategy})`);
35
+ globalStorage[LOGGED_KEY] = true;
36
+ }
21
37
  const runLogic = () => __awaiter(this, void 0, void 0, function* () {
22
38
  // Proactively ensure a valid session if auth is configured
23
39
  if (tokenManager) {
@@ -32,21 +48,18 @@ export function createMiddleware() {
32
48
  }
33
49
  return next();
34
50
  });
35
- // If getContextStore is defined, it means the context is managed externally (e.g., by a superior ALS)
36
- // We skip runWithContext to avoid nesting ALS.run() unnecessarily,
37
- // UNLESS a custom runWithContext is provided.
38
- if (config.getContextStore && !config.runWithContext) {
39
- let storage = config.getContextStore();
40
- if (storage)
41
- // Update existing reference
42
- storage.cookies = ctx.cookies;
43
- else if (config.setContextStore)
44
- config.setContextStore({ cookies: ctx.cookies });
45
- else
46
- console.error("[TokenKit] getContextStore returned null or undefined and no setter was found");
51
+ const setupAndRun = () => __awaiter(this, void 0, void 0, function* () {
52
+ if (config.setContextStore) {
53
+ config.setContextStore(ctx);
54
+ }
47
55
  return runLogic();
56
+ });
57
+ if (config.runWithContext) {
58
+ return config.runWithContext(ctx, setupAndRun);
59
+ }
60
+ if (config.setContextStore) {
61
+ return setupAndRun();
48
62
  }
49
- const runner = (_a = config.runWithContext) !== null && _a !== void 0 ? _a : defaultRunWithContext;
50
- return runner(ctx, runLogic);
63
+ return defaultRunWithContext(ctx, runLogic);
51
64
  });
52
65
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "astro-tokenkit",
3
- "version": "1.0.13",
3
+ "version": "1.0.15",
4
4
  "description": "A powerful API client for Astro with automatic token rotation, session management, and seamless context integration.",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",