@molecule/app-svelte 1.0.0
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/LICENSE +115 -0
- package/README.md +2032 -0
- package/dist/context.d.ts +126 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +217 -0
- package/dist/context.js.map +1 -0
- package/dist/index.d.ts +60 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +60 -0
- package/dist/index.js.map +1 -0
- package/dist/stores/async-state.d.ts +47 -0
- package/dist/stores/async-state.d.ts.map +1 -0
- package/dist/stores/async-state.js +89 -0
- package/dist/stores/async-state.js.map +1 -0
- package/dist/stores/auth.d.ts +54 -0
- package/dist/stores/auth.d.ts.map +1 -0
- package/dist/stores/auth.js +93 -0
- package/dist/stores/auth.js.map +1 -0
- package/dist/stores/capacitor-app.d.ts +46 -0
- package/dist/stores/capacitor-app.d.ts.map +1 -0
- package/dist/stores/capacitor-app.js +59 -0
- package/dist/stores/capacitor-app.js.map +1 -0
- package/dist/stores/change-password.d.ts +17 -0
- package/dist/stores/change-password.d.ts.map +1 -0
- package/dist/stores/change-password.js +17 -0
- package/dist/stores/change-password.js.map +1 -0
- package/dist/stores/device.d.ts +39 -0
- package/dist/stores/device.d.ts.map +1 -0
- package/dist/stores/device.js +51 -0
- package/dist/stores/device.js.map +1 -0
- package/dist/stores/forms.d.ts +121 -0
- package/dist/stores/forms.d.ts.map +1 -0
- package/dist/stores/forms.js +178 -0
- package/dist/stores/forms.js.map +1 -0
- package/dist/stores/http.d.ts +119 -0
- package/dist/stores/http.d.ts.map +1 -0
- package/dist/stores/http.js +206 -0
- package/dist/stores/http.js.map +1 -0
- package/dist/stores/i18n.d.ts +50 -0
- package/dist/stores/i18n.d.ts.map +1 -0
- package/dist/stores/i18n.js +67 -0
- package/dist/stores/i18n.js.map +1 -0
- package/dist/stores/index.d.ts +27 -0
- package/dist/stores/index.d.ts.map +1 -0
- package/dist/stores/index.js +27 -0
- package/dist/stores/index.js.map +1 -0
- package/dist/stores/logger.d.ts +83 -0
- package/dist/stores/logger.d.ts.map +1 -0
- package/dist/stores/logger.js +104 -0
- package/dist/stores/logger.js.map +1 -0
- package/dist/stores/login.d.ts +33 -0
- package/dist/stores/login.d.ts.map +1 -0
- package/dist/stores/login.js +32 -0
- package/dist/stores/login.js.map +1 -0
- package/dist/stores/oauth.d.ts +74 -0
- package/dist/stores/oauth.d.ts.map +1 -0
- package/dist/stores/oauth.js +183 -0
- package/dist/stores/oauth.js.map +1 -0
- package/dist/stores/password-reset.d.ts +31 -0
- package/dist/stores/password-reset.d.ts.map +1 -0
- package/dist/stores/password-reset.js +26 -0
- package/dist/stores/password-reset.js.map +1 -0
- package/dist/stores/platform.d.ts +41 -0
- package/dist/stores/platform.d.ts.map +1 -0
- package/dist/stores/platform.js +44 -0
- package/dist/stores/platform.js.map +1 -0
- package/dist/stores/promise.d.ts +41 -0
- package/dist/stores/promise.d.ts.map +1 -0
- package/dist/stores/promise.js +76 -0
- package/dist/stores/promise.js.map +1 -0
- package/dist/stores/push.d.ts +41 -0
- package/dist/stores/push.d.ts.map +1 -0
- package/dist/stores/push.js +73 -0
- package/dist/stores/push.js.map +1 -0
- package/dist/stores/router.d.ts +74 -0
- package/dist/stores/router.d.ts.map +1 -0
- package/dist/stores/router.js +131 -0
- package/dist/stores/router.js.map +1 -0
- package/dist/stores/signup.d.ts +18 -0
- package/dist/stores/signup.d.ts.map +1 -0
- package/dist/stores/signup.js +17 -0
- package/dist/stores/signup.js.map +1 -0
- package/dist/stores/state.d.ts +62 -0
- package/dist/stores/state.d.ts.map +1 -0
- package/dist/stores/state.js +74 -0
- package/dist/stores/state.js.map +1 -0
- package/dist/stores/storage.d.ts +76 -0
- package/dist/stores/storage.d.ts.map +1 -0
- package/dist/stores/storage.js +159 -0
- package/dist/stores/storage.js.map +1 -0
- package/dist/stores/theme.d.ts +68 -0
- package/dist/stores/theme.d.ts.map +1 -0
- package/dist/stores/theme.js +119 -0
- package/dist/stores/theme.js.map +1 -0
- package/dist/stores/version.d.ts +44 -0
- package/dist/stores/version.d.ts.map +1 -0
- package/dist/stores/version.js +73 -0
- package/dist/stores/version.js.map +1 -0
- package/dist/types.d.ts +46 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +7 -0
- package/dist/types.js.map +1 -0
- package/package.json +59 -0
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Svelte context for molecule.dev providers.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import type { AuthClient } from '@molecule/app-auth';
|
|
7
|
+
import type { HttpClient } from '@molecule/app-http';
|
|
8
|
+
import type { I18nProvider } from '@molecule/app-i18n';
|
|
9
|
+
import type { LoggerProvider } from '@molecule/app-logger';
|
|
10
|
+
import type { Router } from '@molecule/app-routing';
|
|
11
|
+
import type { StateProvider } from '@molecule/app-state';
|
|
12
|
+
import type { StorageProvider } from '@molecule/app-storage';
|
|
13
|
+
import type { ThemeProvider } from '@molecule/app-theme';
|
|
14
|
+
import type { MoleculeConfig } from './types.js';
|
|
15
|
+
/**
|
|
16
|
+
* Set all molecule providers in Svelte context.
|
|
17
|
+
*
|
|
18
|
+
* Call this in your root layout/component.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```svelte
|
|
22
|
+
* <script>
|
|
23
|
+
* import { setMoleculeContext } from '@molecule/app-svelte'
|
|
24
|
+
* import { provider as stateProvider } from '@molecule/app-state-zustand'
|
|
25
|
+
*
|
|
26
|
+
* setMoleculeContext({
|
|
27
|
+
* state: stateProvider,
|
|
28
|
+
* auth: authClient,
|
|
29
|
+
* theme: themeProvider,
|
|
30
|
+
* })
|
|
31
|
+
* </script>
|
|
32
|
+
*
|
|
33
|
+
* <slot />
|
|
34
|
+
* ```
|
|
35
|
+
* @param config - Provider instances to register (state, auth, theme, router, i18n, http, storage, logger).
|
|
36
|
+
*/
|
|
37
|
+
export declare function setMoleculeContext(config: MoleculeConfig): void;
|
|
38
|
+
/**
|
|
39
|
+
* Sets the state context.
|
|
40
|
+
* @param provider - The state provider to store in Svelte context.
|
|
41
|
+
*/
|
|
42
|
+
export declare function setStateContext(provider: StateProvider): void;
|
|
43
|
+
/**
|
|
44
|
+
* Sets the auth context.
|
|
45
|
+
* @param client - The auth client to store in Svelte context.
|
|
46
|
+
*/
|
|
47
|
+
export declare function setAuthContext<T>(client: AuthClient<T>): void;
|
|
48
|
+
/**
|
|
49
|
+
* Sets the theme context.
|
|
50
|
+
* @param provider - The theme provider to store in Svelte context.
|
|
51
|
+
*/
|
|
52
|
+
export declare function setThemeContext(provider: ThemeProvider): void;
|
|
53
|
+
/**
|
|
54
|
+
* Sets the router context.
|
|
55
|
+
* @param router - The router instance to store in Svelte context.
|
|
56
|
+
*/
|
|
57
|
+
export declare function setRouterContext(router: Router): void;
|
|
58
|
+
/**
|
|
59
|
+
* Sets the i18n context.
|
|
60
|
+
* @param provider - The i18n provider to store in Svelte context.
|
|
61
|
+
*/
|
|
62
|
+
export declare function setI18nContext(provider: I18nProvider): void;
|
|
63
|
+
/**
|
|
64
|
+
* Sets the HTTP context.
|
|
65
|
+
* @param client - The HTTP client to store in Svelte context.
|
|
66
|
+
*/
|
|
67
|
+
export declare function setHttpContext(client: HttpClient): void;
|
|
68
|
+
/**
|
|
69
|
+
* Sets the storage context.
|
|
70
|
+
* @param provider - The storage provider to store in Svelte context.
|
|
71
|
+
*/
|
|
72
|
+
export declare function setStorageContext(provider: StorageProvider): void;
|
|
73
|
+
/**
|
|
74
|
+
* Sets the logger context.
|
|
75
|
+
* @param provider - The logger provider to store in Svelte context.
|
|
76
|
+
*/
|
|
77
|
+
export declare function setLoggerContext(provider: LoggerProvider): void;
|
|
78
|
+
/**
|
|
79
|
+
* Gets the state provider from Svelte context.
|
|
80
|
+
* @returns The state provider instance.
|
|
81
|
+
* @throws {Error} If no state provider has been set in context.
|
|
82
|
+
*/
|
|
83
|
+
export declare function getStateProvider(): StateProvider;
|
|
84
|
+
/**
|
|
85
|
+
* Gets the auth client from Svelte context.
|
|
86
|
+
* @returns The auth client instance.
|
|
87
|
+
* @throws {Error} If no auth client has been set in context.
|
|
88
|
+
*/
|
|
89
|
+
export declare function getAuthClient<T = unknown>(): AuthClient<T>;
|
|
90
|
+
/**
|
|
91
|
+
* Gets the theme provider from Svelte context.
|
|
92
|
+
* @returns The theme provider instance.
|
|
93
|
+
* @throws {Error} If no theme provider has been set in context.
|
|
94
|
+
*/
|
|
95
|
+
export declare function getThemeProvider(): ThemeProvider;
|
|
96
|
+
/**
|
|
97
|
+
* Gets the router from Svelte context.
|
|
98
|
+
* @returns The router instance.
|
|
99
|
+
* @throws {Error} If no router has been set in context.
|
|
100
|
+
*/
|
|
101
|
+
export declare function getRouter(): Router;
|
|
102
|
+
/**
|
|
103
|
+
* Gets the i18n provider from Svelte context.
|
|
104
|
+
* @returns The i18n provider instance.
|
|
105
|
+
* @throws {Error} If no i18n provider has been set in context.
|
|
106
|
+
*/
|
|
107
|
+
export declare function getI18nProvider(): I18nProvider;
|
|
108
|
+
/**
|
|
109
|
+
* Gets the HTTP client from Svelte context.
|
|
110
|
+
* @returns The HTTP client instance.
|
|
111
|
+
* @throws {Error} If no HTTP client has been set in context.
|
|
112
|
+
*/
|
|
113
|
+
export declare function getHttpClient(): HttpClient;
|
|
114
|
+
/**
|
|
115
|
+
* Gets the storage provider from Svelte context.
|
|
116
|
+
* @returns The storage provider instance.
|
|
117
|
+
* @throws {Error} If no storage provider has been set in context.
|
|
118
|
+
*/
|
|
119
|
+
export declare function getStorageProvider(): StorageProvider;
|
|
120
|
+
/**
|
|
121
|
+
* Gets the logger provider from Svelte context.
|
|
122
|
+
* @returns The logger provider instance.
|
|
123
|
+
* @throws {Error} If no logger provider has been set in context.
|
|
124
|
+
*/
|
|
125
|
+
export declare function getLoggerProvider(): LoggerProvider;
|
|
126
|
+
//# sourceMappingURL=context.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1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,EAAE,eAAe,GAAG,IAAI,CAEjE;AAED;;;GAGG;AACH,wBAAgB,gBAAgB,CAAC,QAAQ,EAAE,cAAc,GAAG,IAAI,CAE/D;AAGD;;;;GAIG;AACH,wBAAgB,gBAAgB,IAAI,aAAa,CAShD;AAED;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,CAAC,GAAG,OAAO,KAAK,UAAU,CAAC,CAAC,CAAC,CAS1D;AAED;;;;GAIG;AACH,wBAAgB,gBAAgB,IAAI,aAAa,CAShD;AAED;;;;GAIG;AACH,wBAAgB,SAAS,IAAI,MAAM,CAOlC;AAED;;;;GAIG;AACH,wBAAgB,eAAe,IAAI,YAAY,CAS9C;AAED;;;;GAIG;AACH,wBAAgB,aAAa,IAAI,UAAU,CAS1C;AAED;;;;GAIG;AACH,wBAAgB,kBAAkB,IAAI,eAAe,CASpD;AAED;;;;GAIG;AACH,wBAAgB,iBAAiB,IAAI,cAAc,CASlD"}
|
package/dist/context.js
ADDED
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Svelte context for molecule.dev providers.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import { getContext, setContext } from 'svelte';
|
|
7
|
+
import { t } from '@molecule/app-i18n';
|
|
8
|
+
// Context keys
|
|
9
|
+
const STATE_KEY = Symbol('molecule-state');
|
|
10
|
+
const AUTH_KEY = Symbol('molecule-auth');
|
|
11
|
+
const THEME_KEY = Symbol('molecule-theme');
|
|
12
|
+
const ROUTER_KEY = Symbol('molecule-router');
|
|
13
|
+
const I18N_KEY = Symbol('molecule-i18n');
|
|
14
|
+
const HTTP_KEY = Symbol('molecule-http');
|
|
15
|
+
const STORAGE_KEY = Symbol('molecule-storage');
|
|
16
|
+
const LOGGER_KEY = Symbol('molecule-logger');
|
|
17
|
+
/**
|
|
18
|
+
* Set all molecule providers in Svelte context.
|
|
19
|
+
*
|
|
20
|
+
* Call this in your root layout/component.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```svelte
|
|
24
|
+
* <script>
|
|
25
|
+
* import { setMoleculeContext } from '@molecule/app-svelte'
|
|
26
|
+
* import { provider as stateProvider } from '@molecule/app-state-zustand'
|
|
27
|
+
*
|
|
28
|
+
* setMoleculeContext({
|
|
29
|
+
* state: stateProvider,
|
|
30
|
+
* auth: authClient,
|
|
31
|
+
* theme: themeProvider,
|
|
32
|
+
* })
|
|
33
|
+
* </script>
|
|
34
|
+
*
|
|
35
|
+
* <slot />
|
|
36
|
+
* ```
|
|
37
|
+
* @param config - Provider instances to register (state, auth, theme, router, i18n, http, storage, logger).
|
|
38
|
+
*/
|
|
39
|
+
export function setMoleculeContext(config) {
|
|
40
|
+
if (config.state)
|
|
41
|
+
setContext(STATE_KEY, config.state);
|
|
42
|
+
if (config.auth)
|
|
43
|
+
setContext(AUTH_KEY, config.auth);
|
|
44
|
+
if (config.theme)
|
|
45
|
+
setContext(THEME_KEY, config.theme);
|
|
46
|
+
if (config.router)
|
|
47
|
+
setContext(ROUTER_KEY, config.router);
|
|
48
|
+
if (config.i18n)
|
|
49
|
+
setContext(I18N_KEY, config.i18n);
|
|
50
|
+
if (config.http)
|
|
51
|
+
setContext(HTTP_KEY, config.http);
|
|
52
|
+
if (config.storage)
|
|
53
|
+
setContext(STORAGE_KEY, config.storage);
|
|
54
|
+
if (config.logger)
|
|
55
|
+
setContext(LOGGER_KEY, config.logger);
|
|
56
|
+
}
|
|
57
|
+
// Individual setters
|
|
58
|
+
/**
|
|
59
|
+
* Sets the state context.
|
|
60
|
+
* @param provider - The state provider to store in Svelte context.
|
|
61
|
+
*/
|
|
62
|
+
export function setStateContext(provider) {
|
|
63
|
+
setContext(STATE_KEY, provider);
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Sets the auth context.
|
|
67
|
+
* @param client - The auth client to store in Svelte context.
|
|
68
|
+
*/
|
|
69
|
+
export function setAuthContext(client) {
|
|
70
|
+
setContext(AUTH_KEY, client);
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Sets the theme context.
|
|
74
|
+
* @param provider - The theme provider to store in Svelte context.
|
|
75
|
+
*/
|
|
76
|
+
export function setThemeContext(provider) {
|
|
77
|
+
setContext(THEME_KEY, provider);
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Sets the router context.
|
|
81
|
+
* @param router - The router instance to store in Svelte context.
|
|
82
|
+
*/
|
|
83
|
+
export function setRouterContext(router) {
|
|
84
|
+
setContext(ROUTER_KEY, router);
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Sets the i18n context.
|
|
88
|
+
* @param provider - The i18n provider to store in Svelte context.
|
|
89
|
+
*/
|
|
90
|
+
export function setI18nContext(provider) {
|
|
91
|
+
setContext(I18N_KEY, provider);
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Sets the HTTP context.
|
|
95
|
+
* @param client - The HTTP client to store in Svelte context.
|
|
96
|
+
*/
|
|
97
|
+
export function setHttpContext(client) {
|
|
98
|
+
setContext(HTTP_KEY, client);
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Sets the storage context.
|
|
102
|
+
* @param provider - The storage provider to store in Svelte context.
|
|
103
|
+
*/
|
|
104
|
+
export function setStorageContext(provider) {
|
|
105
|
+
setContext(STORAGE_KEY, provider);
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Sets the logger context.
|
|
109
|
+
* @param provider - The logger provider to store in Svelte context.
|
|
110
|
+
*/
|
|
111
|
+
export function setLoggerContext(provider) {
|
|
112
|
+
setContext(LOGGER_KEY, provider);
|
|
113
|
+
}
|
|
114
|
+
// Getters
|
|
115
|
+
/**
|
|
116
|
+
* Gets the state provider from Svelte context.
|
|
117
|
+
* @returns The state provider instance.
|
|
118
|
+
* @throws {Error} If no state provider has been set in context.
|
|
119
|
+
*/
|
|
120
|
+
export function getStateProvider() {
|
|
121
|
+
const provider = getContext(STATE_KEY);
|
|
122
|
+
if (!provider)
|
|
123
|
+
throw new Error(t('svelte.error.noStateProvider', undefined, {
|
|
124
|
+
defaultValue: 'State provider not found in context',
|
|
125
|
+
}));
|
|
126
|
+
return provider;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Gets the auth client from Svelte context.
|
|
130
|
+
* @returns The auth client instance.
|
|
131
|
+
* @throws {Error} If no auth client has been set in context.
|
|
132
|
+
*/
|
|
133
|
+
export function getAuthClient() {
|
|
134
|
+
const client = getContext(AUTH_KEY);
|
|
135
|
+
if (!client)
|
|
136
|
+
throw new Error(t('svelte.error.noAuthClient', undefined, {
|
|
137
|
+
defaultValue: 'Auth client not found in context',
|
|
138
|
+
}));
|
|
139
|
+
return client;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Gets the theme provider from Svelte context.
|
|
143
|
+
* @returns The theme provider instance.
|
|
144
|
+
* @throws {Error} If no theme provider has been set in context.
|
|
145
|
+
*/
|
|
146
|
+
export function getThemeProvider() {
|
|
147
|
+
const provider = getContext(THEME_KEY);
|
|
148
|
+
if (!provider)
|
|
149
|
+
throw new Error(t('svelte.error.noThemeProvider', undefined, {
|
|
150
|
+
defaultValue: 'Theme provider not found in context',
|
|
151
|
+
}));
|
|
152
|
+
return provider;
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Gets the router from Svelte context.
|
|
156
|
+
* @returns The router instance.
|
|
157
|
+
* @throws {Error} If no router has been set in context.
|
|
158
|
+
*/
|
|
159
|
+
export function getRouter() {
|
|
160
|
+
const router = getContext(ROUTER_KEY);
|
|
161
|
+
if (!router)
|
|
162
|
+
throw new Error(t('svelte.error.noRouter', undefined, { defaultValue: 'Router not found in context' }));
|
|
163
|
+
return router;
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Gets the i18n provider from Svelte context.
|
|
167
|
+
* @returns The i18n provider instance.
|
|
168
|
+
* @throws {Error} If no i18n provider has been set in context.
|
|
169
|
+
*/
|
|
170
|
+
export function getI18nProvider() {
|
|
171
|
+
const provider = getContext(I18N_KEY);
|
|
172
|
+
if (!provider)
|
|
173
|
+
throw new Error(t('svelte.error.noI18nProvider', undefined, {
|
|
174
|
+
defaultValue: 'I18n provider not found in context',
|
|
175
|
+
}));
|
|
176
|
+
return provider;
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Gets the HTTP client from Svelte context.
|
|
180
|
+
* @returns The HTTP client instance.
|
|
181
|
+
* @throws {Error} If no HTTP client has been set in context.
|
|
182
|
+
*/
|
|
183
|
+
export function getHttpClient() {
|
|
184
|
+
const client = getContext(HTTP_KEY);
|
|
185
|
+
if (!client)
|
|
186
|
+
throw new Error(t('svelte.error.noHttpClient', undefined, {
|
|
187
|
+
defaultValue: 'HTTP client not found in context',
|
|
188
|
+
}));
|
|
189
|
+
return client;
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Gets the storage provider from Svelte context.
|
|
193
|
+
* @returns The storage provider instance.
|
|
194
|
+
* @throws {Error} If no storage provider has been set in context.
|
|
195
|
+
*/
|
|
196
|
+
export function getStorageProvider() {
|
|
197
|
+
const provider = getContext(STORAGE_KEY);
|
|
198
|
+
if (!provider)
|
|
199
|
+
throw new Error(t('svelte.error.noStorageProvider', undefined, {
|
|
200
|
+
defaultValue: 'Storage provider not found in context',
|
|
201
|
+
}));
|
|
202
|
+
return provider;
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Gets the logger provider from Svelte context.
|
|
206
|
+
* @returns The logger provider instance.
|
|
207
|
+
* @throws {Error} If no logger provider has been set in context.
|
|
208
|
+
*/
|
|
209
|
+
export function getLoggerProvider() {
|
|
210
|
+
const provider = getContext(LOGGER_KEY);
|
|
211
|
+
if (!provider)
|
|
212
|
+
throw new Error(t('svelte.error.noLoggerProvider', undefined, {
|
|
213
|
+
defaultValue: 'Logger provider not found in context',
|
|
214
|
+
}));
|
|
215
|
+
return provider;
|
|
216
|
+
}
|
|
217
|
+
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6B,EAAE,SAAS,EAAE;YAC1C,YAAY,EAAE,oCAAoC;SACnD,CAAC,CACH,CAAA;IACH,OAAO,QAAQ,CAAA;AACjB,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,aAAa;IAC3B,MAAM,MAAM,GAAG,UAAU,CAAa,QAAQ,CAAC,CAAA;IAC/C,IAAI,CAAC,MAAM;QACT,MAAM,IAAI,KAAK,CACb,CAAC,CAAC,2BAA2B,EAAE,SAAS,EAAE;YACxC,YAAY,EAAE,kCAAkC;SACjD,CAAC,CACH,CAAA;IACH,OAAO,MAAM,CAAA;AACf,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,kBAAkB;IAChC,MAAM,QAAQ,GAAG,UAAU,CAAkB,WAAW,CAAC,CAAA;IACzD,IAAI,CAAC,QAAQ;QACX,MAAM,IAAI,KAAK,CACb,CAAC,CAAC,gCAAgC,EAAE,SAAS,EAAE;YAC7C,YAAY,EAAE,uCAAuC;SACtD,CAAC,CACH,CAAA;IACH,OAAO,QAAQ,CAAA;AACjB,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,iBAAiB;IAC/B,MAAM,QAAQ,GAAG,UAAU,CAAiB,UAAU,CAAC,CAAA;IACvD,IAAI,CAAC,QAAQ;QACX,MAAM,IAAI,KAAK,CACb,CAAC,CAAC,+BAA+B,EAAE,SAAS,EAAE;YAC5C,YAAY,EAAE,sCAAsC;SACrD,CAAC,CACH,CAAA;IACH,OAAO,QAAQ,CAAA;AACjB,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Svelte framework bindings for molecule.dev.
|
|
3
|
+
*
|
|
4
|
+
* Provides Svelte stores and context utilities for all molecule core
|
|
5
|
+
* interfaces: call `setMoleculeContext(config)` once in the root layout,
|
|
6
|
+
* then consume services anywhere below via store factories
|
|
7
|
+
* (`createAuthStores`, `createThemeStores`, `createI18nStores`, …).
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```svelte
|
|
11
|
+
* <!-- +layout.svelte — wire providers ONCE, during component init -->
|
|
12
|
+
* <script lang="ts">
|
|
13
|
+
* import { setMoleculeContext } from '@molecule/app-svelte'
|
|
14
|
+
* import { provider as stateProvider } from '@molecule/app-state-zustand'
|
|
15
|
+
* import { provider as themeProvider } from '@molecule/app-theme-css-variables'
|
|
16
|
+
* import { createJWTAuthClient } from '@molecule/app-auth'
|
|
17
|
+
*
|
|
18
|
+
* setMoleculeContext({
|
|
19
|
+
* state: stateProvider,
|
|
20
|
+
* auth: createJWTAuthClient({ baseURL: '/api' }),
|
|
21
|
+
* theme: themeProvider,
|
|
22
|
+
* })
|
|
23
|
+
* </script>
|
|
24
|
+
*
|
|
25
|
+
* <slot />
|
|
26
|
+
*
|
|
27
|
+
* <!-- Component.svelte — consume via stores -->
|
|
28
|
+
* <script lang="ts">
|
|
29
|
+
* import { createAuthStores, createThemeStores } from '@molecule/app-svelte'
|
|
30
|
+
*
|
|
31
|
+
* const { user, isAuthenticated, logout } = createAuthStores<{ name?: string }>()
|
|
32
|
+
* const { theme, toggleTheme, mode } = createThemeStores()
|
|
33
|
+
* </script>
|
|
34
|
+
*
|
|
35
|
+
* {#if $isAuthenticated}
|
|
36
|
+
* <div style:background={$theme.colors.background}>
|
|
37
|
+
* <h1>Welcome, {$user?.name}!</h1>
|
|
38
|
+
* <button on:click={toggleTheme}>Toggle ({$mode})</button>
|
|
39
|
+
* <button on:click={() => logout()}>Logout</button>
|
|
40
|
+
* </div>
|
|
41
|
+
* {/if}
|
|
42
|
+
* ```
|
|
43
|
+
*
|
|
44
|
+
* @remarks
|
|
45
|
+
* - **`setMoleculeContext` and every `create*Stores` factory use Svelte context — they are only
|
|
46
|
+
* legal during component initialization** (the top level of a component `<script>`), never in
|
|
47
|
+
* module scope, after `onMount`, in event handlers, or in async callbacks. Wire the context in
|
|
48
|
+
* the ROOT layout so every route inherits it.
|
|
49
|
+
* - **Factories throw per missing service**: the context carries ONLY the services you passed —
|
|
50
|
+
* `createAuthStores()` under a context without `auth` throws. Fix the `setMoleculeContext`
|
|
51
|
+
* config; don't wrap in try/catch.
|
|
52
|
+
* - Results are Svelte stores — read them with the `$` prefix (`$isAuthenticated`, `$theme`);
|
|
53
|
+
* actions (`logout`, `toggleTheme`, `setTheme`) are plain functions.
|
|
54
|
+
*
|
|
55
|
+
* @module
|
|
56
|
+
*/
|
|
57
|
+
export * from './context.js';
|
|
58
|
+
export * from './stores/index.js';
|
|
59
|
+
export * from './types.js';
|
|
60
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuDG;AAEH,cAAc,cAAc,CAAA;AAC5B,cAAc,mBAAmB,CAAA;AACjC,cAAc,YAAY,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Svelte framework bindings for molecule.dev.
|
|
3
|
+
*
|
|
4
|
+
* Provides Svelte stores and context utilities for all molecule core
|
|
5
|
+
* interfaces: call `setMoleculeContext(config)` once in the root layout,
|
|
6
|
+
* then consume services anywhere below via store factories
|
|
7
|
+
* (`createAuthStores`, `createThemeStores`, `createI18nStores`, …).
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```svelte
|
|
11
|
+
* <!-- +layout.svelte — wire providers ONCE, during component init -->
|
|
12
|
+
* <script lang="ts">
|
|
13
|
+
* import { setMoleculeContext } from '@molecule/app-svelte'
|
|
14
|
+
* import { provider as stateProvider } from '@molecule/app-state-zustand'
|
|
15
|
+
* import { provider as themeProvider } from '@molecule/app-theme-css-variables'
|
|
16
|
+
* import { createJWTAuthClient } from '@molecule/app-auth'
|
|
17
|
+
*
|
|
18
|
+
* setMoleculeContext({
|
|
19
|
+
* state: stateProvider,
|
|
20
|
+
* auth: createJWTAuthClient({ baseURL: '/api' }),
|
|
21
|
+
* theme: themeProvider,
|
|
22
|
+
* })
|
|
23
|
+
* </script>
|
|
24
|
+
*
|
|
25
|
+
* <slot />
|
|
26
|
+
*
|
|
27
|
+
* <!-- Component.svelte — consume via stores -->
|
|
28
|
+
* <script lang="ts">
|
|
29
|
+
* import { createAuthStores, createThemeStores } from '@molecule/app-svelte'
|
|
30
|
+
*
|
|
31
|
+
* const { user, isAuthenticated, logout } = createAuthStores<{ name?: string }>()
|
|
32
|
+
* const { theme, toggleTheme, mode } = createThemeStores()
|
|
33
|
+
* </script>
|
|
34
|
+
*
|
|
35
|
+
* {#if $isAuthenticated}
|
|
36
|
+
* <div style:background={$theme.colors.background}>
|
|
37
|
+
* <h1>Welcome, {$user?.name}!</h1>
|
|
38
|
+
* <button on:click={toggleTheme}>Toggle ({$mode})</button>
|
|
39
|
+
* <button on:click={() => logout()}>Logout</button>
|
|
40
|
+
* </div>
|
|
41
|
+
* {/if}
|
|
42
|
+
* ```
|
|
43
|
+
*
|
|
44
|
+
* @remarks
|
|
45
|
+
* - **`setMoleculeContext` and every `create*Stores` factory use Svelte context — they are only
|
|
46
|
+
* legal during component initialization** (the top level of a component `<script>`), never in
|
|
47
|
+
* module scope, after `onMount`, in event handlers, or in async callbacks. Wire the context in
|
|
48
|
+
* the ROOT layout so every route inherits it.
|
|
49
|
+
* - **Factories throw per missing service**: the context carries ONLY the services you passed —
|
|
50
|
+
* `createAuthStores()` under a context without `auth` throws. Fix the `setMoleculeContext`
|
|
51
|
+
* config; don't wrap in try/catch.
|
|
52
|
+
* - Results are Svelte stores — read them with the `$` prefix (`$isAuthenticated`, `$theme`);
|
|
53
|
+
* actions (`logout`, `toggleTheme`, `setTheme`) are plain functions.
|
|
54
|
+
*
|
|
55
|
+
* @module
|
|
56
|
+
*/
|
|
57
|
+
export * from './context.js';
|
|
58
|
+
export * from './stores/index.js';
|
|
59
|
+
export * from './types.js';
|
|
60
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuDG;AAEH,cAAc,cAAc,CAAA;AAC5B,cAAc,mBAAmB,CAAA;AACjC,cAAc,YAAY,CAAA"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Svelte store for async-capable state management.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import { type Writable } from 'svelte/store';
|
|
7
|
+
/**
|
|
8
|
+
* Async state store.
|
|
9
|
+
*/
|
|
10
|
+
export interface AsyncStateStore<T> extends Writable<T> {
|
|
11
|
+
setState: (value: T | ((prev: T) => T) | Promise<T | ((prev: T) => T)>) => void;
|
|
12
|
+
extendState: (partial: Partial<T> | ((prev: T) => Partial<T>) | Promise<Partial<T> | ((prev: T) => Partial<T>)>) => void;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Creates a writable store with async-capable setState and extendState.
|
|
16
|
+
*
|
|
17
|
+
* @param initialValue - The initial state value
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```svelte
|
|
21
|
+
* <script>
|
|
22
|
+
* import { createAsyncState } from '@molecule/app-svelte'
|
|
23
|
+
*
|
|
24
|
+
* const state = createAsyncState({ count: 0, name: 'world' })
|
|
25
|
+
*
|
|
26
|
+
* // Sync update
|
|
27
|
+
* state.setState({ count: 1, name: 'hello' })
|
|
28
|
+
*
|
|
29
|
+
* // Function update
|
|
30
|
+
* state.setState(prev => ({ ...prev, count: prev.count + 1 }))
|
|
31
|
+
*
|
|
32
|
+
* // Async update
|
|
33
|
+
* state.setState(fetch('/api/state').then(r => r.json()))
|
|
34
|
+
*
|
|
35
|
+
* // Partial merge
|
|
36
|
+
* state.extendState({ count: 5 })
|
|
37
|
+
*
|
|
38
|
+
* // Async partial merge
|
|
39
|
+
* state.extendState(fetch('/api/partial').then(r => r.json()))
|
|
40
|
+
* </script>
|
|
41
|
+
*
|
|
42
|
+
* <p>{$state.count} - {$state.name}</p>
|
|
43
|
+
* ```
|
|
44
|
+
* @returns The created instance.
|
|
45
|
+
*/
|
|
46
|
+
export declare const createAsyncState: <T>(initialValue: T) => AsyncStateStore<T>;
|
|
47
|
+
//# sourceMappingURL=async-state.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"async-state.d.ts","sourceRoot":"","sources":["../../src/stores/async-state.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,EAAE,KAAK,QAAQ,EAAY,MAAM,cAAc,CAAA;AAEtD;;GAEG;AACH,MAAM,WAAW,eAAe,CAAC,CAAC,CAAE,SAAQ,QAAQ,CAAC,CAAC,CAAC;IACrD,QAAQ,EAAE,CAAC,KAAK,EAAE,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC,KAAK,IAAI,CAAA;IAC/E,WAAW,EAAE,CACX,OAAO,EACL,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,KACvF,IAAI,CAAA;CACV;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,eAAO,MAAM,gBAAgB,GAAI,CAAC,EAAE,cAAc,CAAC,KAAG,eAAe,CAAC,CAAC,CAuDtE,CAAA"}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Svelte store for async-capable state management.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import { writable } from 'svelte/store';
|
|
7
|
+
/**
|
|
8
|
+
* Creates a writable store with async-capable setState and extendState.
|
|
9
|
+
*
|
|
10
|
+
* @param initialValue - The initial state value
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```svelte
|
|
14
|
+
* <script>
|
|
15
|
+
* import { createAsyncState } from '@molecule/app-svelte'
|
|
16
|
+
*
|
|
17
|
+
* const state = createAsyncState({ count: 0, name: 'world' })
|
|
18
|
+
*
|
|
19
|
+
* // Sync update
|
|
20
|
+
* state.setState({ count: 1, name: 'hello' })
|
|
21
|
+
*
|
|
22
|
+
* // Function update
|
|
23
|
+
* state.setState(prev => ({ ...prev, count: prev.count + 1 }))
|
|
24
|
+
*
|
|
25
|
+
* // Async update
|
|
26
|
+
* state.setState(fetch('/api/state').then(r => r.json()))
|
|
27
|
+
*
|
|
28
|
+
* // Partial merge
|
|
29
|
+
* state.extendState({ count: 5 })
|
|
30
|
+
*
|
|
31
|
+
* // Async partial merge
|
|
32
|
+
* state.extendState(fetch('/api/partial').then(r => r.json()))
|
|
33
|
+
* </script>
|
|
34
|
+
*
|
|
35
|
+
* <p>{$state.count} - {$state.name}</p>
|
|
36
|
+
* ```
|
|
37
|
+
* @returns The created instance.
|
|
38
|
+
*/
|
|
39
|
+
export const createAsyncState = (initialValue) => {
|
|
40
|
+
const store = writable(initialValue);
|
|
41
|
+
const setState = (value) => {
|
|
42
|
+
if (value instanceof Promise) {
|
|
43
|
+
value.then((resolved) => {
|
|
44
|
+
if (typeof resolved === 'function') {
|
|
45
|
+
store.update(resolved);
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
store.set(resolved);
|
|
49
|
+
}
|
|
50
|
+
}, () => {
|
|
51
|
+
// Silently swallow promise errors
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
else if (typeof value === 'function') {
|
|
55
|
+
store.update(value);
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
store.set(value);
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
const extendState = (partial) => {
|
|
62
|
+
if (partial instanceof Promise) {
|
|
63
|
+
partial.then((resolved) => {
|
|
64
|
+
if (typeof resolved === 'function') {
|
|
65
|
+
store.update((prev) => ({ ...prev, ...resolved(prev) }));
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
store.update((prev) => ({ ...prev, ...resolved }));
|
|
69
|
+
}
|
|
70
|
+
}, () => {
|
|
71
|
+
// Silently swallow promise errors
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
else if (typeof partial === 'function') {
|
|
75
|
+
store.update((prev) => ({ ...prev, ...partial(prev) }));
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
store.update((prev) => ({ ...prev, ...partial }));
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
return {
|
|
82
|
+
subscribe: store.subscribe,
|
|
83
|
+
set: store.set,
|
|
84
|
+
update: store.update,
|
|
85
|
+
setState,
|
|
86
|
+
extendState,
|
|
87
|
+
};
|
|
88
|
+
};
|
|
89
|
+
//# sourceMappingURL=async-state.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"async-state.js","sourceRoot":"","sources":["../../src/stores/async-state.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,EAAiB,QAAQ,EAAE,MAAM,cAAc,CAAA;AAatD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAAI,YAAe,EAAsB,EAAE;IACzE,MAAM,KAAK,GAAG,QAAQ,CAAI,YAAY,CAAC,CAAA;IAEvC,MAAM,QAAQ,GAAG,CAAC,KAA2D,EAAQ,EAAE;QACrF,IAAI,KAAK,YAAY,OAAO,EAAE,CAAC;Y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