@molecule/app-svelte 3.3.0 → 3.5.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/README.md +2393 -220
- package/dist/agent-identity.js +0 -1
- package/dist/context.d.ts +101 -1
- package/dist/context.d.ts.map +1 -1
- package/dist/context.js +199 -3
- package/dist/hooks/external-store.d.ts +28 -0
- package/dist/hooks/external-store.d.ts.map +1 -0
- package/dist/hooks/external-store.js +95 -0
- package/dist/hooks/index.d.ts +37 -0
- package/dist/hooks/index.d.ts.map +1 -0
- package/dist/hooks/index.js +36 -0
- package/dist/hooks/live-store.d.ts +68 -0
- package/dist/hooks/live-store.d.ts.map +1 -0
- package/dist/hooks/live-store.js +76 -0
- package/dist/hooks/useAIModels.d.ts +58 -0
- package/dist/hooks/useAIModels.d.ts.map +1 -0
- package/dist/hooks/useAIModels.js +155 -0
- package/dist/hooks/useAsyncState.d.ts +52 -0
- package/dist/hooks/useAsyncState.d.ts.map +1 -0
- package/dist/hooks/useAsyncState.js +80 -0
- package/dist/hooks/useAuth.d.ts +99 -0
- package/dist/hooks/useAuth.d.ts.map +1 -0
- package/dist/hooks/useAuth.js +88 -0
- package/dist/hooks/useCapacitorApp.d.ts +51 -0
- package/dist/hooks/useCapacitorApp.d.ts.map +1 -0
- package/dist/hooks/useCapacitorApp.js +57 -0
- package/dist/hooks/useChangePassword.d.ts +41 -0
- package/dist/hooks/useChangePassword.d.ts.map +1 -0
- package/dist/hooks/useChangePassword.js +40 -0
- package/dist/hooks/useDevice.d.ts +48 -0
- package/dist/hooks/useDevice.d.ts.map +1 -0
- package/dist/hooks/useDevice.js +37 -0
- package/dist/hooks/useEditor.d.ts +65 -0
- package/dist/hooks/useEditor.d.ts.map +1 -0
- package/dist/hooks/useEditor.js +86 -0
- package/dist/hooks/useForm.d.ts +137 -0
- package/dist/hooks/useForm.d.ts.map +1 -0
- package/dist/hooks/useForm.js +221 -0
- package/dist/hooks/useHttp.d.ts +122 -0
- package/dist/hooks/useHttp.d.ts.map +1 -0
- package/dist/hooks/useHttp.js +220 -0
- package/dist/hooks/useLogger.d.ts +52 -0
- package/dist/hooks/useLogger.d.ts.map +1 -0
- package/dist/hooks/useLogger.js +64 -0
- package/dist/hooks/useLogin.d.ts +47 -0
- package/dist/hooks/useLogin.d.ts.map +1 -0
- package/dist/hooks/useLogin.js +44 -0
- package/dist/hooks/useOAuth.d.ts +96 -0
- package/dist/hooks/useOAuth.d.ts.map +1 -0
- package/dist/hooks/useOAuth.js +363 -0
- package/dist/hooks/useOptimisticValue.d.ts +58 -0
- package/dist/hooks/useOptimisticValue.d.ts.map +1 -0
- package/dist/hooks/useOptimisticValue.js +98 -0
- package/dist/hooks/usePasswordReset.d.ts +50 -0
- package/dist/hooks/usePasswordReset.d.ts.map +1 -0
- package/dist/hooks/usePasswordReset.js +50 -0
- package/dist/hooks/usePlatform.d.ts +45 -0
- package/dist/hooks/usePlatform.d.ts.map +1 -0
- package/dist/hooks/usePlatform.js +35 -0
- package/dist/hooks/usePreview.d.ts +54 -0
- package/dist/hooks/usePreview.d.ts.map +1 -0
- package/dist/hooks/usePreview.js +61 -0
- package/dist/hooks/useProfileForm.d.ts +100 -0
- package/dist/hooks/useProfileForm.d.ts.map +1 -0
- package/dist/hooks/useProfileForm.js +267 -0
- package/dist/hooks/usePromise.d.ts +62 -0
- package/dist/hooks/usePromise.d.ts.map +1 -0
- package/dist/hooks/usePromise.js +105 -0
- package/dist/hooks/usePush.d.ts +68 -0
- package/dist/hooks/usePush.d.ts.map +1 -0
- package/dist/hooks/usePush.js +79 -0
- package/dist/hooks/useRouter.d.ts +169 -0
- package/dist/hooks/useRouter.d.ts.map +1 -0
- package/dist/hooks/useRouter.js +212 -0
- package/dist/hooks/useSignup.d.ts +52 -0
- package/dist/hooks/useSignup.d.ts.map +1 -0
- package/dist/hooks/useSignup.js +42 -0
- package/dist/hooks/useStorage.d.ts +133 -0
- package/dist/hooks/useStorage.d.ts.map +1 -0
- package/dist/hooks/useStorage.js +228 -0
- package/dist/hooks/useStore.d.ts +69 -0
- package/dist/hooks/useStore.d.ts.map +1 -0
- package/dist/hooks/useStore.js +82 -0
- package/dist/hooks/useTheme.d.ts +55 -0
- package/dist/hooks/useTheme.d.ts.map +1 -0
- package/dist/hooks/useTheme.js +102 -0
- package/dist/hooks/useTranslation.d.ts +106 -0
- package/dist/hooks/useTranslation.d.ts.map +1 -0
- package/dist/hooks/useTranslation.js +159 -0
- package/dist/hooks/useVerifyPaymentReturn.d.ts +91 -0
- package/dist/hooks/useVerifyPaymentReturn.d.ts.map +1 -0
- package/dist/hooks/useVerifyPaymentReturn.js +189 -0
- package/dist/hooks/useVersion.d.ts +53 -0
- package/dist/hooks/useVersion.d.ts.map +1 -0
- package/dist/hooks/useVersion.js +70 -0
- package/dist/hooks/useWorkspace.d.ts +47 -0
- package/dist/hooks/useWorkspace.d.ts.map +1 -0
- package/dist/hooks/useWorkspace.js +68 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/session-ready/SessionReady.svelte +20 -0
- package/dist/session-ready/SessionReady.svelte.d.ts +5 -0
- package/dist/session-ready/SessionReady.svelte.d.ts.map +1 -0
- package/dist/session-ready/index.d.ts +7 -0
- package/dist/session-ready/index.d.ts.map +1 -0
- package/dist/session-ready/index.js +6 -0
- package/dist/stores/async-state.js +0 -1
- package/dist/stores/auth.js +0 -1
- package/dist/stores/capacitor-app.js +0 -1
- package/dist/stores/change-password.js +0 -1
- package/dist/stores/chat.d.ts.map +1 -1
- package/dist/stores/chat.js +3 -10
- package/dist/stores/device.js +0 -1
- package/dist/stores/forms.js +0 -1
- package/dist/stores/http.js +0 -1
- package/dist/stores/i18n.d.ts +3 -2
- package/dist/stores/i18n.d.ts.map +1 -1
- package/dist/stores/i18n.js +2 -3
- package/dist/stores/index.js +0 -1
- package/dist/stores/logger.js +0 -1
- package/dist/stores/login.js +0 -1
- package/dist/stores/oauth.js +0 -1
- package/dist/stores/password-reset.js +0 -1
- package/dist/stores/platform.js +0 -1
- package/dist/stores/promise.js +0 -1
- package/dist/stores/push.js +0 -1
- package/dist/stores/router.js +0 -1
- package/dist/stores/signup.js +0 -1
- package/dist/stores/state.js +0 -1
- package/dist/stores/storage.js +0 -1
- package/dist/stores/theme.js +0 -1
- package/dist/stores/version.js +0 -1
- package/dist/types.d.ts +20 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +0 -1
- package/package.json +13 -8
- package/dist/agent-identity.js.map +0 -1
- package/dist/context.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/stores/async-state.js.map +0 -1
- package/dist/stores/auth.js.map +0 -1
- package/dist/stores/capacitor-app.js.map +0 -1
- package/dist/stores/change-password.js.map +0 -1
- package/dist/stores/chat.js.map +0 -1
- package/dist/stores/device.js.map +0 -1
- package/dist/stores/forms.js.map +0 -1
- package/dist/stores/http.js.map +0 -1
- package/dist/stores/i18n.js.map +0 -1
- package/dist/stores/index.js.map +0 -1
- package/dist/stores/logger.js.map +0 -1
- package/dist/stores/login.js.map +0 -1
- package/dist/stores/oauth.js.map +0 -1
- package/dist/stores/password-reset.js.map +0 -1
- package/dist/stores/platform.js.map +0 -1
- package/dist/stores/promise.js.map +0 -1
- package/dist/stores/push.js.map +0 -1
- package/dist/stores/router.js.map +0 -1
- package/dist/stores/signup.js.map +0 -1
- package/dist/stores/state.js.map +0 -1
- package/dist/stores/storage.js.map +0 -1
- package/dist/stores/theme.js.map +0 -1
- package/dist/stores/version.js.map +0 -1
- package/dist/types.js.map +0 -1
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The shared reactive scaffolding of the React-named hooks: a readable store
|
|
3
|
+
* that always holds a service's current value, subscribed eagerly at creation
|
|
4
|
+
* (during component init) and torn down when the owning component is destroyed.
|
|
5
|
+
*
|
|
6
|
+
* A `readable()` store only subscribes its service when someone subscribes the
|
|
7
|
+
* store, so a value read after an update with no live subscriber goes stale;
|
|
8
|
+
* these hooks subscribe the service for the whole component lifetime instead,
|
|
9
|
+
* the way a component's `$store` read does.
|
|
10
|
+
*
|
|
11
|
+
* @module
|
|
12
|
+
*/
|
|
13
|
+
import { onDestroy } from 'svelte';
|
|
14
|
+
import { derived, get, writable } from 'svelte/store';
|
|
15
|
+
/**
|
|
16
|
+
* A readable store that always holds `read()`'s value.
|
|
17
|
+
*
|
|
18
|
+
* The store subscribes `subscribe` eagerly (at creation, during component
|
|
19
|
+
* init), re-reads whenever it notifies and ends the subscription when the
|
|
20
|
+
* owning component is destroyed, so its value is current whether or not a
|
|
21
|
+
* subscriber is attached right now.
|
|
22
|
+
*
|
|
23
|
+
* @param read - Reads the service's current value.
|
|
24
|
+
* @param subscribe - Subscribes the service; returns its unsubscribe.
|
|
25
|
+
* @returns A readable store of the current value.
|
|
26
|
+
*/
|
|
27
|
+
export function liveStore(read, subscribe) {
|
|
28
|
+
const store = writable(read());
|
|
29
|
+
const stop = subscribe(() => store.set(read()));
|
|
30
|
+
onDestroy(stop);
|
|
31
|
+
return { subscribe: store.subscribe };
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* One picked field of a state store, as its own readable store.
|
|
35
|
+
*
|
|
36
|
+
* @param state - The store holding the whole state object.
|
|
37
|
+
* @param key - The field to project out.
|
|
38
|
+
* @returns A readable store of that field's current value.
|
|
39
|
+
*/
|
|
40
|
+
export function fieldStore(state, key) {
|
|
41
|
+
return derived(state, (current) => current[key]);
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Whether a hook's options were passed as a readable store (options that change
|
|
45
|
+
* without a remount) rather than as a plain value.
|
|
46
|
+
*
|
|
47
|
+
* @param value - The options, or a store of them.
|
|
48
|
+
* @returns True for a store.
|
|
49
|
+
*/
|
|
50
|
+
export function isReadableStore(value) {
|
|
51
|
+
return (typeof value === 'object' &&
|
|
52
|
+
value !== null &&
|
|
53
|
+
'subscribe' in value &&
|
|
54
|
+
typeof value.subscribe === 'function');
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Reads a `usePromise` tracker's fields as individual stores and functions,
|
|
58
|
+
* for hooks built on it (`useLogin`, `useSignup`, …).
|
|
59
|
+
*
|
|
60
|
+
* @param T - The resolved value type.
|
|
61
|
+
* @param A - The argument tuple.
|
|
62
|
+
* @param tracker - The tracker store from `usePromise`.
|
|
63
|
+
* @returns The status, value and error as stores, and the call and reset functions.
|
|
64
|
+
*/
|
|
65
|
+
export function promiseFields(tracker) {
|
|
66
|
+
// The state object keeps one identity (mutated in place), so reading it once
|
|
67
|
+
// is enough for the action functions.
|
|
68
|
+
const [state, call] = get(tracker);
|
|
69
|
+
return {
|
|
70
|
+
status: derived(tracker, ([current]) => current.status),
|
|
71
|
+
value: derived(tracker, ([current]) => current.value),
|
|
72
|
+
error: derived(tracker, ([current]) => current.error),
|
|
73
|
+
call,
|
|
74
|
+
reset: () => state.reset(),
|
|
75
|
+
};
|
|
76
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Svelte hook for the AI model catalog, porting React's `useAIModels`.
|
|
3
|
+
*
|
|
4
|
+
* Lazily fetches `GET /ai/models` on first mount and caches the result in a
|
|
5
|
+
* module-level singleton so subsequent mounts return synchronously. The picker
|
|
6
|
+
* shows a brief loading state on first open and never refetches afterwards.
|
|
7
|
+
*
|
|
8
|
+
* When a `projectId` is passed, the fetch is project-scoped
|
|
9
|
+
* (`?projectId=<id>`) so servers that support per-project custom
|
|
10
|
+
* ("bring your own AI") models include them; each scope caches independently.
|
|
11
|
+
*
|
|
12
|
+
* @module
|
|
13
|
+
*/
|
|
14
|
+
import { type Readable } from 'svelte/store';
|
|
15
|
+
import type { AppModelDefinition, AppModeModelDefaults } from '@molecule/app-ai-models';
|
|
16
|
+
/**
|
|
17
|
+
* Test-only: drops every cached model list so the next `useAIModels` call
|
|
18
|
+
* refetches. Exposed for unit tests; do not call from production code.
|
|
19
|
+
*/
|
|
20
|
+
export declare function resetAIModelsCache(): void;
|
|
21
|
+
/**
|
|
22
|
+
* Result returned by `useAIModels`.
|
|
23
|
+
*/
|
|
24
|
+
export interface UseAIModelsResult {
|
|
25
|
+
/** Available models, or an empty array while loading, as a readable store. */
|
|
26
|
+
models: Readable<AppModelDefinition[]>;
|
|
27
|
+
/**
|
|
28
|
+
* Per-mode server default model ids for the requester's tier, or `undefined`
|
|
29
|
+
* while loading or on servers that don't provide them.
|
|
30
|
+
*/
|
|
31
|
+
defaults: Readable<AppModeModelDefaults | undefined>;
|
|
32
|
+
/** The single model marked `freeTier: true`, or `undefined`. */
|
|
33
|
+
freeTierModel: Readable<AppModelDefinition | undefined>;
|
|
34
|
+
/** `true` while the initial fetch is in flight. */
|
|
35
|
+
loading: Readable<boolean>;
|
|
36
|
+
/** Error from the initial fetch, or `null`. */
|
|
37
|
+
error: Readable<Error | null>;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Subscribes to the cached AI model catalog, React's `useAIModels` in Svelte's
|
|
41
|
+
* idiom: call it during component init under `setMoleculeContext({ http })`.
|
|
42
|
+
* The first mount of a scope triggers a single `GET /ai/models` fetch;
|
|
43
|
+
* subsequent mounts return the cached result, and a scope whose fetch failed
|
|
44
|
+
* does not refetch on a later mount.
|
|
45
|
+
*
|
|
46
|
+
* @param projectId - Optional project scope: includes that project's custom
|
|
47
|
+
* ("bring your own AI") models on servers that support them.
|
|
48
|
+
* @returns Models, free-tier model, loading flag, and error, as stores.
|
|
49
|
+
* @example
|
|
50
|
+
* ```svelte
|
|
51
|
+
* <script>
|
|
52
|
+
* import { useAIModels } from '@molecule/app-svelte'
|
|
53
|
+
* const { models, freeTierModel, loading } = useAIModels()
|
|
54
|
+
* </script>
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
export declare function useAIModels(projectId?: string): UseAIModelsResult;
|
|
58
|
+
//# sourceMappingURL=useAIModels.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useAIModels.d.ts","sourceRoot":"","sources":["../../src/hooks/useAIModels.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAGH,OAAO,EAAW,KAAK,QAAQ,EAAY,MAAM,cAAc,CAAA;AAE/D,OAAO,KAAK,EAAE,kBAAkB,EAAE,oBAAoB,EAAE,MAAM,yBAAyB,CAAA;AAmGvF;;;GAGG;AACH,wBAAgB,kBAAkB,IAAI,IAAI,CASzC;AAWD;;GAEG;AACH,MAAM,WAAW,iBAAiB;IAChC,8EAA8E;IAC9E,MAAM,EAAE,QAAQ,CAAC,kBAAkB,EAAE,CAAC,CAAA;IACtC;;;OAGG;IACH,QAAQ,EAAE,QAAQ,CAAC,oBAAoB,GAAG,SAAS,CAAC,CAAA;IACpD,gEAAgE;IAChE,aAAa,EAAE,QAAQ,CAAC,kBAAkB,GAAG,SAAS,CAAC,CAAA;IACvD,mDAAmD;IACnD,OAAO,EAAE,QAAQ,CAAC,OAAO,CAAC,CAAA;IAC1B,+CAA+C;IAC/C,KAAK,EAAE,QAAQ,CAAC,KAAK,GAAG,IAAI,CAAC,CAAA;CAC9B;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,WAAW,CAAC,SAAS,CAAC,EAAE,MAAM,GAAG,iBAAiB,CAkCjE"}
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Svelte hook for the AI model catalog, porting React's `useAIModels`.
|
|
3
|
+
*
|
|
4
|
+
* Lazily fetches `GET /ai/models` on first mount and caches the result in a
|
|
5
|
+
* module-level singleton so subsequent mounts return synchronously. The picker
|
|
6
|
+
* shows a brief loading state on first open and never refetches afterwards.
|
|
7
|
+
*
|
|
8
|
+
* When a `projectId` is passed, the fetch is project-scoped
|
|
9
|
+
* (`?projectId=<id>`) so servers that support per-project custom
|
|
10
|
+
* ("bring your own AI") models include them; each scope caches independently.
|
|
11
|
+
*
|
|
12
|
+
* @module
|
|
13
|
+
*/
|
|
14
|
+
import { onDestroy } from 'svelte';
|
|
15
|
+
import { derived, writable } from 'svelte/store';
|
|
16
|
+
import { loadAIModelCatalog, pickFreeTierModel } from '@molecule/app-ai-models';
|
|
17
|
+
import { useHttpClient } from '../context.js';
|
|
18
|
+
import { fieldStore } from './live-store.js';
|
|
19
|
+
/** One cache entry per scope — `''` for the unscoped catalog, else the projectId. */
|
|
20
|
+
const caches = new Map();
|
|
21
|
+
/**
|
|
22
|
+
* The cache entry for a scope, created on first use.
|
|
23
|
+
*
|
|
24
|
+
* @param scope - `''` for the unscoped catalog, else the projectId.
|
|
25
|
+
* @returns The scope's cache entry.
|
|
26
|
+
*/
|
|
27
|
+
function cacheFor(scope) {
|
|
28
|
+
let cache = caches.get(scope);
|
|
29
|
+
if (!cache) {
|
|
30
|
+
cache = { models: null, defaults: undefined, error: null, inFlight: null };
|
|
31
|
+
caches.set(scope, cache);
|
|
32
|
+
}
|
|
33
|
+
return cache;
|
|
34
|
+
}
|
|
35
|
+
/** Listeners keyed by the same scope as the cache they observe. */
|
|
36
|
+
const listenersByScope = new Map();
|
|
37
|
+
/**
|
|
38
|
+
* The listener set for a scope, created on first use.
|
|
39
|
+
*
|
|
40
|
+
* @param scope - The cache scope.
|
|
41
|
+
* @returns The scope's listener set.
|
|
42
|
+
*/
|
|
43
|
+
function listenersFor(scope) {
|
|
44
|
+
let set = listenersByScope.get(scope);
|
|
45
|
+
if (!set) {
|
|
46
|
+
set = new Set();
|
|
47
|
+
listenersByScope.set(scope, set);
|
|
48
|
+
}
|
|
49
|
+
return set;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Broadcasts a scope's current cache state to its subscribed listeners.
|
|
53
|
+
*
|
|
54
|
+
* @param scope - The cache scope to notify.
|
|
55
|
+
*/
|
|
56
|
+
function notify(scope) {
|
|
57
|
+
const cache = cacheFor(scope);
|
|
58
|
+
for (const listener of listenersFor(scope)) {
|
|
59
|
+
listener({ models: cache.models, defaults: cache.defaults, error: cache.error });
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Triggers (or rejoins) the single in-flight fetch of `/ai/models` for a scope
|
|
64
|
+
* and stores the result in that scope's module-level cache.
|
|
65
|
+
*
|
|
66
|
+
* @param http - HTTP client used for the single fetch.
|
|
67
|
+
* @param scope - `''` for the unscoped catalog, else the projectId.
|
|
68
|
+
* @returns The fetched model list.
|
|
69
|
+
*/
|
|
70
|
+
async function fetchOnce(http, scope) {
|
|
71
|
+
const cache = cacheFor(scope);
|
|
72
|
+
if (cache.models)
|
|
73
|
+
return cache.models;
|
|
74
|
+
if (cache.inFlight)
|
|
75
|
+
return cache.inFlight;
|
|
76
|
+
cache.inFlight = loadAIModelCatalog(http, undefined, scope || undefined)
|
|
77
|
+
.then((catalog) => {
|
|
78
|
+
cache.models = catalog.models;
|
|
79
|
+
cache.defaults = catalog.defaults;
|
|
80
|
+
cache.error = null;
|
|
81
|
+
cache.inFlight = null;
|
|
82
|
+
notify(scope);
|
|
83
|
+
return catalog.models;
|
|
84
|
+
})
|
|
85
|
+
.catch((err) => {
|
|
86
|
+
cache.error = err instanceof Error ? err : new Error(String(err));
|
|
87
|
+
cache.inFlight = null;
|
|
88
|
+
notify(scope);
|
|
89
|
+
throw cache.error;
|
|
90
|
+
});
|
|
91
|
+
return cache.inFlight;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Test-only: drops every cached model list so the next `useAIModels` call
|
|
95
|
+
* refetches. Exposed for unit tests; do not call from production code.
|
|
96
|
+
*/
|
|
97
|
+
export function resetAIModelsCache() {
|
|
98
|
+
for (const [scope, cache] of caches) {
|
|
99
|
+
cache.models = null;
|
|
100
|
+
cache.defaults = undefined;
|
|
101
|
+
cache.error = null;
|
|
102
|
+
cache.inFlight = null;
|
|
103
|
+
notify(scope);
|
|
104
|
+
}
|
|
105
|
+
caches.clear();
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Subscribes to the cached AI model catalog, React's `useAIModels` in Svelte's
|
|
109
|
+
* idiom: call it during component init under `setMoleculeContext({ http })`.
|
|
110
|
+
* The first mount of a scope triggers a single `GET /ai/models` fetch;
|
|
111
|
+
* subsequent mounts return the cached result, and a scope whose fetch failed
|
|
112
|
+
* does not refetch on a later mount.
|
|
113
|
+
*
|
|
114
|
+
* @param projectId - Optional project scope: includes that project's custom
|
|
115
|
+
* ("bring your own AI") models on servers that support them.
|
|
116
|
+
* @returns Models, free-tier model, loading flag, and error, as stores.
|
|
117
|
+
* @example
|
|
118
|
+
* ```svelte
|
|
119
|
+
* <script>
|
|
120
|
+
* import { useAIModels } from '@molecule/app-svelte'
|
|
121
|
+
* const { models, freeTierModel, loading } = useAIModels()
|
|
122
|
+
* </script>
|
|
123
|
+
* ```
|
|
124
|
+
*/
|
|
125
|
+
export function useAIModels(projectId) {
|
|
126
|
+
const http = useHttpClient();
|
|
127
|
+
const scope = projectId ?? '';
|
|
128
|
+
const cache = cacheFor(scope);
|
|
129
|
+
const snapshot = writable({
|
|
130
|
+
models: cache.models,
|
|
131
|
+
defaults: cache.defaults,
|
|
132
|
+
error: cache.error,
|
|
133
|
+
});
|
|
134
|
+
const listener = (next) => snapshot.set(next);
|
|
135
|
+
listenersFor(scope).add(listener);
|
|
136
|
+
onDestroy(() => {
|
|
137
|
+
listenersFor(scope).delete(listener);
|
|
138
|
+
});
|
|
139
|
+
// The writable was created from the same cache; this re-read is React's
|
|
140
|
+
// effect resync (its lazy useState init may have raced a scope change).
|
|
141
|
+
snapshot.set({ models: cache.models, defaults: cache.defaults, error: cache.error });
|
|
142
|
+
if (!cache.models && !cache.error) {
|
|
143
|
+
void fetchOnce(http, scope).catch((_error) => {
|
|
144
|
+
// fetchOnce mirrors the error into cache.error, which the listener delivers.
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
const loading = derived(snapshot, (current) => !current.models && !current.error);
|
|
148
|
+
return {
|
|
149
|
+
models: derived(snapshot, (current) => current.models ?? []),
|
|
150
|
+
defaults: fieldStore(snapshot, 'defaults'),
|
|
151
|
+
freeTierModel: derived(snapshot, (current) => pickFreeTierModel(current.models ?? [])),
|
|
152
|
+
loading,
|
|
153
|
+
error: fieldStore(snapshot, 'error'),
|
|
154
|
+
};
|
|
155
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Svelte hook for async-capable state management, porting React's
|
|
3
|
+
* `useAsyncState`.
|
|
4
|
+
*
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
import { type Readable } from 'svelte/store';
|
|
8
|
+
/**
|
|
9
|
+
* Async-capable setState function.
|
|
10
|
+
*
|
|
11
|
+
* @param T - The state type.
|
|
12
|
+
*/
|
|
13
|
+
export type AsyncSetState<T> = (value: T | ((prev: T) => T) | Promise<T | ((prev: T) => T)>) => Promise<void>;
|
|
14
|
+
/**
|
|
15
|
+
* Async-capable extendState function for partial updates.
|
|
16
|
+
*
|
|
17
|
+
* @param T - The state type.
|
|
18
|
+
*/
|
|
19
|
+
export type AsyncExtendState<T> = (partial: Partial<T> | ((prev: T) => Partial<T>) | Promise<Partial<T> | ((prev: T) => Partial<T>)>) => Promise<void>;
|
|
20
|
+
/**
|
|
21
|
+
* The store {@link useAsyncState} returns: the current
|
|
22
|
+
* `[state, setState, extendState]` tuple.
|
|
23
|
+
*/
|
|
24
|
+
export type UseAsyncStateStore<T> = Readable<readonly [T, AsyncSetState<T>, AsyncExtendState<T>]>;
|
|
25
|
+
/**
|
|
26
|
+
* State that accepts Promises and partial updates, React's `useAsyncState` in
|
|
27
|
+
* Svelte's idiom: a readable store of the `[state, setState, extendState]`
|
|
28
|
+
* tuple.
|
|
29
|
+
*
|
|
30
|
+
* Like React's, this has NO latest-wins guard: a promise that settles after a
|
|
31
|
+
* newer synchronous set overwrites it — whatever settles last wins. Rejections
|
|
32
|
+
* of resolved promises are silently swallowed (React's deliberate behavior),
|
|
33
|
+
* leaving the previous state in place.
|
|
34
|
+
*
|
|
35
|
+
* @param initialState - Initial state value.
|
|
36
|
+
* @returns A readable store of the [state, setState, extendState] tuple.
|
|
37
|
+
* @example
|
|
38
|
+
* ```svelte
|
|
39
|
+
* <script>
|
|
40
|
+
* import { useAsyncState } from '@molecule/app-svelte'
|
|
41
|
+
* const state = useAsyncState({ foo: 'foo', bar: 'bar' })
|
|
42
|
+
* const [value, setState, extendState] = $state
|
|
43
|
+
*
|
|
44
|
+
* setState({ foo: 'Hello', bar: 'World!' })
|
|
45
|
+
* setState((prev) => ({ ...prev, foo: prev.foo.toUpperCase() }))
|
|
46
|
+
* setState(fetchData()) // a promise resolving a value or an updater
|
|
47
|
+
* extendState({ foo: 'Hello' }) // a partial merge
|
|
48
|
+
* </script>
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
export declare function useAsyncState<T>(initialState: T): UseAsyncStateStore<T>;
|
|
52
|
+
//# sourceMappingURL=useAsyncState.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useAsyncState.d.ts","sourceRoot":"","sources":["../../src/hooks/useAsyncState.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,KAAK,QAAQ,EAA2B,MAAM,cAAc,CAAA;AAErE;;;;GAIG;AACH,MAAM,MAAM,aAAa,CAAC,CAAC,IAAI,CAC7B,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,KACxD,OAAO,CAAC,IAAI,CAAC,CAAA;AAElB;;;;GAIG;AACH,MAAM,MAAM,gBAAgB,CAAC,CAAC,IAAI,CAChC,OAAO,EAAE,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,KAC9F,OAAO,CAAC,IAAI,CAAC,CAAA;AAElB;;;GAGG;AACH,MAAM,MAAM,kBAAkB,CAAC,CAAC,IAAI,QAAQ,CAAC,SAAS,CAAC,CAAC,EAAE,aAAa,CAAC,CAAC,CAAC,EAAE,gBAAgB,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA;AAEjG;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,wBAAgB,aAAa,CAAC,CAAC,EAAE,YAAY,EAAE,CAAC,GAAG,kBAAkB,CAAC,CAAC,CAAC,CAwDvE"}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Svelte hook for async-capable state management, porting React's
|
|
3
|
+
* `useAsyncState`.
|
|
4
|
+
*
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
import { writable } from 'svelte/store';
|
|
8
|
+
/**
|
|
9
|
+
* State that accepts Promises and partial updates, React's `useAsyncState` in
|
|
10
|
+
* Svelte's idiom: a readable store of the `[state, setState, extendState]`
|
|
11
|
+
* tuple.
|
|
12
|
+
*
|
|
13
|
+
* Like React's, this has NO latest-wins guard: a promise that settles after a
|
|
14
|
+
* newer synchronous set overwrites it — whatever settles last wins. Rejections
|
|
15
|
+
* of resolved promises are silently swallowed (React's deliberate behavior),
|
|
16
|
+
* leaving the previous state in place.
|
|
17
|
+
*
|
|
18
|
+
* @param initialState - Initial state value.
|
|
19
|
+
* @returns A readable store of the [state, setState, extendState] tuple.
|
|
20
|
+
* @example
|
|
21
|
+
* ```svelte
|
|
22
|
+
* <script>
|
|
23
|
+
* import { useAsyncState } from '@molecule/app-svelte'
|
|
24
|
+
* const state = useAsyncState({ foo: 'foo', bar: 'bar' })
|
|
25
|
+
* const [value, setState, extendState] = $state
|
|
26
|
+
*
|
|
27
|
+
* setState({ foo: 'Hello', bar: 'World!' })
|
|
28
|
+
* setState((prev) => ({ ...prev, foo: prev.foo.toUpperCase() }))
|
|
29
|
+
* setState(fetchData()) // a promise resolving a value or an updater
|
|
30
|
+
* extendState({ foo: 'Hello' }) // a partial merge
|
|
31
|
+
* </script>
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
export function useAsyncState(initialState) {
|
|
35
|
+
let current = initialState;
|
|
36
|
+
const tuple = writable([
|
|
37
|
+
initialState,
|
|
38
|
+
setState,
|
|
39
|
+
extendState,
|
|
40
|
+
]);
|
|
41
|
+
/** Publishes a new state value through the tuple store. */
|
|
42
|
+
function publish(next) {
|
|
43
|
+
current = next;
|
|
44
|
+
tuple.set([current, setState, extendState]);
|
|
45
|
+
}
|
|
46
|
+
/** Applies a resolved value-or-updater against the previous state. */
|
|
47
|
+
function applyValue(value) {
|
|
48
|
+
publish(typeof value === 'function' ? value(current) : value);
|
|
49
|
+
}
|
|
50
|
+
/** Applies a resolved partial-or-updater as a merge into the state. */
|
|
51
|
+
function applyPartial(partial) {
|
|
52
|
+
const resolved = typeof partial === 'function' ? partial(current) : partial;
|
|
53
|
+
publish({ ...current, ...resolved });
|
|
54
|
+
}
|
|
55
|
+
/** {@link AsyncSetState} — replaces the whole state. */
|
|
56
|
+
async function setState(value) {
|
|
57
|
+
try {
|
|
58
|
+
const resolved = value instanceof Promise ? await value : value;
|
|
59
|
+
applyValue(resolved);
|
|
60
|
+
}
|
|
61
|
+
catch (_error) {
|
|
62
|
+
// Silently swallow errors from resolved Promises to avoid unhandled
|
|
63
|
+
// rejections, exactly as React's useAsyncState does; the state keeps
|
|
64
|
+
// its previous value.
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
/** {@link AsyncExtendState} — merges a partial into the state. */
|
|
68
|
+
async function extendState(partial) {
|
|
69
|
+
try {
|
|
70
|
+
const resolved = partial instanceof Promise ? await partial : partial;
|
|
71
|
+
applyPartial(resolved);
|
|
72
|
+
}
|
|
73
|
+
catch (_error) {
|
|
74
|
+
// Silently swallow errors from resolved Promises to avoid unhandled
|
|
75
|
+
// rejections, exactly as React's useAsyncState does; the state keeps
|
|
76
|
+
// its previous value.
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return tuple;
|
|
80
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Svelte hook for authentication, porting React's `useAuth` family: the
|
|
3
|
+
* session state as readable stores plus the client's actions as plain
|
|
4
|
+
* functions.
|
|
5
|
+
*
|
|
6
|
+
* @module
|
|
7
|
+
*/
|
|
8
|
+
import type { Readable } from 'svelte/store';
|
|
9
|
+
import type { AuthClient, AuthState, RegisterExtraShape } from '@molecule/app-auth';
|
|
10
|
+
/** Options for {@link useAuth}. */
|
|
11
|
+
export interface UseAuthOptions {
|
|
12
|
+
/** Whether to automatically refresh the token on mount. */
|
|
13
|
+
autoRefresh?: boolean;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* What {@link useAuth} returns: the session's value fields as readable stores
|
|
17
|
+
* (`$status`, `$user`, …) and the client's actions as plain functions.
|
|
18
|
+
*
|
|
19
|
+
* @template T - The user profile the API returns.
|
|
20
|
+
* @template TRegister - The app's own registration fields.
|
|
21
|
+
*/
|
|
22
|
+
export interface UseAuthResult<T = unknown, TRegister extends RegisterExtraShape<TRegister> = Record<never, never>> {
|
|
23
|
+
/**
|
|
24
|
+
* The session state as one value — render from this instead of combining
|
|
25
|
+
* `initialized`/`authenticated`/`loading`. Only `'loading'` means "no
|
|
26
|
+
* decision yet"; redirect on `'unauthenticated'` alone.
|
|
27
|
+
*/
|
|
28
|
+
status: Readable<AuthState<T>['status']>;
|
|
29
|
+
/** The whole session state. */
|
|
30
|
+
state: Readable<AuthState<T>>;
|
|
31
|
+
/** The client's `login`. */
|
|
32
|
+
login: AuthClient<T>['login'];
|
|
33
|
+
/** The client's `logout`. */
|
|
34
|
+
logout: AuthClient<T>['logout'];
|
|
35
|
+
/** The client's `register` (there is no separate `useRegister`). */
|
|
36
|
+
register: AuthClient<T, TRegister>['register'];
|
|
37
|
+
/** The client's `refresh`. */
|
|
38
|
+
refresh: AuthClient<T>['refresh'];
|
|
39
|
+
/** Saves through the configured auth transport and publishes the saved user. */
|
|
40
|
+
updateProfile: AuthClient<T>['updateProfile'];
|
|
41
|
+
/** Caches a user without signing in. */
|
|
42
|
+
setUser: AuthClient<T>['setUser'];
|
|
43
|
+
/**
|
|
44
|
+
* Adopts a session an exchange outside the client already established (an
|
|
45
|
+
* OAuth code→token redirect, the app's own sign-in endpoint) in one update.
|
|
46
|
+
*/
|
|
47
|
+
setSession: AuthClient<T>['setSession'];
|
|
48
|
+
/** Whether a session is authenticated locally. */
|
|
49
|
+
isAuthenticated: Readable<boolean>;
|
|
50
|
+
/** Whether the client is working on a decision. */
|
|
51
|
+
isLoading: Readable<boolean>;
|
|
52
|
+
/** The cached profile, which can be null while an unavailable candidate is retained. */
|
|
53
|
+
user: Readable<T | null>;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Authentication state and actions, React's `useAuth` in Svelte's idiom: call
|
|
57
|
+
* it during component init under `setMoleculeContext({ auth })`.
|
|
58
|
+
*
|
|
59
|
+
* `status === 'loading'` is undecided: wait for it and redirect only on
|
|
60
|
+
* `status === 'unauthenticated'`. `'unavailable'` keeps the session candidate
|
|
61
|
+
* (and may keep a cached user) for retry; a restore failure other than 401/403
|
|
62
|
+
* never signs out. Save profile edits with `updateProfile(patch)`; an
|
|
63
|
+
* app-owned mutation instead passes its canonical response to `setUser`. For
|
|
64
|
+
* a sign-in the app itself runs, call `setSession({ accessToken, user })`.
|
|
65
|
+
*
|
|
66
|
+
* @param options - Hook options (`autoRefresh`).
|
|
67
|
+
* @returns Auth state as stores and the actions as functions.
|
|
68
|
+
* @example
|
|
69
|
+
* ```svelte
|
|
70
|
+
* <script>
|
|
71
|
+
* import { useAuth } from '@molecule/app-svelte'
|
|
72
|
+
* const { user, status, login, logout } = useAuth()
|
|
73
|
+
* </script>
|
|
74
|
+
*
|
|
75
|
+
* {#if $status === 'loading'}
|
|
76
|
+
* <p>Restoring session…</p>
|
|
77
|
+
* {:else if $user}
|
|
78
|
+
* <p>Welcome, {$user.name}</p>
|
|
79
|
+
* <button onclick={() => logout()}>Sign out</button>
|
|
80
|
+
* {/if}
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
83
|
+
export declare function useAuth<T = unknown, TRegister extends RegisterExtraShape<TRegister> = Record<never, never>>(options?: UseAuthOptions): UseAuthResult<T, TRegister>;
|
|
84
|
+
/**
|
|
85
|
+
* The authenticated user, React's `useUser` in Svelte's idiom: a readable
|
|
86
|
+
* store of the cached profile (or null).
|
|
87
|
+
*
|
|
88
|
+
* @param T - The user profile the API returns.
|
|
89
|
+
* @returns A readable store of the authenticated user or null.
|
|
90
|
+
*/
|
|
91
|
+
export declare function useUser<T = unknown>(): Readable<T | null>;
|
|
92
|
+
/**
|
|
93
|
+
* Whether a session is authenticated, React's `useIsAuthenticated` in
|
|
94
|
+
* Svelte's idiom.
|
|
95
|
+
*
|
|
96
|
+
* @returns A readable store of whether a session exists.
|
|
97
|
+
*/
|
|
98
|
+
export declare function useIsAuthenticated(): Readable<boolean>;
|
|
99
|
+
//# sourceMappingURL=useAuth.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useAuth.d.ts","sourceRoot":"","sources":["../../src/hooks/useAuth.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAGH,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAA;AAE5C,OAAO,KAAK,EACV,UAAU,EAEV,SAAS,EAGT,kBAAkB,EACnB,MAAM,oBAAoB,CAAA;AAK3B,mCAAmC;AACnC,MAAM,WAAW,cAAc;IAC7B,2DAA2D;IAC3D,WAAW,CAAC,EAAE,OAAO,CAAA;CACtB;AAED;;;;;;GAMG;AACH,MAAM,WAAW,aAAa,CAC5B,CAAC,GAAG,OAAO,EACX,SAAS,SAAS,kBAAkB,CAAC,SAAS,CAAC,GAAG,MAAM,CAAC,KAAK,EAAE,KAAK,CAAC;IAEtE;;;;OAIG;IACH,MAAM,EAAE,QAAQ,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAA;IACxC,+BAA+B;IAC/B,KAAK,EAAE,QAAQ,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAA;IAC7B,4BAA4B;IAC5B,KAAK,EAAE,UAAU,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAA;IAC7B,6BAA6B;IAC7B,MAAM,EAAE,UAAU,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAA;IAC/B,oEAAoE;IACpE,QAAQ,EAAE,UAAU,CAAC,CAAC,EAAE,SAAS,CAAC,CAAC,UAAU,CAAC,CAAA;IAC9C,8BAA8B;IAC9B,OAAO,EAAE,UAAU,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAA;IACjC,gFAAgF;IAChF,aAAa,EAAE,UAAU,CAAC,CAAC,CAAC,CAAC,eAAe,CAAC,CAAA;IAC7C,wCAAwC;IACxC,OAAO,EAAE,UAAU,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAA;IACjC;;;OAGG;IACH,UAAU,EAAE,UAAU,CAAC,CAAC,CAAC,CAAC,YAAY,CAAC,CAAA;IACvC,kDAAkD;IAClD,eAAe,EAAE,QAAQ,CAAC,OAAO,CAAC,CAAA;IAClC,mDAAmD;IACnD,SAAS,EAAE,QAAQ,CAAC,OAAO,CAAC,CAAA;IAC5B,wFAAwF;IACxF,IAAI,EAAE,QAAQ,CAAC,CAAC,GAAG,IAAI,CAAC,CAAA;CACzB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,wBAAgB,OAAO,CACrB,CAAC,GAAG,OAAO,EACX,SAAS,SAAS,kBAAkB,CAAC,SAAS,CAAC,GAAG,MAAM,CAAC,KAAK,EAAE,KAAK,CAAC,EACtE,OAAO,CAAC,EAAE,cAAc,GAAG,aAAa,CAAC,CAAC,EAAE,SAAS,CAAC,CAkCvD;AAED;;;;;;GAMG;AACH,wBAAgB,OAAO,CAAC,CAAC,GAAG,OAAO,KAAK,QAAQ,CAAC,CAAC,GAAG,IAAI,CAAC,CAGzD;AAED;;;;;GAKG;AACH,wBAAgB,kBAAkB,IAAI,QAAQ,CAAC,OAAO,CAAC,CAGtD"}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Svelte hook for authentication, porting React's `useAuth` family: the
|
|
3
|
+
* session state as readable stores plus the client's actions as plain
|
|
4
|
+
* functions.
|
|
5
|
+
*
|
|
6
|
+
* @module
|
|
7
|
+
*/
|
|
8
|
+
import { onMount } from 'svelte';
|
|
9
|
+
import { useAuthClient } from '../context.js';
|
|
10
|
+
import { fieldStore, liveStore } from './live-store.js';
|
|
11
|
+
/**
|
|
12
|
+
* Authentication state and actions, React's `useAuth` in Svelte's idiom: call
|
|
13
|
+
* it during component init under `setMoleculeContext({ auth })`.
|
|
14
|
+
*
|
|
15
|
+
* `status === 'loading'` is undecided: wait for it and redirect only on
|
|
16
|
+
* `status === 'unauthenticated'`. `'unavailable'` keeps the session candidate
|
|
17
|
+
* (and may keep a cached user) for retry; a restore failure other than 401/403
|
|
18
|
+
* never signs out. Save profile edits with `updateProfile(patch)`; an
|
|
19
|
+
* app-owned mutation instead passes its canonical response to `setUser`. For
|
|
20
|
+
* a sign-in the app itself runs, call `setSession({ accessToken, user })`.
|
|
21
|
+
*
|
|
22
|
+
* @param options - Hook options (`autoRefresh`).
|
|
23
|
+
* @returns Auth state as stores and the actions as functions.
|
|
24
|
+
* @example
|
|
25
|
+
* ```svelte
|
|
26
|
+
* <script>
|
|
27
|
+
* import { useAuth } from '@molecule/app-svelte'
|
|
28
|
+
* const { user, status, login, logout } = useAuth()
|
|
29
|
+
* </script>
|
|
30
|
+
*
|
|
31
|
+
* {#if $status === 'loading'}
|
|
32
|
+
* <p>Restoring session…</p>
|
|
33
|
+
* {:else if $user}
|
|
34
|
+
* <p>Welcome, {$user.name}</p>
|
|
35
|
+
* <button onclick={() => logout()}>Sign out</button>
|
|
36
|
+
* {/if}
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
export function useAuth(options) {
|
|
40
|
+
const client = useAuthClient();
|
|
41
|
+
const { autoRefresh = false } = options ?? {};
|
|
42
|
+
const state = liveStore(() => client.getState(), (notify) => client.onAuthChange(notify));
|
|
43
|
+
if (autoRefresh) {
|
|
44
|
+
onMount(() => {
|
|
45
|
+
const current = client.getState();
|
|
46
|
+
if (!current.authenticated && !current.loading) {
|
|
47
|
+
client.refresh().catch((_error) => {
|
|
48
|
+
// No session to refresh: the state already says unauthenticated.
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
return {
|
|
54
|
+
status: fieldStore(state, 'status'),
|
|
55
|
+
state,
|
|
56
|
+
login: (credentials) => client.login(credentials),
|
|
57
|
+
logout: () => client.logout(),
|
|
58
|
+
register: (data) => client.register(data),
|
|
59
|
+
refresh: () => client.refresh(),
|
|
60
|
+
updateProfile: (data) => client.updateProfile(data),
|
|
61
|
+
setUser: (user) => client.setUser(user),
|
|
62
|
+
setSession: (session) => client.setSession(session),
|
|
63
|
+
isAuthenticated: fieldStore(state, 'authenticated'),
|
|
64
|
+
isLoading: fieldStore(state, 'loading'),
|
|
65
|
+
user: fieldStore(state, 'user'),
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* The authenticated user, React's `useUser` in Svelte's idiom: a readable
|
|
70
|
+
* store of the cached profile (or null).
|
|
71
|
+
*
|
|
72
|
+
* @param T - The user profile the API returns.
|
|
73
|
+
* @returns A readable store of the authenticated user or null.
|
|
74
|
+
*/
|
|
75
|
+
export function useUser() {
|
|
76
|
+
const { user } = useAuth();
|
|
77
|
+
return user;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Whether a session is authenticated, React's `useIsAuthenticated` in
|
|
81
|
+
* Svelte's idiom.
|
|
82
|
+
*
|
|
83
|
+
* @returns A readable store of whether a session exists.
|
|
84
|
+
*/
|
|
85
|
+
export function useIsAuthenticated() {
|
|
86
|
+
const { isAuthenticated } = useAuth();
|
|
87
|
+
return isAuthenticated;
|
|
88
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Svelte hook for Capacitor app initialization, porting React's
|
|
3
|
+
* `useCapacitorApp`.
|
|
4
|
+
*
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
import type { Readable } from 'svelte/store';
|
|
8
|
+
import type { CapacitorAppOptions } from '@molecule/app-platform';
|
|
9
|
+
/**
|
|
10
|
+
* Hook return type.
|
|
11
|
+
*/
|
|
12
|
+
export type UseCapacitorAppResult = {
|
|
13
|
+
/** Whether the app is fully initialized. */
|
|
14
|
+
ready: Readable<boolean>;
|
|
15
|
+
/** Whether device ready has fired. */
|
|
16
|
+
deviceReady: Readable<boolean>;
|
|
17
|
+
/** Whether push notifications are initialized. */
|
|
18
|
+
pushReady: Readable<boolean>;
|
|
19
|
+
/** The initialization error, if any. */
|
|
20
|
+
error: Readable<Error | null>;
|
|
21
|
+
} & {
|
|
22
|
+
/** Runs the coordinator's initialization again. */
|
|
23
|
+
initialize: () => Promise<void>;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Capacitor app initialization state and actions, React's `useCapacitorApp` in
|
|
27
|
+
* Svelte's idiom: called during component init, one coordinator per hook
|
|
28
|
+
* instance whose state fields are readable stores and whose `initialize` is a
|
|
29
|
+
* plain function.
|
|
30
|
+
*
|
|
31
|
+
* Creates the `CapacitorApp` coordinator during init, subscribes its state and
|
|
32
|
+
* auto-initializes (the way React's mount effect does); the component's
|
|
33
|
+
* destroy unsubscribes and calls `destroy()`, releasing the coordinator's
|
|
34
|
+
* deep-link subscription. Init errors land in the `error` store through the
|
|
35
|
+
* coordinator's own state, so the auto-init rejection is swallowed there.
|
|
36
|
+
*
|
|
37
|
+
* @param options - Capacitor app configuration options
|
|
38
|
+
* @returns Current app state stores and an `initialize` function for manual
|
|
39
|
+
* re-initialization
|
|
40
|
+
* @example
|
|
41
|
+
* ```svelte
|
|
42
|
+
* <script>
|
|
43
|
+
* import { useCapacitorApp } from '@molecule/app-svelte'
|
|
44
|
+
* const { ready, error } = useCapacitorApp({ pushNotifications: true })
|
|
45
|
+
* </script>
|
|
46
|
+
*
|
|
47
|
+
* {#if $ready} <p>Ready</p> {:else if $error} <p>{$error.message}</p> {/if}
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
export declare function useCapacitorApp(options?: CapacitorAppOptions): UseCapacitorAppResult;
|
|
51
|
+
//# sourceMappingURL=useCapacitorApp.d.ts.map
|