@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,169 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Svelte hooks for routing, porting React's `useRouter` family.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import { type Readable } from 'svelte/store';
|
|
7
|
+
import type { ListRouteOptions, ListRouteResult, NavigateOptions, QueryParams, RouteLocation, RouteParams, Router } from '@molecule/app-routing';
|
|
8
|
+
/** What {@link useRouter} returns. */
|
|
9
|
+
export interface UseRouterResult {
|
|
10
|
+
/** The current location. */
|
|
11
|
+
location: Readable<RouteLocation>;
|
|
12
|
+
/** The current route's path parameters. */
|
|
13
|
+
params: Readable<Record<string, string>>;
|
|
14
|
+
/** The current query parameters. */
|
|
15
|
+
query: Readable<QueryParams>;
|
|
16
|
+
/** Navigates to a path. */
|
|
17
|
+
navigate: Router['navigate'];
|
|
18
|
+
/** Navigates to a named route. */
|
|
19
|
+
navigateTo: Router['navigateTo'];
|
|
20
|
+
/** Goes back one history entry. */
|
|
21
|
+
back: Router['back'];
|
|
22
|
+
/** Goes forward one history entry. */
|
|
23
|
+
forward: Router['forward'];
|
|
24
|
+
/** Whether a path is active (prefix, or exact with `exact`). */
|
|
25
|
+
isActive: Router['isActive'];
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Routing state and navigation, React's `useRouter` in Svelte's idiom: call it
|
|
29
|
+
* during component init under `setMoleculeContext({ router })`.
|
|
30
|
+
*
|
|
31
|
+
* @returns Router state as stores and the navigation methods.
|
|
32
|
+
* @example
|
|
33
|
+
* ```svelte
|
|
34
|
+
* <script>
|
|
35
|
+
* import { useRouter } from '@molecule/app-svelte'
|
|
36
|
+
* const { location, navigate } = useRouter()
|
|
37
|
+
* </script>
|
|
38
|
+
*
|
|
39
|
+
* <p>Current path: {$location.pathname}</p>
|
|
40
|
+
* <button onclick={() => navigate('/home')}>Go home</button>
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
export declare function useRouter(): UseRouterResult;
|
|
44
|
+
/**
|
|
45
|
+
* The current location, React's `useLocation` in Svelte's idiom.
|
|
46
|
+
*
|
|
47
|
+
* @returns A readable store of the current route location.
|
|
48
|
+
*/
|
|
49
|
+
export declare function useLocation(): Readable<RouteLocation>;
|
|
50
|
+
/**
|
|
51
|
+
* The current route's path parameters, React's `useParams` in Svelte's idiom.
|
|
52
|
+
*
|
|
53
|
+
* @param T - The params record type.
|
|
54
|
+
* @returns A readable store of the route parameters.
|
|
55
|
+
*/
|
|
56
|
+
export declare function useParams<T extends Record<string, string> = RouteParams>(): Readable<T>;
|
|
57
|
+
/**
|
|
58
|
+
* The current query parameters, React's `useQuery` in Svelte's idiom.
|
|
59
|
+
*
|
|
60
|
+
* @param T - The query record type.
|
|
61
|
+
* @returns A readable store of the query parameters.
|
|
62
|
+
*/
|
|
63
|
+
export declare function useQuery<T extends QueryParams = QueryParams>(): Readable<T>;
|
|
64
|
+
/**
|
|
65
|
+
* The navigate function, React's `useNavigate` in Svelte's idiom.
|
|
66
|
+
*
|
|
67
|
+
* @returns A function that navigates to a path.
|
|
68
|
+
*/
|
|
69
|
+
export declare function useNavigate(): (path: string, options?: NavigateOptions) => void;
|
|
70
|
+
/**
|
|
71
|
+
* Whether a path is active, React's `useIsActive` in Svelte's idiom: a
|
|
72
|
+
* readable store that re-evaluates on every route change.
|
|
73
|
+
*
|
|
74
|
+
* @param path - The path to check.
|
|
75
|
+
* @param exact - Whether to match exactly (default: false).
|
|
76
|
+
* @returns A readable store of whether the path is active.
|
|
77
|
+
*/
|
|
78
|
+
export declare function useIsActive(path: string, exact?: boolean): Readable<boolean>;
|
|
79
|
+
/** What {@link useListRoute} returns: each list field as a readable store, the actions as plain functions. */
|
|
80
|
+
export interface UseListRouteResult<Filter extends string = string, Selection extends string = string> {
|
|
81
|
+
/** The current 1-indexed page. */
|
|
82
|
+
page: Readable<number>;
|
|
83
|
+
/** The configured items per page. */
|
|
84
|
+
pageSize: Readable<number>;
|
|
85
|
+
/** The zero-indexed item offset for an API request. */
|
|
86
|
+
offset: Readable<number>;
|
|
87
|
+
/** The current URL filter values, including values supplied by path parameters. */
|
|
88
|
+
filters: Readable<Record<Filter, string | undefined>>;
|
|
89
|
+
/** The matching view, or the configured fallback. */
|
|
90
|
+
selection: Readable<Selection | undefined>;
|
|
91
|
+
/** The decoded current path parameters, also usable as API-only filters. */
|
|
92
|
+
params: Readable<RouteParams>;
|
|
93
|
+
/** False while a protected selection is awaiting auth, signed out, or unavailable. */
|
|
94
|
+
canLoad: Readable<boolean>;
|
|
95
|
+
/** Readiness for the current view; a public view always stays ready. */
|
|
96
|
+
status: Readable<ListRouteResult<Filter, Selection>['status']>;
|
|
97
|
+
/** The sign-in destination while a signed-out protected view is being redirected. */
|
|
98
|
+
redirectTo: Readable<string | undefined>;
|
|
99
|
+
/** Builds a real link target, preserving the pathname, unrelated query values and hash. */
|
|
100
|
+
href: ListRouteResult<Filter, Selection>['href'];
|
|
101
|
+
/** Navigates atomically; location state is preserved unless options replace it. */
|
|
102
|
+
navigate: ListRouteResult<Filter, Selection>['navigate'];
|
|
103
|
+
/** Changes only the page, retaining the current filters and view. */
|
|
104
|
+
setPage: ListRouteResult<Filter, Selection>['setPage'];
|
|
105
|
+
/** Patches URL filters and returns to page one. */
|
|
106
|
+
setFilters: ListRouteResult<Filter, Selection>['setFilters'];
|
|
107
|
+
/** Selects a configured view at page one. */
|
|
108
|
+
select: ListRouteResult<Filter, Selection>['select'];
|
|
109
|
+
}
|
|
110
|
+
/** The store {@link useConsumedSearchParam} returns: the current `[value, set]` tuple. */
|
|
111
|
+
export type UseConsumedSearchParamStore = Readable<readonly [string, (value: string) => void]>;
|
|
112
|
+
/**
|
|
113
|
+
* URL-backed pagination, filters and selected views, React's `useListRoute` in
|
|
114
|
+
* Svelte's idiom: the page, the filters and the view live in the URL only, so a
|
|
115
|
+
* reload, a shared link and Back all show the same list. Each list field is a
|
|
116
|
+
* readable store that follows the location, and the actions are plain
|
|
117
|
+
* functions, through whichever routing bond the app wired. Built on
|
|
118
|
+
* `resolveListRoute` from `@molecule/app-routing`, so every framework binding
|
|
119
|
+
* returns the same result.
|
|
120
|
+
*
|
|
121
|
+
* A signed-out visit to a view with `requiresAuth` replaces the entry with
|
|
122
|
+
* `options.auth.redirectTo`, carrying `state.returnTo`; until auth is confirmed
|
|
123
|
+
* `canLoad` is `false`.
|
|
124
|
+
*
|
|
125
|
+
* The options may be a readable store: an auth status moving from `loading` to
|
|
126
|
+
* `unauthenticated` re-resolves the list — and performs the sign-in redirect —
|
|
127
|
+
* on the same component, the way React re-renders `useListRoute`.
|
|
128
|
+
*
|
|
129
|
+
* @param options - Filter sources, view paths, pagination and optional auth readiness (or a store of them).
|
|
130
|
+
* @returns Each list field as a readable store, and its link builder and navigation actions.
|
|
131
|
+
* @throws {Error} When `pageSize` is not a positive safe integer, or a protected view has no `auth`.
|
|
132
|
+
* @example
|
|
133
|
+
* ```svelte
|
|
134
|
+
* <script>
|
|
135
|
+
* import { useListRoute } from '@molecule/app-svelte'
|
|
136
|
+
*
|
|
137
|
+
* const { page, offset, href } = useListRoute({ pageSize: 10, filters: { tag: {} } })
|
|
138
|
+
* // Load items from $offset; the page and filters come from the URL.
|
|
139
|
+
* </script>
|
|
140
|
+
*
|
|
141
|
+
* <a href={href({ page: $page + 1 })}>Next</a>
|
|
142
|
+
* ```
|
|
143
|
+
*/
|
|
144
|
+
export declare function useListRoute<Filter extends string = string, Selection extends string = string>(options?: ListRouteOptions<Filter, Selection> | Readable<ListRouteOptions<Filter, Selection>>): UseListRouteResult<Filter, Selection>;
|
|
145
|
+
/**
|
|
146
|
+
* Takes a one-time credential (a reset `token`, an OAuth `code`) out of the URL
|
|
147
|
+
* and keeps it in memory, React's `useConsumedSearchParam` in Svelte's idiom: a
|
|
148
|
+
* readable store of the `[value, set]` tuple, through whichever routing bond
|
|
149
|
+
* the app wired. The parameter is removed with a replace navigation that keeps
|
|
150
|
+
* the path, the hash, the other query values and the location state; the value
|
|
151
|
+
* survives re-renders and later query-only navigation, Back to an entry
|
|
152
|
+
* restores the value consumed there, and a new value arriving on the same
|
|
153
|
+
* mounted route is consumed too.
|
|
154
|
+
*
|
|
155
|
+
* @param name - The query key to consume, for example `token` or `code`.
|
|
156
|
+
* @returns A readable store of the consumed value (`''` before the parameter
|
|
157
|
+
* was ever present) and a setter (a form clears it after a successful submit).
|
|
158
|
+
* @example
|
|
159
|
+
* ```svelte
|
|
160
|
+
* <script>
|
|
161
|
+
* import { useConsumedSearchParam } from '@molecule/app-svelte'
|
|
162
|
+
*
|
|
163
|
+
* const token = useConsumedSearchParam('token')
|
|
164
|
+
* // The form submits $token[0] after the address no longer contains it.
|
|
165
|
+
* </script>
|
|
166
|
+
* ```
|
|
167
|
+
*/
|
|
168
|
+
export declare function useConsumedSearchParam(name: string): UseConsumedSearchParamStore;
|
|
169
|
+
//# sourceMappingURL=useRouter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useRouter.d.ts","sourceRoot":"","sources":["../../src/hooks/useRouter.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,OAAO,EAAO,KAAK,QAAQ,EAAY,MAAM,cAAc,CAAA;AAE3D,OAAO,KAAK,EACV,gBAAgB,EAChB,eAAe,EACf,eAAe,EACf,WAAW,EACX,aAAa,EACb,WAAW,EACX,MAAM,EACP,MAAM,uBAAuB,CAAA;AAU9B,sCAAsC;AACtC,MAAM,WAAW,eAAe;IAC9B,4BAA4B;IAC5B,QAAQ,EAAE,QAAQ,CAAC,aAAa,CAAC,CAAA;IACjC,2CAA2C;IAC3C,MAAM,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAA;IACxC,oCAAoC;IACpC,KAAK,EAAE,QAAQ,CAAC,WAAW,CAAC,CAAA;IAC5B,2BAA2B;IAC3B,QAAQ,EAAE,MAAM,CAAC,UAAU,CAAC,CAAA;IAC5B,kCAAkC;IAClC,UAAU,EAAE,MAAM,CAAC,YAAY,CAAC,CAAA;IAChC,mCAAmC;IACnC,IAAI,EAAE,MAAM,CAAC,MAAM,CAAC,CAAA;IACpB,sCAAsC;IACtC,OAAO,EAAE,MAAM,CAAC,SAAS,CAAC,CAAA;IAC1B,gEAAgE;IAChE,QAAQ,EAAE,MAAM,CAAC,UAAU,CAAC,CAAA;CAC7B;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAg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
1
|
+
/**
|
|
2
|
+
* Svelte hooks for routing, porting React's `useRouter` family.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import { onDestroy, onMount } from 'svelte';
|
|
7
|
+
import { get, writable } from 'svelte/store';
|
|
8
|
+
import { createConsumedSearchParam, listRouteRedirect, resolveListRoute, } from '@molecule/app-routing';
|
|
9
|
+
import { useRouterInstance } from '../context.js';
|
|
10
|
+
import { fieldStore, isReadableStore, liveStore } from './live-store.js';
|
|
11
|
+
/**
|
|
12
|
+
* Routing state and navigation, React's `useRouter` in Svelte's idiom: call it
|
|
13
|
+
* during component init under `setMoleculeContext({ router })`.
|
|
14
|
+
*
|
|
15
|
+
* @returns Router state as stores and the navigation methods.
|
|
16
|
+
* @example
|
|
17
|
+
* ```svelte
|
|
18
|
+
* <script>
|
|
19
|
+
* import { useRouter } from '@molecule/app-svelte'
|
|
20
|
+
* const { location, navigate } = useRouter()
|
|
21
|
+
* </script>
|
|
22
|
+
*
|
|
23
|
+
* <p>Current path: {$location.pathname}</p>
|
|
24
|
+
* <button onclick={() => navigate('/home')}>Go home</button>
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
export function useRouter() {
|
|
28
|
+
const router = useRouterInstance();
|
|
29
|
+
const state = liveStore(() => ({
|
|
30
|
+
location: router.getLocation(),
|
|
31
|
+
params: router.getParams(),
|
|
32
|
+
query: router.getQuery(),
|
|
33
|
+
}), (notify) => router.subscribe(notify));
|
|
34
|
+
return {
|
|
35
|
+
location: fieldStore(state, 'location'),
|
|
36
|
+
params: fieldStore(state, 'params'),
|
|
37
|
+
query: fieldStore(state, 'query'),
|
|
38
|
+
navigate: (path, options) => router.navigate(path, options),
|
|
39
|
+
navigateTo: (name, params, query, options) => router.navigateTo(name, params, query, options),
|
|
40
|
+
back: () => router.back(),
|
|
41
|
+
forward: () => router.forward(),
|
|
42
|
+
isActive: (path, exact) => router.isActive(path, exact),
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* The current location, React's `useLocation` in Svelte's idiom.
|
|
47
|
+
*
|
|
48
|
+
* @returns A readable store of the current route location.
|
|
49
|
+
*/
|
|
50
|
+
export function useLocation() {
|
|
51
|
+
const { location } = useRouter();
|
|
52
|
+
return location;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* The current route's path parameters, React's `useParams` in Svelte's idiom.
|
|
56
|
+
*
|
|
57
|
+
* @param T - The params record type.
|
|
58
|
+
* @returns A readable store of the route parameters.
|
|
59
|
+
*/
|
|
60
|
+
export function useParams() {
|
|
61
|
+
const { params } = useRouter();
|
|
62
|
+
return params;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* The current query parameters, React's `useQuery` in Svelte's idiom.
|
|
66
|
+
*
|
|
67
|
+
* @param T - The query record type.
|
|
68
|
+
* @returns A readable store of the query parameters.
|
|
69
|
+
*/
|
|
70
|
+
export function useQuery() {
|
|
71
|
+
const { query } = useRouter();
|
|
72
|
+
return query;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* The navigate function, React's `useNavigate` in Svelte's idiom.
|
|
76
|
+
*
|
|
77
|
+
* @returns A function that navigates to a path.
|
|
78
|
+
*/
|
|
79
|
+
export function useNavigate() {
|
|
80
|
+
const router = useRouterInstance();
|
|
81
|
+
return (path, options) => router.navigate(path, options);
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Whether a path is active, React's `useIsActive` in Svelte's idiom: a
|
|
85
|
+
* readable store that re-evaluates on every route change.
|
|
86
|
+
*
|
|
87
|
+
* @param path - The path to check.
|
|
88
|
+
* @param exact - Whether to match exactly (default: false).
|
|
89
|
+
* @returns A readable store of whether the path is active.
|
|
90
|
+
*/
|
|
91
|
+
export function useIsActive(path, exact = false) {
|
|
92
|
+
const router = useRouterInstance();
|
|
93
|
+
return liveStore(() => router.isActive(path, exact), (notify) => router.subscribe(notify));
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* URL-backed pagination, filters and selected views, React's `useListRoute` in
|
|
97
|
+
* Svelte's idiom: the page, the filters and the view live in the URL only, so a
|
|
98
|
+
* reload, a shared link and Back all show the same list. Each list field is a
|
|
99
|
+
* readable store that follows the location, and the actions are plain
|
|
100
|
+
* functions, through whichever routing bond the app wired. Built on
|
|
101
|
+
* `resolveListRoute` from `@molecule/app-routing`, so every framework binding
|
|
102
|
+
* returns the same result.
|
|
103
|
+
*
|
|
104
|
+
* A signed-out visit to a view with `requiresAuth` replaces the entry with
|
|
105
|
+
* `options.auth.redirectTo`, carrying `state.returnTo`; until auth is confirmed
|
|
106
|
+
* `canLoad` is `false`.
|
|
107
|
+
*
|
|
108
|
+
* The options may be a readable store: an auth status moving from `loading` to
|
|
109
|
+
* `unauthenticated` re-resolves the list — and performs the sign-in redirect —
|
|
110
|
+
* on the same component, the way React re-renders `useListRoute`.
|
|
111
|
+
*
|
|
112
|
+
* @param options - Filter sources, view paths, pagination and optional auth readiness (or a store of them).
|
|
113
|
+
* @returns Each list field as a readable store, and its link builder and navigation actions.
|
|
114
|
+
* @throws {Error} When `pageSize` is not a positive safe integer, or a protected view has no `auth`.
|
|
115
|
+
* @example
|
|
116
|
+
* ```svelte
|
|
117
|
+
* <script>
|
|
118
|
+
* import { useListRoute } from '@molecule/app-svelte'
|
|
119
|
+
*
|
|
120
|
+
* const { page, offset, href } = useListRoute({ pageSize: 10, filters: { tag: {} } })
|
|
121
|
+
* // Load items from $offset; the page and filters come from the URL.
|
|
122
|
+
* </script>
|
|
123
|
+
*
|
|
124
|
+
* <a href={href({ page: $page + 1 })}>Next</a>
|
|
125
|
+
* ```
|
|
126
|
+
*/
|
|
127
|
+
export function useListRoute(options = {}) {
|
|
128
|
+
const router = useRouterInstance();
|
|
129
|
+
let currentOptions = isReadableStore(options) ? get(options) : options;
|
|
130
|
+
const resolve = () => resolveListRoute({ location: router.getLocation(), routeParams: router.getParams() }, currentOptions, (path, navigateOptions) => router.navigate(path, navigateOptions));
|
|
131
|
+
let latest = resolve();
|
|
132
|
+
const state = writable(latest);
|
|
133
|
+
// One re-resolve per location (or options) change, then the redirect it asks
|
|
134
|
+
// for: navigating notifies this subscription again, and at the sign-in URL
|
|
135
|
+
// `listRouteRedirect` answers undefined, so the update settles.
|
|
136
|
+
const update = () => {
|
|
137
|
+
latest = resolve();
|
|
138
|
+
state.set(latest);
|
|
139
|
+
const redirect = listRouteRedirect(latest, router.getLocation());
|
|
140
|
+
if (redirect)
|
|
141
|
+
router.navigate(redirect.to, redirect.options);
|
|
142
|
+
};
|
|
143
|
+
const stopRouter = router.subscribe(() => update());
|
|
144
|
+
let stopOptions = null;
|
|
145
|
+
if (isReadableStore(options)) {
|
|
146
|
+
stopOptions = options.subscribe((next) => {
|
|
147
|
+
if (next === currentOptions)
|
|
148
|
+
return;
|
|
149
|
+
currentOptions = next;
|
|
150
|
+
update();
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
// React's redirect effect also runs once mounted, for a list that opens
|
|
154
|
+
// directly on a signed-out protected view.
|
|
155
|
+
onMount(update);
|
|
156
|
+
onDestroy(() => {
|
|
157
|
+
stopRouter();
|
|
158
|
+
stopOptions?.();
|
|
159
|
+
});
|
|
160
|
+
return {
|
|
161
|
+
page: fieldStore(state, 'page'),
|
|
162
|
+
pageSize: fieldStore(state, 'pageSize'),
|
|
163
|
+
offset: fieldStore(state, 'offset'),
|
|
164
|
+
filters: fieldStore(state, 'filters'),
|
|
165
|
+
selection: fieldStore(state, 'selection'),
|
|
166
|
+
params: fieldStore(state, 'params'),
|
|
167
|
+
canLoad: fieldStore(state, 'canLoad'),
|
|
168
|
+
status: fieldStore(state, 'status'),
|
|
169
|
+
redirectTo: fieldStore(state, 'redirectTo'),
|
|
170
|
+
href: (change) => latest.href(change),
|
|
171
|
+
navigate: (change, navigateOptions) => latest.navigate(change, navigateOptions),
|
|
172
|
+
setPage: (page, navigateOptions) => latest.setPage(page, navigateOptions),
|
|
173
|
+
setFilters: (filters, navigateOptions) => latest.setFilters(filters, navigateOptions),
|
|
174
|
+
select: (selection, navigateOptions) => latest.select(selection, navigateOptions),
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Takes a one-time credential (a reset `token`, an OAuth `code`) out of the URL
|
|
179
|
+
* and keeps it in memory, React's `useConsumedSearchParam` in Svelte's idiom: a
|
|
180
|
+
* readable store of the `[value, set]` tuple, through whichever routing bond
|
|
181
|
+
* the app wired. The parameter is removed with a replace navigation that keeps
|
|
182
|
+
* the path, the hash, the other query values and the location state; the value
|
|
183
|
+
* survives re-renders and later query-only navigation, Back to an entry
|
|
184
|
+
* restores the value consumed there, and a new value arriving on the same
|
|
185
|
+
* mounted route is consumed too.
|
|
186
|
+
*
|
|
187
|
+
* @param name - The query key to consume, for example `token` or `code`.
|
|
188
|
+
* @returns A readable store of the consumed value (`''` before the parameter
|
|
189
|
+
* was ever present) and a setter (a form clears it after a successful submit).
|
|
190
|
+
* @example
|
|
191
|
+
* ```svelte
|
|
192
|
+
* <script>
|
|
193
|
+
* import { useConsumedSearchParam } from '@molecule/app-svelte'
|
|
194
|
+
*
|
|
195
|
+
* const token = useConsumedSearchParam('token')
|
|
196
|
+
* // The form submits $token[0] after the address no longer contains it.
|
|
197
|
+
* </script>
|
|
198
|
+
* ```
|
|
199
|
+
*/
|
|
200
|
+
export function useConsumedSearchParam(name) {
|
|
201
|
+
const router = useRouterInstance();
|
|
202
|
+
const param = createConsumedSearchParam(router, name);
|
|
203
|
+
const tuple = writable([param.get(), param.set]);
|
|
204
|
+
const unsubscribe = param.subscribe((value) => tuple.set([value, param.set]));
|
|
205
|
+
onDestroy(unsubscribe);
|
|
206
|
+
// connect() consumes what the URL holds now and follows the router; its
|
|
207
|
+
// return stops both. It runs once mounted, as React's
|
|
208
|
+
// `useEffect(() => param.connect(), [param])` does, so a server render never
|
|
209
|
+
// navigates.
|
|
210
|
+
onMount(() => param.connect());
|
|
211
|
+
return tuple;
|
|
212
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Svelte hook for registration with async state tracking, porting React's
|
|
3
|
+
* `useSignup`.
|
|
4
|
+
*
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
import type { Readable } from 'svelte/store';
|
|
8
|
+
import type { AuthResult, RegisterData, RegisterExtraShape } from '@molecule/app-auth';
|
|
9
|
+
import { type UsePromiseState } from './usePromise.js';
|
|
10
|
+
/**
|
|
11
|
+
* What {@link useSignup} returns.
|
|
12
|
+
*
|
|
13
|
+
* @template T - The user profile the API returns.
|
|
14
|
+
* @template TRegister - The app's own registration fields (the second generic
|
|
15
|
+
* of `createJWTAuthClient<T, TRegister>`), so `signup()` is typed like the
|
|
16
|
+
* client's `register()`.
|
|
17
|
+
*/
|
|
18
|
+
export interface UseSignupResult<T = unknown, TRegister extends RegisterExtraShape<TRegister> = Record<never, never>> {
|
|
19
|
+
/** The latest call's status. */
|
|
20
|
+
status: Readable<UsePromiseState<AuthResult<T>>['status']>;
|
|
21
|
+
/** The latest call's resolved value. */
|
|
22
|
+
value: Readable<UsePromiseState<AuthResult<T>>['value']>;
|
|
23
|
+
/** The latest call's rejection. */
|
|
24
|
+
error: Readable<UsePromiseState<AuthResult<T>>['error']>;
|
|
25
|
+
/** Runs the signup. */
|
|
26
|
+
signup: (data: RegisterData<TRegister>) => Promise<AuthResult<T>>;
|
|
27
|
+
/** Returns the tracker to idle. */
|
|
28
|
+
reset: () => void;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* User registration with async state tracking, React's `useSignup` in
|
|
32
|
+
* Svelte's idiom: call it during component init under
|
|
33
|
+
* `setMoleculeContext({ auth })`.
|
|
34
|
+
*
|
|
35
|
+
* @param T - The user profile the API returns.
|
|
36
|
+
* @param TRegister - The app's own registration fields.
|
|
37
|
+
* @returns Signup state as stores and the action.
|
|
38
|
+
* @example
|
|
39
|
+
* ```svelte
|
|
40
|
+
* <script>
|
|
41
|
+
* import { useSignup } from '@molecule/app-svelte'
|
|
42
|
+
* const { status, error, signup } = useSignup()
|
|
43
|
+
* </script>
|
|
44
|
+
*
|
|
45
|
+
* <button onclick={() => signup({ email, password, name })} disabled={$status === 'pending'}>
|
|
46
|
+
* {$status === 'pending' ? 'Creating account…' : 'Sign up'}
|
|
47
|
+
* </button>
|
|
48
|
+
* {#if $error}<p>{$error.message}</p>{/if}
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
export declare function useSignup<T = unknown, TRegister extends RegisterExtraShape<TRegister> = Record<never, never>>(): UseSignupResult<T, TRegister>;
|
|
52
|
+
//# sourceMappingURL=useSignup.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useSignup.d.ts","sourceRoot":"","sources":["../../src/hooks/useSignup.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAA;AAE5C,OAAO,KAAK,EAAE,UAAU,EAAE,YAAY,EAAE,kBAAkB,EAAE,MAAM,oBAAoB,CAAA;AAItF,OAAO,EAAc,KAAK,eAAe,EAAwB,MAAM,iBAAiB,CAAA;AAExF;;;;;;;GAOG;AACH,MAAM,WAAW,eAAe,CAC9B,CAAC,GAAG,OAAO,EACX,SAAS,SAAS,kBAAkB,CAAC,SAAS,CAAC,GAAG,MAAM,CAAC,KAAK,EAAE,KAAK,CAAC;IAEtE,gCAAgC;IAChC,MAAM,EAAE,QAAQ,CAAC,eAAe,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAA;IAC1D,wCAAwC;IACxC,KAAK,EAAE,QAAQ,CAAC,eAAe,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAA;IACxD,mCAAmC;IACnC,KAAK,EAAE,QAAQ,CAAC,eAAe,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAA;IACxD,uBAAuB;IACvB,MAAM,EAAE,CAAC,IAAI,EAAE,YAAY,CAAC,SAAS,CAAC,KAAK,OAAO,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,CAAA;IACjE,mCAAmC;IACnC,KAAK,EAAE,MAAM,IAAI,CAAA;CAClB;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,SAAS,CACvB,CAAC,GAAG,OAAO,EACX,SAAS,SAAS,kBAAkB,CAAC,SAAS,CAAC,GAAG,MAAM,CAAC,KAAK,EAAE,KAAK,CAAC,KACnE,eAAe,CAAC,CAAC,EAAE,SAAS,CAAC,CAajC"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Svelte hook for registration with async state tracking, porting React's
|
|
3
|
+
* `useSignup`.
|
|
4
|
+
*
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
import { useAuthClient } from '../context.js';
|
|
8
|
+
import { promiseFields } from './live-store.js';
|
|
9
|
+
import { usePromise } from './usePromise.js';
|
|
10
|
+
/**
|
|
11
|
+
* User registration with async state tracking, React's `useSignup` in
|
|
12
|
+
* Svelte's idiom: call it during component init under
|
|
13
|
+
* `setMoleculeContext({ auth })`.
|
|
14
|
+
*
|
|
15
|
+
* @param T - The user profile the API returns.
|
|
16
|
+
* @param TRegister - The app's own registration fields.
|
|
17
|
+
* @returns Signup state as stores and the action.
|
|
18
|
+
* @example
|
|
19
|
+
* ```svelte
|
|
20
|
+
* <script>
|
|
21
|
+
* import { useSignup } from '@molecule/app-svelte'
|
|
22
|
+
* const { status, error, signup } = useSignup()
|
|
23
|
+
* </script>
|
|
24
|
+
*
|
|
25
|
+
* <button onclick={() => signup({ email, password, name })} disabled={$status === 'pending'}>
|
|
26
|
+
* {$status === 'pending' ? 'Creating account…' : 'Sign up'}
|
|
27
|
+
* </button>
|
|
28
|
+
* {#if $error}<p>{$error.message}</p>{/if}
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
export function useSignup() {
|
|
32
|
+
const client = useAuthClient();
|
|
33
|
+
const tracker = usePromise((data) => client.register(data));
|
|
34
|
+
const fields = promiseFields(tracker);
|
|
35
|
+
return {
|
|
36
|
+
status: fields.status,
|
|
37
|
+
value: fields.value,
|
|
38
|
+
error: fields.error,
|
|
39
|
+
signup: fields.call,
|
|
40
|
+
reset: fields.reset,
|
|
41
|
+
};
|
|
42
|
+
}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Svelte hooks for storage, porting React's `useStorage` family.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import { type Readable } from 'svelte/store';
|
|
7
|
+
/**
|
|
8
|
+
* Options for {@link useStorageValue}.
|
|
9
|
+
*/
|
|
10
|
+
export interface UseStorageValueOptions<T> {
|
|
11
|
+
/**
|
|
12
|
+
* Default value if key doesn't exist.
|
|
13
|
+
*/
|
|
14
|
+
defaultValue?: T;
|
|
15
|
+
/**
|
|
16
|
+
* Whether to sync across tabs/windows (if supported by storage provider).
|
|
17
|
+
*/
|
|
18
|
+
sync?: boolean;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Result of {@link useStorageValue}.
|
|
22
|
+
*/
|
|
23
|
+
export interface UseStorageValueResult<T> {
|
|
24
|
+
/** The stored value, or the default while none is stored. */
|
|
25
|
+
value: Readable<T | undefined>;
|
|
26
|
+
/** Persists the value; rejects when the provider refuses the write. */
|
|
27
|
+
setValue: (value: T) => Promise<void>;
|
|
28
|
+
/** Removes the key and falls back to the default; rejects on failure. */
|
|
29
|
+
removeValue: () => Promise<void>;
|
|
30
|
+
/** Whether the first read is still in flight. */
|
|
31
|
+
loading: Readable<boolean>;
|
|
32
|
+
/** The failing read's or write's error. */
|
|
33
|
+
error: Readable<Error | null>;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* One storage key as Svelte stores, React's `useStorageValue` in Svelte's
|
|
37
|
+
* idiom: call it during component init under `setMoleculeContext({ storage })`.
|
|
38
|
+
*
|
|
39
|
+
* A read that started before a newer local write keeps that write's value (it
|
|
40
|
+
* still ends `loading`), and with `sync` an outside write reaches the hook
|
|
41
|
+
* through the provider's own subscription — so a non-browser backend
|
|
42
|
+
* (native, encrypted, memory) propagates the same way, not just the browser
|
|
43
|
+
* `storage` event.
|
|
44
|
+
*
|
|
45
|
+
* @param key - Storage key
|
|
46
|
+
* @param options - Hook options
|
|
47
|
+
* @returns Storage value state (value, loading, error) and mutators (setValue, removeValue).
|
|
48
|
+
* @example
|
|
49
|
+
* ```svelte
|
|
50
|
+
* <script>
|
|
51
|
+
* import { useStorageValue } from '@molecule/app-svelte'
|
|
52
|
+
* const { value: theme, setValue: setTheme, loading } = useStorageValue('theme', {
|
|
53
|
+
* defaultValue: 'light',
|
|
54
|
+
* })
|
|
55
|
+
* </script>
|
|
56
|
+
*
|
|
57
|
+
* {#if $loading}<Spinner />
|
|
58
|
+
* {:else}<select onchange={(e) => setTheme(e.currentTarget.value)}>…</select>{/if}
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
61
|
+
export declare function useStorageValue<T>(key: string, options?: UseStorageValueOptions<T>): UseStorageValueResult<T>;
|
|
62
|
+
/**
|
|
63
|
+
* Options for {@link useStoragePreference}.
|
|
64
|
+
*/
|
|
65
|
+
export interface UseStoragePreferenceOptions<T> {
|
|
66
|
+
/**
|
|
67
|
+
* Value reported until the provider supplies one. It is NEVER written to
|
|
68
|
+
* storage: a default is a display fallback, not a decision the user made.
|
|
69
|
+
*/
|
|
70
|
+
defaultValue?: T;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Result of {@link useStoragePreference}.
|
|
74
|
+
*/
|
|
75
|
+
export interface UseStoragePreferenceResult<T> {
|
|
76
|
+
/** The persisted value, or `defaultValue` while none is known. */
|
|
77
|
+
value: Readable<T | undefined>;
|
|
78
|
+
/**
|
|
79
|
+
* Writes the value through the preference store: subscribers update
|
|
80
|
+
* immediately and persistence is best-effort.
|
|
81
|
+
*/
|
|
82
|
+
setValue: (next: T | ((previous: T | undefined) => T)) => void;
|
|
83
|
+
/** Removes the key through the preference store. */
|
|
84
|
+
removeValue: () => void;
|
|
85
|
+
/**
|
|
86
|
+
* `true` once the store has a definite answer — the synchronous snapshot on
|
|
87
|
+
* a synchronous provider, or the hydration result on an asynchronous one.
|
|
88
|
+
*/
|
|
89
|
+
ready: Readable<boolean>;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Subscribes Svelte stores to ONE persisted preference, React's
|
|
93
|
+
* `useStoragePreference` in Svelte's idiom.
|
|
94
|
+
*
|
|
95
|
+
* This is the component-side half of `getPreferenceStore()`: an init-time
|
|
96
|
+
* value is available immediately when the bonded provider can answer
|
|
97
|
+
* synchronously, and an asynchronous provider's hydration CONVERGES onto the
|
|
98
|
+
* same component without a remount. Ordering is the store's: a read that
|
|
99
|
+
* started before a newer write, remove or external change is discarded, so a
|
|
100
|
+
* slow hydration never overwrites an edit the user already made.
|
|
101
|
+
*
|
|
102
|
+
* Pass `null` for `key` while the real key is unknown (no project selected
|
|
103
|
+
* yet): the hook then behaves as local state around `defaultValue` and
|
|
104
|
+
* persists nothing.
|
|
105
|
+
*
|
|
106
|
+
* @param key - The storage key, or `null` when there is nothing to persist.
|
|
107
|
+
* @param options - `defaultValue`, reported until the provider supplies one.
|
|
108
|
+
* @returns The current value, its mutators, and whether it is definite yet.
|
|
109
|
+
* @example
|
|
110
|
+
* ```svelte
|
|
111
|
+
* <script>
|
|
112
|
+
* import { useStoragePreference } from '@molecule/app-svelte'
|
|
113
|
+
* const { value: width, setValue: setWidth, ready } = useStoragePreference(key, {
|
|
114
|
+
* defaultValue: 280,
|
|
115
|
+
* })
|
|
116
|
+
* </script>
|
|
117
|
+
* ```
|
|
118
|
+
*/
|
|
119
|
+
export declare function useStoragePreference<T = unknown>(key: string | null | undefined, options?: UseStoragePreferenceOptions<T>): UseStoragePreferenceResult<T>;
|
|
120
|
+
/**
|
|
121
|
+
* Storage operations without local state sync, React's `useStorage` in
|
|
122
|
+
* Svelte's idiom: plain passthroughs of the context provider.
|
|
123
|
+
*
|
|
124
|
+
* @returns Storage operation methods
|
|
125
|
+
*/
|
|
126
|
+
export declare function useStorage(): {
|
|
127
|
+
get: <T>(key: string) => Promise<T | null>;
|
|
128
|
+
set: <T>(key: string, value: T) => Promise<void>;
|
|
129
|
+
remove: (key: string) => Promise<void>;
|
|
130
|
+
clear: () => Promise<void>;
|
|
131
|
+
keys: () => Promise<string[]>;
|
|
132
|
+
};
|
|
133
|
+
//# sourceMappingURL=useStorage.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useStorage.d.ts","sourceRoot":"","sources":["../../src/hooks/useStorage.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,OAAO,EAAE,KAAK,QAAQ,EAAY,MAAM,cAAc,CAAA;AAMtD;;GAEG;AACH,MAAM,WAAW,sBAAsB,CAAC,CAAC;IACvC;;OAEG;IACH,YAAY,CAAC,EAAE,CAAC,CAAA;IAChB;;OAEG;IACH,IAAI,CAAC,EAAE,OAAO,CAAA;CACf;AAED;;GAEG;AACH,MAAM,WAAW,qBAAqB,CAAC,CAAC;IACtC,6DAA6D;IAC7D,KAAK,EAAE,QAAQ,CAAC,CAAC,GAAG,SAAS,CAAC,CAAA;IAC9B,uEAAuE;IACvE,QAAQ,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,OAAO,CAAC,IAAI,CAAC,CAAA;IACrC,yEAAyE;IACzE,WAAW,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAA;IAChC,iDAAiD;IACjD,OAAO,EAAE,QAAQ,CAAC,OAAO,CAAC,CAAA;IAC1B,2CAA2C;IAC3C,KAAK,EAAE,QAAQ,CAAC,KAAK,GAAG,IAAI,CAAC,CAAA;CAC9B;AAED;;;;;;;;;;;;;;;;;;;;;;;;;GAyB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