@fougere/svelte 0.2.0-alpha.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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Fougere contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,19 @@
1
+ /**
2
+ * `@fougere/svelte` — the four primitives, in Svelte, for any host that renders it.
3
+ *
4
+ * There is no `@fougere/sveltekit` beside it, and that is not an omission: SvelteKit
5
+ * serves Web-standard `Request`/`Response` from `+server.ts`, and it hands every
6
+ * `load` function its own `event`, so there is no ambient-request lookup to package.
7
+ * A SvelteKit app mounts `@fougere/app/web` directly, the way the TanStack and React
8
+ * Router demos do. Next needed 124 lines because `next/headers` exists; SvelteKit
9
+ * needs none because it does not.
10
+ *
11
+ * So: one package per UI framework, and a host package only when the host has
12
+ * something that is genuinely its own.
13
+ */
14
+ export { useQuery, useCommand, type QueryState, type QueryStore, type CommandStore } from './useFougereData.js';
15
+ export { useFormFor, type FormOptions } from './useFormFor.js';
16
+ export { useCurrentUser, hydrateSession, refreshSession } from './useCurrentUser.js';
17
+ export { fetcher, CALL_ENDPOINT } from '@fougere/app/client';
18
+ export type { CallInput, EntityClass, FormEntity, FormField, SessionView, } from '@fougere/app/client';
19
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AACH,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAE,KAAK,UAAU,EAAE,KAAK,UAAU,EAAE,KAAK,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAChH,OAAO,EAAE,UAAU,EAAE,KAAK,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAC/D,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AACrF,OAAO,EAAE,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAE7D,YAAY,EACV,SAAS,EACT,WAAW,EACX,UAAU,EACV,SAAS,EACT,WAAW,GACZ,MAAM,qBAAqB,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,18 @@
1
+ /**
2
+ * `@fougere/svelte` — the four primitives, in Svelte, for any host that renders it.
3
+ *
4
+ * There is no `@fougere/sveltekit` beside it, and that is not an omission: SvelteKit
5
+ * serves Web-standard `Request`/`Response` from `+server.ts`, and it hands every
6
+ * `load` function its own `event`, so there is no ambient-request lookup to package.
7
+ * A SvelteKit app mounts `@fougere/app/web` directly, the way the TanStack and React
8
+ * Router demos do. Next needed 124 lines because `next/headers` exists; SvelteKit
9
+ * needs none because it does not.
10
+ *
11
+ * So: one package per UI framework, and a host package only when the host has
12
+ * something that is genuinely its own.
13
+ */
14
+ export { useQuery, useCommand } from './useFougereData.js';
15
+ export { useFormFor } from './useFormFor.js';
16
+ export { useCurrentUser, hydrateSession, refreshSession } from './useCurrentUser.js';
17
+ export { fetcher, CALL_ENDPOINT } from '@fougere/app/client';
18
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AACH,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAuD,MAAM,qBAAqB,CAAC;AAChH,OAAO,EAAE,UAAU,EAAoB,MAAM,iBAAiB,CAAC;AAC/D,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AACrF,OAAO,EAAE,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC"}
@@ -0,0 +1,20 @@
1
+ /**
2
+ * The session: resolved server-side, read here as a store. `refresh()` re-reads it
3
+ * after a client-side auth change — no page reload, no hand-rolled /api/me.
4
+ *
5
+ * SvelteKit hands a `load` function's data down through the page, so a layout that
6
+ * already resolved the session can `hydrate()` this store and the page arrives
7
+ * knowing its user. Without it, the store fetches once on first read.
8
+ */
9
+ import { type Readable } from 'svelte/store';
10
+ import type { SessionView } from '@fougere/app/client';
11
+ /** Seed the store from what the server already resolved (a `load` function's data). */
12
+ export declare function hydrateSession(view: SessionView): void;
13
+ export declare function refreshSession(): Promise<void>;
14
+ export declare function useCurrentUser<TUser = Record<string, unknown>>(): {
15
+ session: Readable<SessionView>;
16
+ user: Readable<TUser | null>;
17
+ loggedIn: Readable<boolean>;
18
+ refresh: typeof refreshSession;
19
+ };
20
+ //# sourceMappingURL=useCurrentUser.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useCurrentUser.d.ts","sourceRoot":"","sources":["../src/useCurrentUser.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,EAAqB,KAAK,QAAQ,EAAE,MAAM,cAAc,CAAC;AAChE,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAKvD,uFAAuF;AACvF,wBAAgB,cAAc,CAAC,IAAI,EAAE,WAAW,GAAG,IAAI,CAGtD;AAED,wBAAsB,cAAc,IAAI,OAAO,CAAC,IAAI,CAAC,CAIpD;AAED,wBAAgB,cAAc,CAAC,KAAK,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;IAK1D,OAAO,EAAa,QAAQ,CAAC,WAAW,CAAC;IACzC,IAAI;IACJ,QAAQ;IACR,OAAO;EAEV"}
@@ -0,0 +1,33 @@
1
+ /**
2
+ * The session: resolved server-side, read here as a store. `refresh()` re-reads it
3
+ * after a client-side auth change — no page reload, no hand-rolled /api/me.
4
+ *
5
+ * SvelteKit hands a `load` function's data down through the page, so a layout that
6
+ * already resolved the session can `hydrate()` this store and the page arrives
7
+ * knowing its user. Without it, the store fetches once on first read.
8
+ */
9
+ import { writable, derived } from 'svelte/store';
10
+ const session = writable({ user: null });
11
+ let fetched = false;
12
+ /** Seed the store from what the server already resolved (a `load` function's data). */
13
+ export function hydrateSession(view) {
14
+ fetched = true;
15
+ session.set(view);
16
+ }
17
+ export async function refreshSession() {
18
+ const response = await fetch('/_fougere/session');
19
+ fetched = true;
20
+ session.set((await response.json()));
21
+ }
22
+ export function useCurrentUser() {
23
+ // Only reached in the browser, and only when a layout did not hydrate it.
24
+ if (!fetched && typeof window !== 'undefined')
25
+ void refreshSession();
26
+ return {
27
+ session: session,
28
+ user: derived(session, ($session) => ($session.user ?? null)),
29
+ loggedIn: derived(session, ($session) => $session.user != null),
30
+ refresh: refreshSession,
31
+ };
32
+ }
33
+ //# sourceMappingURL=useCurrentUser.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useCurrentUser.js","sourceRoot":"","sources":["../src/useCurrentUser.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAiB,MAAM,cAAc,CAAC;AAGhE,MAAM,OAAO,GAAG,QAAQ,CAAc,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC;AACtD,IAAI,OAAO,GAAG,KAAK,CAAC;AAEpB,uFAAuF;AACvF,MAAM,UAAU,cAAc,CAAC,IAAiB;IAC9C,OAAO,GAAG,IAAI,CAAC;IACf,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;AACpB,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,cAAc;IAClC,MAAM,QAAQ,GAAG,MAAM,KAAK,CAAC,mBAAmB,CAAC,CAAC;IAClD,OAAO,GAAG,IAAI,CAAC;IACf,OAAO,CAAC,GAAG,CAAC,CAAC,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAgB,CAAC,CAAC;AACtD,CAAC;AAED,MAAM,UAAU,cAAc;IAC5B,0EAA0E;IAC1E,IAAI,CAAC,OAAO,IAAI,OAAO,MAAM,KAAK,WAAW;QAAE,KAAK,cAAc,EAAE,CAAC;IAErE,OAAO;QACL,OAAO,EAAE,OAAgC;QACzC,IAAI,EAAE,OAAO,CAAC,OAAO,EAAE,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC,QAAQ,CAAC,IAAI,IAAI,IAAI,CAAiB,CAAC;QAC7E,QAAQ,EAAE,OAAO,CAAC,OAAO,EAAE,CAAC,QAAQ,EAAE,EAAE,CAAC,QAAQ,CAAC,IAAI,IAAI,IAAI,CAAC;QAC/D,OAAO,EAAE,cAAc;KACxB,CAAC;AACJ,CAAC"}
@@ -0,0 +1,32 @@
1
+ /**
2
+ * The form contract — state, validation, submission, error mapping. Never a widget.
3
+ *
4
+ * `values` is a writable store, so a component binds straight into it with
5
+ * `bind:value={$form.values[field.name]}`. That is the Svelte equivalent of Vue's
6
+ * reactive object and of React's `setValue`: the same contract, three ways of
7
+ * holding a value, none of which is a rule.
8
+ */
9
+ import { type Readable, type Writable } from 'svelte/store';
10
+ import { type FormEntity, type FormField } from '@fougere/app/client';
11
+ export interface FormOptions {
12
+ /** Command the submit rides. Default: 'create'. */
13
+ op?: string;
14
+ /** Initial values (edit mode: the loaded entity). */
15
+ initial?: Record<string, unknown>;
16
+ /** Call params designating the target (edit mode: { id }). */
17
+ params?: Record<string, string>;
18
+ }
19
+ export declare function useFormFor<T = Record<string, unknown>>(entity: FormEntity, options?: FormOptions): {
20
+ fields: FormField[];
21
+ /** The same fields, keyed by name — spread `fieldsByName.email.attrs` on an input
22
+ * and the page states no rule of its own. */
23
+ fieldsByName: Record<string, FormField>;
24
+ values: Writable<Record<string, unknown>>;
25
+ errors: Writable<Record<string, string>>;
26
+ submit: () => Promise<T | null>;
27
+ judge: () => boolean;
28
+ valid: Readable<boolean>;
29
+ /** `{ loading, error }` of the underlying command. */
30
+ command: import("./useFougereData.js").CommandStore<T>;
31
+ };
32
+ //# sourceMappingURL=useFormFor.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useFormFor.d.ts","sourceRoot":"","sources":["../src/useFormFor.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,EAA0B,KAAK,QAAQ,EAAE,KAAK,QAAQ,EAAE,MAAM,cAAc,CAAC;AAEpF,OAAO,EAML,KAAK,UAAU,EACf,KAAK,SAAS,EACf,MAAM,qBAAqB,CAAC;AAG7B,MAAM,WAAW,WAAW;IAC1B,mDAAmD;IACnD,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,qDAAqD;IACrD,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAClC,8DAA8D;IAC9D,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CACjC;AAED,wBAAgB,UAAU,CAAC,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,EAAE,UAAU,EAAE,OAAO,GAAE,WAAgB;;IAoCjG;kDAC8C;kBACkC,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC;;;kBAnBlF,OAAO,CAAC,CAAC,GAAG,IAAI,CAAC;iBANxB,OAAO;;IA+BvB,sDAAsD;;EAGzD"}
@@ -0,0 +1,56 @@
1
+ /**
2
+ * The form contract — state, validation, submission, error mapping. Never a widget.
3
+ *
4
+ * `values` is a writable store, so a component binds straight into it with
5
+ * `bind:value={$form.values[field.name]}`. That is the Svelte equivalent of Vue's
6
+ * reactive object and of React's `setValue`: the same contract, three ways of
7
+ * holding a value, none of which is a rule.
8
+ */
9
+ import { writable, derived, get } from 'svelte/store';
10
+ import { FougereError, ErrorCode } from '@fougere/core/contract';
11
+ import { entityKeyOf, errorsByField, formFieldsOf, payloadOf, } from '@fougere/app/client';
12
+ import { useCommand } from './useFougereData.js';
13
+ export function useFormFor(entity, options = {}) {
14
+ const entityKey = entityKeyOf(entity);
15
+ const fields = formFieldsOf(entity, entityKey);
16
+ // `initial` wins over the declared default: editing a row shows the row. On a
17
+ // create form there is none, so the field opens on what is about to be written.
18
+ const values = writable(Object.fromEntries(fields.map((field) => [field.name, options.initial?.[field.name] ?? field.default])));
19
+ const errors = writable({});
20
+ const command = useCommand(entity, options.op ?? 'create');
21
+ /** Local pre-judgment — same rules as the handler, saves a lost round-trip. */
22
+ function judge() {
23
+ const result = entity.validate(payloadOf(get(values)));
24
+ errors.set(result.success ? {} : errorsByField(result.errors));
25
+ return result.success;
26
+ }
27
+ async function submit() {
28
+ if (!judge())
29
+ return null;
30
+ try {
31
+ return await command.execute({ params: options.params, body: payloadOf(get(values)) });
32
+ }
33
+ catch (err) {
34
+ if (err instanceof FougereError && err.code === ErrorCode.VALIDATION_FAILED && Array.isArray(err.details)) {
35
+ errors.set(errorsByField(err.details));
36
+ return null;
37
+ }
38
+ throw err;
39
+ }
40
+ }
41
+ const valid = derived(errors, ($errors) => Object.keys($errors).length === 0);
42
+ return {
43
+ fields,
44
+ /** The same fields, keyed by name — spread `fieldsByName.email.attrs` on an input
45
+ * and the page states no rule of its own. */
46
+ fieldsByName: Object.fromEntries(fields.map((field) => [field.name, field])),
47
+ values,
48
+ errors,
49
+ submit,
50
+ judge,
51
+ valid,
52
+ /** `{ loading, error }` of the underlying command. */
53
+ command,
54
+ };
55
+ }
56
+ //# sourceMappingURL=useFormFor.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useFormFor.js","sourceRoot":"","sources":["../src/useFormFor.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,GAAG,EAAgC,MAAM,cAAc,CAAC;AACpF,OAAO,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AACjE,OAAO,EACL,WAAW,EACX,aAAa,EACb,YAAY,EACZ,SAAS,GAIV,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AAWjD,MAAM,UAAU,UAAU,CAA8B,MAAkB,EAAE,OAAO,GAAgB,EAAE;IACnG,MAAM,SAAS,GAAG,WAAW,CAAC,MAAM,CAAC,CAAC;IACtC,MAAM,MAAM,GAAgB,YAAY,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC;IAE5D,8EAA8E;IAC9E,gFAAgF;IAChF,MAAM,MAAM,GAAsC,QAAQ,CACxD,MAAM,CAAC,WAAW,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,KAAK,CAAC,IAAI,EAAE,OAAO,CAAC,OAAO,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,CACxG,CAAC;IACF,MAAM,MAAM,GAAG,QAAQ,CAAyB,EAAE,CAAC,CAAC;IACpD,MAAM,OAAO,GAAG,UAAU,CAAI,MAAqB,EAAE,OAAO,CAAC,EAAE,IAAI,QAAQ,CAAC,CAAC;IAE7E,+EAA+E;IAC/E,SAAS,KAAK;QACZ,MAAM,MAAM,GAAG,MAAM,CAAC,QAAQ,CAAC,SAAS,CAAC,GAAG,CAAC,MAAM,CAAC,C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R,CAAC;AACJ,CAAC"}
@@ -0,0 +1,43 @@
1
+ /**
2
+ * The couple — useQuery (reads) and useCommand (writes), in Svelte.
3
+ *
4
+ * Stores, not runes, and that is a packaging decision with a reason: a `.svelte.ts`
5
+ * file needs the Svelte compiler to run over it, so a library shipping runes ships
6
+ * SOURCE and forces every consumer's bundler to compile it. `svelte/store` is plain
7
+ * TypeScript, compiles with `tsc` like every other package here, and a store still
8
+ * auto-subscribes with `$query` inside a component. The consumer's ergonomics are
9
+ * the same; the packaging is honest.
10
+ *
11
+ * The name is `useQuery`, not `createQuery`, on purpose — the four primitives are
12
+ * one vocabulary across Nuxt, React and Svelte, and renaming them per ecosystem
13
+ * would make the same gesture read as three different things.
14
+ */
15
+ import { get, type Readable } from 'svelte/store';
16
+ import type { FougereError } from '@fougere/core/contract';
17
+ import { type CallInput, type EntityClass } from '@fougere/app/client';
18
+ export interface QueryState<T> {
19
+ data: T | null;
20
+ items: T[];
21
+ total?: number;
22
+ hasMore?: boolean;
23
+ loading: boolean;
24
+ error: FougereError | null;
25
+ }
26
+ export interface QueryStore<T> extends Readable<QueryState<T>> {
27
+ refresh(): Promise<void>;
28
+ /** Unregister from the link. Call it from `onDestroy` when a component unmounts. */
29
+ dispose(): void;
30
+ }
31
+ export declare function useQuery<T = Record<string, unknown>>(entity: EntityClass, op: string, input?: CallInput, opts?: {
32
+ immediate?: boolean;
33
+ }): QueryStore<T>;
34
+ export interface CommandStore<T> extends Readable<{
35
+ loading: boolean;
36
+ error: FougereError | null;
37
+ }> {
38
+ execute(input?: CallInput): Promise<T>;
39
+ }
40
+ export declare function useCommand<T = unknown>(entity: EntityClass, op: string): CommandStore<T>;
41
+ /** Read a store's current value without subscribing — used by `useFormFor`. */
42
+ export { get };
43
+ //# sourceMappingURL=useFougereData.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useFougereData.d.ts","sourceRoot":"","sources":["../src/useFougereData.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG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,GAAG,YAAY,CAAC,CAAC,CAAC,CAqBxF;AAED,+EAA+E;AAC/E,OAAO,EAAE,GAAG,EAAE,CAAC"}
@@ -0,0 +1,75 @@
1
+ /**
2
+ * The couple — useQuery (reads) and useCommand (writes), in Svelte.
3
+ *
4
+ * Stores, not runes, and that is a packaging decision with a reason: a `.svelte.ts`
5
+ * file needs the Svelte compiler to run over it, so a library shipping runes ships
6
+ * SOURCE and forces every consumer's bundler to compile it. `svelte/store` is plain
7
+ * TypeScript, compiles with `tsc` like every other package here, and a store still
8
+ * auto-subscribes with `$query` inside a component. The consumer's ergonomics are
9
+ * the same; the packaging is honest.
10
+ *
11
+ * The name is `useQuery`, not `createQuery`, on purpose — the four primitives are
12
+ * one vocabulary across Nuxt, React and Svelte, and renaming them per ecosystem
13
+ * would make the same gesture read as three different things.
14
+ */
15
+ import { writable, get } from 'svelte/store';
16
+ import { asFougereError, callOf, entityKeyOf, fetcher, invocationOf, itemsOf, mountedKeys, onRefetch, pageOf, queryKeyOf, revalidate, sendCall, trackQuery, } from '@fougere/app/client';
17
+ export function useQuery(entity, op, input, opts) {
18
+ const entityKey = entityKeyOf(entity);
19
+ const key = queryKeyOf(entityKey, op, input);
20
+ const immediate = opts?.immediate !== false;
21
+ const store = writable({
22
+ data: null,
23
+ items: [],
24
+ loading: immediate,
25
+ error: null,
26
+ });
27
+ async function refresh() {
28
+ store.update((state) => ({ ...state, loading: true, error: null }));
29
+ try {
30
+ const data = (await sendCall(fetcher, callOf(entity, op), invocationOf(input)));
31
+ const page = pageOf(data);
32
+ store.set({ data, items: itemsOf(data), total: page.total, hasMore: page.hasMore, loading: false, error: null });
33
+ }
34
+ catch (err) {
35
+ store.update((state) => ({ ...state, loading: false, error: asFougereError(err, entityKey, op) }));
36
+ }
37
+ }
38
+ const untrack = trackQuery(entityKey, key);
39
+ const off = onRefetch(key, () => void refresh());
40
+ if (immediate)
41
+ void refresh();
42
+ return {
43
+ subscribe: store.subscribe,
44
+ refresh,
45
+ dispose() {
46
+ untrack();
47
+ off();
48
+ },
49
+ };
50
+ }
51
+ export function useCommand(entity, op) {
52
+ const entityKey = entityKeyOf(entity);
53
+ const store = writable({ loading: false, error: null });
54
+ return {
55
+ subscribe: store.subscribe,
56
+ async execute(input) {
57
+ store.set({ loading: true, error: null });
58
+ try {
59
+ const result = (await sendCall(fetcher, callOf(entity, op), invocationOf(input)));
60
+ // The link: same entity designated on both sides → revalidate its queries.
61
+ revalidate(mountedKeys(entityKey));
62
+ store.set({ loading: false, error: null });
63
+ return result;
64
+ }
65
+ catch (err) {
66
+ const failure = asFougereError(err, entityKey, op);
67
+ store.set({ loading: false, error: failure });
68
+ throw failure;
69
+ }
70
+ },
71
+ };
72
+ }
73
+ /** Read a store's current value without subscribing — used by `useFormFor`. */
74
+ export { get };
75
+ //# sourceMappingURL=useFougereData.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useFougereData.js","sourceRoot":"","sources":["../src/useFougereData.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AACH,OAAO,EAAE,QAAQ,EAAE,GAAG,EAAiB,MAAM,cAAc,CAAC;AAE5D,OAAO,EACL,cAAc,EACd,MAAM,EACN,WAAW,EACX,OAAO,EACP,YAAY,EACZ,OAAO,EACP,WAAW,EACX,SAAS,EACT,MAAM,EACN,UAAU,EACV,UAAU,EACV,QAAQ,EACR,UAAU,GAGX,MAAM,qBAAqB,CAAC;AAiB7B,MAAM,UAAU,QAAQ,CACtB,MAAmB,EACnB,EAAU,EACV,KAAiB,EACjB,IAA8B;IAE9B,MAAM,SAAS,GAAG,WAAW,CAAC,MAAM,CAAC,CAAC;IACtC,MAAM,GAAG,GAAG,UAAU,CAAC,SAAS,EAAE,EAAE,EAAE,KAAK,CAAC,CAAC;IAC7C,MAAM,SAAS,GAAG,IAAI,EAAE,SAAS,KAAK,KAAK,CAAC;IAE5C,MAAM,KAAK,GAAG,QAAQ,CAAgB;QACpC,IAAI,EAAE,IAAI;QACV,KAAK,EAA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fougere/svelte",
3
+ "version": "0.2.0-alpha.0",
4
+ "description": "Fougere's four primitives as Svelte stores — host-independent.",
5
+ "keywords": [
6
+ "fougere",
7
+ "svelte",
8
+ "sveltekit",
9
+ "typescript",
10
+ "fullstack",
11
+ "rpc"
12
+ ],
13
+ "license": "MIT",
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "git+https://github.com/chok/fougere.git",
17
+ "directory": "packages/app/svelte"
18
+ },
19
+ "type": "module",
20
+ "main": "dist/index.js",
21
+ "types": "dist/index.d.ts",
22
+ "exports": {
23
+ ".": {
24
+ "types": "./dist/index.d.ts",
25
+ "import": "./dist/index.js"
26
+ }
27
+ },
28
+ "files": [
29
+ "dist"
30
+ ],
31
+ "dependencies": {
32
+ "@fougere/app": "0.2.0-alpha.0",
33
+ "@fougere/core": "0.2.0-alpha.0"
34
+ },
35
+ "devDependencies": {
36
+ "svelte": "^5.53.4",
37
+ "vitest": "^4.1.0",
38
+ "@fougere/schema": "0.2.0-alpha.0"
39
+ },
40
+ "peerDependencies": {
41
+ "svelte": "^5.0.0"
42
+ },
43
+ "publishConfig": {
44
+ "access": "public"
45
+ },
46
+ "scripts": {
47
+ "build": "rm -rf dist && tsc",
48
+ "test": "vitest run",
49
+ "typecheck": "tsc --noEmit -p tsconfig.test.json"
50
+ }
51
+ }