@sincpro/client-framework 1.11.0 → 1.12.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 +19 -2
- package/dist/client/adapters/memory.d.ts +4 -0
- package/dist/client/adapters/rest-operations.d.ts +8 -6
- package/dist/client/adapters/rest-operations.js +12 -19
- package/dist/client/adapters/rest.d.ts +30 -0
- package/dist/client/adapters/rest.js +51 -3
- package/dist/client/domain/entity.d.ts +13 -4
- package/dist/client/domain/entity.js +1 -0
- package/dist/client/domain/transport.d.ts +5 -1
- package/dist/codegen/services/client.d.ts +1 -0
- package/dist/codegen/services/client.js +32 -2
- package/dist/codegen/services/entities.d.ts +3 -3
- package/dist/codegen/services/entities.js +5 -4
- package/dist/common/ids.d.ts +2 -1
- package/dist/common/ids.js +2 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -666,7 +666,7 @@ await forge.sendIssue({ issue_id: "acme-3", message: "hola" }); // ResponseSendI
|
|
|
666
666
|
| Piece | What it does |
|
|
667
667
|
| --------------------------------- | ------------------------------------------------------------------------------------- |
|
|
668
668
|
| `OperationTransport` port | `call(operation, input, options)`: an operation by who it is, over any wire |
|
|
669
|
-
| `restOperations(transport)` | over REST: the path from the input's
|
|
669
|
+
| `restOperations(transport)` | over REST: the path from the input, a Query's and a DELETE's query, a Command's body |
|
|
670
670
|
| `jsonRpcOperations({ endpoint })` | over JSON-RPC 2.0: one `POST`, the operation's method, its input as named params |
|
|
671
671
|
| `restTransport({ base })` | REST over `fetch`: `Idempotency-Key`, the server's `Problem`, model versions, Parquet |
|
|
672
672
|
| `memoryTransport(tables)` | the same REST contract in memory, copying by the wire's rules — for tests and demos |
|
|
@@ -682,6 +682,23 @@ its trace (`traceparent`), its context (`sincpro-context`) and its signal — as
|
|
|
682
682
|
as metadata where a platform's adapter puts them. `screensTransport` and `entity()` stay on the
|
|
683
683
|
REST `Transport`: they are how a screen reads pages of an entity, with its model and its drafts.
|
|
684
684
|
|
|
685
|
+
An edit — the backend's `Edit`, role `edit` — creates a record under the key its caller names.
|
|
686
|
+
Its generated method mints one when the input names none (`newEntityId`, a UUID v7, as the
|
|
687
|
+
server would), so `forge.editProject({ values, action: "save" })` creates a project over any
|
|
688
|
+
wire, REST's `PUT /projects/{id}/edit` included. A screen that keeps a draft of a new record
|
|
689
|
+
names its key once, so every save of the draft reaches the same record. A Command that answers
|
|
690
|
+
nothing — a `DELETE`'s 204 — resolves `null`, never `undefined`.
|
|
691
|
+
|
|
692
|
+
A `DELETE` — the backend's `Delete`, role `delete`, an entity's `delete` route — is read from its
|
|
693
|
+
URL, never from a body. `restTransport` sends its fields in the query string, as the server decodes
|
|
694
|
+
them (plain values as text, anything else as JSON): `DELETE /projects/acme?dry_run=true` answers
|
|
695
|
+
the `Preview` of what would go, and `origin_version` refuses a delete of a record that moved on.
|
|
696
|
+
Sent in a body they are dropped, and a rehearsal would run the real delete.
|
|
697
|
+
|
|
698
|
+
Which of the ways a screen reaches the backend fits a call — an entity's screens, the generated
|
|
699
|
+
client over the screens transport, or a Command and its Feature — is in
|
|
700
|
+
[docs/WEB.md](docs/WEB.md#which-way-a-screen-reaches-the-backend).
|
|
701
|
+
|
|
685
702
|
A wrapper over a screens transport — a cache, a guard — keeps its mark, so the screens are not made
|
|
686
703
|
twice: `guardedTransport` does it, and a platform's own wrapper calls `markedAs(wrapper, inner)`.
|
|
687
704
|
One span then answers each screen read, whether the wrapper answered it from its own memory or not.
|
|
@@ -754,7 +771,7 @@ each file in the layer it belongs to, under the bounded context's folder (`--out
|
|
|
754
771
|
| `domain/generated/events.ts` | the domain events and `DomainEvent`, their union — when the contract carries them |
|
|
755
772
|
| `domain/generated/index.ts` | the three, re-exported: `import type * as remote from "../domain/generated"` |
|
|
756
773
|
| `adapters/generated/client.ts` | `clientOf(operations)`: a method per operation, over any wire; `operations` and `events` data |
|
|
757
|
-
| `adapters/generated/entities.ts` | each entity's list, record, edit, export, Commands and history, for screens
|
|
774
|
+
| `adapters/generated/entities.ts` | each entity's list, record, edit, delete, export, Commands and history, for screens |
|
|
758
775
|
|
|
759
776
|
The backend keeps them apart — the domain's words, the application layer's DTOs, the facts, the
|
|
760
777
|
adapters to another system —, and so does the application: each beside its own code of that layer.
|
|
@@ -30,6 +30,10 @@ export type MemoryOptions = {
|
|
|
30
30
|
* Answer a Command — a write, an action, a preview — the way the service would. It may
|
|
31
31
|
* change the tables' rows (they are read on every request) and throw a `TransportError` to
|
|
32
32
|
* refuse. Without it, every Command is a 404.
|
|
33
|
+
*
|
|
34
|
+
* Its `body` is the Command's fields whatever the method — a `DELETE`'s too, which REST sends
|
|
35
|
+
* in its query string (`restTransport`): this stands for the service, which reads a DTO, not a
|
|
36
|
+
* URL.
|
|
33
37
|
*/
|
|
34
38
|
handle?: (method: Method, route: string, body: unknown) => unknown;
|
|
35
39
|
/** Answer a read that is neither a page nor a record (`GET /me`), its route with its query.
|
|
@@ -9,11 +9,13 @@ import { type Transport } from "../domain/transport.js";
|
|
|
9
9
|
*
|
|
10
10
|
* 1. The operation's REST binding (`operation.rest`); none: `OperationNotPublished`.
|
|
11
11
|
* 2. Each `{field}` of its path taken from the input.
|
|
12
|
-
* 3. A `GET` — a Query — sends every other field as its query
|
|
13
|
-
*
|
|
14
|
-
* an export) is downloaded.
|
|
15
|
-
* 4. Anything else — a Command —
|
|
16
|
-
* `
|
|
17
|
-
*
|
|
12
|
+
* 3. A `GET` — a Query — sends every other field as its query, as the wire reads it back
|
|
13
|
+
* (`queryOf`: plain values as text, `criteria` and any other value as JSON), and reads the
|
|
14
|
+
* answer as it came; one that answers bytes (`streamed`, an export) is downloaded.
|
|
15
|
+
* 4. Anything else — a Command — is sent with the gesture's `Idempotency-Key`: the input whole as
|
|
16
|
+
* its body; a `DELETE`, read from its URL as a `GET` is, only the fields its path did not take
|
|
17
|
+
* — the REST transport writes them as its query (`restTransport`), and a key is not said twice.
|
|
18
|
+
* Final: the operation's answer, as the contract describes it — `null` for one that answers
|
|
19
|
+
* nothing (a `DELETE`'s 204).
|
|
18
20
|
*/
|
|
19
21
|
export declare function restOperations(transport: Transport): OperationTransport;
|
|
@@ -1,11 +1,8 @@
|
|
|
1
|
+
import { queryOf, withQuery } from "./rest.js";
|
|
1
2
|
import { OperationNotPublished, } from "../domain/operation.js";
|
|
2
3
|
import { downloadRoute, readRoute, } from "../domain/transport.js";
|
|
3
4
|
import { bindableOperations } from "../services/propagation.js";
|
|
4
5
|
const PATH_KEY = /\{([A-Za-z_][A-Za-z0-9_]*)\}/g;
|
|
5
|
-
/** What a value is in a query string: text as it is, anything else as JSON (`criteria`). */
|
|
6
|
-
function queryValue(value) {
|
|
7
|
-
return typeof value === "string" ? value : JSON.stringify(value);
|
|
8
|
-
}
|
|
9
6
|
/** The path with each `{field}` taken from the input, and the input's fields it did not take. */
|
|
10
7
|
function placed(operation, path, input) {
|
|
11
8
|
const rest = { ...input };
|
|
@@ -18,13 +15,6 @@ function placed(operation, path, input) {
|
|
|
18
15
|
});
|
|
19
16
|
return { route, rest };
|
|
20
17
|
}
|
|
21
|
-
/** The route with every other field of a GET as its query. */
|
|
22
|
-
function withQuery(route, fields) {
|
|
23
|
-
const query = new URLSearchParams(Object.entries(fields)
|
|
24
|
-
.filter(([, value]) => value !== undefined && value !== null && value !== "")
|
|
25
|
-
.map(([name, value]) => [name, queryValue(value)])).toString();
|
|
26
|
-
return query === "" ? route : `${route}${route.includes("?") ? "&" : "?"}${query}`;
|
|
27
|
-
}
|
|
28
18
|
/**
|
|
29
19
|
* Operations over REST, as sincpro_framework's REST gateway publishes them — on a `Transport`
|
|
30
20
|
* (`restTransport`, `memoryTransport`, a platform's):
|
|
@@ -34,12 +24,14 @@ function withQuery(route, fields) {
|
|
|
34
24
|
*
|
|
35
25
|
* 1. The operation's REST binding (`operation.rest`); none: `OperationNotPublished`.
|
|
36
26
|
* 2. Each `{field}` of its path taken from the input.
|
|
37
|
-
* 3. A `GET` — a Query — sends every other field as its query
|
|
38
|
-
*
|
|
39
|
-
* an export) is downloaded.
|
|
40
|
-
* 4. Anything else — a Command —
|
|
41
|
-
* `
|
|
42
|
-
*
|
|
27
|
+
* 3. A `GET` — a Query — sends every other field as its query, as the wire reads it back
|
|
28
|
+
* (`queryOf`: plain values as text, `criteria` and any other value as JSON), and reads the
|
|
29
|
+
* answer as it came; one that answers bytes (`streamed`, an export) is downloaded.
|
|
30
|
+
* 4. Anything else — a Command — is sent with the gesture's `Idempotency-Key`: the input whole as
|
|
31
|
+
* its body; a `DELETE`, read from its URL as a `GET` is, only the fields its path did not take
|
|
32
|
+
* — the REST transport writes them as its query (`restTransport`), and a key is not said twice.
|
|
33
|
+
* Final: the operation's answer, as the contract describes it — `null` for one that answers
|
|
34
|
+
* nothing (a `DELETE`'s 204).
|
|
43
35
|
*/
|
|
44
36
|
export function restOperations(transport) {
|
|
45
37
|
return bindableOperations({
|
|
@@ -52,12 +44,13 @@ export function restOperations(transport) {
|
|
|
52
44
|
try {
|
|
53
45
|
const { route, rest: others } = placed(operation, rest.path, fields);
|
|
54
46
|
if (rest.method === "GET") {
|
|
55
|
-
const asked = withQuery(route, others);
|
|
47
|
+
const asked = withQuery(route, queryOf(others));
|
|
56
48
|
return operation.streamed
|
|
57
49
|
? downloadRoute(transport, asked, signal, call)
|
|
58
50
|
: readRoute(transport, asked, signal, call);
|
|
59
51
|
}
|
|
60
|
-
|
|
52
|
+
const sent = rest.method === "DELETE" ? others : fields;
|
|
53
|
+
return transport.send(rest.method, route, sent, signal, call);
|
|
61
54
|
}
|
|
62
55
|
catch (error) {
|
|
63
56
|
return Promise.reject(error);
|
|
@@ -10,6 +10,19 @@ import { type Criteria } from "@sincpro/criteria";
|
|
|
10
10
|
* one record (`one()`), a write (`send()`, with its `Idempotency-Key`), and its refusals as a
|
|
11
11
|
* `TransportError`, the one error every screen explains.
|
|
12
12
|
*
|
|
13
|
+
* A write's fields travel where the wire reads them. sincpro_framework reads a `DELETE`'s DTO
|
|
14
|
+
* from its URL, as a `GET`'s (`READ_FROM_URL`), and never its body: a field sent there is dropped
|
|
15
|
+
* — or refused with a 422 (`out_of_place`) where the server checks it. So a `DELETE` sends its
|
|
16
|
+
* fields as its query string (`queryOf`) and no body; every other method sends them as a JSON
|
|
17
|
+
* body. Sent in a body, `dry_run` is lost and a rehearsal would run the real delete, and so is
|
|
18
|
+
* `origin_version`, so a stale delete would go through.
|
|
19
|
+
*
|
|
20
|
+
* The fields reach `send` as its `body` whatever the method — the change feed reads `dry_run`
|
|
21
|
+
* there — and `send` gets the path already filled, so leaving out a key the path took is the
|
|
22
|
+
* caller's, who knows the template: `restOperations` does, and an entity's `delete` puts its key
|
|
23
|
+
* only in the path. A key that travels in both is harmless: the wire reads a `DELETE` as
|
|
24
|
+
* `{**query, **path_params}` — the path wins — and looks for misplaced fields only in its body.
|
|
25
|
+
*
|
|
13
26
|
* The definition of each model — what a list, a form and a select are built from — is read
|
|
14
27
|
* from the bounded context's discovery document (`document`, `/openapi.json` by default), once,
|
|
15
28
|
* and read again only when an answer says another version of its model (`model_version`); the
|
|
@@ -55,5 +68,22 @@ export type VersionDrift = {
|
|
|
55
68
|
snapshot: string;
|
|
56
69
|
live: string;
|
|
57
70
|
};
|
|
71
|
+
/**
|
|
72
|
+
* The fields of a DTO as a query string, the way sincpro_framework's REST wire reads them back
|
|
73
|
+
* (`payload_from_query`): a plain value — text, a number, a boolean — as its text, which the
|
|
74
|
+
* server coerces to the field's type; anything else — a list, an object, a `Money` — as JSON,
|
|
75
|
+
* which it decodes. A value with a wire form of its own is written as that first (`valuesOf`):
|
|
76
|
+
* a `Decimal` as its text, a `Date` as its instant. A field with no value (`undefined`, `null`,
|
|
77
|
+
* `""`) is not sent, and the DTO's default stands — as a filter left blank.
|
|
78
|
+
*
|
|
79
|
+
* queryOf({ dry_run: true, origin_version: 3, tags: ["a"] })
|
|
80
|
+
* -> "dry_run=true&origin_version=3&tags=%5B%22a%22%5D"
|
|
81
|
+
*
|
|
82
|
+
* Context: the client does not know each field's schema, so `null` cannot be told apart: as
|
|
83
|
+
* text it would be the word «null» for a plain field. Left out, the field takes its default.
|
|
84
|
+
*/
|
|
85
|
+
export declare function queryOf(fields: Readonly<Record<string, unknown>>): string;
|
|
86
|
+
/** `route` with `query` added to the query it may already carry. */
|
|
87
|
+
export declare function withQuery(route: string, query: string): string;
|
|
58
88
|
export declare function urlOf<T>(base: string, route: string, criteria?: Criteria<T>): string;
|
|
59
89
|
export declare function restTransport(options?: RestOptions): Transport;
|
|
@@ -2,6 +2,7 @@ import { TransportError, } from "../domain/transport.js";
|
|
|
2
2
|
import { announced, processChanges } from "../services/changes.js";
|
|
3
3
|
import { definitionOf, modelAt, } from "../services/discovery.js";
|
|
4
4
|
import { bindableTransport } from "../services/propagation.js";
|
|
5
|
+
import { valuesOf } from "../../common/serialization.js";
|
|
5
6
|
import { readProblem } from "@sincpro/criteria";
|
|
6
7
|
import { RestError, restSource } from "@sincpro/criteria/rest";
|
|
7
8
|
/** The keys of an answer that are not the record: what is left is the record's own field. */
|
|
@@ -25,6 +26,47 @@ function filenameOf(disposition) {
|
|
|
25
26
|
const plain = /filename\s*=\s*("([^"]*)"|[^;]+)/i.exec(said);
|
|
26
27
|
return (plain?.[2] ?? plain?.[1] ?? "").trim();
|
|
27
28
|
}
|
|
29
|
+
/**
|
|
30
|
+
* The fields of a DTO as a query string, the way sincpro_framework's REST wire reads them back
|
|
31
|
+
* (`payload_from_query`): a plain value — text, a number, a boolean — as its text, which the
|
|
32
|
+
* server coerces to the field's type; anything else — a list, an object, a `Money` — as JSON,
|
|
33
|
+
* which it decodes. A value with a wire form of its own is written as that first (`valuesOf`):
|
|
34
|
+
* a `Decimal` as its text, a `Date` as its instant. A field with no value (`undefined`, `null`,
|
|
35
|
+
* `""`) is not sent, and the DTO's default stands — as a filter left blank.
|
|
36
|
+
*
|
|
37
|
+
* queryOf({ dry_run: true, origin_version: 3, tags: ["a"] })
|
|
38
|
+
* -> "dry_run=true&origin_version=3&tags=%5B%22a%22%5D"
|
|
39
|
+
*
|
|
40
|
+
* Context: the client does not know each field's schema, so `null` cannot be told apart: as
|
|
41
|
+
* text it would be the word «null» for a plain field. Left out, the field takes its default.
|
|
42
|
+
*/
|
|
43
|
+
export function queryOf(fields) {
|
|
44
|
+
return new URLSearchParams(Object.entries(fields).flatMap(([name, value]) => {
|
|
45
|
+
const wire = value === undefined ? null : valuesOf(value);
|
|
46
|
+
if (wire === null || wire === "")
|
|
47
|
+
return [];
|
|
48
|
+
return [[name, typeof wire === "object" ? JSON.stringify(wire) : String(wire)]];
|
|
49
|
+
})).toString();
|
|
50
|
+
}
|
|
51
|
+
/** `route` with `query` added to the query it may already carry. */
|
|
52
|
+
export function withQuery(route, query) {
|
|
53
|
+
return query === "" ? route : `${route}${route.includes("?") ? "&" : "?"}${query}`;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* The fields a `DELETE` carries in its URL: its body, which must be a set of fields as the wire
|
|
57
|
+
* writes it — a list or a lone value has no field to put in a query, and dropping it silently is
|
|
58
|
+
* the bug this exists to prevent.
|
|
59
|
+
*/
|
|
60
|
+
function urlFieldsOf(route, body) {
|
|
61
|
+
if (body === undefined || body === null)
|
|
62
|
+
return {};
|
|
63
|
+
const fields = valuesOf(body);
|
|
64
|
+
if (fields === null || typeof fields !== "object" || Array.isArray(fields))
|
|
65
|
+
throw new TypeError(`DELETE ${route} sends its fields in the URL: its body is an object of them, not ${Array.isArray(fields) ? "a list" : typeof fields}`);
|
|
66
|
+
return fields;
|
|
67
|
+
}
|
|
68
|
+
/** The methods of a write sincpro_framework reads from the URL alone (its `READ_FROM_URL`). */
|
|
69
|
+
const FROM_URL = new Set(["DELETE"]);
|
|
28
70
|
export function urlOf(base, route, criteria) {
|
|
29
71
|
const url = `${base}${route}`;
|
|
30
72
|
if (criteria === undefined)
|
|
@@ -114,18 +156,22 @@ export function restTransport(options = {}) {
|
|
|
114
156
|
const again = await documentRead(true);
|
|
115
157
|
return (again === null ? undefined : definitionOf(again, name))?.meta ?? known.meta;
|
|
116
158
|
}
|
|
159
|
+
/** A request and its answer: a write's body as JSON — none for one read from its URL. */
|
|
117
160
|
async function get(route, url, signal, write, call) {
|
|
161
|
+
const json = write !== undefined && !FROM_URL.has(write.method);
|
|
118
162
|
const headers = write
|
|
119
163
|
? {
|
|
120
164
|
...own(),
|
|
121
165
|
...call?.headers,
|
|
122
|
-
"Content-Type": "application/json",
|
|
166
|
+
...(json ? { "Content-Type": "application/json" } : {}),
|
|
123
167
|
...(write.key === undefined ? {} : { "Idempotency-Key": write.key }),
|
|
124
168
|
}
|
|
125
169
|
: { ...own(), ...call?.headers };
|
|
126
170
|
const response = await doFetch(url, {
|
|
127
171
|
headers,
|
|
128
|
-
...(write
|
|
172
|
+
...(write
|
|
173
|
+
? { method: write.method, ...(json ? { body: JSON.stringify(write.body) } : {}) }
|
|
174
|
+
: {}),
|
|
129
175
|
...(signal ? { signal } : {}),
|
|
130
176
|
});
|
|
131
177
|
const text = await response.text();
|
|
@@ -185,8 +231,10 @@ export function restTransport(options = {}) {
|
|
|
185
231
|
};
|
|
186
232
|
},
|
|
187
233
|
async send(method, route, body, signal, sending) {
|
|
234
|
+
// A DELETE is read from its URL: its fields go in the query, and it has no body.
|
|
235
|
+
const url = urlOf(base, FROM_URL.has(method) ? withQuery(route, queryOf(urlFieldsOf(route, body))) : route);
|
|
188
236
|
return announced(changes, method, route, body, async () => {
|
|
189
|
-
return (await get(route,
|
|
237
|
+
return (await get(route, url, signal, { method, body, key: sending?.idempotencyKey }, sending));
|
|
190
238
|
});
|
|
191
239
|
},
|
|
192
240
|
});
|
|
@@ -36,8 +36,8 @@ export type CommandAnswers = Readonly<Record<string, unknown>>;
|
|
|
36
36
|
* Everything a screen needs to know about where an entity lives on the server, declared once.
|
|
37
37
|
*
|
|
38
38
|
* Context: a screen should not know a URL. It names the entity, and the entity says where its
|
|
39
|
-
* list is, where one record is, which Commands it takes, how it is saved and
|
|
40
|
-
* draft is kept and where its history is. `key` is how the workspace refers to it
|
|
39
|
+
* list is, where one record is, which Commands it takes, how it is saved, previewed and deleted,
|
|
40
|
+
* where a draft is kept and where its history is. `key` is how the workspace refers to it
|
|
41
41
|
* (`domain:kind`, the shape of a composer `ResourceRef`).
|
|
42
42
|
*
|
|
43
43
|
* The routes are declared, not guessed: sincpro_framework publishes a route per use case and
|
|
@@ -48,6 +48,7 @@ export type CommandAnswers = Readonly<Record<string, unknown>>;
|
|
|
48
48
|
* create: "POST /invoices",
|
|
49
49
|
* save: "PUT /invoices/{id}",
|
|
50
50
|
* preview: "POST /invoices/preview",
|
|
51
|
+
* delete: "DELETE /invoices/{id}",
|
|
51
52
|
* commands: { confirm: "POST /invoices/{id}/confirm", cancel: "POST /invoices/{id}/cancel" },
|
|
52
53
|
* history: "/invoices/{id}/events",
|
|
53
54
|
* });
|
|
@@ -89,9 +90,15 @@ export type Entity<T extends AnyRecord = AnyRecord, C extends CommandBodies = Co
|
|
|
89
90
|
/** Where this caller keeps an edit between requests — sincpro_framework's `Drafts`. */
|
|
90
91
|
draft?: WriteRoute;
|
|
91
92
|
/** sincpro_framework's `Edit`: ONE route that keeps the draft, saves and discards, by its
|
|
92
|
-
* `action`. Declared, a save
|
|
93
|
-
*
|
|
93
|
+
* `action`. Declared, a save goes through it — what changed, with the draft and stored
|
|
94
|
+
* versions it started from: the server puts it over what it stored and the caller's draft —
|
|
95
|
+
* and it is also where the draft is kept. */
|
|
94
96
|
edit?: WriteRoute;
|
|
97
|
+
/** sincpro_framework's `Delete`: the record deleted by its key, `{id}` in the path. It answers
|
|
98
|
+
* nothing (`null`); with `dry_run: true` it answers the `Preview` of what would go and deletes
|
|
99
|
+
* nothing, and `origin_version` refuses it when the record moved past the version read. Over
|
|
100
|
+
* REST those fields travel in the query string — a `DELETE` is read from its URL. */
|
|
101
|
+
delete?: WriteRoute;
|
|
95
102
|
/** Its business events: where ONE record's events are read (sincpro_framework's
|
|
96
103
|
* `DomainEvents`), `{id}` where its key goes — a page of them, as a list answers. */
|
|
97
104
|
history?: string;
|
|
@@ -189,6 +196,8 @@ export type EntityRoutes<C extends CommandBodies = CommandBodies> = CommandRoute
|
|
|
189
196
|
draft?: string;
|
|
190
197
|
/** `"PUT /invoices/{id}/edit"`: the use case that answers sincpro_framework's `Edit`. */
|
|
191
198
|
edit?: string;
|
|
199
|
+
/** `"DELETE /invoices/{id}"`: the use case that answers sincpro_framework's `Delete`. */
|
|
200
|
+
delete?: string;
|
|
192
201
|
/** `"/invoices/{id}/events"`: a record's events (`DomainEvents[Invoice, …]`), oldest first. */
|
|
193
202
|
history?: string;
|
|
194
203
|
/** `"/invoices/export"`: every record of a criteria as a file (`Export[Invoice, Streamed]`),
|
|
@@ -194,6 +194,7 @@ export function entity(key, routes) {
|
|
|
194
194
|
...(routes.preview === undefined ? {} : { preview: writeRoute(routes.preview) }),
|
|
195
195
|
...(routes.draft === undefined ? {} : { draft: writeRoute(routes.draft) }),
|
|
196
196
|
...(routes.edit === undefined ? {} : { edit: writeRoute(routes.edit) }),
|
|
197
|
+
...(routes.delete === undefined ? {} : { delete: writeRoute(routes.delete) }),
|
|
197
198
|
...(routes.history === undefined ? {} : { history: routes.history }),
|
|
198
199
|
...(routes.export === undefined ? {} : { export: routes.export }),
|
|
199
200
|
summary: routes.summary ?? {},
|
|
@@ -27,7 +27,11 @@ export interface Transport {
|
|
|
27
27
|
list<T>(route: string, criteria: Criteria<T>, signal?: AbortSignal, options?: CallOptions): Promise<Page<T>>;
|
|
28
28
|
/** One record, with the definition of its model. */
|
|
29
29
|
one<T>(route: string, signal?: AbortSignal, options?: CallOptions): Promise<One<T>>;
|
|
30
|
-
/**
|
|
30
|
+
/**
|
|
31
|
+
* A Command — a write, an action, a preview: `method` and its fields (`body`), its answer back,
|
|
32
|
+
* `null` when it answers nothing. Where the fields travel is the wire's: over REST a `DELETE` is
|
|
33
|
+
* read from its URL, so its fields go in the query string; any other method's, in a JSON body.
|
|
34
|
+
*/
|
|
31
35
|
send<T>(method: Method, route: string, body: unknown, signal?: AbortSignal, options?: SendOptions): Promise<T>;
|
|
32
36
|
/**
|
|
33
37
|
* A read that is neither a page nor one record — a Query of the context's own, `GET /me` —:
|
|
@@ -7,6 +7,7 @@ import { type GenerateOptions } from "./rest.js";
|
|
|
7
7
|
* whether it answers bytes, and the binding of each wire that publishes it.
|
|
8
8
|
* 2. A method per operation, named by `methodOf`: its input — optional when nothing in it is
|
|
9
9
|
* required — and the call's options (headers, signal, idempotency key), answering its answer.
|
|
10
|
+
* An edit's method mints the record's key when the input names none (`mintsKey`).
|
|
10
11
|
* 3. When the contract carries them, every domain event as data, by its type's name: its wire
|
|
11
12
|
* name, and whether the bounded context records it or handles it.
|
|
12
13
|
* Final: `clientOf(transport)` over an `OperationTransport`, bound to each run that uses it; two
|
|
@@ -27,6 +27,29 @@ function isStreamed(operation) {
|
|
|
27
27
|
return (operation.output !== null &&
|
|
28
28
|
operation.output["x-sincpro-streamed"] !== undefined);
|
|
29
29
|
}
|
|
30
|
+
/**
|
|
31
|
+
* The field of an edit that names its record, minted by the client when the caller names none.
|
|
32
|
+
*
|
|
33
|
+
* Context: an operation of role `edit` is sincpro_framework's `Edit` (`role_of`: what descends
|
|
34
|
+
* from it), whose `id` — the record's key — the server mints with `new_entity_id()` when none is
|
|
35
|
+
* sent. Over REST its route carries the key in its path (`PUT /invoices/{id}/edit`), so creating
|
|
36
|
+
* a record through the generated method failed before it left («needs "id"»). The method mints it
|
|
37
|
+
* as the server would (`newEntityId`, a UUID v7), the same on every wire; a key the caller names
|
|
38
|
+
* — a stored record's, or the one a form minted for its draft — goes as it is.
|
|
39
|
+
*/
|
|
40
|
+
const EDIT_KEY = "id";
|
|
41
|
+
/** Whether `operation` is an edit whose key the caller may leave out — the method mints it. */
|
|
42
|
+
function mintsKey(operation) {
|
|
43
|
+
return (operation.role === "edit" &&
|
|
44
|
+
operation.input.properties?.[EDIT_KEY] !== undefined &&
|
|
45
|
+
!(operation.input.required ?? []).includes(EDIT_KEY));
|
|
46
|
+
}
|
|
47
|
+
/** The input a method hands the transport: as the caller gave it, an edit's key minted if absent. */
|
|
48
|
+
function inputOf(operation, optional) {
|
|
49
|
+
if (mintsKey(operation))
|
|
50
|
+
return `{ ...input, ${EDIT_KEY}: input?.${EDIT_KEY} || newEntityId() }`;
|
|
51
|
+
return optional ? "input ?? {}" : "input";
|
|
52
|
+
}
|
|
30
53
|
/** The data of one operation: what it is, and how each wire reaches it. */
|
|
31
54
|
function dataOf(metamodel, identity, operation, rest) {
|
|
32
55
|
const lines = [
|
|
@@ -58,6 +81,7 @@ function dataOf(metamodel, identity, operation, rest) {
|
|
|
58
81
|
* whether it answers bytes, and the binding of each wire that publishes it.
|
|
59
82
|
* 2. A method per operation, named by `methodOf`: its input — optional when nothing in it is
|
|
60
83
|
* required — and the call's options (headers, signal, idempotency key), answering its answer.
|
|
84
|
+
* An edit's method mints the record's key when the input names none (`mintsKey`).
|
|
61
85
|
* 3. When the contract carries them, every domain event as data, by its type's name: its wire
|
|
62
86
|
* name, and whether the bounded context records it or handles it.
|
|
63
87
|
* Final: `clientOf(transport)` over an `OperationTransport`, bound to each run that uses it; two
|
|
@@ -67,26 +91,32 @@ export function clientFileOf(metamodel, options = {}) {
|
|
|
67
91
|
const claim = claimed();
|
|
68
92
|
const data = [];
|
|
69
93
|
const methods = [];
|
|
94
|
+
let minting = false;
|
|
70
95
|
for (const [identity, operation] of sorted(metamodel.operations)) {
|
|
71
96
|
const local = localOf(metamodel, identity);
|
|
72
97
|
data.push(dataOf(metamodel, identity, operation, restOf(metamodel, identity, options)));
|
|
73
98
|
const method = methodOf(local);
|
|
74
99
|
claim(method, `operation ${identity}`);
|
|
75
100
|
const wires = wiresOf(metamodel, identity);
|
|
101
|
+
const mints = mintsKey(operation);
|
|
102
|
+
minting ||= mints;
|
|
76
103
|
const doc = docOf([
|
|
77
104
|
operation.description,
|
|
78
105
|
deprecationOf(metamodel, operation),
|
|
106
|
+
mints
|
|
107
|
+
? `A new record's \`${EDIT_KEY}\` is minted here when the input names none (\`newEntityId\`).`
|
|
108
|
+
: undefined,
|
|
79
109
|
wires.length === 0 ? undefined : `Published over ${wires.join(", ")}.`,
|
|
80
110
|
], " ");
|
|
81
111
|
const optional = (operation.input.required ?? []).length === 0;
|
|
82
112
|
methods.push([
|
|
83
113
|
`${doc} ${method}: (input${optional ? "?" : ""}: dto.${local}, options?: OperationOptions) =>`,
|
|
84
|
-
` transport.call<dto.${answerOf(metamodel, identity)}>(operations.${local},
|
|
114
|
+
` transport.call<dto.${answerOf(metamodel, identity)}>(operations.${local}, ${inputOf(operation, optional)}, options),`,
|
|
85
115
|
].join("\n"));
|
|
86
116
|
}
|
|
87
117
|
const events = sorted(metamodel.events ?? {}).map(([name, event]) => ` ${identifierOf(event.title)}: { name: ${JSON.stringify(name)}, recorded: ${event.recorded}, handled: ${event.handled} },`);
|
|
88
118
|
const imports = [
|
|
89
|
-
`import { bindable, type OperationOptions, type OperationTransport } from "${FRAMEWORK}";`,
|
|
119
|
+
`import { bindable,${minting ? " newEntityId," : ""} type OperationOptions, type OperationTransport } from "${FRAMEWORK}";`,
|
|
90
120
|
"",
|
|
91
121
|
`import type * as dto from "${DOMAIN_MODULE}";`,
|
|
92
122
|
].join("\n");
|
|
@@ -5,9 +5,9 @@ import { type GenerateOptions } from "./rest.js";
|
|
|
5
5
|
*
|
|
6
6
|
* 1. Per model, by name: the one `search` a transport reads for it (`own` when there are
|
|
7
7
|
* several) — none, and the model gets no entity, said in the file's own doc.
|
|
8
|
-
* 2. Its `get`, `edit` and `
|
|
9
|
-
* `events` — a record's `DomainEvents` — as `history`, the same way; and its `export` —
|
|
10
|
-
* record of a criteria as a file — by its path.
|
|
8
|
+
* 2. Its `get`, `edit`, `preview` and `delete` the same way, the record's field written `{id}`;
|
|
9
|
+
* its `events` — a record's `DomainEvents` — as `history`, the same way; and its `export` —
|
|
10
|
+
* every record of a criteria as a file — by its path.
|
|
11
11
|
* 3. Every write of role `command` that concerns it: with `target` when its body names the
|
|
12
12
|
* record, as it is when it creates one; its body typed without that field.
|
|
13
13
|
* Final: an `entity<T, C>()` per model listed, keyed `<domain>:<kind>`, and `entities`, all of
|
|
@@ -78,7 +78,8 @@ function entitySourceOf(metamodel, model, domain, options, claim) {
|
|
|
78
78
|
}
|
|
79
79
|
else if (one !== undefined)
|
|
80
80
|
missing.push(`one: ${one.missing}`);
|
|
81
|
-
|
|
81
|
+
// sincpro_framework's `Delete` (role `delete`) is the record's own route too, never a Command.
|
|
82
|
+
for (const role of ["edit", "preview", "delete"]) {
|
|
82
83
|
const write = oneOf(reads(role, isWrite), role);
|
|
83
84
|
if (write === undefined)
|
|
84
85
|
continue;
|
|
@@ -155,9 +156,9 @@ function entitySourceOf(metamodel, model, domain, options, claim) {
|
|
|
155
156
|
*
|
|
156
157
|
* 1. Per model, by name: the one `search` a transport reads for it (`own` when there are
|
|
157
158
|
* several) — none, and the model gets no entity, said in the file's own doc.
|
|
158
|
-
* 2. Its `get`, `edit` and `
|
|
159
|
-
* `events` — a record's `DomainEvents` — as `history`, the same way; and its `export` —
|
|
160
|
-
* record of a criteria as a file — by its path.
|
|
159
|
+
* 2. Its `get`, `edit`, `preview` and `delete` the same way, the record's field written `{id}`;
|
|
160
|
+
* its `events` — a record's `DomainEvents` — as `history`, the same way; and its `export` —
|
|
161
|
+
* every record of a criteria as a file — by its path.
|
|
161
162
|
* 3. Every write of role `command` that concerns it: with `target` when its body names the
|
|
162
163
|
* record, as it is when it creates one; its body typed without that field.
|
|
163
164
|
* Final: an `entity<T, C>()` per model listed, keyed `<domain>:<kind>`, and `entities`, all of
|
package/dist/common/ids.d.ts
CHANGED
|
@@ -5,7 +5,8 @@
|
|
|
5
5
|
* Context: sincpro_framework's `Edit` creates a new record under the id the CLIENT names («a
|
|
6
6
|
* UUID v7 it mints»), and the draft and the stored record share it from the first draft save.
|
|
7
7
|
* A form that waited for the server to name it could neither keep a draft of a new record nor
|
|
8
|
-
* reach the edit route at all (`PUT /invoices/{id}/edit` needs the id in its path)
|
|
8
|
+
* reach the edit route at all (`PUT /invoices/{id}/edit` needs the id in its path) — so a
|
|
9
|
+
* generated client's edit mints one here when its caller names none. The layout
|
|
9
10
|
* is the backend's (RFC 9562): 48 bits of unix milliseconds, the version, a 12-bit counter
|
|
10
11
|
* within the millisecond, the variant and 62 random bits — so ids minted here sort by when
|
|
11
12
|
* they were minted, like the server's, and ordering by `id` is still ordering by creation.
|
package/dist/common/ids.js
CHANGED
|
@@ -5,7 +5,8 @@
|
|
|
5
5
|
* Context: sincpro_framework's `Edit` creates a new record under the id the CLIENT names («a
|
|
6
6
|
* UUID v7 it mints»), and the draft and the stored record share it from the first draft save.
|
|
7
7
|
* A form that waited for the server to name it could neither keep a draft of a new record nor
|
|
8
|
-
* reach the edit route at all (`PUT /invoices/{id}/edit` needs the id in its path)
|
|
8
|
+
* reach the edit route at all (`PUT /invoices/{id}/edit` needs the id in its path) — so a
|
|
9
|
+
* generated client's edit mints one here when its caller names none. The layout
|
|
9
10
|
* is the backend's (RFC 9562): 48 bits of unix milliseconds, the version, a 12-bit counter
|
|
10
11
|
* within the millisecond, the variant and 62 random bits — so ids minted here sort by when
|
|
11
12
|
* they were minted, like the server's, and ordering by `id` is still ordering by creation.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sincpro/client-framework",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.12.0",
|
|
4
4
|
"description": "The client side of sincpro_framework, for every platform: Feature and ApplicationService traced and reported by default, applications made of domains, and the typed client generated from a service's contract. No React, no DOM.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|