@sincpro/client-framework 2.10.0 → 3.0.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 (49) hide show
  1. package/AGENTS.md +16 -7
  2. package/README.md +83 -45
  3. package/dist/client/adapters/json-rpc.d.ts +4 -0
  4. package/dist/client/adapters/json-rpc.js +44 -38
  5. package/dist/client/adapters/memory.d.ts +5 -7
  6. package/dist/client/adapters/memory.js +82 -62
  7. package/dist/client/adapters/rest-operations.d.ts +6 -2
  8. package/dist/client/adapters/rest-operations.js +8 -5
  9. package/dist/client/adapters/rest.d.ts +2 -7
  10. package/dist/client/adapters/rest.js +12 -18
  11. package/dist/client/domain/answer.d.ts +28 -0
  12. package/dist/client/domain/answer.js +52 -0
  13. package/dist/client/domain/changes.d.ts +24 -19
  14. package/dist/client/domain/changes.js +18 -9
  15. package/dist/client/domain/entity.d.ts +74 -212
  16. package/dist/client/domain/entity.js +39 -117
  17. package/dist/client/domain/operation.d.ts +22 -1
  18. package/dist/client/domain/operation.js +4 -0
  19. package/dist/client/domain/transport.d.ts +8 -6
  20. package/dist/client/domain/transport.js +3 -3
  21. package/dist/client/index.d.ts +13 -7
  22. package/dist/client/index.js +13 -7
  23. package/dist/client/services/api.d.ts +39 -0
  24. package/dist/client/services/api.js +38 -0
  25. package/dist/client/services/changes.d.ts +13 -12
  26. package/dist/client/services/changes.js +14 -13
  27. package/dist/client/services/guarded.js +3 -3
  28. package/dist/client/services/propagation.d.ts +9 -11
  29. package/dist/client/services/propagation.js +9 -11
  30. package/dist/client/services/reads.d.ts +38 -0
  31. package/dist/client/services/reads.js +46 -0
  32. package/dist/client/services/selection.d.ts +7 -6
  33. package/dist/client/services/selection.js +6 -5
  34. package/dist/codegen/domain/names.d.ts +16 -1
  35. package/dist/codegen/domain/names.js +43 -0
  36. package/dist/codegen/services/client.d.ts +7 -7
  37. package/dist/codegen/services/client.js +120 -52
  38. package/dist/codegen/services/entities.d.ts +9 -12
  39. package/dist/codegen/services/entities.js +76 -130
  40. package/dist/codegen/services/rest.d.ts +1 -6
  41. package/dist/codegen/services/rest.js +0 -11
  42. package/dist/codegen/services/scaffold.js +5 -2
  43. package/dist/core/domain/services.d.ts +1 -1
  44. package/dist/core/domain/services.js +1 -1
  45. package/dist/core/entrypoint/screens.d.ts +14 -4
  46. package/dist/core/entrypoint/screens.js +33 -141
  47. package/dist/core/index.d.ts +2 -2
  48. package/dist/core/index.js +1 -1
  49. package/package.json +1 -1
@@ -1,6 +1,7 @@
1
1
  import { queryOf, withQuery } from "./rest.js";
2
2
  import { OperationNotPublished, } from "../domain/operation.js";
3
3
  import { downloadRoute, readRoute, } from "../domain/transport.js";
4
+ import { announced, processChanges } from "../services/changes.js";
4
5
  import { bindableOperations } from "../services/propagation.js";
5
6
  const PATH_KEY = /\{([A-Za-z_][A-Za-z0-9_]*)\}/g;
6
7
  /** The path with each `{field}` taken from the input, and the input's fields it did not take. */
@@ -29,17 +30,19 @@ function placed(operation, path, input) {
29
30
  * answer as it came; one that answers bytes (`streamed`, an export) is downloaded.
30
31
  * 4. Anything else — a Command — is sent with the gesture's `Idempotency-Key`: the input whole as
31
32
  * 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
+ * — the REST transport writes them as its query (`restTransport`), and a key is not said twice
34
+ * —, and its change announced on `changes` (`processChanges` by default), whatever it answered.
33
35
  * Final: the operation's answer, as the contract describes it — `null` for one that answers
34
36
  * nothing (a `DELETE`'s 204).
35
37
  */
36
- export function restOperations(transport) {
38
+ export function restOperations(transport, options = {}) {
39
+ const changes = options.changes ?? processChanges;
37
40
  return bindableOperations({
38
- call(operation, input, options = {}) {
41
+ call(operation, input, called = {}) {
39
42
  const rest = operation.rest;
40
43
  if (rest === undefined)
41
44
  return Promise.reject(new OperationNotPublished(operation.identity, "REST"));
42
- const { signal, ...call } = options;
45
+ const { signal, ...call } = called;
43
46
  const fields = (input ?? {});
44
47
  try {
45
48
  const { route, rest: others } = placed(operation, rest.path, fields);
@@ -50,7 +53,7 @@ export function restOperations(transport) {
50
53
  : readRoute(transport, asked, signal, call);
51
54
  }
52
55
  const sent = rest.method === "DELETE" ? others : fields;
53
- return transport.send(rest.method, route, sent, signal, call);
56
+ return announced(changes, operation, fields, () => transport.send(rest.method, route, sent, signal, call));
54
57
  }
55
58
  catch (error) {
56
59
  return Promise.reject(error);
@@ -1,4 +1,3 @@
1
- import type { ChangeFeed } from "../domain/changes.js";
2
1
  import { type Decoder, type Transport } from "../domain/transport.js";
3
2
  import { type Criteria } from "@sincpro/criteria";
4
3
  /**
@@ -17,9 +16,8 @@ import { type Criteria } from "@sincpro/criteria";
17
16
  * body. Sent in a body, `dry_run` is lost and a rehearsal would run the real delete, and so is
18
17
  * `origin_version`, so a stale delete would go through.
19
18
  *
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
19
+ * The fields reach `send` as its `body` whatever the method, and `send` gets the path already
20
+ * filled, so leaving out a key the path took is the caller's, who knows the template: `restOperations` does, and an entity's `delete` puts its key
23
21
  * only in the path. A key that travels in both is harmless: the wire reads a `DELETE` as
24
22
  * `{**query, **path_params}` — the path wins — and looks for misplaced fields only in its body.
25
23
  *
@@ -63,9 +61,6 @@ export type RestOptions = {
63
61
  /** Called once per model and version the server answers that the snapshot does not hold —
64
62
  * never an error: the screen keeps working on the live definition. */
65
63
  onVersionDrift?: (drift: VersionDrift) => void;
66
- /** Where each write is announced (`Change`): the process's feed (`processChanges`) by default,
67
- * so a cache or a screen hears it whoever wrote — a screen, or a use case's client. */
68
- changes?: ChangeFeed;
69
64
  /**
70
65
  * What an upload is sent with, to tell its progress: the platform's `XMLHttpRequest` by default
71
66
  * (a browser, React Native). `null`, or a platform with none: it goes over `fetch`, and its
@@ -1,12 +1,10 @@
1
+ import { RecordNotAnswered, recordOf } from "../domain/answer.js";
1
2
  import { TransportError, } from "../domain/transport.js";
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
5
  import { valuesOf } from "../../common/serialization.js";
6
- import { readProblem, } from "@sincpro/criteria";
6
+ import { readProblem } from "@sincpro/criteria";
7
7
  import { RestError, restSource } from "@sincpro/criteria/rest";
8
- /** The keys of an answer that are not the record: what is left is the record's own field. */
9
- const ENVELOPE = new Set(["cursor", "count", "model_version", "dropped", "draft", "missing"]);
10
8
  /**
11
9
  * The name a `Content-Disposition` says to save a file as; `""` when it says none. The encoded
12
10
  * form (`filename*=UTF-8'es'informe%20final.parquet`, RFC 5987) wins, its charset and language
@@ -112,7 +110,6 @@ function documentUrl(base, document) {
112
110
  export function restTransport(options = {}) {
113
111
  const base = options.base ?? "";
114
112
  const doFetch = options.fetch ?? ((input, init) => fetch(input, init));
115
- const changes = options.changes ?? processChanges;
116
113
  const xhr = options.xmlHttpRequest === undefined
117
114
  ? globalThis.XMLHttpRequest
118
115
  : (options.xmlHttpRequest ?? undefined);
@@ -318,23 +315,20 @@ export function restTransport(options = {}) {
318
315
  },
319
316
  async one(route, signal, call) {
320
317
  const answer = (await get(route, urlOf(base, route), signal, undefined, call));
321
- const own = Object.keys(answer).filter((key) => !ENVELOPE.has(key));
322
- const [key] = own;
323
- if (own.length !== 1 || key === undefined)
324
- throw new TransportError(route, 200, null, `${route} answered ${own.length} fields of its own (${own.join(", ")}); a record answers one`);
325
- return {
326
- record: answer[key],
327
- meta: await definitionAt(urlOf(base, route), "get", answer["model_version"]),
328
- dropped: answer["dropped"] ?? [],
329
- draft: answer["draft"] ?? null,
330
- };
318
+ const meta = await definitionAt(urlOf(base, route), "get", answer["model_version"]);
319
+ try {
320
+ return recordOf(answer, meta, route);
321
+ }
322
+ catch (error) {
323
+ if (error instanceof RecordNotAnswered)
324
+ throw new TransportError(route, 200, null, error.message);
325
+ throw error;
326
+ }
331
327
  },
332
328
  async send(method, route, body, signal, sending) {
333
329
  // A DELETE is read from its URL: its fields go in the query, and it has no body.
334
330
  const url = urlOf(base, FROM_URL.has(method) ? withQuery(route, queryOf(urlFieldsOf(route, body))) : route);
335
- return announced(changes, method, route, body, async () => {
336
- return (await get(route, url, signal, { method, body, key: sending?.idempotencyKey }, sending));
337
- });
331
+ return (await get(route, url, signal, { method, body, key: sending?.idempotencyKey }, sending));
338
332
  },
339
333
  });
340
334
  }
@@ -0,0 +1,28 @@
1
+ import type { One } from "./transport.js";
2
+ import type { Meta } from "@sincpro/criteria";
3
+ /**
4
+ * The keys of sincpro_framework's answer that are not its record: the envelope. What is left is
5
+ * the record's own field, named after its model (`issue`, `issues`).
6
+ */
7
+ export declare const ENVELOPE_KEYS: readonly ["cursor", "count", "model_version", "dropped", "draft", "missing"];
8
+ export type EnvelopeKey = (typeof ENVELOPE_KEYS)[number];
9
+ /** An answer that is not the one record it was asked for: none, or more than one field of its own. */
10
+ export declare class RecordNotAnswered extends Error {
11
+ readonly from: string;
12
+ readonly own: readonly string[];
13
+ static readonly failureKind = "unknown_outcome";
14
+ constructor(from: string, own: readonly string[]);
15
+ }
16
+ /** The fields of an answer that are not its envelope. */
17
+ export declare function ownFieldsOf(answer: Readonly<Record<string, unknown>>): string[];
18
+ /**
19
+ * One record as a `Get` answers it — the one field of its own, beside the envelope —, with the
20
+ * definition of its model, what was dropped of it and the caller's draft.
21
+ *
22
+ * recordOf({ issue: { id: "i1" }, model_version: "3", draft: null }, meta, "project.QueryGetIssue")
23
+ * -> { record: { id: "i1" }, meta, dropped: [], draft: null }
24
+ *
25
+ * Context: the same reading over every wire — REST's `one`, an entity's `get` over any
26
+ * `OperationTransport` —, as criteria's `pageFrom` reads a page whatever brought it.
27
+ */
28
+ export declare function recordOf<T>(answer: unknown, meta: Meta | null, from: string): One<T>;
@@ -0,0 +1,52 @@
1
+ /**
2
+ * The keys of sincpro_framework's answer that are not its record: the envelope. What is left is
3
+ * the record's own field, named after its model (`issue`, `issues`).
4
+ */
5
+ export const ENVELOPE_KEYS = [
6
+ "cursor",
7
+ "count",
8
+ "model_version",
9
+ "dropped",
10
+ "draft",
11
+ "missing",
12
+ ];
13
+ const ENVELOPE = new Set(ENVELOPE_KEYS);
14
+ /** An answer that is not the one record it was asked for: none, or more than one field of its own. */
15
+ export class RecordNotAnswered extends Error {
16
+ from;
17
+ own;
18
+ static failureKind = "unknown_outcome";
19
+ constructor(from, own) {
20
+ super(`${from} answered ${own.length} fields of its own (${own.join(", ")}); a record answers one`);
21
+ this.from = from;
22
+ this.own = own;
23
+ this.name = "RecordNotAnswered";
24
+ }
25
+ }
26
+ /** The fields of an answer that are not its envelope. */
27
+ export function ownFieldsOf(answer) {
28
+ return Object.keys(answer).filter((key) => !ENVELOPE.has(key));
29
+ }
30
+ /**
31
+ * One record as a `Get` answers it — the one field of its own, beside the envelope —, with the
32
+ * definition of its model, what was dropped of it and the caller's draft.
33
+ *
34
+ * recordOf({ issue: { id: "i1" }, model_version: "3", draft: null }, meta, "project.QueryGetIssue")
35
+ * -> { record: { id: "i1" }, meta, dropped: [], draft: null }
36
+ *
37
+ * Context: the same reading over every wire — REST's `one`, an entity's `get` over any
38
+ * `OperationTransport` —, as criteria's `pageFrom` reads a page whatever brought it.
39
+ */
40
+ export function recordOf(answer, meta, from) {
41
+ const fields = (answer ?? {});
42
+ const own = ownFieldsOf(fields);
43
+ const [key] = own;
44
+ if (own.length !== 1 || key === undefined)
45
+ throw new RecordNotAnswered(from, own);
46
+ return {
47
+ record: fields[key],
48
+ meta,
49
+ dropped: fields["dropped"] ?? [],
50
+ draft: fields["draft"] ?? null,
51
+ };
52
+ }
@@ -1,28 +1,33 @@
1
- import type { Method } from "./transport.js";
1
+ import type { RemoteOperation } from "./operation.js";
2
2
  /**
3
- * A write that may have changed what was read: how it was sent and where.
3
+ * A write that may have changed what was read: the operation that ran, the entities it concerns
4
+ * and the records it named.
4
5
  *
5
6
  * Context: a client keeps what it read — a cache, a list on screen, a record, a name in a cell —
6
7
  * and every one of them goes stale the moment a write lands, whoever wrote: a screen's save, a use
7
- * case's Command, a second panel. Each used to learn it only when the write went through the same
8
- * object it read through, so a use case writing over its generated client left every screen
9
- * reading the old answer. A change is the fact every reader can hear instead (`ChangeFeed`). It
10
- * says only what was written — what that touches (the collections a route names, a record's
11
- * history) is each reader's to decide, as a cache and a screen decide differently.
8
+ * case's Command, a second panel. A change is the fact every reader can hear (`ChangeFeed`). It
9
+ * is said in the contract's words, never a route's: the same over REST, JSON-RPC or gRPC, so a
10
+ * reader keyed by entity forgets exactly what the write touched — the records it named (`keys`),
11
+ * or every reading of the entity when it named none (a Command that creates). What that touches
12
+ * is each reader's to decide, as a cache and a screen decide differently.
12
13
  *
13
- * { method: "POST", route: "/issues/odoo-acme-7/messages" }
14
+ * { operation: "shop.CommandPayOrder", entities: ["Order"], keys: { Order: ["O-7"] } }
14
15
  */
15
16
  export type Change = {
16
- readonly method: Method;
17
- /** As the transport received it: a path under its base, maybe with a query. */
18
- readonly route: string;
17
+ /** The operation's identity: `<bounded context>.<Command>`. */
18
+ readonly operation: string;
19
+ /** The entities it concerns, as the contract names them. */
20
+ readonly entities: readonly string[];
21
+ /** The keys of the records it named, by entity (`targets`); an entity it names none of is
22
+ * absent. */
23
+ readonly keys: Readonly<Record<string, readonly string[]>>;
19
24
  };
20
25
  /**
21
26
  * Who hears what was written: one feed the writers announce to and the readers follow, so neither
22
27
  * knows the other.
23
28
  *
24
- * const stop = processChanges.follow((change) => cache.forget(change.route));
25
- * processChanges.announce({ method: "PUT", route: "/projects/odoo-acme/edit" });
29
+ * const stop = processChanges.follow((change) => cache.forget(change.entities));
30
+ * processChanges.announce({ operation: "shop.CommandPayOrder", entities: ["Order"], keys: {} });
26
31
  * stop();
27
32
  *
28
33
  * Context: a listener that fails is ignored — hearing a write never changes its answer, as
@@ -35,11 +40,11 @@ export type ChangeFeed = {
35
40
  follow(listener: (change: Change) => void): () => void;
36
41
  };
37
42
  /**
38
- * The change a `send` makes, or `undefined` when it writes nothing: a preview (`…/preview`) and a
39
- * dry run (`dry_run: true`, sincpro_framework's `DryRunnable`) only say what a write WOULD do.
43
+ * The change running `operation` with `input` makes, or `undefined` when it writes nothing: a
44
+ * Query, a preview (`role: "preview"`) and a dry run (`dry_run: true`, sincpro_framework's
45
+ * `DryRunnable`) only read, or say what a write WOULD do.
40
46
  *
41
- * changeOf("POST", "/issues/7/cancel", {}) -> { method: "POST", route: "/issues/7/cancel" }
42
- * changeOf("POST", "/issues/7/cancel", { dry_run: true }) -> undefined
43
- * changeOf("POST", "/invoices/preview", { lines: [] }) -> undefined
47
+ * changeOf(payOrder, { order_id: "O-7" }) -> { operation, entities: ["Order"], keys: { Order: ["O-7"] } }
48
+ * changeOf(payOrder, { order_id: "O-7", dry_run: true }) -> undefined
44
49
  */
45
- export declare function changeOf(method: Method, route: string, body: unknown): Change | undefined;
50
+ export declare function changeOf(operation: RemoteOperation, input: unknown): Change | undefined;
@@ -1,15 +1,24 @@
1
+ /** The keys a field of the input holds: one, a list of them, or none. */
2
+ function keysIn(value) {
3
+ const all = Array.isArray(value) ? value : [value];
4
+ return all.filter((one) => one !== undefined && one !== null && one !== "").map(String);
5
+ }
1
6
  /**
2
- * The change a `send` makes, or `undefined` when it writes nothing: a preview (`…/preview`) and a
3
- * dry run (`dry_run: true`, sincpro_framework's `DryRunnable`) only say what a write WOULD do.
7
+ * The change running `operation` with `input` makes, or `undefined` when it writes nothing: a
8
+ * Query, a preview (`role: "preview"`) and a dry run (`dry_run: true`, sincpro_framework's
9
+ * `DryRunnable`) only read, or say what a write WOULD do.
4
10
  *
5
- * changeOf("POST", "/issues/7/cancel", {}) -> { method: "POST", route: "/issues/7/cancel" }
6
- * changeOf("POST", "/issues/7/cancel", { dry_run: true }) -> undefined
7
- * changeOf("POST", "/invoices/preview", { lines: [] }) -> undefined
11
+ * changeOf(payOrder, { order_id: "O-7" }) -> { operation, entities: ["Order"], keys: { Order: ["O-7"] } }
12
+ * changeOf(payOrder, { order_id: "O-7", dry_run: true }) -> undefined
8
13
  */
9
- export function changeOf(method, route, body) {
10
- if ((route.split("?")[0] ?? route).endsWith("/preview"))
14
+ export function changeOf(operation, input) {
15
+ if (operation.query || operation.role === "preview")
11
16
  return undefined;
12
- if (typeof body === "object" && body !== null && "dry_run" in body && body.dry_run === true)
17
+ const fields = (input ?? {});
18
+ if (fields["dry_run"] === true)
13
19
  return undefined;
14
- return { method, route };
20
+ const keys = Object.fromEntries(Object.entries(operation.targets)
21
+ .map(([entity, field]) => [entity, keysIn(fields[field])])
22
+ .filter(([, named]) => named.length > 0));
23
+ return { operation: operation.identity, entities: operation.entities, keys };
15
24
  }