astro-tokenkit 1.0.12 → 1.0.13
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/config.js +34 -14
- package/dist/index.cjs +48 -16
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +48 -16
- package/dist/index.js.map +1 -1
- package/dist/integration.js +14 -2
- package/package.json +1 -1
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
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
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
|
|
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
|
-
|
|
26
|
+
globalStorage[CONFIG_KEY] = finalConfig;
|
|
21
27
|
// Re-initialize global token manager if auth changed
|
|
22
|
-
if (
|
|
23
|
-
|
|
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
|
|
39
|
+
return globalStorage[CONFIG_KEY];
|
|
31
40
|
}
|
|
32
41
|
/**
|
|
33
42
|
* Get global token manager
|
|
34
43
|
*/
|
|
35
44
|
export function getTokenManager() {
|
|
36
|
-
return
|
|
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
|
-
|
|
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
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
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
|
|
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
|
-
|
|
715
|
+
globalStorage[CONFIG_KEY] = finalConfig;
|
|
710
716
|
// Re-initialize global token manager if auth changed
|
|
711
|
-
if (
|
|
712
|
-
|
|
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
|
|
728
|
+
return globalStorage[CONFIG_KEY];
|
|
720
729
|
}
|
|
721
730
|
/**
|
|
722
731
|
* Get global token manager
|
|
723
732
|
*/
|
|
724
733
|
function getTokenManager() {
|
|
725
|
-
return
|
|
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
|
-
|
|
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
|
|
@@ -1213,11 +1233,23 @@ function createClient(config) {
|
|
|
1213
1233
|
*/
|
|
1214
1234
|
function tokenKit(config) {
|
|
1215
1235
|
setConfig(config);
|
|
1236
|
+
// Create a serializable version of the config for the runtime
|
|
1237
|
+
const serializableConfig = JSON.parse(JSON.stringify(config, (key, value) => {
|
|
1238
|
+
if (typeof value === 'function')
|
|
1239
|
+
return undefined;
|
|
1240
|
+
return value;
|
|
1241
|
+
}));
|
|
1216
1242
|
return {
|
|
1217
1243
|
name: 'astro-tokenkit',
|
|
1218
1244
|
hooks: {
|
|
1219
|
-
'astro:config:setup': () => {
|
|
1220
|
-
|
|
1245
|
+
'astro:config:setup': ({ updateConfig }) => {
|
|
1246
|
+
updateConfig({
|
|
1247
|
+
vite: {
|
|
1248
|
+
define: {
|
|
1249
|
+
'__TOKENKIT_CONFIG__': JSON.stringify(serializableConfig)
|
|
1250
|
+
}
|
|
1251
|
+
}
|
|
1252
|
+
});
|
|
1221
1253
|
console.log('[TokenKit] Integration initialized');
|
|
1222
1254
|
},
|
|
1223
1255
|
},
|