@sincpro/client-framework 1.9.0 → 1.11.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 +74 -18
- package/dist/client/adapters/memory.d.ts +4 -0
- package/dist/client/adapters/memory.js +25 -21
- package/dist/client/adapters/rest.d.ts +4 -0
- package/dist/client/adapters/rest.js +5 -1
- package/dist/client/domain/changes.d.ts +45 -0
- package/dist/client/domain/changes.js +15 -0
- package/dist/client/domain/transport.d.ts +13 -0
- package/dist/client/domain/transport.js +27 -0
- package/dist/client/index.d.ts +6 -3
- package/dist/client/index.js +6 -3
- package/dist/client/services/changes.d.ts +32 -0
- package/dist/client/services/changes.js +60 -0
- package/dist/client/services/guarded.d.ts +1 -1
- package/dist/client/services/guarded.js +2 -1
- package/dist/codegen/domain/names.d.ts +10 -0
- package/dist/codegen/domain/names.js +13 -1
- package/dist/codegen/entrypoint/cli.js +28 -23
- package/dist/codegen/index.d.ts +5 -3
- package/dist/codegen/index.js +2 -1
- package/dist/codegen/services/domain.d.ts +18 -16
- package/dist/codegen/services/domain.js +140 -61
- package/dist/codegen/services/generated.d.ts +23 -0
- package/dist/codegen/services/generated.js +34 -0
- package/dist/codegen/services/scaffold.d.ts +3 -2
- package/dist/codegen/services/scaffold.js +5 -3
- package/dist/common/index.d.ts +1 -1
- package/dist/common/index.js +1 -1
- package/dist/common/time.d.ts +28 -0
- package/dist/common/time.js +49 -0
- package/dist/core/entrypoint/screens.d.ts +1 -1
- package/dist/core/entrypoint/screens.js +3 -3
- package/dist/core/infrastructure/process.d.ts +9 -0
- package/dist/core/infrastructure/process.js +2 -0
- package/dist/model/domain/meta.d.ts +2 -2
- package/dist/model/domain/meta.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -171,8 +171,8 @@ their entries — `operations.X` (an identity), `events.X` (a wire name) — as
|
|
|
171
171
|
registered for, and executes them as data:
|
|
172
172
|
|
|
173
173
|
```ts
|
|
174
|
-
import { events, operations } from "../generated/client";
|
|
175
|
-
import type * as remote from "../generated
|
|
174
|
+
import { events, operations } from "../adapters/generated/client";
|
|
175
|
+
import type * as remote from "../domain/generated";
|
|
176
176
|
|
|
177
177
|
@projectBoundedContext.feature([events.IssueClosed, operations.CommandArchiveIssue])
|
|
178
178
|
class Archive extends Feature<remote.IssueClosed | RemoteCommand<remote.CommandArchiveIssue>, void> {
|
|
@@ -358,7 +358,7 @@ Three things a platform keeps in mind:
|
|
|
358
358
|
`static fromJSON` to read them back as such.
|
|
359
359
|
|
|
360
360
|
The events a remote service publishes arrive as generated types (`DomainEvent` in
|
|
361
|
-
`generated/
|
|
361
|
+
`domain/generated/events.ts`, a union narrowed by `name`); a local event is a class, like a local
|
|
362
362
|
Command.
|
|
363
363
|
|
|
364
364
|
## Extending the pipeline
|
|
@@ -509,6 +509,7 @@ import { sentryReporter } from "@sincpro/client-framework/sentry";
|
|
|
509
509
|
|
|
510
510
|
configureProcess({
|
|
511
511
|
identity: { release: "forge-web:1.4.0" }, // + tenant, when the deployment serves one
|
|
512
|
+
timeZone: { keys: ["timezone"] }, // where a run's zone is read from — the backend's key, the default
|
|
512
513
|
tracer: openTelemetryTracer(provider.getTracer("forge-web")), // the app's provider
|
|
513
514
|
reporter: sentryReporter(client), // the app's Sentry or GlitchTip client
|
|
514
515
|
logger: { mode: production ? "production" : "development" },
|
|
@@ -625,6 +626,26 @@ optional `watch`. `memoryContexts()` is the default; a browser, a phone or a ser
|
|
|
625
626
|
its own. `values` is the same object until it changes, so a screen can hand it to React's
|
|
626
627
|
`useSyncExternalStore`.
|
|
627
628
|
|
|
629
|
+
### The time zone of a run
|
|
630
|
+
|
|
631
|
+
A run reads days and times in the zone its context says, not in the device's: a user in La Paz
|
|
632
|
+
looking at a company in Madrid sees «today» as Madrid's. The zone is the context's `timezone` —
|
|
633
|
+
the key sincpro_framework reads to cut `today` and `period` —; with none, the process's zone
|
|
634
|
+
(`installTimeZone`, else the platform's).
|
|
635
|
+
|
|
636
|
+
```ts
|
|
637
|
+
await salesBoundedContext.scoped({ timezone: "Europe/Madrid" }).execute(new CommandQuote(1));
|
|
638
|
+
|
|
639
|
+
const zone = timeZoneOf(this.execution); // or timeZoneOf(context)
|
|
640
|
+
dayOf(instant, zone); // «today» for that company
|
|
641
|
+
```
|
|
642
|
+
|
|
643
|
+
The context travels to the server in `sincpro-context`, so under `timezone` the backend cuts days
|
|
644
|
+
the same way. An application that keeps the zone under another key names it —
|
|
645
|
+
`configureProcess({ timeZone: { keys: ["company_zone", "timezone"] } })`, the first found wins —,
|
|
646
|
+
but the backend reads only `timezone`: a zone under another key is the client's alone, and the
|
|
647
|
+
server cuts in UTC.
|
|
648
|
+
|
|
628
649
|
## Talking to a server
|
|
629
650
|
|
|
630
651
|
A remote service is reached through a port. The generated client never names a wire: it says
|
|
@@ -632,7 +653,7 @@ which operation, with what input, and the adapter the application injects decide
|
|
|
632
653
|
travels.
|
|
633
654
|
|
|
634
655
|
```ts
|
|
635
|
-
import { clientOf } from "./generated/client";
|
|
656
|
+
import { clientOf } from "./adapters/generated/client";
|
|
636
657
|
|
|
637
658
|
// pick the wire; the client, the Features and the tests stay the same
|
|
638
659
|
const forge = clientOf(restOperations(restTransport({ base: "/v1", headers: session })));
|
|
@@ -653,6 +674,7 @@ await forge.sendIssue({ issue_id: "acme-3", message: "hola" }); // ResponseSendI
|
|
|
653
674
|
| `retryKeys()` | one `Idempotency-Key` per gesture, kept while a retry may repeat it |
|
|
654
675
|
| `guardedTransport(t, breaker)` | every call of a transport through a circuit breaker |
|
|
655
676
|
| `screensTransport(transport)` | screen reads and writes run as use cases of `screens`: traced and reported |
|
|
677
|
+
| `markedAs(wrapper, inner)` | a wrapper of a screens transport keeps its mark: one `screens` context, not two |
|
|
656
678
|
| `parquetDecoder` (`./parquet`) | large pages read as Parquet through the same transport |
|
|
657
679
|
|
|
658
680
|
Inside a Feature, inject the generated client as it is: the run binds it, so every call carries
|
|
@@ -660,6 +682,30 @@ its trace (`traceparent`), its context (`sincpro-context`) and its signal — as
|
|
|
660
682
|
as metadata where a platform's adapter puts them. `screensTransport` and `entity()` stay on the
|
|
661
683
|
REST `Transport`: they are how a screen reads pages of an entity, with its model and its drafts.
|
|
662
684
|
|
|
685
|
+
A wrapper over a screens transport — a cache, a guard — keeps its mark, so the screens are not made
|
|
686
|
+
twice: `guardedTransport` does it, and a platform's own wrapper calls `markedAs(wrapper, inner)`.
|
|
687
|
+
One span then answers each screen read, whether the wrapper answered it from its own memory or not.
|
|
688
|
+
|
|
689
|
+
### What a write changed
|
|
690
|
+
|
|
691
|
+
A client keeps what it read — a cache, a list on screen, a record — and every one of them goes
|
|
692
|
+
stale when a write lands, whoever wrote it. So every write says so: `restTransport` and
|
|
693
|
+
`memoryTransport` announce each `send` on the process's change feed (`processChanges`), whether it
|
|
694
|
+
came from a screen or from a use case's generated client, answered or refused. A preview and a dry
|
|
695
|
+
run announce nothing. A reader follows the feed, never the writer's transport:
|
|
696
|
+
|
|
697
|
+
```ts
|
|
698
|
+
import { processChanges } from "@sincpro/client-framework";
|
|
699
|
+
|
|
700
|
+
const stop = processChanges.follow((change) => forgetUnder(change.route)); // { method, route }
|
|
701
|
+
stop();
|
|
702
|
+
```
|
|
703
|
+
|
|
704
|
+
A change says only what was written; what it touches — the collections a route names, a record's
|
|
705
|
+
history — is each reader's to decide. A transport given `changes` announces there instead (a test,
|
|
706
|
+
a transport that must not be heard), and a platform's own adapter wraps its `send` in
|
|
707
|
+
`announced(feed, method, route, body, send)`.
|
|
708
|
+
|
|
663
709
|
### Writing an adapter for another wire
|
|
664
710
|
|
|
665
711
|
An adapter is one function: read the operation's binding for its wire, send the input, answer the
|
|
@@ -693,20 +739,29 @@ Every operation in `operations` carries its identity and each wire's binding (`r
|
|
|
693
739
|
|
|
694
740
|
```bash
|
|
695
741
|
python -m sincpro_framework.entrypoints export-client-contract app.http:gateways contracts [--events]
|
|
696
|
-
sincpro-client generate contracts/project.contract.json --out src/domains/project
|
|
742
|
+
sincpro-client generate contracts/project.contract.json --out src/domains/project --domain project
|
|
697
743
|
sincpro-client scaffold contracts/project.contract.json --out src/domains/project
|
|
698
744
|
sincpro-client check --root src
|
|
699
745
|
```
|
|
700
746
|
|
|
701
|
-
`generate` writes, from the contract the backend exports — never edited, written again whole
|
|
747
|
+
`generate` writes, from the contract the backend exports — never edited, written again whole —,
|
|
748
|
+
each file in the layer it belongs to, under the bounded context's folder (`--out`):
|
|
749
|
+
|
|
750
|
+
| File | What |
|
|
751
|
+
| -------------------------------- | --------------------------------------------------------------------------------------------- |
|
|
752
|
+
| `domain/generated/models.ts` | the entities and value objects: a type per model, and the definitions they hold |
|
|
753
|
+
| `domain/generated/commands.ts` | every operation's Command and Response, and the `<Name>Input` a caller sends |
|
|
754
|
+
| `domain/generated/events.ts` | the domain events and `DomainEvent`, their union — when the contract carries them |
|
|
755
|
+
| `domain/generated/index.ts` | the three, re-exported: `import type * as remote from "../domain/generated"` |
|
|
756
|
+
| `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 |
|
|
702
758
|
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
| `entities.ts` | each entity's list, record, edit, export, Commands and history, for screens |
|
|
759
|
+
The backend keeps them apart — the domain's words, the application layer's DTOs, the facts, the
|
|
760
|
+
adapters to another system —, and so does the application: each beside its own code of that layer.
|
|
761
|
+
A shared definition goes with the first kind that names it (the models, then the operations, then
|
|
762
|
+
the events), and a file that names one written elsewhere imports it from there.
|
|
708
763
|
|
|
709
|
-
Each method takes the operation's input and answers its answer, as `
|
|
764
|
+
Each method takes the operation's input and answers its answer, as `commands.ts` types them — a
|
|
710
765
|
search answers its `Response…` with its records and cursor, as the backend's callers get it, on
|
|
711
766
|
every wire. `scaffold` writes, once, the application's own files: its dependencies (the client
|
|
712
767
|
over the `operations` the application exports — `--operations <module>` —), its `framework.ts`
|
|
@@ -719,7 +774,7 @@ exported with `--events`. Then each event is a type as it is received — every
|
|
|
719
774
|
`name` its wire name as a literal — and `DomainEvent`, their union, narrows by `name`:
|
|
720
775
|
|
|
721
776
|
```ts
|
|
722
|
-
import type * as remote from "./generated
|
|
777
|
+
import type * as remote from "./domain/generated";
|
|
723
778
|
|
|
724
779
|
function subjectOf(event: remote.DomainEvent): string {
|
|
725
780
|
switch (event.name) {
|
|
@@ -730,7 +785,7 @@ function subjectOf(event: remote.DomainEvent): string {
|
|
|
730
785
|
}
|
|
731
786
|
```
|
|
732
787
|
|
|
733
|
-
`events` in `client.ts` says, per event, its wire name and whether the bounded context records it
|
|
788
|
+
`events` in `adapters/generated/client.ts` says, per event, its wire name and whether the bounded context records it
|
|
734
789
|
(its aggregates' own) or handles it (a Feature subscribes).
|
|
735
790
|
|
|
736
791
|
### Types, not classes
|
|
@@ -747,13 +802,13 @@ types; the application's own Commands are classes, written by hand.
|
|
|
747
802
|
a local Feature, through the injected client.
|
|
748
803
|
- **Events read best as a union.** A stream of facts — a queue, a history page — is narrowed by
|
|
749
804
|
`name`, with no registry of constructors to rebuild each one.
|
|
750
|
-
- **Nothing runs.** `domain
|
|
805
|
+
- **Nothing runs.** `domain/generated/` adds no byte to a bundle and no side effect; a contract of three
|
|
751
806
|
hundred models costs nothing at runtime.
|
|
752
807
|
|
|
753
808
|
A local class takes the contract's shape and stays checked against it:
|
|
754
809
|
|
|
755
810
|
```ts
|
|
756
|
-
import type * as remote from "../generated
|
|
811
|
+
import type * as remote from "../domain/generated";
|
|
757
812
|
|
|
758
813
|
class CommandCancelIssue
|
|
759
814
|
extends Command<ResponseCancelIssue>
|
|
@@ -777,7 +832,7 @@ needs them.
|
|
|
777
832
|
| Decimal | exact text, `"10.50"` | `Decimal` |
|
|
778
833
|
| Money | `{ amount: "10.50", currency: "BOB" }` | `Money` |
|
|
779
834
|
| Day | `"2026-10-02"`, never an instant | `isDay`, `dayOf` |
|
|
780
|
-
| Instant | ISO text | `wallTimeOf`, `instantOf`
|
|
835
|
+
| Instant | ISO text | `wallTimeOf`, `instantOf`, `timeZoneOf` |
|
|
781
836
|
| ID | UUID v7 as 32 hex | `newEntityId`, `isEntityId`, `momentOf` |
|
|
782
837
|
| Bytes | `{ "$bytes": "<base64>" }` | `valuesOf`, `readValues` |
|
|
783
838
|
| Collection | items, count and cursor | `collectionOf`, `isPartial`, `relatedCriteria` |
|
|
@@ -800,9 +855,10 @@ src/
|
|
|
800
855
|
domains/
|
|
801
856
|
common/infrastructure/operations.ts export const operations — the wire every client uses
|
|
802
857
|
<bounded context>/
|
|
803
|
-
generated/ sincpro-client generate — never edited
|
|
804
858
|
domain/ local Commands, Responses, DomainEvents, value rules
|
|
859
|
+
generated/ sincpro-client generate: models, commands, events — never edited
|
|
805
860
|
adapters/ repositories, printers, other systems
|
|
861
|
+
generated/ sincpro-client generate: the client and the entities
|
|
806
862
|
services/ the use cases, one folder per aggregate or entity
|
|
807
863
|
<aggregate>/ sale_order/: its Features and ApplicationServices
|
|
808
864
|
confirm-quotation.feature.ts
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { ChangeFeed } from "../domain/changes.js";
|
|
1
2
|
import { type Draft, type Method, type Transport } from "../domain/transport.js";
|
|
2
3
|
import type { AnyRecord, Criteria, Meta, Problem } from "@sincpro/criteria";
|
|
3
4
|
/**
|
|
@@ -42,5 +43,8 @@ export type MemoryOptions = {
|
|
|
42
43
|
refuse?: (route: string) => (Problem & {
|
|
43
44
|
status?: number;
|
|
44
45
|
}) | undefined;
|
|
46
|
+
/** Where each write is announced (`Change`), as a real server's transport does: the
|
|
47
|
+
* process's feed (`processChanges`) by default. */
|
|
48
|
+
changes?: ChangeFeed;
|
|
45
49
|
};
|
|
46
50
|
export declare function memoryTransport(tables: Readonly<Record<string, MemoryTable>>, options?: MemoryOptions): Transport;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { TransportError, } from "../domain/transport.js";
|
|
2
|
+
import { announced, processChanges } from "../services/changes.js";
|
|
2
3
|
import { bindableTransport } from "../services/propagation.js";
|
|
3
4
|
import { pack, readValues } from "../../common/serialization.js";
|
|
4
5
|
import { answer, answerGroups } from "@sincpro/criteria/engine";
|
|
@@ -24,6 +25,7 @@ function wait(ms, signal) {
|
|
|
24
25
|
}
|
|
25
26
|
export function memoryTransport(tables, options = {}) {
|
|
26
27
|
const answered = new Map();
|
|
28
|
+
const changes = options.changes ?? processChanges;
|
|
27
29
|
async function check(route, signal) {
|
|
28
30
|
await wait(options.latency ?? 0, signal);
|
|
29
31
|
const refused = options.refuse?.(route);
|
|
@@ -86,27 +88,29 @@ export function memoryTransport(tables, options = {}) {
|
|
|
86
88
|
};
|
|
87
89
|
},
|
|
88
90
|
async send(method, route, body, signal, sending) {
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
91
|
+
return announced(changes, method, route, body, async () => {
|
|
92
|
+
await check(route, signal);
|
|
93
|
+
// A key already answered is answered again, without running the Command twice — what
|
|
94
|
+
// sincpro_framework's `@idempotency` does with a repeated `Idempotency-Key`.
|
|
95
|
+
const key = sending?.idempotencyKey;
|
|
96
|
+
if (key !== undefined && answered.has(key))
|
|
97
|
+
return copyOf(answered.get(key));
|
|
98
|
+
if (options.handle === undefined)
|
|
99
|
+
throw new TransportError(route, 404, {
|
|
100
|
+
kind: "not_found",
|
|
101
|
+
reason: "ROUTE_NOT_FOUND",
|
|
102
|
+
next_step: "fix_request",
|
|
103
|
+
retryable: false,
|
|
104
|
+
errors: [],
|
|
105
|
+
extensions: {},
|
|
106
|
+
detail: `Nothing answers ${method} ${route}.`,
|
|
107
|
+
});
|
|
108
|
+
// A deep copy: what the caller sent is not what the server keeps.
|
|
109
|
+
const answer = await options.handle(method, route, copyOf(body));
|
|
110
|
+
if (key !== undefined)
|
|
111
|
+
answered.set(key, copyOf(answer));
|
|
112
|
+
return answer;
|
|
113
|
+
});
|
|
110
114
|
},
|
|
111
115
|
...(options.read === undefined
|
|
112
116
|
? {}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { ChangeFeed } from "../domain/changes.js";
|
|
1
2
|
import { type Decoder, type Transport } from "../domain/transport.js";
|
|
2
3
|
import { type Criteria } from "@sincpro/criteria";
|
|
3
4
|
/**
|
|
@@ -44,6 +45,9 @@ export type RestOptions = {
|
|
|
44
45
|
/** Called once per model and version the server answers that the snapshot does not hold —
|
|
45
46
|
* never an error: the screen keeps working on the live definition. */
|
|
46
47
|
onVersionDrift?: (drift: VersionDrift) => void;
|
|
48
|
+
/** Where each write is announced (`Change`): the process's feed (`processChanges`) by default,
|
|
49
|
+
* so a cache or a screen hears it whoever wrote — a screen, or a use case's client. */
|
|
50
|
+
changes?: ChangeFeed;
|
|
47
51
|
};
|
|
48
52
|
/** A model the server answers at another version than the snapshot the code came from. */
|
|
49
53
|
export type VersionDrift = {
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { TransportError, } from "../domain/transport.js";
|
|
2
|
+
import { announced, processChanges } from "../services/changes.js";
|
|
2
3
|
import { definitionOf, modelAt, } from "../services/discovery.js";
|
|
3
4
|
import { bindableTransport } from "../services/propagation.js";
|
|
4
5
|
import { readProblem } from "@sincpro/criteria";
|
|
@@ -45,6 +46,7 @@ function documentUrl(base, document) {
|
|
|
45
46
|
export function restTransport(options = {}) {
|
|
46
47
|
const base = options.base ?? "";
|
|
47
48
|
const doFetch = options.fetch ?? ((input, init) => fetch(input, init));
|
|
49
|
+
const changes = options.changes ?? processChanges;
|
|
48
50
|
const decoders = options.decoders ?? [];
|
|
49
51
|
const accept = decoders.length === 0
|
|
50
52
|
? {}
|
|
@@ -183,7 +185,9 @@ export function restTransport(options = {}) {
|
|
|
183
185
|
};
|
|
184
186
|
},
|
|
185
187
|
async send(method, route, body, signal, sending) {
|
|
186
|
-
return (
|
|
188
|
+
return announced(changes, method, route, body, async () => {
|
|
189
|
+
return (await get(route, urlOf(base, route), signal, { method, body, key: sending?.idempotencyKey }, sending));
|
|
190
|
+
});
|
|
187
191
|
},
|
|
188
192
|
});
|
|
189
193
|
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { Method } from "./transport.js";
|
|
2
|
+
/**
|
|
3
|
+
* A write that may have changed what was read: how it was sent and where.
|
|
4
|
+
*
|
|
5
|
+
* Context: a client keeps what it read — a cache, a list on screen, a record, a name in a cell —
|
|
6
|
+
* 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.
|
|
12
|
+
*
|
|
13
|
+
* { method: "POST", route: "/issues/odoo-acme-7/messages" }
|
|
14
|
+
*/
|
|
15
|
+
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;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Who hears what was written: one feed the writers announce to and the readers follow, so neither
|
|
22
|
+
* knows the other.
|
|
23
|
+
*
|
|
24
|
+
* const stop = processChanges.follow((change) => cache.forget(change.route));
|
|
25
|
+
* processChanges.announce({ method: "PUT", route: "/projects/odoo-acme/edit" });
|
|
26
|
+
* stop();
|
|
27
|
+
*
|
|
28
|
+
* Context: a listener that fails is ignored — hearing a write never changes its answer, as
|
|
29
|
+
* tracing and listening never do.
|
|
30
|
+
*/
|
|
31
|
+
export type ChangeFeed = {
|
|
32
|
+
/** Tells everyone who follows now: this was written. */
|
|
33
|
+
announce(change: Change): void;
|
|
34
|
+
/** `listener` hears every change announced from now on; answers the way to stop. */
|
|
35
|
+
follow(listener: (change: Change) => void): () => void;
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
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.
|
|
40
|
+
*
|
|
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
|
|
44
|
+
*/
|
|
45
|
+
export declare function changeOf(method: Method, route: string, body: unknown): Change | undefined;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
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.
|
|
4
|
+
*
|
|
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
|
|
8
|
+
*/
|
|
9
|
+
export function changeOf(method, route, body) {
|
|
10
|
+
if ((route.split("?")[0] ?? route).endsWith("/preview"))
|
|
11
|
+
return undefined;
|
|
12
|
+
if (typeof body === "object" && body !== null && "dry_run" in body && body.dry_run === true)
|
|
13
|
+
return undefined;
|
|
14
|
+
return { method, route };
|
|
15
|
+
}
|
|
@@ -11,7 +11,18 @@ import type { AnyRecord, Criteria, Dropped, Envelope, Meta, Page, Problem } from
|
|
|
11
11
|
* Every adapter throws `TransportError` for a refusal, so a screen explains a failure once
|
|
12
12
|
* whichever wire it came over.
|
|
13
13
|
*/
|
|
14
|
+
/**
|
|
15
|
+
* The mark of a screens transport: `screensTransport` answers one it already made, and a wrapper
|
|
16
|
+
* over one (`cachedTransport`, `guardedTransport`) copies the mark, so the screens are not made
|
|
17
|
+
* twice — one span per screen read, however many wrappers stand between.
|
|
18
|
+
*
|
|
19
|
+
* Context: a mark, not a remembered object — a wrapper is a new object, and the screens context
|
|
20
|
+
* it sits on is the one that answers. A platform's wrapper copies it with `markedAs`.
|
|
21
|
+
*/
|
|
22
|
+
export declare const SCREENS: unique symbol;
|
|
14
23
|
export interface Transport {
|
|
24
|
+
/** Set on a screens transport and on what wraps one (`SCREENS`). */
|
|
25
|
+
[SCREENS]?: true;
|
|
15
26
|
/** A page of records under the criteria. */
|
|
16
27
|
list<T>(route: string, criteria: Criteria<T>, signal?: AbortSignal, options?: CallOptions): Promise<Page<T>>;
|
|
17
28
|
/** One record, with the definition of its model. */
|
|
@@ -123,3 +134,5 @@ export declare class TransportError extends Error {
|
|
|
123
134
|
readonly problem: Problem | null;
|
|
124
135
|
constructor(route: string, status: number | null, problem: Problem | null, message?: string);
|
|
125
136
|
}
|
|
137
|
+
/** Carries the screens mark from `from` onto `to`, a wrapper of it; `to` is answered. */
|
|
138
|
+
export declare function markedAs<T extends Transport>(to: T, from: Transport): T;
|
|
@@ -1,3 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The one seam between this package and a server.
|
|
3
|
+
*
|
|
4
|
+
* Context: a list and a record are the same two questions whatever the wire is — «a page of
|
|
5
|
+
* these, under this criteria» and «this one». REST today; JSON-RPC and gRPC are the other
|
|
6
|
+
* wires sincpro_framework serves. Each of those is
|
|
7
|
+
* one adapter implementing this, and nothing above it changes. A route is a string the adapter
|
|
8
|
+
* understands: a path for REST, a method for RPC.
|
|
9
|
+
*
|
|
10
|
+
* Every adapter throws `TransportError` for a refusal, so a screen explains a failure once
|
|
11
|
+
* whichever wire it came over.
|
|
12
|
+
*/
|
|
13
|
+
/**
|
|
14
|
+
* The mark of a screens transport: `screensTransport` answers one it already made, and a wrapper
|
|
15
|
+
* over one (`cachedTransport`, `guardedTransport`) copies the mark, so the screens are not made
|
|
16
|
+
* twice — one span per screen read, however many wrappers stand between.
|
|
17
|
+
*
|
|
18
|
+
* Context: a mark, not a remembered object — a wrapper is a new object, and the screens context
|
|
19
|
+
* it sits on is the one that answers. A platform's wrapper copies it with `markedAs`.
|
|
20
|
+
*/
|
|
21
|
+
export const SCREENS = Symbol.for("sincpro.client-framework.screens");
|
|
1
22
|
/** A download through `transport.download`, or the reason it cannot be, by name. */
|
|
2
23
|
export function downloadRoute(transport, route, signal, options) {
|
|
3
24
|
if (transport.download === undefined)
|
|
@@ -30,3 +51,9 @@ export class TransportError extends Error {
|
|
|
30
51
|
this.name = "TransportError";
|
|
31
52
|
}
|
|
32
53
|
}
|
|
54
|
+
/** Carries the screens mark from `from` onto `to`, a wrapper of it; `to` is answered. */
|
|
55
|
+
export function markedAs(to, from) {
|
|
56
|
+
if (from[SCREENS] === true)
|
|
57
|
+
to[SCREENS] = true;
|
|
58
|
+
return to;
|
|
59
|
+
}
|
package/dist/client/index.d.ts
CHANGED
|
@@ -3,18 +3,21 @@
|
|
|
3
3
|
* commands, the service's client contract, and how a run's trace and context reach the wire.
|
|
4
4
|
*
|
|
5
5
|
* domain/ the port and the server's words: `Transport`, `TransportError`, `entity()`, the
|
|
6
|
-
* client contract
|
|
6
|
+
* client contract, what a write changed (`Change`, `ChangeFeed`)
|
|
7
7
|
* adapters/ REST, memory, and Parquet (optional entry `./parquet`)
|
|
8
|
-
* services/ discovery, retry keys, propagation (`traceparent`, `sincpro-context`)
|
|
8
|
+
* services/ discovery, retry keys, propagation (`traceparent`, `sincpro-context`), the
|
|
9
|
+
* process's change feed (`processChanges`)
|
|
9
10
|
*/
|
|
10
11
|
export { JsonRpcError, jsonRpcOperations, type JsonRpcOptions, } from "./adapters/json-rpc.js";
|
|
11
12
|
export { type MemoryOptions, type MemoryTable, memoryTransport, } from "./adapters/memory.js";
|
|
12
13
|
export { type RestOptions, restTransport, urlOf, type VersionDrift, } from "./adapters/rest.js";
|
|
13
14
|
export { restOperations } from "./adapters/rest-operations.js";
|
|
15
|
+
export { type Change, type ChangeFeed, changeOf, } from "./domain/changes.js";
|
|
14
16
|
export { type ClientContract, entityKeyOf, kindOf, metasOf, versionsOf, } from "./domain/contract.js";
|
|
15
17
|
export { bodyFor, type CommandAnswers, type CommandBodies, type CommandRoute, type Entity, entity, type EntityRef, type EntityRoutes, type ExportFormat, exportRoute, listRoute, NEW_PARAM, pathFor, PICK_PARAM, QUERY_PARAM, SCOPE_PARAM, scopeOfRef, startingValuesOf, type SummaryFields, type SummaryPart, TRAIL_PARAM, WITH_PARAM, type WriteRoute, writeRoute, } from "./domain/entity.js";
|
|
16
18
|
export { OperationNotPublished, type OperationOptions, type OperationTransport, type RemoteOperation, } from "./domain/operation.js";
|
|
17
|
-
export { type CallOptions, type Decoder, type Download, downloadRoute, type Draft, type Method, type One, readRoute, type SendOptions, type Transport, TransportError, } from "./domain/transport.js";
|
|
19
|
+
export { type CallOptions, type Decoder, type Download, downloadRoute, type Draft, markedAs, type Method, type One, readRoute, SCREENS, type SendOptions, type Transport, TransportError, } from "./domain/transport.js";
|
|
20
|
+
export { announced, changeFeed, processChanges, } from "./services/changes.js";
|
|
18
21
|
export { guardedTransport } from "./services/guarded.js";
|
|
19
22
|
export { bindable, bindableOperations, bindableTransport, bindOperations, bindTransport, headersOf, } from "./services/propagation.js";
|
|
20
23
|
export { type RetryKeys, retryKeys, unanswered, } from "./services/retry.js";
|
package/dist/client/index.js
CHANGED
|
@@ -3,18 +3,21 @@
|
|
|
3
3
|
* commands, the service's client contract, and how a run's trace and context reach the wire.
|
|
4
4
|
*
|
|
5
5
|
* domain/ the port and the server's words: `Transport`, `TransportError`, `entity()`, the
|
|
6
|
-
* client contract
|
|
6
|
+
* client contract, what a write changed (`Change`, `ChangeFeed`)
|
|
7
7
|
* adapters/ REST, memory, and Parquet (optional entry `./parquet`)
|
|
8
|
-
* services/ discovery, retry keys, propagation (`traceparent`, `sincpro-context`)
|
|
8
|
+
* services/ discovery, retry keys, propagation (`traceparent`, `sincpro-context`), the
|
|
9
|
+
* process's change feed (`processChanges`)
|
|
9
10
|
*/
|
|
10
11
|
export { JsonRpcError, jsonRpcOperations, } from "./adapters/json-rpc.js";
|
|
11
12
|
export { memoryTransport, } from "./adapters/memory.js";
|
|
12
13
|
export { restTransport, urlOf, } from "./adapters/rest.js";
|
|
13
14
|
export { restOperations } from "./adapters/rest-operations.js";
|
|
15
|
+
export { changeOf, } from "./domain/changes.js";
|
|
14
16
|
export { entityKeyOf, kindOf, metasOf, versionsOf, } from "./domain/contract.js";
|
|
15
17
|
export { bodyFor, entity, exportRoute, listRoute, NEW_PARAM, pathFor, PICK_PARAM, QUERY_PARAM, SCOPE_PARAM, scopeOfRef, startingValuesOf, TRAIL_PARAM, WITH_PARAM, writeRoute, } from "./domain/entity.js";
|
|
16
18
|
export { OperationNotPublished, } from "./domain/operation.js";
|
|
17
|
-
export { downloadRoute, readRoute, TransportError, } from "./domain/transport.js";
|
|
19
|
+
export { downloadRoute, markedAs, readRoute, SCREENS, TransportError, } from "./domain/transport.js";
|
|
20
|
+
export { announced, changeFeed, processChanges, } from "./services/changes.js";
|
|
18
21
|
export { guardedTransport } from "./services/guarded.js";
|
|
19
22
|
export { bindable, bindableOperations, bindableTransport, bindOperations, bindTransport, headersOf, } from "./services/propagation.js";
|
|
20
23
|
export { retryKeys, unanswered, } from "./services/retry.js";
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { type ChangeFeed } from "../domain/changes.js";
|
|
2
|
+
import type { Method } from "../domain/transport.js";
|
|
3
|
+
/**
|
|
4
|
+
* A feed in the process's memory: every listener hears every change announced while it follows.
|
|
5
|
+
*
|
|
6
|
+
* const changes = changeFeed();
|
|
7
|
+
* const stop = changes.follow((change) => console.log(change.route));
|
|
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.
|
|
11
|
+
* A listener added while a change is being announced hears the next one, not that one.
|
|
12
|
+
*/
|
|
13
|
+
export declare function changeFeed(): ChangeFeed;
|
|
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.
|
|
18
|
+
*
|
|
19
|
+
* processChanges.follow((change) => …);
|
|
20
|
+
*
|
|
21
|
+
* Context: it lives as long as the process — `configureProcess` does not clear it —, so whoever
|
|
22
|
+
* follows keeps the function that stops it, as with `processListeners`.
|
|
23
|
+
*/
|
|
24
|
+
export declare const processChanges: ChangeFeed;
|
|
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`).
|
|
29
|
+
*
|
|
30
|
+
* send: (method, route, body) => announced(changes, method, route, body, () => post(…)),
|
|
31
|
+
*/
|
|
32
|
+
export declare function announced<T>(feed: ChangeFeed, method: Method, route: string, body: unknown, write: () => Promise<T>): Promise<T>;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { changeOf, } from "../domain/changes.js";
|
|
2
|
+
/**
|
|
3
|
+
* A feed in the process's memory: every listener hears every change announced while it follows.
|
|
4
|
+
*
|
|
5
|
+
* const changes = changeFeed();
|
|
6
|
+
* const stop = changes.follow((change) => console.log(change.route));
|
|
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.
|
|
10
|
+
* A listener added while a change is being announced hears the next one, not that one.
|
|
11
|
+
*/
|
|
12
|
+
export function changeFeed() {
|
|
13
|
+
const listeners = new Set();
|
|
14
|
+
return {
|
|
15
|
+
announce(change) {
|
|
16
|
+
for (const listener of [...listeners]) {
|
|
17
|
+
try {
|
|
18
|
+
listener(change);
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
// Hearing a write never changes its answer, as tracing and listening never do.
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
follow(listener) {
|
|
26
|
+
if (typeof listener !== "function")
|
|
27
|
+
throw new TypeError("A listener is a function");
|
|
28
|
+
listeners.add(listener);
|
|
29
|
+
return () => void listeners.delete(listener);
|
|
30
|
+
},
|
|
31
|
+
};
|
|
32
|
+
}
|
|
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.
|
|
37
|
+
*
|
|
38
|
+
* processChanges.follow((change) => …);
|
|
39
|
+
*
|
|
40
|
+
* Context: it lives as long as the process — `configureProcess` does not clear it —, so whoever
|
|
41
|
+
* follows keeps the function that stops it, as with `processListeners`.
|
|
42
|
+
*/
|
|
43
|
+
export const processChanges = changeFeed();
|
|
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`).
|
|
48
|
+
*
|
|
49
|
+
* send: (method, route, body) => announced(changes, method, route, body, () => post(…)),
|
|
50
|
+
*/
|
|
51
|
+
export async function announced(feed, method, route, body, write) {
|
|
52
|
+
try {
|
|
53
|
+
return await write();
|
|
54
|
+
}
|
|
55
|
+
finally {
|
|
56
|
+
const change = changeOf(method, route, body);
|
|
57
|
+
if (change !== undefined)
|
|
58
|
+
feed.announce(change);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { CircuitBreaker } from "../../circuit_breaker/entrypoint/circuit-breaker.js";
|
|
2
2
|
import { type OperationTransport } from "../domain/operation.js";
|
|
3
|
-
import type
|
|
3
|
+
import { type Transport } from "../domain/transport.js";
|
|
4
4
|
/**
|
|
5
5
|
* `transport`, every call through `breaker` — an `OperationTransport` of any wire, or REST's
|
|
6
6
|
* `Transport`:
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { isOperationTransport, } from "../domain/operation.js";
|
|
2
|
+
import { markedAs, } from "../domain/transport.js";
|
|
2
3
|
import { bindableOperations, bindableTransport, } from "./propagation.js";
|
|
3
4
|
export function guardedTransport(transport, breaker) {
|
|
4
5
|
if (isOperationTransport(transport))
|
|
@@ -16,5 +17,5 @@ export function guardedTransport(transport, breaker) {
|
|
|
16
17
|
const download = transport.download;
|
|
17
18
|
if (download !== undefined)
|
|
18
19
|
guarded.download = (route, signal, options) => breaker.call(() => download.call(transport, route, signal, options));
|
|
19
|
-
return bindableTransport(guarded);
|
|
20
|
+
return bindableTransport(markedAs(guarded, transport));
|
|
20
21
|
}
|
|
@@ -5,7 +5,17 @@ import type { Metamodel } from "./metamodel.js";
|
|
|
5
5
|
* whole, the generated files came out importing `"../index.js"` and `"./domain.js"`.
|
|
6
6
|
*/
|
|
7
7
|
export declare const FRAMEWORK: string;
|
|
8
|
+
/**
|
|
9
|
+
* Where `generate` writes each layer of a bounded context, from its folder: the domain's types
|
|
10
|
+
* (`models.ts`, `commands.ts`, `events.ts`, `index.ts`) and the adapters over the service's wire
|
|
11
|
+
* (`client.ts`, `entities.ts`) — each beside the code of its layer, as the backend lays it out.
|
|
12
|
+
*/
|
|
13
|
+
export declare const GENERATED_DOMAIN: string;
|
|
14
|
+
export declare const GENERATED_ADAPTERS: string;
|
|
15
|
+
/** What a generated adapter imports the domain's types from: `../../domain/generated/index`. */
|
|
8
16
|
export declare const DOMAIN_MODULE: string;
|
|
17
|
+
/** A file of `domain/generated/` as its sibling imports it: `models` → `./models`. */
|
|
18
|
+
export declare function siblingOf(file: string): string;
|
|
9
19
|
/** A name of the server's as a TypeScript identifier: `shop__orders__Line` stays, `-` is `_`. */
|
|
10
20
|
export declare function identifierOf(name: string): string;
|
|
11
21
|
/** A property key as TypeScript writes it: bare when it can be, quoted otherwise. */
|