@fougere/svelte 0.16.0-alpha.0 → 0.16.3-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,6 +1,6 @@
1
1
  /** The form contract — state, validation, submission, error mapping. */
2
2
  import { type Readable, type Writable } from 'svelte/store';
3
- import { type FormEntity, type FormField } from '@fougere/app/client';
3
+ import { type FormEntity, type FormField, type FormFieldName, type FormRow } from '@fougere/app/client';
4
4
  export interface FormOptions {
5
5
  /** Command the submit rides. Default: 'create'. */
6
6
  op?: string;
@@ -9,14 +9,14 @@ export interface FormOptions {
9
9
  /** Call params designating the target (edit mode: { id }). */
10
10
  params?: Record<string, string>;
11
11
  }
12
- export declare function useFormFor<T = Record<string, unknown>>(entity: FormEntity, options?: FormOptions): {
13
- fields: FormField[];
12
+ export declare function useFormFor<E extends FormEntity>(entity: E, options?: FormOptions): {
13
+ fields: FormField<FormFieldName<E>>[];
14
14
  /** The same fields, keyed by name — spread `fieldsByName.email.attrs` on an input
15
15
  * and the page states no rule of its own. */
16
- fieldsByName: Record<string, FormField>;
17
- values: Writable<Record<string, unknown>>;
18
- errors: Writable<Record<string, string>>;
19
- submit: () => Promise<T | null>;
16
+ fieldsByName: Partial<Record<FormFieldName<E>, FormField<FormFieldName<E>>>>;
17
+ values: Writable<Partial<FormRow<E>>>;
18
+ errors: Writable<Partial<Record<FormFieldName<E>, string>>>;
19
+ submit: () => Promise<FormRow<E> | null>;
20
20
  validator: () => boolean;
21
21
  valid: Readable<boolean>;
22
22
  /** `{ loading, error }` of the underlying command. */
@@ -1 +1 @@
1
- {"version":3,"file":"useFormFor.d.ts","sourceRoot":"","sources":["../src/useFormFor.ts"],"names":[],"mappings":"AAAA,wEAAwE;AACxE,OAAO,EAA0B,KAAK,QAAQ,EAAE,KAAK,QAAQ,EAAE,MAAM,cAAc,CAAC;AAEpF,OAAO,EAML,KAAK,UAAU,EACf,KAAK,SAAS,EACf,MAAM,qBAAqB,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Aa,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;;;kBAhBvH,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC;qBAP7B,OAAO;;IA6B3B,sDAAsD;;EAGzD"}
@@ -11,7 +11,7 @@ export function useFormFor(entity, options = {}) {
11
11
  const values = writable(Object.fromEntries(fields.map((field) => [field.name, options.initial?.[field.name] ?? field.default])));
12
12
  const errors = writable({});
13
13
  // A form is designated by its ENTITY — it is a set of fields — so the facade it submits to is
14
- // an address with no handler type behind it, and `T` stays the caller's to state.
14
+ // an address with no handler type behind it, and what it answers is the entity's row.
15
15
  const command = useCommand(facadeOf(entity), options.op ?? 'create');
16
16
  /** Local pre-verdict — same rules as the handler, saves a lost round-trip. */
17
17
  function validator() {
@@ -22,17 +22,12 @@ export function useFormFor(entity, options = {}) {
22
22
  async function submit() {
23
23
  if (!validator())
24
24
  return null;
25
- try {
26
- return (await command.execute({ params: options.params, input: payloadOf(entity, get(values)) }));
27
- }
28
- catch (err) {
29
- const refusals = validationErrorsOf(err);
30
- if (refusals) {
31
- errors.set(errorsByField(refusals));
32
- return null;
33
- }
34
- throw err;
35
- }
25
+ const answer = await command.execute({ params: options.params, input: payloadOf(entity, get(values)) });
26
+ const failure = get(command).error;
27
+ const refusals = failure && validationErrorsOf(failure);
28
+ if (refusals)
29
+ errors.set(errorsByField(refusals));
30
+ return answer;
36
31
  }
37
32
  const valid = derived(errors, ($errors) => Object.keys($errors).length === 0);
38
33
  return {
@@ -1 +1 @@
1
- {"version":3,"file":"useFormFor.js","sourceRoot":"","sources":["../src/useFormFor.ts"],"names":[],"mappings":"AAAA,wEAAwE;AACxE,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,GAAG,EAAgC,MAAM,cAAc,CAAC;AACpF,OAAO,EAAE,kBAAkB,EAAE,MAAM,wBAAwB,CAAC;AAC5D,OAAO,EACL,WAAW,EACX,QAAQ,EACR,aAAa,EACb,YAAY,EACZ,SAAS,GAGV,MAA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C,CAAC;QACxG,MAAM,OAAO,GAAG,GAAG,CAAC,OAAO,CAAC,CAAC,KAAK,CAAC;QACnC,MAAM,QAAQ,GAAG,OAAO,IAAI,kBAAkB,CAAC,OAAO,CAAC,CAAC;QACxD,IAAI,QAAQ;YAAE,MAAM,CAAC,GAAG,CAAC,aAAa,CAAI,QAAQ,CAAC,CAAC,CAAC;QAErD,OAAO,MAA2B,CAAC;IACrC,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,CAAmE;QAC9I,MAAM;QACN,MAAM;QACN,MAAM;QACN,SAAS;QACT,KAAK;QACL,sDAAsD;QACtD,OAAO;KACR,CAAC;AACJ,CAAC"}
@@ -5,7 +5,8 @@ export interface CommandStore<T, Refused extends ErrorCode = ErrorCode> extends
5
5
  loading: boolean;
6
6
  error: FougereError<Refused> | null;
7
7
  }> {
8
- execute(input?: CallInput): Promise<T>;
8
+ /** The answer, or `null` when the call was refused — the refusal is in `error`, and nothing is thrown. */
9
+ execute(input?: CallInput): Promise<T | null>;
9
10
  }
10
11
  export declare function useCommand<Handler, Address extends string, Op extends keyof Handler & string>(facade: FacadeName<Handler, Address>, op: Op): CommandStore<Answer<Handler, Op>, Refused<Address, Op>>;
11
12
  /** Read a store's current value without subscribing — used by `useFormFor`. */
@@ -1 +1 @@
1
- {"version":3,"file":"CommandStore.d.ts","sourceRoot":"","sources":["../../src/useFougereData/CommandStore.ts"],"names":[],"mappings":"AAAA,OAAO,EAAY,GAAG,EAAE,KAAK,QAAQ,EAA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,CACpE,SAAQ,QAAQ,CAAC;IAAE,OAAO,EAAE,OAAO,CAAC;IAAC,KAAK,EAAE,YAAY,CAAC,OAAO,CAAC,GAAG,IAAI,CAAA;CAAE,CAAC;IAC3E,0GAA0G;IAC1G,OAAO,CAAC,KAAK,CAAC,EAAE,SAAS,GAAG,OAAO,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC;CAC/C;AAED,wBAAgB,UAAU,CACxB,OAAO,EACP,OAAO,SAAS,MAAM,EACtB,EAAE,SAAS,MAAM,OAAO,GAAG,MAAM,EACjC,MAAM,EAAE,UAAU,CAAC,OAAO,EAAE,OAAO,CAAC,EAAE,EAAE,EAAE,EAAE,GAAG,YAAY,CAAC,MAAM,CAAC,OAAO,EAAE,EAAE,CAAC,EAAE,OAAO,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC,CAuBvG;AAED,+EAA+E;AAC/E,OAAO,EAAE,GAAG,EAAE,CAAC"}
@@ -17,7 +17,7 @@ export function useCommand(facade, op) {
17
17
  catch (err) {
18
18
  const failure = asFougereError(err, entityKey, op);
19
19
  store.set({ loading: false, error: failure });
20
- throw failure;
20
+ return null;
21
21
  }
22
22
  },
23
23
  };
@@ -1 +1 @@
1
- {"version":3,"file":"CommandStore.js","sourceRoot":"","sources":["../../src/useFougereData/CommandStore.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,GAAG,EAAiB,MAAM,cAAc,CAAC;AAE5D,OAAO,EAAE,cAAc,EAAE,MAAM,EAAE,SAAS,EAAE,OAAO,EAAE,YAAY,EAAE,WAAW,EAAE,UAAU,EAAE,QAAQ,EAA8D,MAAM,qBAAqB,CAAC;AAO9L,MAAM,UAAU,UAAU,CAIxB,MAAoC,EAAE,EAAM;IAC5C,MAAM,SAAS,GAAG,SAAS,CAAC,MAAM,CAAC,CAAC;IACpC,MAAM,KAAK,GAAG,QAAQ,CAAmD,EAAE,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC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C5C,MAAM,SAAS,GAAG,SAAS,CAAC,MAAM,CAAC,CAAC;IACpC,MAAM,KAAK,GAAG,QAAQ,CAAmD,EAAE,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;IAE1G,OAAO;QACL,SAAS,EAAE,KAAK,CAAC,SAAS;QAC1B,KAAK,CAAC,OAAO,CAAC,KAAiB;YAC7B,KAAK,CAAC,GAAG,CAAC,EAAE,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;YAC1C,IAAI,CAAC;gBACH,MAAM,MAAM,GAAG,CAAC,MAAM,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,EAAE,CAAC,EAAE,YAAY,CAAC,KAAK,CAAC,CAAC,CAAwB,CAAC;gBACzG,2EAA2E;gBAC3E,UAAU,CAAC,WAAW,CAAC,SAAS,CAAC,CAAC,CAAC;gBACnC,KAAK,CAAC,GAAG,CAAC,EAAE,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;gBAE3C,OAAO,MAAM,CAAC;YAChB,CAAC;YAAC,OAAO,GAAG,EAAE,CAAC;gBACb,MAAM,OAAO,GAAG,cAAc,CAAC,GAAG,EAAE,SAAS,EAAE,EAAE,CAAC,CAAC;gBACnD,KAAK,CAAC,GAAG,CAAC,EAAE,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,OAAO,EAAE,CAAC,CAAC;gBAE9C,OAAO,IAAI,CAAC;YACd,CAAC;QACH,CAAC;KACF,CAAC;AACJ,CAAC;AAED,+EAA+E;AAC/E,OAAO,EAAE,GAAG,EAAE,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fougere/svelte",
3
- "version": "0.16.0-alpha.0",
3
+ "version": "0.16.3-alpha.0",
4
4
  "description": "Fougere's four primitives as Svelte stores — host-independent.",
5
5
  "keywords": [
6
6
  "fougere",
@@ -31,13 +31,13 @@
31
31
  "template"
32
32
  ],
33
33
  "dependencies": {
34
- "@fougere/app": "0.16.0-alpha.0",
35
- "@fougere/core": "0.16.0-alpha.0"
34
+ "@fougere/app": "0.16.3-alpha.0",
35
+ "@fougere/core": "0.16.3-alpha.0"
36
36
  },
37
37
  "devDependencies": {
38
38
  "svelte": "^5.53.4",
39
39
  "vitest": "^4.1.0",
40
- "@fougere/schema": "0.16.0-alpha.0"
40
+ "@fougere/schema": "0.16.3-alpha.0"
41
41
  },
42
42
  "peerDependencies": {
43
43
  "svelte": "^5.0.0"
package/src/useFormFor.ts CHANGED
@@ -8,7 +8,11 @@ import {
8
8
  formFieldsOf,
9
9
  payloadOf,
10
10
  type FormEntity,
11
+ type FormErrors,
11
12
  type FormField,
13
+ type FormFieldName,
14
+ type FormRow,
15
+ type FormValues,
12
16
  } from '@fougere/app/client';
13
17
  import { useCommand } from './useFougereData/CommandStore.js';
14
18
 
@@ -21,41 +25,36 @@ export interface FormOptions {
21
25
  params?: Record<string, string>;
22
26
  }
23
27
 
24
- export function useFormFor<T = Record<string, unknown>>(entity: FormEntity, options: FormOptions = {}) {
28
+ export function useFormFor<E extends FormEntity>(entity: E, options: FormOptions = {}) {
25
29
  const entityKey = entityKeyOf(entity);
26
- const fields: FormField[] = formFieldsOf(entity, entityKey);
30
+ const fields = formFieldsOf(entity, entityKey);
27
31
 
28
32
  // `initial` wins over the declared default: editing a row shows the row. On a
29
33
  // create form there is none, so the field opens on what is about to be written.
30
- const values: Writable<Record<string, unknown>> = writable(
31
- Object.fromEntries(fields.map((field) => [field.name, options.initial?.[field.name] ?? field.default])),
34
+ const values: Writable<FormValues<E>> = writable(
35
+ Object.fromEntries(fields.map((field) => [field.name, options.initial?.[field.name] ?? field.default])) as FormValues<E>,
32
36
  );
33
- const errors = writable<Record<string, string>>({});
37
+ const errors = writable<FormErrors<E>>({});
34
38
  // A form is designated by its ENTITY — it is a set of fields — so the facade it submits to is
35
- // an address with no handler type behind it, and `T` stays the caller's to state.
39
+ // an address with no handler type behind it, and what it answers is the entity's row.
36
40
  const command = useCommand(facadeOf(entity), options.op ?? 'create');
37
41
 
38
42
  /** Local pre-verdict — same rules as the handler, saves a lost round-trip. */
39
43
  function validator(): boolean {
40
44
  const result = entity.validate(payloadOf(entity, get(values)));
41
- errors.set(result.success ? {} : errorsByField(result.errors));
45
+ errors.set(result.success ? {} : errorsByField<E>(result.errors));
42
46
 
43
47
  return result.success;
44
48
  }
45
49
 
46
- async function submit(): Promise<T | null> {
50
+ async function submit(): Promise<FormRow<E> | null> {
47
51
  if (!validator()) return null;
48
- try {
49
- return (await command.execute({ params: options.params, input: payloadOf(entity, get(values)) })) as T;
50
- } catch (err) {
51
- const refusals = validationErrorsOf(err);
52
- if (refusals) {
53
- errors.set(errorsByField(refusals));
52
+ const answer = await command.execute({ params: options.params, input: payloadOf(entity, get(values)) });
53
+ const failure = get(command).error;
54
+ const refusals = failure && validationErrorsOf(failure);
55
+ if (refusals) errors.set(errorsByField<E>(refusals));
54
56
 
55
- return null;
56
- }
57
- throw err;
58
- }
57
+ return answer as FormRow<E> | null;
59
58
  }
60
59
 
61
60
  const valid: Readable<boolean> = derived(errors, ($errors) => Object.keys($errors).length === 0);
@@ -64,7 +63,7 @@ export function useFormFor<T = Record<string, unknown>>(entity: FormEntity, opti
64
63
  fields,
65
64
  /** The same fields, keyed by name — spread `fieldsByName.email.attrs` on an input
66
65
  * and the page states no rule of its own. */
67
- fieldsByName: Object.fromEntries(fields.map((field) => [field.name, field])) as Record<string, FormField>,
66
+ fieldsByName: Object.fromEntries(fields.map((field) => [field.name, field])) as Partial<Record<FormFieldName<E>, FormField<FormFieldName<E>>>>,
68
67
  values,
69
68
  errors,
70
69
  submit,
@@ -4,7 +4,8 @@ import { asFougereError, callOf, addressOf, fetcher, invocationOf, mountedKeys,
4
4
 
5
5
  export interface CommandStore<T, Refused extends ErrorCode = ErrorCode>
6
6
  extends Readable<{ loading: boolean; error: FougereError<Refused> | null }> {
7
- execute(input?: CallInput): Promise<T>;
7
+ /** The answer, or `null` when the call was refused — the refusal is in `error`, and nothing is thrown. */
8
+ execute(input?: CallInput): Promise<T | null>;
8
9
  }
9
10
 
10
11
  export function useCommand<
@@ -17,7 +18,7 @@ export function useCommand<
17
18
 
18
19
  return {
19
20
  subscribe: store.subscribe,
20
- async execute(input?: CallInput): Promise<Answer<Handler, Op>> {
21
+ async execute(input?: CallInput): Promise<Answer<Handler, Op> | null> {
21
22
  store.set({ loading: true, error: null });
22
23
  try {
23
24
  const result = (await sendCall(fetcher, callOf(facade, op), invocationOf(input))) as Answer<Handler, Op>;
@@ -29,7 +30,8 @@ export function useCommand<
29
30
  } catch (err) {
30
31
  const failure = asFougereError(err, entityKey, op);
31
32
  store.set({ loading: false, error: failure });
32
- throw failure;
33
+
34
+ return null;
33
35
  }
34
36
  },
35
37
  };
@@ -27,5 +27,6 @@
27
27
  "file": "vite.config.ts",
28
28
  "from": "@fougere/vite",
29
29
  "imported": "fougere"
30
- }
30
+ },
31
+ "facade": { "kind": "kitAlias", "file": "vite.config.ts" }
31
32
  }