@sincpro/web-framework 1.0.0 → 1.1.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.
Files changed (50) hide show
  1. package/README.md +36 -1
  2. package/dist/codegen/cli.d.ts +2 -0
  3. package/dist/codegen/cli.js +82 -0
  4. package/dist/codegen/client.d.ts +14 -0
  5. package/dist/codegen/client.js +178 -0
  6. package/dist/codegen/domain.d.ts +14 -0
  7. package/dist/codegen/domain.js +195 -0
  8. package/dist/codegen/entities.d.ts +14 -0
  9. package/dist/codegen/entities.js +158 -0
  10. package/dist/codegen/index.d.ts +11 -0
  11. package/dist/codegen/index.js +9 -0
  12. package/dist/codegen/metamodel.d.ts +89 -0
  13. package/dist/codegen/metamodel.js +1 -0
  14. package/dist/codegen/names.d.ts +40 -0
  15. package/dist/codegen/names.js +114 -0
  16. package/dist/codegen/rest.d.ts +31 -0
  17. package/dist/codegen/rest.js +27 -0
  18. package/dist/core/cache.js +7 -0
  19. package/dist/core/contract.d.ts +34 -0
  20. package/dist/core/contract.js +41 -0
  21. package/dist/core/entity.d.ts +77 -9
  22. package/dist/core/entity.js +51 -6
  23. package/dist/core/index.d.ts +4 -3
  24. package/dist/core/index.js +4 -3
  25. package/dist/core/memory.d.ts +3 -0
  26. package/dist/core/memory.js +9 -0
  27. package/dist/core/relation.d.ts +11 -1
  28. package/dist/core/relation.js +30 -2
  29. package/dist/core/rest.d.ts +15 -0
  30. package/dist/core/rest.js +16 -0
  31. package/dist/core/transport.d.ts +11 -0
  32. package/dist/core/transport.js +9 -0
  33. package/dist/react/index.d.ts +2 -1
  34. package/dist/react/index.js +1 -0
  35. package/dist/react/useCommand.d.ts +15 -5
  36. package/dist/react/useCommand.js +7 -4
  37. package/dist/react/useListing.d.ts +5 -4
  38. package/dist/react/useListing.js +3 -2
  39. package/dist/react/useNameOf.d.ts +13 -0
  40. package/dist/react/useNameOf.js +44 -0
  41. package/dist/web/list/EntityList.d.ts +5 -4
  42. package/dist/web/list/EntityList.js +1 -1
  43. package/dist/web/list/presentation.d.ts +3 -3
  44. package/dist/web/list/presentation.js +1 -0
  45. package/dist/web/list/rowCommands.d.ts +16 -12
  46. package/dist/web/list/rowCommands.js +3 -2
  47. package/dist/web/record/CommandButton.d.ts +9 -6
  48. package/dist/web/record/edit.js +19 -7
  49. package/dist/web/record/lines.js +7 -1
  50. package/package.json +16 -5
@@ -0,0 +1,44 @@
1
+ "use client";
2
+ import { listRoute } from "../core/entity.js";
3
+ import { lookupOf } from "../core/lookup.js";
4
+ import { useFramework } from "./context.js";
5
+ import { useEffect, useState } from "react";
6
+ /**
7
+ * The name of one record of an entity, by its key — what a field shows when it holds only the
8
+ * key (`EntityID[Tenant]`) and not the record it points at.
9
+ *
10
+ * Context: asked ONCE by key, the way `useLookup` names a chosen record it never saw: a page
11
+ * narrowed to that key, one row, read with the definition the application knows. Unnamed is no
12
+ * error — the field shows the key — and an entity listed under another record
13
+ * (`/tenants/{tenant_id}/members`) has no list to ask by key, so it stays unnamed.
14
+ *
15
+ * const name = useNameOf(tenants, record.tenant_id) ?? record.tenant_id;
16
+ */
17
+ export function useNameOf(entity, id) {
18
+ const { transport, metas } = useFramework();
19
+ const [named, setNamed] = useState(undefined);
20
+ useEffect(() => {
21
+ if (entity === undefined || id === "")
22
+ return;
23
+ let route;
24
+ try {
25
+ route = listRoute(entity.list);
26
+ }
27
+ catch {
28
+ return;
29
+ }
30
+ const known = metas.get(entity.key);
31
+ const key = known?.get_id || known?.identity || "id";
32
+ const aborter = new AbortController();
33
+ transport
34
+ .list(route, { selection: { field: key, operator: "=", value: id }, pagination: { limit: 1 } }, aborter.signal)
35
+ .then((page) => {
36
+ const [one] = lookupOf(page.meta ?? known ?? null, page.rows, entity.summary);
37
+ if (one !== undefined)
38
+ setNamed({ id, name: one.label });
39
+ })
40
+ .catch(() => undefined);
41
+ return () => aborter.abort();
42
+ }, [transport, metas, entity, id]);
43
+ return named?.id === id ? named.name : undefined;
44
+ }
@@ -1,5 +1,6 @@
1
1
  import { type AnyRecord } from "@sincpro/criteria";
2
2
  import { type WorkspaceApi } from "@sincpro/web-composer";
3
+ import { type CommandBodies } from "../../core/entity.js";
3
4
  import type { GroupLevel, NamedFilter } from "../../core/list.js";
4
5
  import type { Listing } from "../../react/useListing.js";
5
6
  import { type RowCommand } from "./rowCommands.js";
@@ -28,8 +29,8 @@ import { type ReactNode } from "react";
28
29
  * screen passes `onOpen`.
29
30
  */
30
31
  export type { NamedFilter };
31
- export type EntityListProps<T extends AnyRecord> = {
32
- listing: Listing<T>;
32
+ export type EntityListProps<T extends AnyRecord, C extends CommandBodies = CommandBodies> = {
33
+ listing: Listing<T, C>;
33
34
  /**
34
35
  * The workspace this list is a view of. With it, a row opens its record as a page, the
35
36
  * identity column is the composer's `RecordLink` (peek, side panel, window on hover) and the
@@ -107,7 +108,7 @@ export type EntityListProps<T extends AnyRecord> = {
107
108
  * The entity's Commands offered on each row («Confirmar»), sent with their idempotency key;
108
109
  * the list is read again when one is done (`useRowCommands`).
109
110
  */
110
- commands?: readonly RowCommand<T>[];
111
+ commands?: readonly RowCommand<T, C>[];
111
112
  /** Any other action of a row — or the commands built by hand with `useRowCommands`. */
112
113
  rowActions?: (row: T) => readonly RowAction[];
113
114
  /**
@@ -128,4 +129,4 @@ export type EntityFace<T> = {
128
129
  render: (set: ListSet<T>) => ReactNode;
129
130
  };
130
131
  export type View = "table" | "rows" | "board" | "sheet" | "pivot";
131
- export declare function EntityList<T extends AnyRecord>({ listing, api, columns: askedColumns, cells, views: askedViews, title: askedTitle, description: askedDescription, crumbs, actions, onOpen, onPeek, openTargets, onOpenIn, pivot: pivotAsked, empty, search, sums, filters: askedFilters, selectable, selectionActions, embedded, create, commands, rowActions: askedRowActions, faces, }: EntityListProps<T>): import("react").JSX.Element;
132
+ export declare function EntityList<T extends AnyRecord, C extends CommandBodies = CommandBodies>({ listing, api, columns: askedColumns, cells, views: askedViews, title: askedTitle, description: askedDescription, crumbs, actions, onOpen, onPeek, openTargets, onOpenIn, pivot: pivotAsked, empty, search, sums, filters: askedFilters, selectable, selectionActions, embedded, create, commands, rowActions: askedRowActions, faces, }: EntityListProps<T, C>): import("react").JSX.Element;
@@ -3,7 +3,7 @@ import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-run
3
3
  import { nameOf } from "@sincpro/criteria";
4
4
  import { RecordLink, useCrumbs } from "@sincpro/web-composer";
5
5
  import { frameworkCopy } from "../../core/copy.js";
6
- import { PICK_PARAM, QUERY_PARAM, TRAIL_PARAM } from "../../core/entity.js";
6
+ import { PICK_PARAM, QUERY_PARAM, TRAIL_PARAM, } from "../../core/entity.js";
7
7
  import { textOf, viewOf } from "../../core/present.js";
8
8
  import { explain } from "../../core/transport.js";
9
9
  import { useFramework } from "../../react/context.js";
@@ -1,5 +1,5 @@
1
1
  import type { AnyRecord } from "@sincpro/criteria";
2
- import type { Entity } from "../../core/entity.js";
2
+ import type { CommandAnswers, CommandBodies, Entity } from "../../core/entity.js";
3
3
  import { type ListDefinition } from "../../core/list.js";
4
4
  import type { PickerItem, RecordCard, RowOf } from "@sincpro/web-ui";
5
5
  import type { ComponentProps } from "react";
@@ -40,11 +40,11 @@ export type Presentation<T> = {
40
40
  row?: (record: T) => RowLook;
41
41
  card?: (record: T) => CardLook;
42
42
  };
43
- export type Presented<T extends AnyRecord> = Entity<T> & {
43
+ export type Presented<T extends AnyRecord, C extends CommandBodies = CommandBodies, A extends CommandAnswers = CommandAnswers> = Entity<T, C, A> & {
44
44
  readonly presentation: Presentation<T>;
45
45
  };
46
46
  /** The entity with how its records look: pass it wherever an entity goes. */
47
- export declare function presented<T extends AnyRecord>(entity: Entity<T>, presentation: Presentation<T>): Presented<T>;
47
+ export declare function presented<T extends AnyRecord, C extends CommandBodies = CommandBodies, A extends CommandAnswers = CommandAnswers>(entity: Entity<T, C, A>, presentation: Presentation<T>): Presented<T, C, A>;
48
48
  /** The presentation an entity — or the entity of a named list — was declared with; none is `{}`. */
49
49
  export declare function presentationOf<T extends AnyRecord>(source: Entity<T> | ListDefinition<T>): Presentation<T>;
50
50
  /** `look` over `base`, leaving out what `look` sets to `undefined`: «no tags» is not a hole. */
@@ -1,6 +1,7 @@
1
1
  import { isList } from "../../core/list.js";
2
2
  /** The entity with how its records look: pass it wherever an entity goes. */
3
3
  export function presented(entity, presentation) {
4
+ // Its typed Commands and answers go with it: a generated entity, presented, is still checked.
4
5
  return { ...entity, presentation };
5
6
  }
6
7
  /** The presentation an entity — or the entity of a named list — was declared with; none is `{}`. */
@@ -1,4 +1,5 @@
1
1
  import { type AnyRecord, type Expression } from "@sincpro/criteria";
2
+ import { type CommandBodies } from "../../core/entity.js";
2
3
  import type { Listing } from "../../react/useListing.js";
3
4
  import type { RowAction } from "@sincpro/web-ui";
4
5
  import type { LucideIcon } from "lucide-react";
@@ -21,21 +22,24 @@ import type { LucideIcon } from "lucide-react";
21
22
  * ]);
22
23
  * <EntityList listing={listing} rowActions={commands.rowActions} />
23
24
  */
24
- export type RowCommand<T> = {
25
- /** A Command the entity declares (`entity({ commands })`). */
26
- command: string;
27
- label: string;
28
- icon: LucideIcon;
29
- tone?: RowAction["tone"];
30
- /** Offered only on rows that hold it — a hint, not protection: the server decides. */
31
- when?: Expression<T>;
32
- /** What the Command is sent with. Without it, an empty body: the route names the row. */
33
- body?: (row: T) => AnyRecord;
34
- };
25
+ export type RowCommand<T, C extends CommandBodies = CommandBodies> = {
26
+ [N in keyof C & string]: {
27
+ /** A Command the entity declares (`entity({ commands })`); a typed entity, only its own. */
28
+ command: N;
29
+ label: string;
30
+ icon: LucideIcon;
31
+ tone?: RowAction["tone"];
32
+ /** Offered only on rows that hold it — a hint, not protection: the server decides. */
33
+ when?: Expression<T>;
34
+ /** What the Command is sent with — checked against a typed entity's `C[command]`. Without
35
+ * it, an empty body: the route (or the Command's `target`) names the row. */
36
+ body?: (row: T) => C[N];
37
+ };
38
+ }[keyof C & string];
35
39
  export type RowCommands<T> = {
36
40
  rowActions: (row: T) => readonly RowAction[];
37
41
  /** The last refusal, until `dismiss`. */
38
42
  error: unknown;
39
43
  dismiss: () => void;
40
44
  };
41
- export declare function useRowCommands<T extends AnyRecord>(listing: Listing<T>, commands: readonly RowCommand<T>[]): RowCommands<T>;
45
+ export declare function useRowCommands<T extends AnyRecord, C extends CommandBodies = CommandBodies>(listing: Listing<T, C>, commands: readonly RowCommand<T, C>[]): RowCommands<T>;
@@ -1,6 +1,6 @@
1
1
  "use client";
2
2
  import { matches } from "@sincpro/criteria";
3
- import { pathFor } from "../../core/entity.js";
3
+ import { bodyFor, pathFor } from "../../core/entity.js";
4
4
  import { retryKeys } from "../../core/retry.js";
5
5
  import { useFramework } from "../../react/context.js";
6
6
  import { useCallback, useState } from "react";
@@ -20,7 +20,8 @@ export function useRowCommands(listing, commands) {
20
20
  const gesture = `${one.command}\u0000${id}`;
21
21
  const retry = keys.get(gesture) ?? retryKeys();
22
22
  keys.set(gesture, retry);
23
- const body = one.body?.(row) ?? {};
23
+ // A Command whose body names its record (`target`) gets the row's id there.
24
+ const body = bodyFor(route, id, one.body?.(row) ?? {});
24
25
  setRunning((now) => new Set(now).add(gesture));
25
26
  setError(null);
26
27
  try {
@@ -1,5 +1,6 @@
1
1
  import type { AnyRecord } from "@sincpro/criteria";
2
- import type { Entity } from "../../core/entity.js";
2
+ import type { CommandAnswers, CommandBodies, Entity } from "../../core/entity.js";
3
+ import { type AnswerOf } from "../../react/useCommand.js";
3
4
  /**
4
5
  * An entity's Command as a button.
5
6
  *
@@ -9,16 +10,18 @@ import type { Entity } from "../../core/entity.js";
9
10
  * shows THAT in the window: the confirmation is the consequence, in the server's words, not a
10
11
  * question. A refusal is shown where it happened, with what to do.
11
12
  */
12
- export declare function CommandButton<T extends AnyRecord, R = unknown>({ entity, command, id, body, label, destructive, role, disabled, onDone, }: {
13
- entity: Entity<T>;
14
- command: string;
13
+ export declare function CommandButton<T extends AnyRecord, R = unknown, C extends CommandBodies = CommandBodies, N extends keyof C & string = keyof C & string, A extends CommandAnswers = CommandAnswers>({ entity, command, id, body, label, destructive, role, disabled, onDone, }: {
14
+ entity: Entity<T, C, A>;
15
+ /** One the entity declares; a typed entity takes only its own names. */
16
+ command: N;
15
17
  id?: string;
16
- body?: AnyRecord;
18
+ /** What it is sent with — checked against a typed entity's `C[command]`. */
19
+ body?: C[N];
17
20
  label: string;
18
21
  /** Asks the server what it would do before doing it. */
19
22
  destructive?: boolean;
20
23
  role?: "primary" | "secondary" | "ghost";
21
24
  /** Not offered right now — «Mover» with no other tenant chosen. The server still decides. */
22
25
  disabled?: boolean;
23
- onDone?: (answer: R) => void;
26
+ onDone?: (answer: AnswerOf<R, A, N>) => void;
24
27
  }): import("react").JSX.Element;
@@ -7,12 +7,19 @@ import { editableLines } from "../../core/lines.js";
7
7
  import { textOf, viewOf } from "../../core/present.js";
8
8
  import { relationFieldOf } from "../../core/relation.js";
9
9
  import { useEntityFor, useFramework } from "../../react/context.js";
10
+ import { useNameOf } from "../../react/useNameOf.js";
10
11
  import { controlFor, RelationPicker, } from "./control.js";
11
12
  import { RecordLinesEditor } from "./lines.js";
12
13
  import { useRecordOpening } from "./opening.js";
13
14
  import { RecordLines } from "./record.js";
14
15
  import { RelatedList } from "./RelatedList.js";
15
16
  import { Checkbox, Field, FormSection, ReadValue, RelationValue, } from "@sincpro/web-ui";
17
+ /** A key alone, read: the name of the record it points at, asked once by key — the key until
18
+ * then, and when nothing answers. */
19
+ function KeyValue({ target, id, ...rest }) {
20
+ const name = useNameOf(target, id);
21
+ return _jsx(RelationValue, { name: name ?? id, ...rest });
22
+ }
16
23
  /**
17
24
  * One field of a record being edited, from the model: its word, whether it is required or
18
25
  * read-only now (the hints, evaluated over what is on screen), the server's error for it, and a
@@ -99,13 +106,18 @@ export function RecordField({ record, name, as, bare = false, lines: look, list:
99
106
  return null;
100
107
  const key = edit.values[relation.key];
101
108
  const id = key === null || key === undefined ? "" : String(key);
102
- if (state.readonly)
103
- return (_jsx(RelationValue, { name: textOf(meta, relation.relation, edit.values[relation.relation], locale) ?? id, ...(opening === null || id === ""
104
- ? {}
105
- : {
106
- onOpenIn: (to) => opening.open(target.ref(id), to),
107
- openTargets: opening.targets,
108
- }) }));
109
+ if (state.readonly) {
110
+ const opens = opening === null || id === ""
111
+ ? {}
112
+ : {
113
+ onOpenIn: (to) => opening.open(target.ref(id), to),
114
+ openTargets: opening.targets,
115
+ };
116
+ // A relation held beside its key names itself; a key alone (`EntityID`) asks its name.
117
+ if (!relation.held)
118
+ return _jsx(KeyValue, { target: target, id: id, ...opens });
119
+ return (_jsx(RelationValue, { name: textOf(meta, relation.relation, edit.values[relation.relation], locale) ?? id, ...opens }));
120
+ }
109
121
  return (_jsx(RelationPicker, { target: target, value: id, onChange: (next) => edit.set(relation.key, next), label: view.label }));
110
122
  }
111
123
  if (view.type === "boolean" && as === undefined)
@@ -42,7 +42,13 @@ function LineValue({ definition, field, line, set, label, invalid, locale, inCel
42
42
  });
43
43
  if (relation !== undefined && !relation.many && target !== undefined) {
44
44
  const key = line[relation.key];
45
- return (_jsx(RelationPicker, { target: target, value: key === null || key === undefined ? "" : String(key), label: label, standalone: inCell, onChange: (id, chosen) => write({ [relation.key]: id, [relation.relation]: chosen ?? null }) }));
45
+ return (_jsx(RelationPicker, { target: target, value: key === null || key === undefined ? "" : String(key), label: label, standalone: inCell, onChange: (id, chosen) =>
46
+ // A relation held beside its key keeps the chosen record, so the cell names it at
47
+ // once; a key alone (`EntityID`) is only its key — writing the record there too
48
+ // would put the record IN the key.
49
+ write(relation.held
50
+ ? { [relation.key]: id, [relation.relation]: chosen ?? null }
51
+ : { [relation.key]: id }) }));
46
52
  }
47
53
  return controlFor({
48
54
  value: line[field],
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sincpro/web-framework",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "The web client of sincpro_framework: entities, lists and records bound to the metadata the server publishes, drawn with @sincpro/web-ui and arranged with @sincpro/web-composer.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -9,8 +9,19 @@
9
9
  ".": {
10
10
  "types": "./dist/index.d.ts",
11
11
  "default": "./dist/index.js"
12
+ },
13
+ "./core": {
14
+ "types": "./dist/core/index.d.ts",
15
+ "default": "./dist/core/index.js"
16
+ },
17
+ "./codegen": {
18
+ "types": "./dist/codegen/index.d.ts",
19
+ "default": "./dist/codegen/index.js"
12
20
  }
13
21
  },
22
+ "bin": {
23
+ "sincpro-web": "./dist/codegen/cli.js"
24
+ },
14
25
  "publishConfig": {
15
26
  "access": "public"
16
27
  },
@@ -23,8 +34,8 @@
23
34
  "printWidth": 94
24
35
  },
25
36
  "peerDependencies": {
26
- "@sincpro/criteria": "^1.0.0",
27
- "@sincpro/criteria-react": "^1.0.0",
37
+ "@sincpro/criteria": "^1.1.0",
38
+ "@sincpro/criteria-react": "^1.1.0",
28
39
  "@sincpro/web-composer": "^1.5.0",
29
40
  "@sincpro/web-ui": "^1.11.1",
30
41
  "react": ">=19",
@@ -32,8 +43,8 @@
32
43
  },
33
44
  "devDependencies": {
34
45
  "@eslint/js": "^10.0.1",
35
- "@sincpro/criteria": "^1.0.0",
36
- "@sincpro/criteria-react": "^1.0.0",
46
+ "@sincpro/criteria": "^1.1.0",
47
+ "@sincpro/criteria-react": "^1.1.0",
37
48
  "@sincpro/web-composer": "^1.5.0",
38
49
  "@sincpro/web-ui": "^1.11.1",
39
50
  "@storybook/addon-a11y": "10.6.5",