@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.
- package/README.md +36 -1
- package/dist/codegen/cli.d.ts +2 -0
- package/dist/codegen/cli.js +82 -0
- package/dist/codegen/client.d.ts +14 -0
- package/dist/codegen/client.js +178 -0
- package/dist/codegen/domain.d.ts +14 -0
- package/dist/codegen/domain.js +195 -0
- package/dist/codegen/entities.d.ts +14 -0
- package/dist/codegen/entities.js +158 -0
- package/dist/codegen/index.d.ts +11 -0
- package/dist/codegen/index.js +9 -0
- package/dist/codegen/metamodel.d.ts +89 -0
- package/dist/codegen/metamodel.js +1 -0
- package/dist/codegen/names.d.ts +40 -0
- package/dist/codegen/names.js +114 -0
- package/dist/codegen/rest.d.ts +31 -0
- package/dist/codegen/rest.js +27 -0
- package/dist/core/cache.js +7 -0
- package/dist/core/contract.d.ts +34 -0
- package/dist/core/contract.js +41 -0
- package/dist/core/entity.d.ts +77 -9
- package/dist/core/entity.js +51 -6
- package/dist/core/index.d.ts +4 -3
- package/dist/core/index.js +4 -3
- package/dist/core/memory.d.ts +3 -0
- package/dist/core/memory.js +9 -0
- package/dist/core/relation.d.ts +11 -1
- package/dist/core/relation.js +30 -2
- package/dist/core/rest.d.ts +15 -0
- package/dist/core/rest.js +16 -0
- package/dist/core/transport.d.ts +11 -0
- package/dist/core/transport.js +9 -0
- package/dist/react/index.d.ts +2 -1
- package/dist/react/index.js +1 -0
- package/dist/react/useCommand.d.ts +15 -5
- package/dist/react/useCommand.js +7 -4
- package/dist/react/useListing.d.ts +5 -4
- package/dist/react/useListing.js +3 -2
- package/dist/react/useNameOf.d.ts +13 -0
- package/dist/react/useNameOf.js +44 -0
- package/dist/web/list/EntityList.d.ts +5 -4
- package/dist/web/list/EntityList.js +1 -1
- package/dist/web/list/presentation.d.ts +3 -3
- package/dist/web/list/presentation.js +1 -0
- package/dist/web/list/rowCommands.d.ts +16 -12
- package/dist/web/list/rowCommands.js +3 -2
- package/dist/web/record/CommandButton.d.ts +9 -6
- package/dist/web/record/edit.js +19 -7
- package/dist/web/record/lines.js +7 -1
- package/package.json +16 -5
package/dist/core/entity.d.ts
CHANGED
|
@@ -1,6 +1,22 @@
|
|
|
1
1
|
import { type AnyRecord, type Expression } from "@sincpro/criteria";
|
|
2
2
|
import type { SummaryFields } from "./summary.js";
|
|
3
3
|
import type { Method } from "./transport.js";
|
|
4
|
+
/**
|
|
5
|
+
* What each Command of an entity is sent with, by its name: `{ send: { message: string } }`.
|
|
6
|
+
*
|
|
7
|
+
* Context: the default is any name and any body — every entity declared before Commands had
|
|
8
|
+
* types keeps compiling. A typed entity is still one of these (`Entity<T, C>` is assignable to
|
|
9
|
+
* `Entity<T>`), so every piece that takes an entity takes it.
|
|
10
|
+
*/
|
|
11
|
+
export type CommandBodies = Readonly<Record<string, AnyRecord>>;
|
|
12
|
+
/**
|
|
13
|
+
* What each Command of an entity answers, by its name: `{ send: { run_id: string } }` — what a
|
|
14
|
+
* generated entity reads off each operation's `output`.
|
|
15
|
+
*
|
|
16
|
+
* Context: `useCommand(issues, "send").run(…)` returns it with no type argument written; a
|
|
17
|
+
* Command it does not name answers `unknown`, as every Command did before.
|
|
18
|
+
*/
|
|
19
|
+
export type CommandAnswers = Readonly<Record<string, unknown>>;
|
|
4
20
|
/**
|
|
5
21
|
* Everything a screen needs to know about where an entity lives on the server, declared once.
|
|
6
22
|
*
|
|
@@ -20,8 +36,14 @@ import type { Method } from "./transport.js";
|
|
|
20
36
|
* commands: { confirm: "POST /invoices/{id}/confirm", cancel: "POST /invoices/{id}/cancel" },
|
|
21
37
|
* history: { route: "/billing-events", type: "Invoice" },
|
|
22
38
|
* });
|
|
39
|
+
*
|
|
40
|
+
* Its Commands may be typed too — what each one is sent with, by name (`CommandBodies`), and
|
|
41
|
+
* what each one answers (`CommandAnswers`) — and then `useCommand` and `CommandButton` take
|
|
42
|
+
* only those names and those bodies, and give back that answer:
|
|
43
|
+
*
|
|
44
|
+
* entity<Issue, { send: { message: string } }, { send: { run_id: string } }>(…)
|
|
23
45
|
*/
|
|
24
|
-
export type Entity<T extends AnyRecord = AnyRecord> = {
|
|
46
|
+
export type Entity<T extends AnyRecord = AnyRecord, C extends CommandBodies = CommandBodies, A extends CommandAnswers = CommandAnswers> = {
|
|
25
47
|
/** `domain:kind`, e.g. `"billing:invoice"`. */
|
|
26
48
|
key: string;
|
|
27
49
|
domain: string;
|
|
@@ -42,7 +64,7 @@ export type Entity<T extends AnyRecord = AnyRecord> = {
|
|
|
42
64
|
/** The route of one record. */
|
|
43
65
|
one: (id: string) => string;
|
|
44
66
|
/** Its Commands, by name. */
|
|
45
|
-
commands: Readonly<Record<string, WriteRoute>>;
|
|
67
|
+
commands: Readonly<Record<keyof C & string, WriteRoute>>;
|
|
46
68
|
/** How a new record is created: the list's own route, usually. */
|
|
47
69
|
create?: WriteRoute;
|
|
48
70
|
/** How an edit is saved: the record's own route with `{id}`. */
|
|
@@ -68,6 +90,10 @@ export type Entity<T extends AnyRecord = AnyRecord> = {
|
|
|
68
90
|
aggregate?: string;
|
|
69
91
|
/** Only carries the record type; never read. */
|
|
70
92
|
readonly _record?: T;
|
|
93
|
+
/** Only carries what each Command is sent with; never read. */
|
|
94
|
+
readonly _commands?: C;
|
|
95
|
+
/** Only carries what each Command answers; never read. */
|
|
96
|
+
readonly _answers?: A;
|
|
71
97
|
};
|
|
72
98
|
/** A composer `ResourceRef`, structurally: `core/` does not import the composer. */
|
|
73
99
|
export type EntityRef = {
|
|
@@ -111,17 +137,36 @@ export declare function startingValuesOf(ref: Pick<EntityRef, "params">): AnyRec
|
|
|
111
137
|
* OFFERED; the server still decides what may be stored.
|
|
112
138
|
*/
|
|
113
139
|
export declare function scopeOfRef(ref: Pick<EntityRef, "params">): Expression | undefined;
|
|
114
|
-
/**
|
|
140
|
+
/**
|
|
141
|
+
* A write: its method and its path, `{id}` where the record's key goes. A Command may also say
|
|
142
|
+
* which field of its body names the record it runs on (`target`: `issue_id`), and then the
|
|
143
|
+
* record's id is written there when it is sent (`bodyFor`).
|
|
144
|
+
*/
|
|
115
145
|
export type WriteRoute = {
|
|
116
146
|
method: Method;
|
|
117
147
|
path: string;
|
|
148
|
+
target?: string;
|
|
118
149
|
};
|
|
119
|
-
|
|
150
|
+
/**
|
|
151
|
+
* A Command's route: `"POST /invoices/{id}/confirm"`, or, when its body names the record —
|
|
152
|
+
* sincpro_framework's `targets`, what a generated entity declares —, the route and that field:
|
|
153
|
+
* `{ route: "POST /issues", target: "project_id" }`.
|
|
154
|
+
*/
|
|
155
|
+
export type CommandRoute = string | {
|
|
156
|
+
route: string;
|
|
157
|
+
target: string;
|
|
158
|
+
};
|
|
159
|
+
/** Each Command's route, by name. Typed Commands MUST each have their route — a name a screen
|
|
160
|
+
* may run with nowhere to send it is caught here, not on the click. */
|
|
161
|
+
type CommandRoutes<C extends CommandBodies> = string extends keyof C ? {
|
|
162
|
+
commands?: Readonly<Record<string, CommandRoute>>;
|
|
163
|
+
} : {
|
|
164
|
+
commands: Readonly<Record<keyof C & string, CommandRoute>>;
|
|
165
|
+
};
|
|
166
|
+
export type EntityRoutes<C extends CommandBodies = CommandBodies> = CommandRoutes<C> & {
|
|
120
167
|
list: string;
|
|
121
168
|
/** With `{id}` where the key goes. Without it, `<list>/<id>` — sincpro_framework's convention. */
|
|
122
169
|
one?: string;
|
|
123
|
-
/** `"POST /invoices/{id}/confirm"`, by name. */
|
|
124
|
-
commands?: Readonly<Record<string, string>>;
|
|
125
170
|
/** `"POST /invoices"`: where a new record goes. */
|
|
126
171
|
create?: string;
|
|
127
172
|
save?: string;
|
|
@@ -153,7 +198,30 @@ export declare function writeRoute(text: string): WriteRoute;
|
|
|
153
198
|
* preview of a new record has none.
|
|
154
199
|
*/
|
|
155
200
|
export declare function pathFor(route: WriteRoute, id?: string, body?: AnyRecord): string;
|
|
156
|
-
/**
|
|
157
|
-
*
|
|
201
|
+
/**
|
|
202
|
+
* What a Command of a record is sent with: the body as given, and — when its route says which
|
|
203
|
+
* field names the record (`target`) — the record's id in that field.
|
|
204
|
+
*
|
|
205
|
+
* bodyFor({ method: "POST", path: "/issues", target: "project_id" }, "p1", { title: "x" })
|
|
206
|
+
* -> { title: "x", project_id: "p1" }
|
|
207
|
+
*
|
|
208
|
+
* Context: sincpro_framework says, per operation, which input field holds the key of each
|
|
209
|
+
* entity it runs on (`targets`). A path that carries it (`/issues/{issue_id}/messages`) and a
|
|
210
|
+
* body that carries it (`POST /issues` with `project_id`) are the same thing to the server — a
|
|
211
|
+
* path parameter is read as a field of the DTO — so it goes in the body, and `pathFor` takes it
|
|
212
|
+
* from there. Without an id (a Command that creates) the body is sent as it is.
|
|
213
|
+
*/
|
|
214
|
+
export declare function bodyFor(route: WriteRoute, id: string | undefined, body: AnyRecord): AnyRecord;
|
|
215
|
+
/**
|
|
216
|
+
* A list's route with its own parameters: `{ tenant_id: "acme" }` → `/projects?tenant_id=acme`.
|
|
217
|
+
* An empty value is no parameter — a filter left blank. A list under a record names its key in
|
|
218
|
+
* the path (`/tenants/{tenant_id}/members`): that parameter fills it, and only the rest goes in
|
|
219
|
+
* the query.
|
|
220
|
+
*
|
|
221
|
+
* Context: sincpro_framework reads a path parameter as a field of the search's DTO, the same
|
|
222
|
+
* field a query parameter would fill, so the screen gives them all as one set of parameters. A
|
|
223
|
+
* key the path needs and the parameters lack is the screen's bug, said before anything is sent.
|
|
224
|
+
*/
|
|
158
225
|
export declare function listRoute(list: string, params?: Readonly<Record<string, string>>): string;
|
|
159
|
-
export declare function entity<T extends AnyRecord = AnyRecord>(key: string, routes: EntityRoutes): Entity<T>;
|
|
226
|
+
export declare function entity<T extends AnyRecord = AnyRecord, C extends CommandBodies = CommandBodies, A extends CommandAnswers = CommandAnswers>(key: string, routes: EntityRoutes<C>): Entity<T, C, A>;
|
|
227
|
+
export {};
|
package/dist/core/entity.js
CHANGED
|
@@ -87,13 +87,54 @@ export function pathFor(route, id, body) {
|
|
|
87
87
|
return encodeURIComponent(String(value));
|
|
88
88
|
});
|
|
89
89
|
}
|
|
90
|
-
/**
|
|
91
|
-
*
|
|
90
|
+
/**
|
|
91
|
+
* What a Command of a record is sent with: the body as given, and — when its route says which
|
|
92
|
+
* field names the record (`target`) — the record's id in that field.
|
|
93
|
+
*
|
|
94
|
+
* bodyFor({ method: "POST", path: "/issues", target: "project_id" }, "p1", { title: "x" })
|
|
95
|
+
* -> { title: "x", project_id: "p1" }
|
|
96
|
+
*
|
|
97
|
+
* Context: sincpro_framework says, per operation, which input field holds the key of each
|
|
98
|
+
* entity it runs on (`targets`). A path that carries it (`/issues/{issue_id}/messages`) and a
|
|
99
|
+
* body that carries it (`POST /issues` with `project_id`) are the same thing to the server — a
|
|
100
|
+
* path parameter is read as a field of the DTO — so it goes in the body, and `pathFor` takes it
|
|
101
|
+
* from there. Without an id (a Command that creates) the body is sent as it is.
|
|
102
|
+
*/
|
|
103
|
+
export function bodyFor(route, id, body) {
|
|
104
|
+
if (route.target === undefined || id === undefined || id === "")
|
|
105
|
+
return body;
|
|
106
|
+
return { ...body, [route.target]: id };
|
|
107
|
+
}
|
|
108
|
+
/** A Command's route as declared: a `"METHOD /path"`, or that and the field naming its record. */
|
|
109
|
+
function commandRoute(declared) {
|
|
110
|
+
if (typeof declared === "string")
|
|
111
|
+
return writeRoute(declared);
|
|
112
|
+
return { ...writeRoute(declared.route), target: declared.target };
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* A list's route with its own parameters: `{ tenant_id: "acme" }` → `/projects?tenant_id=acme`.
|
|
116
|
+
* An empty value is no parameter — a filter left blank. A list under a record names its key in
|
|
117
|
+
* the path (`/tenants/{tenant_id}/members`): that parameter fills it, and only the rest goes in
|
|
118
|
+
* the query.
|
|
119
|
+
*
|
|
120
|
+
* Context: sincpro_framework reads a path parameter as a field of the search's DTO, the same
|
|
121
|
+
* field a query parameter would fill, so the screen gives them all as one set of parameters. A
|
|
122
|
+
* key the path needs and the parameters lack is the screen's bug, said before anything is sent.
|
|
123
|
+
*/
|
|
92
124
|
export function listRoute(list, params) {
|
|
93
|
-
const
|
|
125
|
+
const given = params ?? {};
|
|
126
|
+
const placed = new Set();
|
|
127
|
+
const path = list.replace(PATH_KEY, (_whole, name) => {
|
|
128
|
+
const value = given[name];
|
|
129
|
+
if (value === undefined || value === "")
|
|
130
|
+
throw new Error(`the list "${list}" needs "${name}" in its parameters`);
|
|
131
|
+
placed.add(name);
|
|
132
|
+
return encodeURIComponent(value);
|
|
133
|
+
});
|
|
134
|
+
const query = new URLSearchParams(Object.entries(given).filter(([name, value]) => value !== "" && !placed.has(name))).toString();
|
|
94
135
|
if (query === "")
|
|
95
|
-
return
|
|
96
|
-
return `${
|
|
136
|
+
return path;
|
|
137
|
+
return `${path}${path.includes("?") ? "&" : "?"}${query}`;
|
|
97
138
|
}
|
|
98
139
|
export function entity(key, routes) {
|
|
99
140
|
if (!/^[^:\s]+:[^:\s]+$/.test(key))
|
|
@@ -129,7 +170,11 @@ export function entity(key, routes) {
|
|
|
129
170
|
}),
|
|
130
171
|
list: routes.list,
|
|
131
172
|
one: (id) => template.replace("{id}", encodeURIComponent(id)),
|
|
132
|
-
|
|
173
|
+
// Built from exactly the names `routes.commands` was typed with.
|
|
174
|
+
commands: Object.fromEntries(Object.entries(routes.commands ?? {}).map(([name, declared]) => [
|
|
175
|
+
name,
|
|
176
|
+
commandRoute(declared),
|
|
177
|
+
])),
|
|
133
178
|
...(routes.create === undefined ? {} : { create: writeRoute(routes.create) }),
|
|
134
179
|
...(routes.save === undefined ? {} : { save: writeRoute(routes.save) }),
|
|
135
180
|
...(routes.preview === undefined ? {} : { preview: writeRoute(routes.preview) }),
|
package/dist/core/index.d.ts
CHANGED
|
@@ -4,9 +4,10 @@
|
|
|
4
4
|
* `make doctor` keeps it that way.
|
|
5
5
|
*/
|
|
6
6
|
export { type CachedTransport, cachedTransport, type CacheOptions, } from "./cache.js";
|
|
7
|
+
export { type ClientContract, entityKeyOf, metasOf, versionsOf, } from "./contract.js";
|
|
7
8
|
export { ENGLISH_FRAMEWORK_COPY, type FrameworkCopy, frameworkCopy, installFrameworkCopy, SPANISH_FRAMEWORK_COPY, } from "./copy.js";
|
|
8
9
|
export { type Advice, adviceAt, changesOf, computedLocally, creationOf, defaultsOf, errorAt, errorsByField, type FieldState, fieldStateOf, type FieldStates, linePlacesOf, lineStatesOf, missingOf, needsPreview, pathOf, type ServerFieldState, } from "./edit.js";
|
|
9
|
-
export { type Entity, entity, type EntityRef, type EntityRoutes, listRoute, NEW_PARAM, pathFor, PICK_PARAM, QUERY_PARAM, SCOPE_PARAM, scopeOfRef, startingValuesOf, TRAIL_PARAM, WITH_PARAM, type WriteRoute, writeRoute, } from "./entity.js";
|
|
10
|
+
export { bodyFor, type CommandAnswers, type CommandBodies, type CommandRoute, type Entity, entity, type EntityRef, type EntityRoutes, listRoute, NEW_PARAM, pathFor, PICK_PARAM, QUERY_PARAM, SCOPE_PARAM, scopeOfRef, startingValuesOf, TRAIL_PARAM, WITH_PARAM, type WriteRoute, writeRoute, } from "./entity.js";
|
|
10
11
|
export { type FormLayout, formOf } from "./form.js";
|
|
11
12
|
export { type HistoryEntry, historyOf } from "./history.js";
|
|
12
13
|
export { isEntityId, newEntityId } from "./ids.js";
|
|
@@ -20,10 +21,10 @@ export { type MetaStore, metaStore } from "./metas.js";
|
|
|
20
21
|
export { BOOKKEEPING, defaultFields, type FieldView, sortKeyOf, textOf, viewOf, } from "./present.js";
|
|
21
22
|
export { type Related, relatedOf } from "./related.js";
|
|
22
23
|
export { entityFor, type RelationField, relationFieldOf, } from "./relation.js";
|
|
23
|
-
export { type RestOptions, restTransport, urlOf } from "./rest.js";
|
|
24
|
+
export { type RestOptions, restTransport, urlOf, type VersionDrift, } from "./rest.js";
|
|
24
25
|
export { type RetryKeys, retryKeys, unanswered } from "./retry.js";
|
|
25
26
|
export { completeMeta, type JsonSchema, metaFromSchema, type SchemaComponents, } from "./schema.js";
|
|
26
27
|
export { type RecordSummary, type SummaryFields, summaryFieldsOf, summaryOf, type SummaryPart, } from "./summary.js";
|
|
27
28
|
export { placeOf, stepIn, type Trail } from "./trail.js";
|
|
28
|
-
export { type Draft, explain, type Method, type One, type SendOptions, type Transport, TransportError, } from "./transport.js";
|
|
29
|
+
export { type Draft, explain, type Method, type One, readRoute, type SendOptions, type Transport, TransportError, } from "./transport.js";
|
|
29
30
|
export { installTimeZone, instantOf, timeZoneNow, wallTimeOf, } from "./zone.js";
|
package/dist/core/index.js
CHANGED
|
@@ -4,9 +4,10 @@
|
|
|
4
4
|
* `make doctor` keeps it that way.
|
|
5
5
|
*/
|
|
6
6
|
export { cachedTransport, } from "./cache.js";
|
|
7
|
+
export { entityKeyOf, metasOf, versionsOf, } from "./contract.js";
|
|
7
8
|
export { ENGLISH_FRAMEWORK_COPY, frameworkCopy, installFrameworkCopy, SPANISH_FRAMEWORK_COPY, } from "./copy.js";
|
|
8
9
|
export { adviceAt, changesOf, computedLocally, creationOf, defaultsOf, errorAt, errorsByField, fieldStateOf, linePlacesOf, lineStatesOf, missingOf, needsPreview, pathOf, } from "./edit.js";
|
|
9
|
-
export { entity, listRoute, NEW_PARAM, pathFor, PICK_PARAM, QUERY_PARAM, SCOPE_PARAM, scopeOfRef, startingValuesOf, TRAIL_PARAM, WITH_PARAM, writeRoute, } from "./entity.js";
|
|
10
|
+
export { bodyFor, entity, listRoute, NEW_PARAM, pathFor, PICK_PARAM, QUERY_PARAM, SCOPE_PARAM, scopeOfRef, startingValuesOf, TRAIL_PARAM, WITH_PARAM, writeRoute, } from "./entity.js";
|
|
10
11
|
export { formOf } from "./form.js";
|
|
11
12
|
export { historyOf } from "./history.js";
|
|
12
13
|
export { isEntityId, newEntityId } from "./ids.js";
|
|
@@ -20,10 +21,10 @@ export { metaStore } from "./metas.js";
|
|
|
20
21
|
export { BOOKKEEPING, defaultFields, sortKeyOf, textOf, viewOf, } from "./present.js";
|
|
21
22
|
export { relatedOf } from "./related.js";
|
|
22
23
|
export { entityFor, relationFieldOf, } from "./relation.js";
|
|
23
|
-
export { restTransport, urlOf } from "./rest.js";
|
|
24
|
+
export { restTransport, urlOf, } from "./rest.js";
|
|
24
25
|
export { retryKeys, unanswered } from "./retry.js";
|
|
25
26
|
export { completeMeta, metaFromSchema, } from "./schema.js";
|
|
26
27
|
export { summaryFieldsOf, summaryOf, } from "./summary.js";
|
|
27
28
|
export { placeOf, stepIn } from "./trail.js";
|
|
28
|
-
export { explain, TransportError, } from "./transport.js";
|
|
29
|
+
export { explain, readRoute, TransportError, } from "./transport.js";
|
|
29
30
|
export { installTimeZone, instantOf, timeZoneNow, wallTimeOf, } from "./zone.js";
|
package/dist/core/memory.d.ts
CHANGED
|
@@ -31,6 +31,9 @@ export type MemoryOptions = {
|
|
|
31
31
|
* refuse. Without it, every Command is a 404.
|
|
32
32
|
*/
|
|
33
33
|
handle?: (method: Method, route: string, body: unknown) => unknown;
|
|
34
|
+
/** Answer a read that is neither a page nor a record (`GET /me`), its route with its query.
|
|
35
|
+
* Without it, the transport has no `read`. */
|
|
36
|
+
read?: (route: string) => unknown;
|
|
34
37
|
/** The draft this caller kept of a record, answered with it — sincpro_framework's `Drafts`. */
|
|
35
38
|
draft?: (route: string) => Draft | null;
|
|
36
39
|
/** Every list request, as it arrived: what a story or a test checks was really asked. */
|
package/dist/core/memory.js
CHANGED
|
@@ -95,5 +95,14 @@ export function memoryTransport(tables, options = {}) {
|
|
|
95
95
|
answered.set(key, structuredClone(answer));
|
|
96
96
|
return answer;
|
|
97
97
|
},
|
|
98
|
+
...(options.read === undefined
|
|
99
|
+
? {}
|
|
100
|
+
: {
|
|
101
|
+
async read(route, signal) {
|
|
102
|
+
await check(route, signal);
|
|
103
|
+
// A copy, as every answer: over a wire nobody holds the server's object.
|
|
104
|
+
return structuredClone(await options.read(route));
|
|
105
|
+
},
|
|
106
|
+
}),
|
|
98
107
|
};
|
|
99
108
|
}
|
package/dist/core/relation.d.ts
CHANGED
|
@@ -9,6 +9,11 @@ import type { Entity } from "./entity.js";
|
|
|
9
9
|
* joined by `identified_by`. A form may name either; both mean «the customer». A relation
|
|
10
10
|
* names the other side by its AGGREGATE («Cliente»), which says nothing about where it is read
|
|
11
11
|
* — that is the entity the application declared (`entityFor`).
|
|
12
|
+
*
|
|
13
|
+
* A key may also stand alone: `tenant_id: EntityID[Tenant]` on the server is a scalar that says
|
|
14
|
+
* whose record it names (`relation: "auth.Tenant"`) with no relation field beside it. It is a
|
|
15
|
+
* many2one all the same — chosen with the picker of that entity — but only its key is in hand
|
|
16
|
+
* (`held: false`): nothing else is written beside it, and its name is asked for by key.
|
|
12
17
|
*/
|
|
13
18
|
export type RelationField = {
|
|
14
19
|
/** The relation field: `customer`. */
|
|
@@ -19,10 +24,15 @@ export type RelationField = {
|
|
|
19
24
|
target: string;
|
|
20
25
|
many: boolean;
|
|
21
26
|
field: FieldMeta;
|
|
27
|
+
/** The record it points at comes with the record (a relation field the server expands), or
|
|
28
|
+
* only its key does (`EntityID[Tenant]`): then `relation` IS the key. */
|
|
29
|
+
held: boolean;
|
|
22
30
|
};
|
|
23
31
|
export declare function relationFieldOf(meta: Meta | null | undefined, name: string): RelationField | undefined;
|
|
24
32
|
/**
|
|
25
33
|
* The declared entity a relation points at: the one that says it is that aggregate, else the
|
|
26
|
-
* one whose model definition is, else the one whose key it is (`"ventas:cliente"`)
|
|
34
|
+
* one whose model definition is, else the one whose key it is (`"ventas:cliente"`), else the one
|
|
35
|
+
* its model names (`"auth.Tenant"` → `auth:tenant`), else the only one of that kind — a context
|
|
36
|
+
* named by its own name (`sincpro-forge-auth.Tenant`) where the application keys it by alias.
|
|
27
37
|
*/
|
|
28
38
|
export declare function entityFor(entities: readonly Entity[], target: string, metaOf?: (key: string) => Meta | undefined): Entity | undefined;
|
package/dist/core/relation.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { entityKeyOf, kindOf } from "./contract.js";
|
|
1
2
|
export function relationFieldOf(meta, name) {
|
|
2
3
|
if (!meta)
|
|
3
4
|
return undefined;
|
|
@@ -9,6 +10,7 @@ export function relationFieldOf(meta, name) {
|
|
|
9
10
|
target: field.relation,
|
|
10
11
|
many: field.many === true,
|
|
11
12
|
field,
|
|
13
|
+
held: true,
|
|
12
14
|
};
|
|
13
15
|
const own = meta.fields[name];
|
|
14
16
|
if (own?.kind === "relation")
|
|
@@ -17,14 +19,40 @@ export function relationFieldOf(meta, name) {
|
|
|
17
19
|
for (const [relation, field] of Object.entries(meta.fields))
|
|
18
20
|
if (field.kind === "relation" && field.identified_by === name)
|
|
19
21
|
return of(relation, field);
|
|
22
|
+
// A key alone that says whose record it names: the record is not in hand, its key is.
|
|
23
|
+
if (own !== undefined && own.kind !== "embedded" && own.relation)
|
|
24
|
+
return {
|
|
25
|
+
relation: name,
|
|
26
|
+
key: name,
|
|
27
|
+
target: own.relation,
|
|
28
|
+
many: own.many === true,
|
|
29
|
+
field: own,
|
|
30
|
+
held: false,
|
|
31
|
+
};
|
|
20
32
|
return undefined;
|
|
21
33
|
}
|
|
34
|
+
/**
|
|
35
|
+
* Whether `target` names `one` the way a key does — by its model, `auth.Tenant` in another
|
|
36
|
+
* context or `Tenant` in its own —: the name a generated entity's key is made of (`entityKeyOf`).
|
|
37
|
+
*/
|
|
38
|
+
function namedBy(one, target) {
|
|
39
|
+
const dot = target.lastIndexOf(".");
|
|
40
|
+
if (dot === -1)
|
|
41
|
+
return one.key === entityKeyOf(one.domain, target);
|
|
42
|
+
return one.key === entityKeyOf(target.slice(0, dot), target.slice(dot + 1));
|
|
43
|
+
}
|
|
22
44
|
/**
|
|
23
45
|
* The declared entity a relation points at: the one that says it is that aggregate, else the
|
|
24
|
-
* one whose model definition is, else the one whose key it is (`"ventas:cliente"`)
|
|
46
|
+
* one whose model definition is, else the one whose key it is (`"ventas:cliente"`), else the one
|
|
47
|
+
* its model names (`"auth.Tenant"` → `auth:tenant`), else the only one of that kind — a context
|
|
48
|
+
* named by its own name (`sincpro-forge-auth.Tenant`) where the application keys it by alias.
|
|
25
49
|
*/
|
|
26
50
|
export function entityFor(entities, target, metaOf = () => undefined) {
|
|
51
|
+
const model = target.slice(target.lastIndexOf(".") + 1);
|
|
52
|
+
const ofKind = entities.filter((one) => one.kind === kindOf(model));
|
|
27
53
|
return (entities.find((one) => one.aggregate === target) ??
|
|
28
54
|
entities.find((one) => metaOf(one.key)?.aggregate === target) ??
|
|
29
|
-
entities.find((one) => one.key === target)
|
|
55
|
+
entities.find((one) => one.key === target) ??
|
|
56
|
+
entities.find((one) => namedBy(one, target)) ??
|
|
57
|
+
(ofKind.length === 1 ? ofKind[0] : undefined));
|
|
30
58
|
}
|
package/dist/core/rest.d.ts
CHANGED
|
@@ -29,6 +29,21 @@ export type RestOptions = {
|
|
|
29
29
|
/** Where the bounded context's discovery document is: `"/openapi.json"` by default, at the
|
|
30
30
|
* origin of `base`. `null`: none is read, and the definitions come only from `metas`. */
|
|
31
31
|
document?: string | null;
|
|
32
|
+
/**
|
|
33
|
+
* The version of each model the application was written against — the snapshot it was
|
|
34
|
+
* generated from (`versionsOf(contract)`). With it, an answer of another version is told to
|
|
35
|
+
* `onVersionDrift`: the server moved, the generated code did not.
|
|
36
|
+
*/
|
|
37
|
+
snapshot?: Readonly<Record<string, string>>;
|
|
38
|
+
/** Called once per model and version the server answers that the snapshot does not hold —
|
|
39
|
+
* never an error: the screen keeps working on the live definition. */
|
|
40
|
+
onVersionDrift?: (drift: VersionDrift) => void;
|
|
41
|
+
};
|
|
42
|
+
/** A model the server answers at another version than the snapshot the code came from. */
|
|
43
|
+
export type VersionDrift = {
|
|
44
|
+
model: string;
|
|
45
|
+
snapshot: string;
|
|
46
|
+
live: string;
|
|
32
47
|
};
|
|
33
48
|
export declare function urlOf<T>(base: string, route: string, criteria?: Criteria<T>): string;
|
|
34
49
|
export declare function restTransport(options?: RestOptions): Transport;
|
package/dist/core/rest.js
CHANGED
|
@@ -56,6 +56,18 @@ export function restTransport(options = {}) {
|
|
|
56
56
|
return held;
|
|
57
57
|
}
|
|
58
58
|
/** The definition of the model `url` answers; read again when `version` is not the one held. */
|
|
59
|
+
// Each drift is told once: a list read every few seconds would say it on every page.
|
|
60
|
+
const told = new Set();
|
|
61
|
+
function noticeDrift(model, live) {
|
|
62
|
+
const snapshot = options.snapshot?.[model];
|
|
63
|
+
if (snapshot === undefined || !live || live === snapshot)
|
|
64
|
+
return;
|
|
65
|
+
const key = `${model}\u0000${live}`;
|
|
66
|
+
if (told.has(key))
|
|
67
|
+
return;
|
|
68
|
+
told.add(key);
|
|
69
|
+
options.onVersionDrift?.({ model, snapshot, live });
|
|
70
|
+
}
|
|
59
71
|
async function definitionAt(url, method, version) {
|
|
60
72
|
const published = await documentRead(false);
|
|
61
73
|
if (published === null)
|
|
@@ -63,6 +75,7 @@ export function restTransport(options = {}) {
|
|
|
63
75
|
const name = modelAt(published, pathOf(url), method);
|
|
64
76
|
if (name === undefined)
|
|
65
77
|
return null;
|
|
78
|
+
noticeDrift(name, version);
|
|
66
79
|
const known = definitionOf(published, name);
|
|
67
80
|
if (known === undefined || !version || known.version === version)
|
|
68
81
|
return known?.meta ?? null;
|
|
@@ -103,6 +116,9 @@ export function restTransport(options = {}) {
|
|
|
103
116
|
throw error;
|
|
104
117
|
}
|
|
105
118
|
},
|
|
119
|
+
async read(route, signal) {
|
|
120
|
+
return (await get(route, urlOf(base, route), signal));
|
|
121
|
+
},
|
|
106
122
|
async one(route, signal) {
|
|
107
123
|
const answer = (await get(route, urlOf(base, route), signal));
|
|
108
124
|
const own = Object.keys(answer).filter((key) => !ENVELOPE.has(key));
|
package/dist/core/transport.d.ts
CHANGED
|
@@ -18,7 +18,18 @@ export interface Transport {
|
|
|
18
18
|
one<T>(route: string, signal?: AbortSignal): Promise<One<T>>;
|
|
19
19
|
/** A Command — a write, an action, a preview: `method` and a JSON body, its answer back. */
|
|
20
20
|
send<T>(method: Method, route: string, body: unknown, signal?: AbortSignal, options?: SendOptions): Promise<T>;
|
|
21
|
+
/**
|
|
22
|
+
* A read that is neither a page nor one record — a Query of the context's own, `GET /me` —:
|
|
23
|
+
* the route with its parameters already in it (`listRoute`), its answer as it came. Optional,
|
|
24
|
+
* so a transport written before it keeps compiling; call it through `readRoute`.
|
|
25
|
+
*/
|
|
26
|
+
read?<T>(route: string, signal?: AbortSignal): Promise<T>;
|
|
21
27
|
}
|
|
28
|
+
/**
|
|
29
|
+
* A read through `transport.read`, or the reason it cannot be: a transport that has no `read`
|
|
30
|
+
* is said so by name, never a `TypeError` from somewhere inside a screen.
|
|
31
|
+
*/
|
|
32
|
+
export declare function readRoute<T>(transport: Transport, route: string, signal?: AbortSignal): Promise<T>;
|
|
22
33
|
export type Method = "POST" | "PUT" | "PATCH" | "DELETE";
|
|
23
34
|
/**
|
|
24
35
|
* How a Command travels besides its body.
|
package/dist/core/transport.js
CHANGED
|
@@ -1,4 +1,13 @@
|
|
|
1
1
|
import { frameworkCopy } from "./copy.js";
|
|
2
|
+
/**
|
|
3
|
+
* A read through `transport.read`, or the reason it cannot be: a transport that has no `read`
|
|
4
|
+
* is said so by name, never a `TypeError` from somewhere inside a screen.
|
|
5
|
+
*/
|
|
6
|
+
export function readRoute(transport, route, signal) {
|
|
7
|
+
if (transport.read === undefined)
|
|
8
|
+
return Promise.reject(new Error(`this transport has no read(): it cannot ask GET ${route} — give it one`));
|
|
9
|
+
return transport.read(route, signal);
|
|
10
|
+
}
|
|
2
11
|
/**
|
|
3
12
|
* A refusal, from any wire. `problem` is what the server said about it (RFC 9457 plus the
|
|
4
13
|
* engine's `kind`, `next_step`, `guidance`) or `null` when it said nothing readable — a proxy's
|
package/dist/react/index.d.ts
CHANGED
|
@@ -5,11 +5,12 @@
|
|
|
5
5
|
*/
|
|
6
6
|
export { FrameworkProvider, useEntityFor, useFramework, } from "./context.js";
|
|
7
7
|
export { type ListScope, ListScopeProvider, useListScope, } from "./scope.js";
|
|
8
|
-
export { type CommandState, type Rehearsal, useCommand, } from "./useCommand.js";
|
|
8
|
+
export { type AnswerOf, type CommandState, type Rehearsal, useCommand, } from "./useCommand.js";
|
|
9
9
|
export { type Groups, useGroups } from "./useGroups.js";
|
|
10
10
|
export { useHistory } from "./useHistory.js";
|
|
11
11
|
export { type Listing, type ListingOptions, type Paging, useListing, } from "./useListing.js";
|
|
12
12
|
export { excluding, type Lookup, useLookup } from "./useLookup.js";
|
|
13
|
+
export { useNameOf } from "./useNameOf.js";
|
|
13
14
|
export { type PivotAxes, type PivotReading, usePivot, } from "./usePivot.js";
|
|
14
15
|
export { type Editing, type RecordReading, type RecordState, useRecord, } from "./useRecord.js";
|
|
15
16
|
export { useRelated } from "./useRelated.js";
|
package/dist/react/index.js
CHANGED
|
@@ -10,6 +10,7 @@ export { useGroups } from "./useGroups.js";
|
|
|
10
10
|
export { useHistory } from "./useHistory.js";
|
|
11
11
|
export { useListing, } from "./useListing.js";
|
|
12
12
|
export { excluding, useLookup } from "./useLookup.js";
|
|
13
|
+
export { useNameOf } from "./useNameOf.js";
|
|
13
14
|
export { usePivot, } from "./usePivot.js";
|
|
14
15
|
export { useRecord, } from "./useRecord.js";
|
|
15
16
|
export { useRelated } from "./useRelated.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, type CommandBodies, type Entity } from "../core/entity.js";
|
|
3
3
|
/**
|
|
4
4
|
* One Command of an entity: run it, or ask first what it would do.
|
|
5
5
|
*
|
|
@@ -12,19 +12,29 @@ import { type Entity } from "../core/entity.js";
|
|
|
12
12
|
* One at a time: `busy` turns the button off, because a payment sent twice is two payments. And
|
|
13
13
|
* once sent, the same gesture keeps its `Idempotency-Key` until the server answers: a retry
|
|
14
14
|
* after a dropped request is the same payment, never a second one (`retryKeys`).
|
|
15
|
+
*
|
|
16
|
+
* A typed entity (`entity<Issue, { send: { message: string } }>`) gets its names and bodies
|
|
17
|
+
* checked: `useCommand(issues, "send").run(id, { mesage })` does not compile. TypeScript infers
|
|
18
|
+
* that only when no type argument is written — `useCommand<Issue, Answer>(…)` stays loose.
|
|
15
19
|
*/
|
|
16
20
|
export type Rehearsal = {
|
|
17
21
|
text: string;
|
|
18
22
|
data: Record<string, unknown>;
|
|
19
23
|
};
|
|
20
|
-
export type CommandState<R> = {
|
|
21
|
-
|
|
24
|
+
export type CommandState<R, B extends AnyRecord = AnyRecord> = {
|
|
25
|
+
/** Without a body, an empty one: the route may name everything. A body given is checked. */
|
|
26
|
+
run: (id?: string, body?: B) => Promise<R | undefined>;
|
|
22
27
|
/** The dry run: what it would do, nothing touched. */
|
|
23
|
-
rehearse: (id?: string, body?:
|
|
28
|
+
rehearse: (id?: string, body?: B) => Promise<Rehearsal | undefined>;
|
|
24
29
|
busy: boolean;
|
|
25
30
|
error: unknown;
|
|
26
31
|
/** The last dry run's answer, until `reset`. */
|
|
27
32
|
rehearsal: Rehearsal | null;
|
|
28
33
|
reset: () => void;
|
|
29
34
|
};
|
|
30
|
-
|
|
35
|
+
/**
|
|
36
|
+
* What a Command answers: `R` when the screen writes it, else what its entity says the Command
|
|
37
|
+
* answers (`CommandAnswers`), else `unknown`.
|
|
38
|
+
*/
|
|
39
|
+
export type AnswerOf<R, A, N> = unknown extends R ? (N extends keyof A ? A[N] : unknown) : R;
|
|
40
|
+
export declare function useCommand<T extends AnyRecord, R = unknown, C extends CommandBodies = CommandBodies, N extends keyof C & string = keyof C & string, A extends CommandAnswers = CommandAnswers>(entity: Entity<T, C, A>, name: N): CommandState<AnswerOf<R, A, N>, C[N]>;
|
package/dist/react/useCommand.js
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import { pathFor } from "../core/entity.js";
|
|
2
|
+
import { bodyFor, pathFor, } from "../core/entity.js";
|
|
3
3
|
import { retryKeys } from "../core/retry.js";
|
|
4
4
|
import { useFramework } from "./context.js";
|
|
5
5
|
import { useCallback, useEffect, useRef, useState } from "react";
|
|
6
6
|
export function useCommand(entity, name) {
|
|
7
7
|
const { transport } = useFramework();
|
|
8
|
+
// A name outside the declared ones still reaches here from untyped code: say which.
|
|
8
9
|
const route = entity.commands[name];
|
|
9
10
|
if (route === undefined)
|
|
10
11
|
throw new Error(`${entity.key} declares no command "${name}"`);
|
|
@@ -25,8 +26,10 @@ export function useCommand(entity, name) {
|
|
|
25
26
|
setBusy(true);
|
|
26
27
|
setError(null);
|
|
27
28
|
const idempotencyKey = gesture.keyFor({ id, body });
|
|
29
|
+
// A Command whose body names its record (`target`) gets the id there.
|
|
30
|
+
const sent = bodyFor(route, id, body);
|
|
28
31
|
try {
|
|
29
|
-
const answer = await transport.send(route.method, pathFor(route, id,
|
|
32
|
+
const answer = await transport.send(route.method, pathFor(route, id, sent), sent, undefined, { idempotencyKey });
|
|
30
33
|
gesture.settle(null);
|
|
31
34
|
return answer;
|
|
32
35
|
}
|
|
@@ -42,8 +45,8 @@ export function useCommand(entity, name) {
|
|
|
42
45
|
}
|
|
43
46
|
}, [transport, route]);
|
|
44
47
|
return {
|
|
45
|
-
run: (id, body
|
|
46
|
-
rehearse: async (id, body
|
|
48
|
+
run: (id, body) => send(id, body ?? {}, keys.current),
|
|
49
|
+
rehearse: async (id, body) => {
|
|
47
50
|
const answer = await send(id, { ...body, dry_run: true }, retryKeys());
|
|
48
51
|
if (answer !== undefined && mounted.current)
|
|
49
52
|
setRehearsal(answer);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type AnyRecord, type Count, type Criteria, type Dropped, type Expression, type Meta, type ReadingStatus, type Sort } from "@sincpro/criteria";
|
|
2
|
-
import { type Entity } from "../core/entity.js";
|
|
2
|
+
import { type CommandBodies, type Entity } from "../core/entity.js";
|
|
3
3
|
import { type ListDefinition } from "../core/list.js";
|
|
4
4
|
/**
|
|
5
5
|
* N records of an entity, live: what a list draws and the gestures that drive it.
|
|
@@ -17,8 +17,9 @@ import { type ListDefinition } from "../core/list.js";
|
|
|
17
17
|
* All three refine the same reading instead of starting another. It holds state and draws
|
|
18
18
|
* nothing: `EntityList` draws it, and a screen that needs another shape draws it itself.
|
|
19
19
|
*/
|
|
20
|
-
export type Listing<T> = {
|
|
21
|
-
entity
|
|
20
|
+
export type Listing<T, C extends CommandBodies = CommandBodies> = {
|
|
21
|
+
/** Its entity, with the Commands it types (`entity<T, C>`): what `useRowCommands` checks. */
|
|
22
|
+
entity: Entity<T & AnyRecord, C>;
|
|
22
23
|
/** The named list it reads, when it was handed one (`defineList`): what `EntityList` draws
|
|
23
24
|
* by default — its columns, filters, faces, title — and how `useGroups` starts. */
|
|
24
25
|
definition?: ListDefinition<T & AnyRecord>;
|
|
@@ -94,4 +95,4 @@ export type ListingOptions<T> = {
|
|
|
94
95
|
/** `more` mode: the most rows ever on screen. 500 by default. */
|
|
95
96
|
maxRows?: number;
|
|
96
97
|
};
|
|
97
|
-
export declare function useListing<T extends AnyRecord>(given: Entity<T> | ListDefinition<T>, own?: ListingOptions<T>): Listing<T>;
|
|
98
|
+
export declare function useListing<T extends AnyRecord, C extends CommandBodies = CommandBodies>(given: Entity<T, C> | ListDefinition<T>, own?: ListingOptions<T>): Listing<T, C>;
|
package/dist/react/useListing.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
import { merge, resource, resumingFrom, searchOver, } from "@sincpro/criteria";
|
|
3
3
|
import { useReading } from "@sincpro/criteria-react";
|
|
4
|
-
import { listRoute } from "../core/entity.js";
|
|
4
|
+
import { listRoute, } from "../core/entity.js";
|
|
5
5
|
import { isList, scopeOf } from "../core/list.js";
|
|
6
6
|
import { useFramework } from "./context.js";
|
|
7
7
|
import { useListScope } from "./scope.js";
|
|
@@ -12,7 +12,8 @@ export function useListing(given, own = {}) {
|
|
|
12
12
|
const { transport, locale, metas } = useFramework();
|
|
13
13
|
// A named list is its entity under a fixed scope: the scope ANDs under the caller's own.
|
|
14
14
|
const definition = isList(given) ? given : undefined;
|
|
15
|
-
|
|
15
|
+
// A named list's entity has no typed Commands of its own: the default, as `C` then is.
|
|
16
|
+
const entity = (definition?.entity ?? given);
|
|
16
17
|
// The screen's cut for this entity (a chooser's, `ListScopeProvider`) ANDs like a scope.
|
|
17
18
|
const cut = useListScope(entity.key);
|
|
18
19
|
const scopes = [
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { type Entity } from "../core/entity.js";
|
|
2
|
+
/**
|
|
3
|
+
* The name of one record of an entity, by its key — what a field shows when it holds only the
|
|
4
|
+
* key (`EntityID[Tenant]`) and not the record it points at.
|
|
5
|
+
*
|
|
6
|
+
* Context: asked ONCE by key, the way `useLookup` names a chosen record it never saw: a page
|
|
7
|
+
* narrowed to that key, one row, read with the definition the application knows. Unnamed is no
|
|
8
|
+
* error — the field shows the key — and an entity listed under another record
|
|
9
|
+
* (`/tenants/{tenant_id}/members`) has no list to ask by key, so it stays unnamed.
|
|
10
|
+
*
|
|
11
|
+
* const name = useNameOf(tenants, record.tenant_id) ?? record.tenant_id;
|
|
12
|
+
*/
|
|
13
|
+
export declare function useNameOf(entity: Entity | undefined, id: string): string | undefined;
|