@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,26 +1,32 @@
1
1
  /**
2
- * The seam with a server: the `Transport` port and its implementations, entities with typed
3
- * commands, the service's client contract, and how a run's trace and context reach the wire.
2
+ * The seam with a server: the `OperationTransport` port and its wires, the API a contract
3
+ * describes, entities as an index over their operations, the service's client contract, and how a
4
+ * run's trace and context reach the wire. REST's own `Transport` is its wire's.
4
5
  *
5
6
  * domain/ the port and the server's words: `Transport`, `TransportError`, `entity()`, the
6
- * client contract, what a write changed (`Change`, `ChangeFeed`), which records
7
- * were chosen (`Selection`)
7
+ * client contract, how an answer holds its record (`recordOf`), what a write
8
+ * changed (`Change`, `ChangeFeed`), which records were chosen (`Selection`)
8
9
  * adapters/ REST, memory, and Parquet (optional entry `./parquet`)
9
- * services/ discovery, retry keys, propagation (`traceparent`, `sincpro-context`), the
10
+ * services/ the API a description implements (`apiOf`), an entity's page and record over any
11
+ * wire (`readPage`, `readRecord`), discovery, retry keys, propagation
12
+ * (`traceparent`, `sincpro-context`), the
10
13
  * process's change feed (`processChanges`), the keys of a selection (`keysOf`)
11
14
  */
12
15
  export { JsonRpcError, jsonRpcOperations, type JsonRpcOptions, } from "./adapters/json-rpc.js";
13
16
  export { type AssignableOptions, assignableValues, type MemoryOptions, type MemoryTable, memoryTransport, } from "./adapters/memory.js";
14
17
  export { type RestOptions, restTransport, urlOf, type VersionDrift, type XhrConstructor, } from "./adapters/rest.js";
15
18
  export { restOperations } from "./adapters/rest-operations.js";
19
+ export { ENVELOPE_KEYS, type EnvelopeKey, ownFieldsOf, RecordNotAnswered, recordOf, } from "./domain/answer.js";
16
20
  export { type Change, type ChangeFeed, changeOf, } from "./domain/changes.js";
17
21
  export { type ClientContract, entityKeyOf, eventMetasOf, kindOf, metasOf, versionsOf, } from "./domain/contract.js";
18
- export { bodyFor, type CommandAnswers, type CommandBodies, type CommandRoute, type Entity, entity, type EntityRoutes, type ExportFormat, exportRoute, listRoute, NEW_PARAM, pathFor, PICK_PARAM, QUERY_PARAM, type ResourceRef, SCOPE_NAME_PARAM, SCOPE_PARAM, scopeNameOf, scopeOfRef, startingValuesOf, type SummaryFields, type SummaryPart, TRAIL_PARAM, WITH_PARAM, type WriteRoute, writeRoute, } from "./domain/entity.js";
19
- export { OperationNotPublished, type OperationOptions, type OperationTransport, type RemoteOperation, } from "./domain/operation.js";
22
+ export { type Entity, entity, ENTITY_ROLES, type EntityOperations, type EntityOptions, type EntityRole, NEW_PARAM, PICK_PARAM, QUERY_PARAM, type RecordOf, type ResourceRef, SCOPE_NAME_PARAM, SCOPE_PARAM, scopeNameOf, scopeOfRef, startingValuesOf, type SummaryFields, type SummaryPart, targeted, TRAIL_PARAM, WITH_PARAM, } from "./domain/entity.js";
23
+ export { type Operation, operation, OperationNotPublished, type OperationOptions, type OperationTransport, type RemoteOperation, } from "./domain/operation.js";
20
24
  export { type AllMatching, allMatching, chosen, type ChosenKeys, criteriaOf, isChosen, isEmpty, NOTHING_CHOSEN, type Selection, SelectionTooLarge, sizeOf, toggled, } from "./domain/selection.js";
21
25
  export { type CallOptions, type Decoder, type Download, downloadRoute, type Draft, markedAs, type Method, type One, readRoute, SCREENS, type SendOptions, type Transport, TransportError, type Uploaded, type UploadOptions, type UploadProgress, uploadTo, } from "./domain/transport.js";
26
+ export { type ApiDescription, type ApiOf, apiOf, type Call, type Overrides, } from "./services/api.js";
22
27
  export { announced, changeFeed, processChanges, } from "./services/changes.js";
23
28
  export { guardedTransport } from "./services/guarded.js";
24
29
  export { bindable, bindableOperations, bindableTransport, bindOperations, bindTransport, headersOf, } from "./services/propagation.js";
30
+ export { type ReadOptions, readPage, readRecord, } from "./services/reads.js";
25
31
  export { type RetryKeys, retryKeys, unanswered, } from "./services/retry.js";
26
32
  export { keysOf } from "./services/selection.js";
@@ -1,26 +1,32 @@
1
1
  /**
2
- * The seam with a server: the `Transport` port and its implementations, entities with typed
3
- * commands, the service's client contract, and how a run's trace and context reach the wire.
2
+ * The seam with a server: the `OperationTransport` port and its wires, the API a contract
3
+ * describes, entities as an index over their operations, the service's client contract, and how a
4
+ * run's trace and context reach the wire. REST's own `Transport` is its wire's.
4
5
  *
5
6
  * domain/ the port and the server's words: `Transport`, `TransportError`, `entity()`, the
6
- * client contract, what a write changed (`Change`, `ChangeFeed`), which records
7
- * were chosen (`Selection`)
7
+ * client contract, how an answer holds its record (`recordOf`), what a write
8
+ * changed (`Change`, `ChangeFeed`), which records were chosen (`Selection`)
8
9
  * adapters/ REST, memory, and Parquet (optional entry `./parquet`)
9
- * services/ discovery, retry keys, propagation (`traceparent`, `sincpro-context`), the
10
+ * services/ the API a description implements (`apiOf`), an entity's page and record over any
11
+ * wire (`readPage`, `readRecord`), discovery, retry keys, propagation
12
+ * (`traceparent`, `sincpro-context`), the
10
13
  * process's change feed (`processChanges`), the keys of a selection (`keysOf`)
11
14
  */
12
15
  export { JsonRpcError, jsonRpcOperations, } from "./adapters/json-rpc.js";
13
16
  export { assignableValues, memoryTransport, } from "./adapters/memory.js";
14
17
  export { restTransport, urlOf, } from "./adapters/rest.js";
15
18
  export { restOperations } from "./adapters/rest-operations.js";
19
+ export { ENVELOPE_KEYS, ownFieldsOf, RecordNotAnswered, recordOf, } from "./domain/answer.js";
16
20
  export { changeOf, } from "./domain/changes.js";
17
21
  export { entityKeyOf, eventMetasOf, kindOf, metasOf, versionsOf, } from "./domain/contract.js";
18
- export { bodyFor, entity, exportRoute, listRoute, NEW_PARAM, pathFor, PICK_PARAM, QUERY_PARAM, SCOPE_NAME_PARAM, SCOPE_PARAM, scopeNameOf, scopeOfRef, startingValuesOf, TRAIL_PARAM, WITH_PARAM, writeRoute, } from "./domain/entity.js";
19
- export { OperationNotPublished, } from "./domain/operation.js";
22
+ export { entity, ENTITY_ROLES, NEW_PARAM, PICK_PARAM, QUERY_PARAM, SCOPE_NAME_PARAM, SCOPE_PARAM, scopeNameOf, scopeOfRef, startingValuesOf, targeted, TRAIL_PARAM, WITH_PARAM, } from "./domain/entity.js";
23
+ export { operation, OperationNotPublished, } from "./domain/operation.js";
20
24
  export { allMatching, chosen, criteriaOf, isChosen, isEmpty, NOTHING_CHOSEN, SelectionTooLarge, sizeOf, toggled, } from "./domain/selection.js";
21
25
  export { downloadRoute, markedAs, readRoute, SCREENS, TransportError, uploadTo, } from "./domain/transport.js";
26
+ export { apiOf, } from "./services/api.js";
22
27
  export { announced, changeFeed, processChanges, } from "./services/changes.js";
23
28
  export { guardedTransport } from "./services/guarded.js";
24
29
  export { bindable, bindableOperations, bindableTransport, bindOperations, bindTransport, headersOf, } from "./services/propagation.js";
30
+ export { readPage, readRecord, } from "./services/reads.js";
25
31
  export { retryKeys, unanswered, } from "./services/retry.js";
26
32
  export { keysOf } from "./services/selection.js";
@@ -0,0 +1,39 @@
1
+ import type { Operation, OperationOptions, OperationTransport } from "../domain/operation.js";
2
+ /**
3
+ * The API of a bounded context as its contract describes it: operations, grouped by the entity
4
+ * they work on — `issue.cancel`, `project.edit` — and those of no entity at the top. What a
5
+ * generated `api` is.
6
+ */
7
+ export type ApiDescription = {
8
+ readonly [name: string]: Operation | ApiDescription;
9
+ };
10
+ /** One method of an API: the operation's input — optional when nothing of it is required — and
11
+ * the call's options, answering the operation's answer. */
12
+ export type Call<I, O> = undefined extends I ? (input?: I, options?: OperationOptions) => Promise<O> : (input: I, options?: OperationOptions) => Promise<O>;
13
+ /** The methods a description implements: the same groups, each operation a `Call`. */
14
+ export type ApiOf<D> = {
15
+ readonly [K in keyof D]: D[K] extends Operation<infer I, infer O> ? Call<I, O> : ApiOf<D[K]>;
16
+ };
17
+ /**
18
+ * What an application changes of an API: a method replaced by one that receives the original and
19
+ * answers what is called instead — the same signature, so whoever uses the API cannot tell.
20
+ *
21
+ * clientOf(transport, { issue: { get: (original) => (input, options) => cached(input) ?? original(input, options) } })
22
+ */
23
+ export type Overrides<A> = {
24
+ readonly [K in keyof A]?: A[K] extends (...args: never[]) => unknown ? (original: A[K]) => A[K] : Overrides<A[K]>;
25
+ };
26
+ /**
27
+ * The API a description declares, implemented over a transport: each operation a method that
28
+ * calls it by who it is, over whatever wire the transport speaks.
29
+ *
30
+ * const project = apiOf(api, restOperations(restTransport({ base: "/v1" })));
31
+ * await project.issue.cancel({ issue_id: "acme-3" });
32
+ *
33
+ * Context: the contract already says everything a method does — its input, its answer, the field
34
+ * an edit mints —, so no method is written, generated or by hand: the generator writes the
35
+ * description and its types, and this implements it. Injected into a bounded context, it is
36
+ * built again over the transport bound to each run (`bindable`), overrides included: a replaced
37
+ * method's original carries the run's trace, context and signal too.
38
+ */
39
+ export declare function apiOf<D extends ApiDescription>(description: D, transport: OperationTransport, overrides?: Overrides<ApiOf<D>>): ApiOf<D>;
@@ -0,0 +1,38 @@
1
+ import { bindable } from "./propagation.js";
2
+ import { newEntityId } from "../../common/ids.js";
3
+ function isOperation(entry) {
4
+ return typeof entry.identity === "string";
5
+ }
6
+ function callOf(operation, transport) {
7
+ const field = operation.mints;
8
+ return (input, options) => {
9
+ const given = (input ?? {});
10
+ const sent = field === undefined ? given : { ...given, [field]: given[field] || newEntityId() };
11
+ return transport.call(operation, sent, options);
12
+ };
13
+ }
14
+ function built(description, transport, overrides) {
15
+ return Object.fromEntries(Object.entries(description).map(([name, entry]) => {
16
+ const own = overrides?.[name];
17
+ if (!isOperation(entry))
18
+ return [name, built(entry, transport, own)];
19
+ const call = callOf(entry, transport);
20
+ return [name, own === undefined ? call : own(call)];
21
+ }));
22
+ }
23
+ /**
24
+ * The API a description declares, implemented over a transport: each operation a method that
25
+ * calls it by who it is, over whatever wire the transport speaks.
26
+ *
27
+ * const project = apiOf(api, restOperations(restTransport({ base: "/v1" })));
28
+ * await project.issue.cancel({ issue_id: "acme-3" });
29
+ *
30
+ * Context: the contract already says everything a method does — its input, its answer, the field
31
+ * an edit mints —, so no method is written, generated or by hand: the generator writes the
32
+ * description and its types, and this implements it. Injected into a bounded context, it is
33
+ * built again over the transport bound to each run (`bindable`), overrides included: a replaced
34
+ * method's original carries the run's trace, context and signal too.
35
+ */
36
+ export function apiOf(description, transport, overrides) {
37
+ return bindable(transport, (wire) => built(description, wire, overrides));
38
+ }
@@ -1,20 +1,20 @@
1
1
  import { type ChangeFeed } from "../domain/changes.js";
2
- import type { Method } from "../domain/transport.js";
2
+ import type { RemoteOperation } from "../domain/operation.js";
3
3
  /**
4
4
  * A feed in the process's memory: every listener hears every change announced while it follows.
5
5
  *
6
6
  * const changes = changeFeed();
7
- * const stop = changes.follow((change) => console.log(change.route));
7
+ * const stop = changes.follow((change) => console.log(change.operation));
8
8
  *
9
- * Context: one is enough for an application — `processChanges`, which every transport of this
10
- * package announces to by default —; another is for a test, or a transport that must not be heard.
9
+ * Context: one is enough for an application — `processChanges`, which every operation adapter of
10
+ * this package announces to by default —; another is for a test, or a transport that must not be heard.
11
11
  * A listener added while a change is being announced hears the next one, not that one.
12
12
  */
13
13
  export declare function changeFeed(): ChangeFeed;
14
14
  /**
15
- * What the process wrote, for whoever keeps what it read: the feed this package's transports
16
- * (`restTransport`, `memoryTransport`) announce every write to unless told otherwise, and the one
17
- * a platform's caches and screens follow.
15
+ * What the process wrote, for whoever keeps what it read: the feed this package's operation
16
+ * adapters (`restOperations`, `jsonRpcOperations`) announce every write to unless told otherwise,
17
+ * and the one a platform's caches and screens follow.
18
18
  *
19
19
  * processChanges.follow((change) => …);
20
20
  *
@@ -23,10 +23,11 @@ export declare function changeFeed(): ChangeFeed;
23
23
  */
24
24
  export declare const processChanges: ChangeFeed;
25
25
  /**
26
- * `write` — a transport's `send` —, and then its change announced on `feed`, whatever it answered:
27
- * a refusal may have changed something too (a 409 says someone else did), and a request with no
28
- * answer may have run. A preview or a dry run announces nothing (`changeOf`).
26
+ * `call` — an operation adapter's request —, and then its change announced on `feed`, whatever it
27
+ * answered: a refusal may have changed something too (a 409 says someone else did), and a request
28
+ * with no answer may have run. A Query, a preview or a dry run announces nothing (`changeOf`). A
29
+ * platform's adapter (gRPC) announces through it as this package's do.
29
30
  *
30
- * send: (method, route, body) => announced(changes, method, route, body, () => post(…)),
31
+ * call: (operation, input, options) => announced(changes, operation, input, () => send(…)),
31
32
  */
32
- export declare function announced<T>(feed: ChangeFeed, method: Method, route: string, body: unknown, write: () => Promise<T>): Promise<T>;
33
+ export declare function announced<T>(feed: ChangeFeed, operation: RemoteOperation, input: unknown, call: () => Promise<T>): Promise<T>;
@@ -3,10 +3,10 @@ import { changeOf, } from "../domain/changes.js";
3
3
  * A feed in the process's memory: every listener hears every change announced while it follows.
4
4
  *
5
5
  * const changes = changeFeed();
6
- * const stop = changes.follow((change) => console.log(change.route));
6
+ * const stop = changes.follow((change) => console.log(change.operation));
7
7
  *
8
- * Context: one is enough for an application — `processChanges`, which every transport of this
9
- * package announces to by default —; another is for a test, or a transport that must not be heard.
8
+ * Context: one is enough for an application — `processChanges`, which every operation adapter of
9
+ * this package announces to by default —; another is for a test, or a transport that must not be heard.
10
10
  * A listener added while a change is being announced hears the next one, not that one.
11
11
  */
12
12
  export function changeFeed() {
@@ -31,9 +31,9 @@ export function changeFeed() {
31
31
  };
32
32
  }
33
33
  /**
34
- * What the process wrote, for whoever keeps what it read: the feed this package's transports
35
- * (`restTransport`, `memoryTransport`) announce every write to unless told otherwise, and the one
36
- * a platform's caches and screens follow.
34
+ * What the process wrote, for whoever keeps what it read: the feed this package's operation
35
+ * adapters (`restOperations`, `jsonRpcOperations`) announce every write to unless told otherwise,
36
+ * and the one a platform's caches and screens follow.
37
37
  *
38
38
  * processChanges.follow((change) => …);
39
39
  *
@@ -42,18 +42,19 @@ export function changeFeed() {
42
42
  */
43
43
  export const processChanges = changeFeed();
44
44
  /**
45
- * `write` — a transport's `send` —, and then its change announced on `feed`, whatever it answered:
46
- * a refusal may have changed something too (a 409 says someone else did), and a request with no
47
- * answer may have run. A preview or a dry run announces nothing (`changeOf`).
45
+ * `call` — an operation adapter's request —, and then its change announced on `feed`, whatever it
46
+ * answered: a refusal may have changed something too (a 409 says someone else did), and a request
47
+ * with no answer may have run. A Query, a preview or a dry run announces nothing (`changeOf`). A
48
+ * platform's adapter (gRPC) announces through it as this package's do.
48
49
  *
49
- * send: (method, route, body) => announced(changes, method, route, body, () => post(…)),
50
+ * call: (operation, input, options) => announced(changes, operation, input, () => send(…)),
50
51
  */
51
- export async function announced(feed, method, route, body, write) {
52
+ export async function announced(feed, operation, input, call) {
52
53
  try {
53
- return await write();
54
+ return await call();
54
55
  }
55
56
  finally {
56
- const change = changeOf(method, route, body);
57
+ const change = changeOf(operation, input);
57
58
  if (change !== undefined)
58
59
  feed.announce(change);
59
60
  }
@@ -3,9 +3,9 @@ import { markedAs, } from "../domain/transport.js";
3
3
  import { bindableOperations, bindableTransport, } from "./propagation.js";
4
4
  export function guardedTransport(transport, breaker) {
5
5
  if (isOperationTransport(transport))
6
- return bindableOperations({
6
+ return bindableOperations(markedAs({
7
7
  call: (operation, input, options) => breaker.call(() => transport.call(operation, input, options)),
8
- });
8
+ }, transport));
9
9
  const guarded = {
10
10
  list: (route, criteria, signal, options) => breaker.call(() => transport.list(route, criteria, signal, options)),
11
11
  one: (route, signal, options) => breaker.call(() => transport.one(route, signal, options)),
@@ -20,5 +20,5 @@ export function guardedTransport(transport, breaker) {
20
20
  const upload = transport.upload;
21
21
  if (upload !== undefined)
22
22
  guarded.upload = (link, file, signal, options) => breaker.call(() => upload.call(transport, link, file, signal, options));
23
- return bindableTransport(markedAs(guarded, transport));
23
+ return bindableTransport(guarded);
24
24
  }
@@ -18,8 +18,8 @@ export declare function bindTransport(transport: Transport, execution: Execution
18
18
  export declare function bindOperations(transport: OperationTransport, execution: Execution): OperationTransport;
19
19
  /**
20
20
  * A REST `Transport` a run binds to itself when it is a dependency: its requests carry the run's
21
- * trace, context and signal. `restTransport`, `memoryTransport`, `guardedTransport` and
22
- * `screensTransport` answer one; a platform's own marks itself with it.
21
+ * trace, context and signal. `restTransport`, `memoryTransport` and `guardedTransport` answer
22
+ * one; a platform's own marks itself with it.
23
23
  *
24
24
  * Context: a run binds only what says it can be bound — never an object because it has `list`,
25
25
  * `one` and `send`: an application's own object with those names is the application's.
@@ -27,8 +27,8 @@ export declare function bindOperations(transport: OperationTransport, execution:
27
27
  export declare function bindableTransport<T extends Transport>(transport: T): T;
28
28
  /**
29
29
  * An `OperationTransport` a run binds to itself when it is a dependency: its `call` carries the
30
- * run's trace, context and signal. `restOperations`, `jsonRpcOperations` and `guardedTransport`
31
- * answer one; a platform's own (gRPC) marks itself with it.
30
+ * run's trace, context and signal. `restOperations`, `jsonRpcOperations`, `guardedTransport` and
31
+ * `screensOperations` answer one; a platform's own (gRPC) marks itself with it.
32
32
  *
33
33
  * boundedContext.addDependency("operations", bindableOperations(grpcOperations(channel)));
34
34
  *
@@ -41,14 +41,12 @@ export declare function bindableOperations<T extends OperationTransport>(transpo
41
41
  export declare function bound<W extends Transport | OperationTransport>(wire: W, execution: Execution): W;
42
42
  /**
43
43
  * A client made over a transport, made again over the transport bound to each execution it is
44
- * used in. What a generated `clientOf(operations)` answers, so a Feature that calls
45
- * `this.client.sendIssue(…)` sends that request with its own trace, context and signal.
44
+ * used in. What `apiOf` — and so a generated `clientOf(operations)` — answers, so a Feature that
45
+ * calls `this.client.issue.send(…)` sends that request with its own trace, context and signal.
46
46
  *
47
- * export function clientOf(operations: OperationTransport) {
48
- * return bindable(operations, (operations) => ({
49
- * sendIssue: (input) => operations.call(operations_.CommandSendIssue, input),
50
- * }));
51
- * }
47
+ * bindable(operations, (operations) => ({
48
+ * send: (input) => operations.call(sendIssue, input),
49
+ * }));
52
50
  *
53
51
  * Context: it takes the function that makes the client, not the client — rebuilding through
54
52
  * `clientOf` itself would make TypeScript infer `clientOf` from its own return. A REST
@@ -103,8 +103,8 @@ export function bindOperations(transport, execution) {
103
103
  }
104
104
  /**
105
105
  * A REST `Transport` a run binds to itself when it is a dependency: its requests carry the run's
106
- * trace, context and signal. `restTransport`, `memoryTransport`, `guardedTransport` and
107
- * `screensTransport` answer one; a platform's own marks itself with it.
106
+ * trace, context and signal. `restTransport`, `memoryTransport` and `guardedTransport` answer
107
+ * one; a platform's own marks itself with it.
108
108
  *
109
109
  * Context: a run binds only what says it can be bound — never an object because it has `list`,
110
110
  * `one` and `send`: an application's own object with those names is the application's.
@@ -119,8 +119,8 @@ export function bindableTransport(transport) {
119
119
  }
120
120
  /**
121
121
  * An `OperationTransport` a run binds to itself when it is a dependency: its `call` carries the
122
- * run's trace, context and signal. `restOperations`, `jsonRpcOperations` and `guardedTransport`
123
- * answer one; a platform's own (gRPC) marks itself with it.
122
+ * run's trace, context and signal. `restOperations`, `jsonRpcOperations`, `guardedTransport` and
123
+ * `screensOperations` answer one; a platform's own (gRPC) marks itself with it.
124
124
  *
125
125
  * boundedContext.addDependency("operations", bindableOperations(grpcOperations(channel)));
126
126
  *
@@ -144,14 +144,12 @@ export function bound(wire, execution) {
144
144
  }
145
145
  /**
146
146
  * A client made over a transport, made again over the transport bound to each execution it is
147
- * used in. What a generated `clientOf(operations)` answers, so a Feature that calls
148
- * `this.client.sendIssue(…)` sends that request with its own trace, context and signal.
147
+ * used in. What `apiOf` — and so a generated `clientOf(operations)` — answers, so a Feature that
148
+ * calls `this.client.issue.send(…)` sends that request with its own trace, context and signal.
149
149
  *
150
- * export function clientOf(operations: OperationTransport) {
151
- * return bindable(operations, (operations) => ({
152
- * sendIssue: (input) => operations.call(operations_.CommandSendIssue, input),
153
- * }));
154
- * }
150
+ * bindable(operations, (operations) => ({
151
+ * send: (input) => operations.call(sendIssue, input),
152
+ * }));
155
153
  *
156
154
  * Context: it takes the function that makes the client, not the client — rebuilding through
157
155
  * `clientOf` itself would make TypeScript infer `clientOf` from its own return. A REST
@@ -0,0 +1,38 @@
1
+ import { type Entity } from "../domain/entity.js";
2
+ import type { OperationOptions, OperationTransport } from "../domain/operation.js";
3
+ import type { One } from "../domain/transport.js";
4
+ import { type AnyRecord, type CriteriaLike, type Meta, type Page } from "@sincpro/criteria";
5
+ /**
6
+ * What a reading carries besides its question: the call's options, and the definition of the
7
+ * entity's model — the contract's (`metasOf`) or one a platform keeps —, since an answer says only
8
+ * which version of it it is (`model_version`).
9
+ */
10
+ export type ReadOptions = OperationOptions & {
11
+ meta?: Meta | null;
12
+ };
13
+ /**
14
+ * A page of an entity's records under `criteria`, through its `search` — grouped when the
15
+ * criteria groups, as sincpro_framework's `Search` answers both on the same operation —, over
16
+ * whatever wire `transport` speaks.
17
+ *
18
+ * const page = await readPage(operations, issues, criteria<Issue>().limit(20));
19
+ * page.rows -> the issues; page.cursor -> where the next page starts (`resumingFrom`)
20
+ *
21
+ * Context: a search's input is its criteria as plain JSON (`{ criteria }`), on every wire: REST
22
+ * writes it in `?criteria=`, JSON-RPC as its params, as criteria's own sources do. Its answer is
23
+ * read as `pageFrom` reads any page — its one field of records, beside the envelope.
24
+ */
25
+ export declare function readPage<T extends AnyRecord>(transport: OperationTransport, entity: Entity<T>, criteria: CriteriaLike<T>, options?: ReadOptions): Promise<Page<T>>;
26
+ /**
27
+ * One record of an entity, through its `get`: the record named in the field its operation names
28
+ * for the model (`targeted`), with the caller's draft when the entity keeps them (`drafts`).
29
+ *
30
+ * const { record, draft } = await readRecord(operations, projects, "acme");
31
+ *
32
+ * 1. Its `get`; an entity without one cannot open a record — a `TypeError`, a contract that does
33
+ * not publish it.
34
+ * 2. Its input: the id where `get` names the record, and `draft: true` when the entity is edited
35
+ * through `Edit` (sincpro_framework answers the caller's draft only when asked).
36
+ * Final: the one field of its own the answer has (`recordOf`), with its envelope.
37
+ */
38
+ export declare function readRecord<T extends AnyRecord>(transport: OperationTransport, entity: Entity<T>, id: string, options?: ReadOptions): Promise<One<T>>;
@@ -0,0 +1,46 @@
1
+ import { recordOf } from "../domain/answer.js";
2
+ import { targeted } from "../domain/entity.js";
3
+ import { pageFrom, plain, } from "@sincpro/criteria";
4
+ /** Separated from the call's options: the definition never travels. */
5
+ function split(options) {
6
+ const { meta = null, ...call } = options;
7
+ return { meta, call };
8
+ }
9
+ /**
10
+ * A page of an entity's records under `criteria`, through its `search` — grouped when the
11
+ * criteria groups, as sincpro_framework's `Search` answers both on the same operation —, over
12
+ * whatever wire `transport` speaks.
13
+ *
14
+ * const page = await readPage(operations, issues, criteria<Issue>().limit(20));
15
+ * page.rows -> the issues; page.cursor -> where the next page starts (`resumingFrom`)
16
+ *
17
+ * Context: a search's input is its criteria as plain JSON (`{ criteria }`), on every wire: REST
18
+ * writes it in `?criteria=`, JSON-RPC as its params, as criteria's own sources do. Its answer is
19
+ * read as `pageFrom` reads any page — its one field of records, beside the envelope.
20
+ */
21
+ export async function readPage(transport, entity, criteria, options = {}) {
22
+ const { meta, call } = split(options);
23
+ const answer = await transport.call(entity.operations.search, { criteria: plain(criteria) }, call);
24
+ return pageFrom(answer, meta);
25
+ }
26
+ /**
27
+ * One record of an entity, through its `get`: the record named in the field its operation names
28
+ * for the model (`targeted`), with the caller's draft when the entity keeps them (`drafts`).
29
+ *
30
+ * const { record, draft } = await readRecord(operations, projects, "acme");
31
+ *
32
+ * 1. Its `get`; an entity without one cannot open a record — a `TypeError`, a contract that does
33
+ * not publish it.
34
+ * 2. Its input: the id where `get` names the record, and `draft: true` when the entity is edited
35
+ * through `Edit` (sincpro_framework answers the caller's draft only when asked).
36
+ * Final: the one field of its own the answer has (`recordOf`), with its envelope.
37
+ */
38
+ export async function readRecord(transport, entity, id, options = {}) {
39
+ const get = entity.operations.get;
40
+ if (get === undefined)
41
+ throw new TypeError(`${entity.key} has no get: no record is opened`);
42
+ const { meta, call } = split(options);
43
+ const input = targeted(entity, get, id, entity.drafts ? { draft: true } : {});
44
+ const answer = await transport.call(get, input, call);
45
+ return recordOf(answer, meta, get.identity);
46
+ }
@@ -1,10 +1,11 @@
1
+ import type { Entity } from "../domain/entity.js";
2
+ import type { OperationTransport } from "../domain/operation.js";
1
3
  import { type Selection } from "../domain/selection.js";
2
- import type { Transport } from "../domain/transport.js";
3
4
  /**
4
5
  * The keys of the chosen records — what a Command run once per record needs, what a many2many
5
6
  * stores —, never more than `limit`.
6
7
  *
7
- * await keysOf(chosen(["F-1", "F-7"]), { transport, route: "/invoices", identity: "number", limit: 200 })
8
+ * await keysOf(chosen(["F-1", "F-7"]), { transport, entity: invoices, identity: "number", limit: 200 })
8
9
  * -> ["F-1", "F-7"]
9
10
  * await keysOf(allMatching(ofSincpro, 37), { …, identity: "id", limit: 200 })
10
11
  * -> the 37 keys, read as one page of references
@@ -17,13 +18,13 @@ import type { Transport } from "../domain/transport.js";
17
18
  * 1. A size already known past the ceiling is refused before anything is read.
18
19
  * 2. Keys are in hand: they are the answer.
19
20
  * 3. Else one page of references for exactly the chosen records (`criteriaOf`), one more than the
20
- * ceiling — the one more says the ceiling was passed.
21
+ * ceiling — the one more says the ceiling was passed —, through the entity's `search`.
21
22
  * Final: the keys, in the order the server answered them.
22
23
  */
23
24
  export declare function keysOf<T>(selection: Selection<T>, options: {
24
- transport: Transport;
25
- /** Where the entity is listed: `listRoute(entity.list)`. */
26
- route: string;
25
+ transport: OperationTransport;
26
+ /** Whose records they are: read through its `search`. */
27
+ entity: Entity;
27
28
  /** The field the keys are of: the model's `get_id`, else its `identity`. */
28
29
  identity: string;
29
30
  /** The most keys the caller takes at once. */
@@ -1,9 +1,10 @@
1
1
  import { criteriaOf, SelectionTooLarge, sizeOf, } from "../domain/selection.js";
2
+ import { readPage } from "./reads.js";
2
3
  /**
3
4
  * The keys of the chosen records — what a Command run once per record needs, what a many2many
4
5
  * stores —, never more than `limit`.
5
6
  *
6
- * await keysOf(chosen(["F-1", "F-7"]), { transport, route: "/invoices", identity: "number", limit: 200 })
7
+ * await keysOf(chosen(["F-1", "F-7"]), { transport, entity: invoices, identity: "number", limit: 200 })
7
8
  * -> ["F-1", "F-7"]
8
9
  * await keysOf(allMatching(ofSincpro, 37), { …, identity: "id", limit: 200 })
9
10
  * -> the 37 keys, read as one page of references
@@ -16,11 +17,11 @@ import { criteriaOf, SelectionTooLarge, sizeOf, } from "../domain/selection.js";
16
17
  * 1. A size already known past the ceiling is refused before anything is read.
17
18
  * 2. Keys are in hand: they are the answer.
18
19
  * 3. Else one page of references for exactly the chosen records (`criteriaOf`), one more than the
19
- * ceiling — the one more says the ceiling was passed.
20
+ * ceiling — the one more says the ceiling was passed —, through the entity's `search`.
20
21
  * Final: the keys, in the order the server answered them.
21
22
  */
22
23
  export async function keysOf(selection, options) {
23
- const { transport, route, identity, limit, signal } = options;
24
+ const { transport, entity, identity, limit, signal } = options;
24
25
  const size = sizeOf(selection);
25
26
  if (size !== null && size > limit)
26
27
  throw new SelectionTooLarge(limit, size);
@@ -28,11 +29,11 @@ export async function keysOf(selection, options) {
28
29
  return selection.keys;
29
30
  // Read untyped: what comes back is references — the identity alone —, never a `T`.
30
31
  const question = criteriaOf(selection, identity);
31
- const page = await transport.list(route, {
32
+ const page = await readPage(transport, entity, {
32
33
  ...(question === undefined ? {} : { selection: question }),
33
34
  pagination: { limit: limit + 1 },
34
35
  specification: { [identity]: {} },
35
- }, signal);
36
+ }, signal === undefined ? {} : { signal });
36
37
  if (page.rows.length > limit)
37
38
  throw new SelectionTooLarge(limit, page.count?.value ?? null);
38
39
  return page.rows.map((row) => String(row[identity]));
@@ -1,4 +1,4 @@
1
- import type { Metamodel } from "./metamodel.js";
1
+ import type { Metamodel, Operation } from "./metamodel.js";
2
2
  /**
3
3
  * What a generated file imports, built in parts. The build rewrites this package's own
4
4
  * specifiers to relative paths (`tsc-alias`), and it reads `from "…"` inside a string too: written
@@ -14,6 +14,8 @@ export declare const GENERATED_DOMAIN: string;
14
14
  export declare const GENERATED_ADAPTERS: string;
15
15
  /** What a generated adapter imports the domain's types from: `../../domain/generated/index`. */
16
16
  export declare const DOMAIN_MODULE: string;
17
+ /** What the generated entities import their operations from: the client beside them. */
18
+ export declare const CLIENT_MODULE: string;
17
19
  /** A file of `domain/generated/` as its sibling imports it: `models` → `./models`. */
18
20
  export declare function siblingOf(file: string): string;
19
21
  /** A name of the server's as a TypeScript identifier: `shop__orders__Line` stays, `-` is `_`. */
@@ -59,3 +61,16 @@ export declare function deprecationOf(metamodel: Metamodel, operation: {
59
61
  }): string | undefined;
60
62
  /** Refuses a second thing under a name already given, naming both. */
61
63
  export declare function claimed(): (name: string, by: string) => void;
64
+ /**
65
+ * The one entity a read belongs to: its only entity, or — when it concerns several — the one
66
+ * whose model its answer carries. `undefined` when that still leaves more than one.
67
+ */
68
+ export declare function ownerOf(metamodel: Metamodel, operation: Operation): string | undefined;
69
+ /** `Issue` → `issue`: how the API names an entity's group. */
70
+ export declare function groupNameOf(model: string): string;
71
+ /**
72
+ * The method an operation is called by in its entity's group: its name without the entity's —
73
+ * `CommandCancelIssue` → `cancel`, `CommandListIssues` → `list`, `CommandAprobarInscripcion` →
74
+ * `aprobar` —, the whole name when the entity's is not a word of it.
75
+ */
76
+ export declare function memberOf(local: string, model: string): string;
@@ -23,6 +23,8 @@ export const GENERATED_DOMAIN = ["domain", "generated"].join("/");
23
23
  export const GENERATED_ADAPTERS = ["adapters", "generated"].join("/");
24
24
  /** What a generated adapter imports the domain's types from: `../../domain/generated/index`. */
25
25
  export const DOMAIN_MODULE = ["..", "..", GENERATED_DOMAIN, "index"].join("/");
26
+ /** What the generated entities import their operations from: the client beside them. */
27
+ export const CLIENT_MODULE = [".", "client"].join("/");
26
28
  /** A file of `domain/generated/` as its sibling imports it: `models` → `./models`. */
27
29
  export function siblingOf(file) {
28
30
  return [".", file].join("/");
@@ -181,3 +183,44 @@ export function claimed() {
181
183
  named.set(name, by);
182
184
  };
183
185
  }
186
+ /**
187
+ * The one entity a read belongs to: its only entity, or — when it concerns several — the one
188
+ * whose model its answer carries. `undefined` when that still leaves more than one.
189
+ */
190
+ export function ownerOf(metamodel, operation) {
191
+ if (operation.entities.length === 1)
192
+ return operation.entities[0];
193
+ const answer = JSON.stringify(operation.output ?? {});
194
+ const carried = operation.entities.filter((one) => one in metamodel.models && answer.includes(`"#/models/${one}"`));
195
+ return carried.length === 1 ? carried[0] : undefined;
196
+ }
197
+ /** `Issue` → `issue`: how the API names an entity's group. */
198
+ export function groupNameOf(model) {
199
+ const name = identifierOf(model);
200
+ return `${name.charAt(0).toLowerCase()}${name.slice(1)}`;
201
+ }
202
+ /**
203
+ * The method an operation is called by in its entity's group: its name without the entity's —
204
+ * `CommandCancelIssue` → `cancel`, `CommandListIssues` → `list`, `CommandAprobarInscripcion` →
205
+ * `aprobar` —, the whole name when the entity's is not a word of it.
206
+ */
207
+ export function memberOf(local, model) {
208
+ const bare = local.replace(/^(Command|Query)(?=[A-Z])/, "");
209
+ const name = identifierOf(model);
210
+ const plural = pluralOf(name);
211
+ const words = [
212
+ ...new Set([
213
+ `${name}es`,
214
+ `${name}s`,
215
+ `${plural.charAt(0).toUpperCase()}${plural.slice(1)}`,
216
+ ]),
217
+ ]
218
+ .sort((a, b) => b.length - a.length)
219
+ .concat(name);
220
+ for (const word of words) {
221
+ const stripped = bare.replace(new RegExp(`${word}(?=[A-Z0-9_]|$)`), "");
222
+ if (stripped !== bare && stripped !== "")
223
+ return methodOf(stripped);
224
+ }
225
+ return methodOf(bare);
226
+ }