astro-tokenkit 1.0.12 → 1.0.14

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/config.js CHANGED
@@ -1,43 +1,63 @@
1
1
  // packages/astro-tokenkit/src/config.ts
2
2
  import { TokenManager } from "./auth/manager";
3
- let config = {
4
- runWithContext: undefined,
5
- getContextStore: undefined,
6
- setContextStore: undefined,
7
- baseURL: "",
8
- };
9
- let tokenManager;
3
+ const CONFIG_KEY = Symbol.for('astro-tokenkit.config');
4
+ const MANAGER_KEY = Symbol.for('astro-tokenkit.manager');
5
+ const globalStorage = globalThis;
6
+ // Initialize global storage if not present
7
+ if (!globalStorage[CONFIG_KEY]) {
8
+ globalStorage[CONFIG_KEY] = {
9
+ runWithContext: undefined,
10
+ getContextStore: undefined,
11
+ setContextStore: undefined,
12
+ baseURL: "",
13
+ };
14
+ }
10
15
  /**
11
16
  * Set configuration
12
17
  */
13
18
  export function setConfig(userConfig) {
14
- const finalConfig = Object.assign(Object.assign({}, config), userConfig);
19
+ const currentConfig = globalStorage[CONFIG_KEY];
20
+ const finalConfig = Object.assign(Object.assign({}, currentConfig), userConfig);
15
21
  // Validate that getter and setter are defined together
16
22
  if ((finalConfig.getContextStore && !finalConfig.setContextStore) ||
17
23
  (!finalConfig.getContextStore && finalConfig.setContextStore)) {
18
24
  throw new Error("[TokenKit] getContextStore and setContextStore must be defined together.");
19
25
  }
20
- config = finalConfig;
26
+ globalStorage[CONFIG_KEY] = finalConfig;
21
27
  // Re-initialize global token manager if auth changed
22
- if (config.auth) {
23
- tokenManager = new TokenManager(config.auth, config.baseURL);
28
+ if (finalConfig.auth) {
29
+ globalStorage[MANAGER_KEY] = new TokenManager(finalConfig.auth, finalConfig.baseURL);
30
+ }
31
+ else {
32
+ globalStorage[MANAGER_KEY] = undefined;
24
33
  }
25
34
  }
26
35
  /**
27
36
  * Get current configuration
28
37
  */
29
38
  export function getConfig() {
30
- return config;
39
+ return globalStorage[CONFIG_KEY];
31
40
  }
32
41
  /**
33
42
  * Get global token manager
34
43
  */
35
44
  export function getTokenManager() {
36
- return tokenManager;
45
+ return globalStorage[MANAGER_KEY];
37
46
  }
38
47
  /**
39
48
  * Set global token manager (mainly for testing)
40
49
  */
41
50
  export function setTokenManager(manager) {
42
- tokenManager = manager;
51
+ globalStorage[MANAGER_KEY] = manager;
52
+ }
53
+ // Handle injected configuration from Astro integration
54
+ try {
55
+ // @ts-ignore
56
+ const injectedConfig = typeof __TOKENKIT_CONFIG__ !== 'undefined' ? __TOKENKIT_CONFIG__ : undefined;
57
+ if (injectedConfig) {
58
+ setConfig(injectedConfig);
59
+ }
60
+ }
61
+ catch (e) {
62
+ // Ignore errors in environments where __TOKENKIT_CONFIG__ might be restricted
43
63
  }
package/dist/index.cjs CHANGED
@@ -689,46 +689,66 @@ class TokenManager {
689
689
  }
690
690
 
691
691
  // packages/astro-tokenkit/src/config.ts
692
- let config = {
693
- runWithContext: undefined,
694
- getContextStore: undefined,
695
- setContextStore: undefined,
696
- baseURL: "",
697
- };
698
- let tokenManager;
692
+ const CONFIG_KEY = Symbol.for('astro-tokenkit.config');
693
+ const MANAGER_KEY = Symbol.for('astro-tokenkit.manager');
694
+ const globalStorage = globalThis;
695
+ // Initialize global storage if not present
696
+ if (!globalStorage[CONFIG_KEY]) {
697
+ globalStorage[CONFIG_KEY] = {
698
+ runWithContext: undefined,
699
+ getContextStore: undefined,
700
+ setContextStore: undefined,
701
+ baseURL: "",
702
+ };
703
+ }
699
704
  /**
700
705
  * Set configuration
701
706
  */
702
707
  function setConfig(userConfig) {
703
- const finalConfig = Object.assign(Object.assign({}, config), userConfig);
708
+ const currentConfig = globalStorage[CONFIG_KEY];
709
+ const finalConfig = Object.assign(Object.assign({}, currentConfig), userConfig);
704
710
  // Validate that getter and setter are defined together
705
711
  if ((finalConfig.getContextStore && !finalConfig.setContextStore) ||
706
712
  (!finalConfig.getContextStore && finalConfig.setContextStore)) {
707
713
  throw new Error("[TokenKit] getContextStore and setContextStore must be defined together.");
708
714
  }
709
- config = finalConfig;
715
+ globalStorage[CONFIG_KEY] = finalConfig;
710
716
  // Re-initialize global token manager if auth changed
711
- if (config.auth) {
712
- tokenManager = new TokenManager(config.auth, config.baseURL);
717
+ if (finalConfig.auth) {
718
+ globalStorage[MANAGER_KEY] = new TokenManager(finalConfig.auth, finalConfig.baseURL);
719
+ }
720
+ else {
721
+ globalStorage[MANAGER_KEY] = undefined;
713
722
  }
714
723
  }
715
724
  /**
716
725
  * Get current configuration
717
726
  */
718
727
  function getConfig() {
719
- return config;
728
+ return globalStorage[CONFIG_KEY];
720
729
  }
721
730
  /**
722
731
  * Get global token manager
723
732
  */
724
733
  function getTokenManager() {
725
- return tokenManager;
734
+ return globalStorage[MANAGER_KEY];
726
735
  }
727
736
  /**
728
737
  * Set global token manager (mainly for testing)
729
738
  */
730
739
  function setTokenManager(manager) {
731
- tokenManager = manager;
740
+ globalStorage[MANAGER_KEY] = manager;
741
+ }
742
+ // Handle injected configuration from Astro integration
743
+ try {
744
+ // @ts-ignore
745
+ const injectedConfig = typeof __TOKENKIT_CONFIG__ !== 'undefined' ? __TOKENKIT_CONFIG__ : undefined;
746
+ if (injectedConfig) {
747
+ setConfig(injectedConfig);
748
+ }
749
+ }
750
+ catch (e) {
751
+ // Ignore errors in environments where __TOKENKIT_CONFIG__ might be restricted
732
752
  }
733
753
 
734
754
  // packages/astro-tokenkit/src/client/context.ts
@@ -811,7 +831,6 @@ function sleep(ms) {
811
831
  */
812
832
  function createMiddleware() {
813
833
  return (ctx, next) => __awaiter(this, void 0, void 0, function* () {
814
- var _a;
815
834
  const tokenManager = getTokenManager();
816
835
  const config = getConfig();
817
836
  const runLogic = () => __awaiter(this, void 0, void 0, function* () {
@@ -828,22 +847,19 @@ function createMiddleware() {
828
847
  }
829
848
  return next();
830
849
  });
831
- // If getContextStore is defined, it means the context is managed externally (e.g., by a superior ALS)
832
- // We skip runWithContext to avoid nesting ALS.run() unnecessarily,
833
- // UNLESS a custom runWithContext is provided.
834
- if (config.getContextStore && !config.runWithContext) {
835
- let storage = config.getContextStore();
836
- if (storage)
837
- // Update existing reference
838
- storage.cookies = ctx.cookies;
839
- else if (config.setContextStore)
840
- config.setContextStore({ cookies: ctx.cookies });
841
- else
842
- console.error("[TokenKit] getContextStore returned null or undefined and no setter was found");
850
+ const setupAndRun = () => __awaiter(this, void 0, void 0, function* () {
851
+ if (config.setContextStore) {
852
+ config.setContextStore(ctx);
853
+ }
843
854
  return runLogic();
855
+ });
856
+ if (config.runWithContext) {
857
+ return config.runWithContext(ctx, setupAndRun);
858
+ }
859
+ if (config.setContextStore) {
860
+ return setupAndRun();
844
861
  }
845
- const runner = (_a = config.runWithContext) !== null && _a !== void 0 ? _a : runWithContext;
846
- return runner(ctx, runLogic);
862
+ return runWithContext(ctx, runLogic);
847
863
  });
848
864
  }
849
865
 
@@ -1213,11 +1229,23 @@ function createClient(config) {
1213
1229
  */
1214
1230
  function tokenKit(config) {
1215
1231
  setConfig(config);
1232
+ // Create a serializable version of the config for the runtime
1233
+ const serializableConfig = JSON.parse(JSON.stringify(config, (key, value) => {
1234
+ if (typeof value === 'function')
1235
+ return undefined;
1236
+ return value;
1237
+ }));
1216
1238
  return {
1217
1239
  name: 'astro-tokenkit',
1218
1240
  hooks: {
1219
- 'astro:config:setup': () => {
1220
- // Future-proofing: could add vite aliases or other setup here
1241
+ 'astro:config:setup': ({ updateConfig }) => {
1242
+ updateConfig({
1243
+ vite: {
1244
+ define: {
1245
+ '__TOKENKIT_CONFIG__': JSON.stringify(serializableConfig)
1246
+ }
1247
+ }
1248
+ });
1221
1249
  console.log('[TokenKit] Integration initialized');
1222
1250
  },
1223
1251
  },