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