@fougere/svelte 0.2.0-alpha.2 → 0.4.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.
@@ -1 +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"}
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;;IAqCjG;kDAC8C;kBACkC,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC;;;kBApBlF,OAAO,CAAC,CAAC,GAAG,IAAI,CAAC;iBANxB,OAAO;;IAgCvB,sDAAsD;;EAGzD"}
@@ -7,7 +7,7 @@
7
7
  * holding a value, none of which is a rule.
8
8
  */
9
9
  import { writable, derived, get } from 'svelte/store';
10
- import { FougereError, ErrorCode } from '@fougere/core/contract';
10
+ import { validationErrorsOf } from '@fougere/core/contract';
11
11
  import { entityKeyOf, errorsByField, formFieldsOf, payloadOf, } from '@fougere/app/client';
12
12
  import { useCommand } from './useFougereData.js';
13
13
  export function useFormFor(entity, options = {}) {
@@ -31,8 +31,9 @@ export function useFormFor(entity, options = {}) {
31
31
  return await command.execute({ params: options.params, body: payloadOf(get(values)) });
32
32
  }
33
33
  catch (err) {
34
- if (err instanceof FougereError && err.code === ErrorCode.VALIDATION_FAILED && Array.isArray(err.details)) {
35
- errors.set(errorsByField(err.details));
34
+ const refusals = validationErrorsOf(err);
35
+ if (refusals) {
36
+ errors.set(errorsByField(refusals));
36
37
  return null;
37
38
  }
38
39
  throw err;
@@ -1 +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,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EAAE,CAAC;YACb,MAAM,QAAQ,GAAG,kBAAkB,CAAC,GAAG,CAAC,CAAC;YACzC,IAAI,QAAQ,EAAE,CAAC;gBACb,MAAM,CAAC,GAAG,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC,CAAC;gBACpC,OAAO,IAAI,CAAC;YACd,CAAC;YACD,MAAM,GAAG,CAAC;QACZ,CAAC;IACH,CAAC;IAED,MAAM,KAAK,GAAsB,OAAO,CAAC,MAAM,EAAE,CAAC,OAAO,EAAE,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC;IAEjG,OAAO;QACL,MAAM;QACN;sDAC8C;QAC9C,YAAY,EAAE,MAAM,CAAC,WAAW,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,KAAK,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC,CAA8B;QACzG,MAAM;QACN,MAAM;QACN,MAAM;QACN,KAAK;QACL,KAAK;QACL,sDAAsD;QACtD,OAAO;KACR,CAAC;AACJ,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fougere/svelte",
3
- "version": "0.2.0-alpha.2",
3
+ "version": "0.4.0-alpha.0",
4
4
  "description": "Fougere's four primitives as Svelte stores — host-independent.",
5
5
  "keywords": [
6
6
  "fougere",
@@ -26,16 +26,17 @@
26
26
  }
27
27
  },
28
28
  "files": [
29
- "dist"
29
+ "dist",
30
+ "src"
30
31
  ],
31
32
  "dependencies": {
32
- "@fougere/core": "0.2.0-alpha.2",
33
- "@fougere/app": "0.2.0-alpha.2"
33
+ "@fougere/app": "0.4.0-alpha.0",
34
+ "@fougere/core": "0.4.0-alpha.0"
34
35
  },
35
36
  "devDependencies": {
36
37
  "svelte": "^5.53.4",
37
38
  "vitest": "^4.1.0",
38
- "@fougere/schema": "0.2.0-alpha.2"
39
+ "@fougere/schema": "0.4.0-alpha.0"
39
40
  },
40
41
  "peerDependencies": {
41
42
  "svelte": "^5.0.0"
package/src/index.ts ADDED
@@ -0,0 +1,25 @@
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
+
19
+ export type {
20
+ CallInput,
21
+ EntityClass,
22
+ FormEntity,
23
+ FormField,
24
+ SessionView,
25
+ } from '@fougere/app/client';
@@ -0,0 +1,37 @@
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, type Readable } from 'svelte/store';
10
+ import type { SessionView } from '@fougere/app/client';
11
+
12
+ const session = writable<SessionView>({ user: null });
13
+ let fetched = false;
14
+
15
+ /** Seed the store from what the server already resolved (a `load` function's data). */
16
+ export function hydrateSession(view: SessionView): void {
17
+ fetched = true;
18
+ session.set(view);
19
+ }
20
+
21
+ export async function refreshSession(): Promise<void> {
22
+ const response = await fetch('/_fougere/session');
23
+ fetched = true;
24
+ session.set((await response.json()) as SessionView);
25
+ }
26
+
27
+ export function useCurrentUser<TUser = Record<string, unknown>>() {
28
+ // Only reached in the browser, and only when a layout did not hydrate it.
29
+ if (!fetched && typeof window !== 'undefined') void refreshSession();
30
+
31
+ return {
32
+ session: session as Readable<SessionView>,
33
+ user: derived(session, ($session) => ($session.user ?? null) as TUser | null),
34
+ loggedIn: derived(session, ($session) => $session.user != null),
35
+ refresh: refreshSession,
36
+ };
37
+ }
@@ -0,0 +1,79 @@
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, type Readable, type Writable } from 'svelte/store';
10
+ import { validationErrorsOf } from '@fougere/core/contract';
11
+ import {
12
+ entityKeyOf,
13
+ errorsByField,
14
+ formFieldsOf,
15
+ payloadOf,
16
+ type EntityClass,
17
+ type FormEntity,
18
+ type FormField,
19
+ } from '@fougere/app/client';
20
+ import { useCommand } from './useFougereData.js';
21
+
22
+ export interface FormOptions {
23
+ /** Command the submit rides. Default: 'create'. */
24
+ op?: string;
25
+ /** Initial values (edit mode: the loaded entity). */
26
+ initial?: Record<string, unknown>;
27
+ /** Call params designating the target (edit mode: { id }). */
28
+ params?: Record<string, string>;
29
+ }
30
+
31
+ export function useFormFor<T = Record<string, unknown>>(entity: FormEntity, options: FormOptions = {}) {
32
+ const entityKey = entityKeyOf(entity);
33
+ const fields: FormField[] = formFieldsOf(entity, entityKey);
34
+
35
+ // `initial` wins over the declared default: editing a row shows the row. On a
36
+ // create form there is none, so the field opens on what is about to be written.
37
+ const values: Writable<Record<string, unknown>> = writable(
38
+ Object.fromEntries(fields.map((field) => [field.name, options.initial?.[field.name] ?? field.default])),
39
+ );
40
+ const errors = writable<Record<string, string>>({});
41
+ const command = useCommand<T>(entity as EntityClass, options.op ?? 'create');
42
+
43
+ /** Local pre-judgment — same rules as the handler, saves a lost round-trip. */
44
+ function judge(): boolean {
45
+ const result = entity.validate(payloadOf(get(values)));
46
+ errors.set(result.success ? {} : errorsByField(result.errors));
47
+ return result.success;
48
+ }
49
+
50
+ async function submit(): Promise<T | null> {
51
+ if (!judge()) return null;
52
+ try {
53
+ return await command.execute({ params: options.params, body: payloadOf(get(values)) });
54
+ } catch (err) {
55
+ const refusals = validationErrorsOf(err);
56
+ if (refusals) {
57
+ errors.set(errorsByField(refusals));
58
+ return null;
59
+ }
60
+ throw err;
61
+ }
62
+ }
63
+
64
+ const valid: Readable<boolean> = derived(errors, ($errors) => Object.keys($errors).length === 0);
65
+
66
+ return {
67
+ fields,
68
+ /** The same fields, keyed by name — spread `fieldsByName.email.attrs` on an input
69
+ * and the page states no rule of its own. */
70
+ fieldsByName: Object.fromEntries(fields.map((field) => [field.name, field])) as Record<string, FormField>,
71
+ values,
72
+ errors,
73
+ submit,
74
+ judge,
75
+ valid,
76
+ /** `{ loading, error }` of the underlying command. */
77
+ command,
78
+ };
79
+ }
@@ -0,0 +1,120 @@
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, type Readable } from 'svelte/store';
16
+ import type { FougereError } from '@fougere/core/contract';
17
+ import {
18
+ asFougereError,
19
+ callOf,
20
+ entityKeyOf,
21
+ fetcher,
22
+ invocationOf,
23
+ itemsOf,
24
+ mountedKeys,
25
+ onRefetch,
26
+ pageOf,
27
+ queryKeyOf,
28
+ revalidate,
29
+ sendCall,
30
+ trackQuery,
31
+ type CallInput,
32
+ type EntityClass,
33
+ } from '@fougere/app/client';
34
+
35
+ export interface QueryState<T> {
36
+ data: T | null;
37
+ items: T[];
38
+ total?: number;
39
+ hasMore?: boolean;
40
+ loading: boolean;
41
+ error: FougereError | null;
42
+ }
43
+
44
+ export interface QueryStore<T> extends Readable<QueryState<T>> {
45
+ refresh(): Promise<void>;
46
+ /** Unregister from the link. Call it from `onDestroy` when a component unmounts. */
47
+ dispose(): void;
48
+ }
49
+
50
+ export function useQuery<T = Record<string, unknown>>(
51
+ entity: EntityClass,
52
+ op: string,
53
+ input?: CallInput,
54
+ opts?: { immediate?: boolean },
55
+ ): QueryStore<T> {
56
+ const entityKey = entityKeyOf(entity);
57
+ const key = queryKeyOf(entityKey, op, input);
58
+ const immediate = opts?.immediate !== false;
59
+
60
+ const store = writable<QueryState<T>>({
61
+ data: null,
62
+ items: [],
63
+ loading: immediate,
64
+ error: null,
65
+ });
66
+
67
+ async function refresh(): Promise<void> {
68
+ store.update((state) => ({ ...state, loading: true, error: null }));
69
+ try {
70
+ const data = (await sendCall(fetcher, callOf(entity, op), invocationOf(input))) as T;
71
+ const page = pageOf(data);
72
+ store.set({ data, items: itemsOf<T>(data), total: page.total, hasMore: page.hasMore, loading: false, error: null });
73
+ } catch (err) {
74
+ store.update((state) => ({ ...state, loading: false, error: asFougereError(err, entityKey, op) }));
75
+ }
76
+ }
77
+
78
+ const untrack = trackQuery(entityKey, key);
79
+ const off = onRefetch(key, () => void refresh());
80
+ if (immediate) void refresh();
81
+
82
+ return {
83
+ subscribe: store.subscribe,
84
+ refresh,
85
+ dispose() {
86
+ untrack();
87
+ off();
88
+ },
89
+ };
90
+ }
91
+
92
+ export interface CommandStore<T> extends Readable<{ loading: boolean; error: FougereError | null }> {
93
+ execute(input?: CallInput): Promise<T>;
94
+ }
95
+
96
+ export function useCommand<T = unknown>(entity: EntityClass, op: string): CommandStore<T> {
97
+ const entityKey = entityKeyOf(entity);
98
+ const store = writable<{ loading: boolean; error: FougereError | null }>({ loading: false, error: null });
99
+
100
+ return {
101
+ subscribe: store.subscribe,
102
+ async execute(input?: CallInput): Promise<T> {
103
+ store.set({ loading: true, error: null });
104
+ try {
105
+ const result = (await sendCall(fetcher, callOf(entity, op), invocationOf(input))) as T;
106
+ // The link: same entity designated on both sides → revalidate its queries.
107
+ revalidate(mountedKeys(entityKey));
108
+ store.set({ loading: false, error: null });
109
+ return result;
110
+ } catch (err) {
111
+ const failure = asFougereError(err, entityKey, op);
112
+ store.set({ loading: false, error: failure });
113
+ throw failure;
114
+ }
115
+ },
116
+ };
117
+ }
118
+
119
+ /** Read a store's current value without subscribing — used by `useFormFor`. */
120
+ export { get };