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 +1 -0
- package/dist/config.js +34 -14
- package/dist/index.cjs +59 -31
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +59 -31
- package/dist/index.js.map +1 -1
- package/dist/integration.js +14 -2
- package/dist/middleware.js +11 -15
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -687,46 +687,66 @@ class TokenManager {
|
|
|
687
687
|
}
|
|
688
688
|
|
|
689
689
|
// packages/astro-tokenkit/src/config.ts
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
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
|
|
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
|
-
|
|
713
|
+
globalStorage[CONFIG_KEY] = finalConfig;
|
|
708
714
|
// Re-initialize global token manager if auth changed
|
|
709
|
-
if (
|
|
710
|
-
|
|
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
|
|
726
|
+
return globalStorage[CONFIG_KEY];
|
|
718
727
|
}
|
|
719
728
|
/**
|
|
720
729
|
* Get global token manager
|
|
721
730
|
*/
|
|
722
731
|
function getTokenManager() {
|
|
723
|
-
return
|
|
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
|
-
|
|
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
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
},
|