@sincpro/client-framework 1.1.0 → 1.3.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/AGENTS.md +51 -17
- package/MANIFESTO.md +9 -7
- package/README.md +261 -131
- package/dist/client/services/guarded.d.ts +1 -1
- package/dist/client/services/propagation.js +4 -3
- package/dist/codegen/services/scaffold.d.ts +12 -10
- package/dist/codegen/services/scaffold.js +29 -51
- package/dist/context/entrypoint/context.d.ts +5 -4
- package/dist/core/domain/bus.d.ts +5 -3
- package/dist/core/domain/bus.js +8 -1
- package/dist/core/domain/dto.d.ts +71 -9
- package/dist/core/domain/dto.js +25 -5
- package/dist/core/domain/errors.d.ts +2 -2
- package/dist/core/domain/pipeline.d.ts +8 -5
- package/dist/core/domain/services.d.ts +40 -13
- package/dist/core/domain/services.js +31 -7
- package/dist/core/entrypoint/bounded-context.d.ts +71 -41
- package/dist/core/entrypoint/bounded-context.js +155 -56
- package/dist/core/entrypoint/screens.js +0 -5
- package/dist/core/index.d.ts +4 -3
- package/dist/core/index.js +4 -3
- package/dist/core/infrastructure/listeners.d.ts +6 -5
- package/dist/core/infrastructure/listeners.js +2 -2
- package/dist/core/services/dto-keys.d.ts +44 -0
- package/dist/core/services/dto-keys.js +97 -0
- package/dist/core/services/execution.d.ts +15 -10
- package/dist/core/services/execution.js +36 -23
- package/dist/core/services/interceptors.d.ts +2 -2
- package/dist/core/services/interceptors.js +5 -5
- package/dist/core/services/listening.d.ts +6 -8
- package/dist/core/services/listening.js +3 -2
- package/dist/event_driven/adapters/stored-queue.d.ts +8 -7
- package/dist/event_driven/adapters/stored-queue.js +8 -12
- package/dist/event_driven/domain/envelope.d.ts +2 -2
- package/dist/event_driven/domain/envelope.js +7 -1
- package/dist/event_driven/domain/messages.d.ts +34 -0
- package/dist/event_driven/domain/messages.js +1 -0
- package/dist/event_driven/domain/queue.d.ts +9 -5
- package/dist/event_driven/entrypoint/publisher.d.ts +3 -3
- package/dist/event_driven/entrypoint/publisher.js +5 -1
- package/dist/event_driven/entrypoint/subscriber.d.ts +66 -19
- package/dist/event_driven/entrypoint/subscriber.js +122 -15
- package/dist/event_driven/index.d.ts +11 -7
- package/dist/event_driven/index.js +9 -6
- package/dist/event_driven/services/messages.d.ts +41 -0
- package/dist/event_driven/services/messages.js +147 -0
- package/dist/observability/adapters/memory.d.ts +1 -1
- package/dist/observability/domain/logging.d.ts +2 -2
- package/dist/observability/services/logging.d.ts +6 -3
- package/dist/observability/services/logging.js +1 -1
- package/dist/observability/services/trace-context.d.ts +10 -0
- package/dist/observability/services/trace-context.js +29 -0
- package/package.json +2 -2
|
@@ -1,10 +1,4 @@
|
|
|
1
1
|
import { aliasOf, FRAMEWORK } from "../domain/names.js";
|
|
2
|
-
/** Names the skeleton's `index.ts` cannot give its constant. */
|
|
3
|
-
const TAKEN = new Set([
|
|
4
|
-
"operations",
|
|
5
|
-
"registerServices",
|
|
6
|
-
..."break case catch class const continue debugger default delete do else enum export extends false finally for function if import in instanceof new null return super switch this throw true try typeof var void while with yield let static implements interface package private protected public await".split(" "),
|
|
7
|
-
]);
|
|
8
2
|
/** `sales-orders` → `SalesOrders`. */
|
|
9
3
|
function pascalOf(alias) {
|
|
10
4
|
const joined = alias
|
|
@@ -16,14 +10,17 @@ function pascalOf(alias) {
|
|
|
16
10
|
}
|
|
17
11
|
/** A module the skeleton imports, built in parts — see `FRAMEWORK` (`codegen/names.ts`). */
|
|
18
12
|
const at = (...parts) => parts.join("/");
|
|
13
|
+
/** A path given from the bounded context's folder, as `infrastructure/` reaches it. */
|
|
14
|
+
function fromInfrastructure(path) {
|
|
15
|
+
return path.startsWith(".") ? at("..", path) : path;
|
|
16
|
+
}
|
|
19
17
|
const HEADER = (what) => `// ${what} — written once by \`sincpro-client scaffold\`; the application's to change.`;
|
|
20
18
|
export function scaffoldFilesOf(metamodel, options = {}) {
|
|
21
19
|
const alias = aliasOf(metamodel);
|
|
22
20
|
const Name = pascalOf(alias);
|
|
23
|
-
|
|
24
|
-
//
|
|
25
|
-
|
|
26
|
-
const name = TAKEN.has(bare) ? `${bare}BoundedContext` : bare;
|
|
21
|
+
// Named for what it is — `projectBoundedContext`, never `project` (an entity?) or `ctx` —:
|
|
22
|
+
// nobody misreads it, and no word of the language or name `index.ts` imports can take it.
|
|
23
|
+
const name = `${Name.charAt(0).toLowerCase()}${Name.slice(1)}BoundedContext`;
|
|
27
24
|
const Dependencies = `${Name}Dependencies`;
|
|
28
25
|
const client = metamodel.bindings !== undefined;
|
|
29
26
|
const operations = options.operations ?? at("..", "common", "infrastructure", "operations");
|
|
@@ -44,75 +41,56 @@ export function scaffoldFilesOf(metamodel, options = {}) {
|
|
|
44
41
|
"}",
|
|
45
42
|
"",
|
|
46
43
|
"export function registerDependencies(",
|
|
47
|
-
`
|
|
44
|
+
` boundedContext: BoundedContext<${Dependencies}>,`,
|
|
48
45
|
` ${client ? "operations" : "_operations"}: OperationTransport,`,
|
|
49
46
|
") {",
|
|
50
|
-
...(client ? [`
|
|
51
|
-
" return
|
|
47
|
+
...(client ? [` boundedContext.addDependency("client", clientOf(operations));`] : []),
|
|
48
|
+
" return boundedContext;",
|
|
52
49
|
"}",
|
|
53
50
|
"",
|
|
54
51
|
].join("\n");
|
|
55
|
-
const
|
|
52
|
+
const framework = [
|
|
56
53
|
HEADER(`How «${alias}» is made`),
|
|
57
54
|
"",
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
" Feature as BaseFeature,",
|
|
61
|
-
" BoundedContext,",
|
|
62
|
-
" type Command,",
|
|
63
|
-
" type DomainEvent,",
|
|
64
|
-
`} from "${at(FRAMEWORK, "core")}";`,
|
|
65
|
-
`import type { OperationTransport } from "${FRAMEWORK}";`,
|
|
55
|
+
`import { type OperationTransport } from "${FRAMEWORK}";`,
|
|
56
|
+
`import { BoundedContext, servicesWith } from "${at(FRAMEWORK, "core")}";`,
|
|
66
57
|
"",
|
|
58
|
+
`import { operations } from "${fromInfrastructure(operations)}";`,
|
|
67
59
|
`import { type ${Dependencies}, registerDependencies } from "${at(".", "dependencies")}";`,
|
|
68
60
|
"",
|
|
69
|
-
`/**
|
|
70
|
-
|
|
71
|
-
...(client ? [` declare readonly client: ${Dependencies}["client"];`] : []),
|
|
72
|
-
"}",
|
|
61
|
+
`/** The Feature and ApplicationService of «${alias}»: every dependency typed on \`this\`, declared nowhere else. */`,
|
|
62
|
+
`export const { Feature, ApplicationService } = servicesWith<${Dependencies}>();`,
|
|
73
63
|
"",
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
...(client ? [` declare readonly client: ${Dependencies}["client"];`] : []),
|
|
64
|
+
`export function config${Name}BoundedContext(name: string, transport: OperationTransport) {`,
|
|
65
|
+
` return registerDependencies(new BoundedContext<${Dependencies}>(name), transport);`,
|
|
77
66
|
"}",
|
|
78
67
|
"",
|
|
79
|
-
|
|
80
|
-
`
|
|
81
|
-
"}",
|
|
68
|
+
`/** «${alias}»: \`${name}.execute(new Command(...))\`; a service registers itself with \`@${name}.feature(Command)\`. */`,
|
|
69
|
+
`export const ${name} = config${Name}BoundedContext(${JSON.stringify(alias)}, operations);`,
|
|
82
70
|
"",
|
|
83
71
|
].join("\n");
|
|
84
72
|
const services = [
|
|
85
73
|
HEADER(`What «${alias}» offers`),
|
|
86
74
|
"",
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
"
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
" * and let a local Command implement one (`implements remote.CommandCancelIssue`).",
|
|
94
|
-
" * Register with context.feature(CommandClass)(FeatureClass) or",
|
|
95
|
-
" * context.appService(CommandClass)(ApplicationServiceClass).",
|
|
96
|
-
" */",
|
|
97
|
-
`export function registerServices(_context: BoundedContext<${Dependencies}>): void {}`,
|
|
75
|
+
"// One file per use case, in its aggregate's folder (`services/<aggregate>/`, a related entity",
|
|
76
|
+
"// with use cases of its own in a folder inside it). Each registers itself with",
|
|
77
|
+
`// \`@${name}.feature(CommandX)\` — or a list: \`@${name}.feature([CommandX, EventY])\` — and is`,
|
|
78
|
+
"// imported here, so it is registered before anyone executes. A generated DTO is a remote",
|
|
79
|
+
`// contract (a type): \`import type * as remote from "${at("..", "generated", "domain")}"\`.`,
|
|
80
|
+
"export {};",
|
|
98
81
|
"",
|
|
99
82
|
].join("\n");
|
|
100
83
|
const index = [
|
|
101
84
|
HEADER(`«${alias}», ready to use`),
|
|
102
85
|
"",
|
|
103
|
-
`import {
|
|
86
|
+
`import "${at(".", "services")}";`,
|
|
104
87
|
"",
|
|
105
|
-
`
|
|
106
|
-
`import { registerServices } from "${at(".", "services")}";`,
|
|
107
|
-
"",
|
|
108
|
-
`/** «${alias}»: \`${name}.execute(new Command(...))\`. */`,
|
|
109
|
-
`export const ${name} = config${Name}BoundedContext(${JSON.stringify(alias)}, operations);`,
|
|
110
|
-
`registerServices(${name});`,
|
|
88
|
+
`export { ApplicationService, Feature, ${name} } from "${at(".", "infrastructure", "framework")}";`,
|
|
111
89
|
"",
|
|
112
90
|
].join("\n");
|
|
113
91
|
return {
|
|
114
92
|
"infrastructure/dependencies.ts": dependencies,
|
|
115
|
-
"infrastructure/
|
|
93
|
+
"infrastructure/framework.ts": framework,
|
|
116
94
|
"services/index.ts": services,
|
|
117
95
|
"index.ts": index,
|
|
118
96
|
};
|
|
@@ -1,18 +1,19 @@
|
|
|
1
1
|
import type { ContextStore, ContextValues } from "../domain/store.js";
|
|
2
2
|
/**
|
|
3
3
|
* One level of the context: the process's (`processContext`) or a bounded context's
|
|
4
|
-
* (`
|
|
4
|
+
* (`projectBoundedContext.context`), read and written the same way on every platform.
|
|
5
5
|
*
|
|
6
6
|
* processContext.set({ tenant_id: "acme" }); the company changed — every screen hears it
|
|
7
7
|
* processContext.get("tenant_id") → "acme"
|
|
8
|
-
*
|
|
8
|
+
* projectBoundedContext.context.set({ warehouse_id: 4 }); only what it runs
|
|
9
9
|
* const stop = processContext.watch(() => redraw());
|
|
10
10
|
*
|
|
11
11
|
* Context: sincpro_framework reads its context from a tree — the process, the bounded context,
|
|
12
12
|
* the scope, the execution — and the nearest wins. A client has the same levels: a use case runs
|
|
13
13
|
* with the process's values, under them its bounded context's, under them a scope's
|
|
14
|
-
* (`
|
|
15
|
-
* it runs. That snapshot is `this.context` and what travels to the server in
|
|
14
|
+
* (`projectBoundedContext.scoped({...})`), read once where the gesture starts and inherited by
|
|
15
|
+
* every Feature it runs. That snapshot is `this.context` and what travels to the server in
|
|
16
|
+
* `sincpro-context`.
|
|
16
17
|
*
|
|
17
18
|
* Where the values live is the platform's (`ContextStore`): this is only the door. `values` is
|
|
18
19
|
* the same object until something changes it, so a screen can hand it straight to React's
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Command, DomainEvent, Executable, ResponseClass } from "./dto.js";
|
|
1
|
+
import type { Command, DomainEvent, Executable, RemoteCommand, RemoteEvent, ResponseClass } from "./dto.js";
|
|
2
2
|
import type { Cause, ExecuteOptions } from "./pipeline.js";
|
|
3
3
|
/** What the second argument of `execute` is: options, a delivered event's cause, or a type. */
|
|
4
4
|
export declare function optionsOf(second: unknown): ExecuteOptions;
|
|
@@ -10,7 +10,8 @@ export declare function optionsOf(second: unknown): ExecuteOptions;
|
|
|
10
10
|
export interface CommandBus {
|
|
11
11
|
execute<R>(command: Command<R>, returnType?: ResponseClass<NoInfer<R>>): Promise<R>;
|
|
12
12
|
execute<R>(command: Command<R>, options: ExecuteOptions): Promise<R>;
|
|
13
|
-
execute(
|
|
13
|
+
execute<I, R>(command: RemoteCommand<I, R>, options?: ExecuteOptions): Promise<R>;
|
|
14
|
+
execute(event: DomainEvent | RemoteEvent, options?: ExecuteOptions | Cause): Promise<unknown>;
|
|
14
15
|
}
|
|
15
16
|
/** @internal — how a bus hands what it executes to its generation. */
|
|
16
17
|
export type Dispatch = (executable: Executable, options: ExecuteOptions) => Promise<unknown>;
|
|
@@ -23,5 +24,6 @@ export declare class FeatureBus implements CommandBus {
|
|
|
23
24
|
constructor(dispatch: Dispatch);
|
|
24
25
|
execute<R>(command: Command<R>, returnType?: ResponseClass<NoInfer<R>>): Promise<R>;
|
|
25
26
|
execute<R>(command: Command<R>, options: ExecuteOptions): Promise<R>;
|
|
26
|
-
execute(
|
|
27
|
+
execute<I, R>(command: RemoteCommand<I, R>, options?: ExecuteOptions): Promise<R>;
|
|
28
|
+
execute(event: DomainEvent | RemoteEvent, options?: ExecuteOptions | Cause): Promise<unknown>;
|
|
27
29
|
}
|
package/dist/core/domain/bus.js
CHANGED
|
@@ -4,7 +4,14 @@ export function optionsOf(second) {
|
|
|
4
4
|
return {};
|
|
5
5
|
const given = second;
|
|
6
6
|
if (typeof given.id === "string" && typeof given.correlationId === "string")
|
|
7
|
-
return {
|
|
7
|
+
return {
|
|
8
|
+
cause: {
|
|
9
|
+
id: given.id,
|
|
10
|
+
correlationId: given.correlationId,
|
|
11
|
+
...(given.traceparent ? { traceparent: given.traceparent } : {}),
|
|
12
|
+
...(given.tracestate ? { tracestate: given.tracestate } : {}),
|
|
13
|
+
},
|
|
14
|
+
};
|
|
8
15
|
return {
|
|
9
16
|
...(given.signal ? { signal: given.signal } : {}),
|
|
10
17
|
...(given.cause ? { cause: given.cause } : {}),
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* class CommandCancelIssue extends Command<ResponseCancelIssue> {
|
|
6
6
|
* constructor(readonly issue_id: string) { super(); }
|
|
7
7
|
* }
|
|
8
|
-
* const answer = await
|
|
8
|
+
* const answer = await projectBoundedContext.execute(new CommandCancelIssue("x")); // ResponseCancelIssue
|
|
9
9
|
*
|
|
10
10
|
* Context: the backend asks `bus(dto, ResponseType)` for the type; here the Command carries it, so
|
|
11
11
|
* `execute` infers it with no second argument. The association is type-only: absent from a
|
|
@@ -18,6 +18,12 @@ export declare abstract class DataTransferObject {
|
|
|
18
18
|
export declare abstract class Response extends DataTransferObject {
|
|
19
19
|
}
|
|
20
20
|
declare const responseType: unique symbol;
|
|
21
|
+
/**
|
|
22
|
+
* A request for work, to one bus. Its identity is the framework's, taken when a bounded context
|
|
23
|
+
* registers it — `<context>.<Command>`, as the backend's `registered_name` —: what its message
|
|
24
|
+
* says (`type`), what the receiving bus finds its class by, and what a generated contract's
|
|
25
|
+
* `operations` carry. A production build keeps class names; `static commandName` fixes another.
|
|
26
|
+
*/
|
|
21
27
|
export declare abstract class Command<R> extends DataTransferObject {
|
|
22
28
|
/** Type-only association: absent from serialized DTOs and not a runtime validator. */
|
|
23
29
|
readonly [responseType]: R;
|
|
@@ -31,12 +37,12 @@ export declare abstract class Command<R> extends DataTransferObject {
|
|
|
31
37
|
* static readonly eventName = "shop.order.v1.paid";
|
|
32
38
|
* constructor(readonly order_id: string) { super(); }
|
|
33
39
|
* }
|
|
34
|
-
*
|
|
35
|
-
* await
|
|
40
|
+
* projectBoundedContext.feature(OrderPaid)(NotifyCustomer);
|
|
41
|
+
* await projectBoundedContext.execute(new OrderPaid("o-1"));
|
|
36
42
|
*
|
|
37
43
|
* Context: the backend names an event on the wire by `name`, `<context>.<aggregate>.vN.<fact>`,
|
|
38
|
-
* and defaults it to the class's name;
|
|
39
|
-
*
|
|
44
|
+
* and defaults it to the class's name; here it is declared: it is the contract other processes
|
|
45
|
+
* read, with its version, not a class's name. `name` is reserved, as the backend reserves it: every event reads its class's
|
|
40
46
|
* `eventName` there, and a subclass cannot make it a field.
|
|
41
47
|
*/
|
|
42
48
|
export declare abstract class DomainEvent extends DataTransferObject {
|
|
@@ -44,19 +50,75 @@ export declare abstract class DomainEvent extends DataTransferObject {
|
|
|
44
50
|
/** Its wire name: its class's `eventName`. */
|
|
45
51
|
get name(): string;
|
|
46
52
|
}
|
|
53
|
+
/** An event's wire name: dotted, as the backend's `<context>.<aggregate>.vN.<fact>`. */
|
|
54
|
+
export declare const EVENT_NAME: RegExp;
|
|
47
55
|
/** What an event class declares: the name it is known by on the wire. */
|
|
48
56
|
type EventNamed = {
|
|
49
57
|
readonly eventName: string;
|
|
50
58
|
};
|
|
51
59
|
export type AnyCommand = Command<unknown>;
|
|
52
|
-
/**
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
60
|
+
/**
|
|
61
|
+
* A generated event as it travels: plain data with its wire name — the type a contract exported
|
|
62
|
+
* with `--events` describes (`dto.OrderPaid`), no class written for it.
|
|
63
|
+
*/
|
|
64
|
+
export type RemoteEvent = {
|
|
65
|
+
readonly name: string;
|
|
66
|
+
/** A DTO class's instance is not a generated event: it is a Command, a Response or a DomainEvent. */
|
|
67
|
+
readonly dtoBrand?: never;
|
|
68
|
+
/** Whatever else the contract says it carries. */
|
|
69
|
+
readonly [field: string]: unknown;
|
|
70
|
+
};
|
|
71
|
+
/** What a generated Command carries beside its input: the contract's identity. Never serialized with its data. */
|
|
72
|
+
export declare const COMMAND_IDENTITY: unique symbol;
|
|
73
|
+
declare const remoteAnswer: unique symbol;
|
|
74
|
+
/**
|
|
75
|
+
* A generated Command as the bus executes it: its input, as the contract types it, tagged with
|
|
76
|
+
* the operation's identity (`commandOf(operations.CommandSendIssue, input)`).
|
|
77
|
+
*/
|
|
78
|
+
export type RemoteCommand<I = unknown, R = unknown> = Readonly<I> & {
|
|
79
|
+
readonly [COMMAND_IDENTITY]: string;
|
|
80
|
+
/** Type-only: what it answers. */
|
|
81
|
+
readonly [remoteAnswer]?: R;
|
|
82
|
+
};
|
|
83
|
+
/** A generated event a service is registered for: its entry in the contract's `events` (`events.OrderPaid`). */
|
|
84
|
+
export type EventDescriptor = {
|
|
85
|
+
readonly name: string;
|
|
86
|
+
readonly prototype?: never;
|
|
87
|
+
};
|
|
88
|
+
/** A generated Command a service is registered for: its entry in the contract's `operations`. */
|
|
89
|
+
export type CommandDescriptor = {
|
|
90
|
+
readonly identity: string;
|
|
91
|
+
readonly prototype?: never;
|
|
92
|
+
};
|
|
93
|
+
/**
|
|
94
|
+
* The generated Command `operation` names, with `input`, as the bus executes it:
|
|
95
|
+
*
|
|
96
|
+
* await projectBoundedContext.execute(commandOf(operations.CommandSendIssue, input));
|
|
97
|
+
*/
|
|
98
|
+
export declare function commandOf<I extends object, R = unknown>(operation: CommandDescriptor, input: I): RemoteCommand<I, R>;
|
|
99
|
+
/**
|
|
100
|
+
* What a service is registered for and executes: a Command, a domain event — or a generated
|
|
101
|
+
* one, recognized by its identity or its wire name.
|
|
102
|
+
*/
|
|
103
|
+
export type Executable = AnyCommand | DomainEvent | RemoteEvent | RemoteCommand;
|
|
104
|
+
/** What it answers: a Command's Response; whatever its use case returns, for an event. */
|
|
105
|
+
export type Answer<T> = T extends Command<infer R> ? R : T extends RemoteCommand<unknown, infer R> ? R : unknown;
|
|
56
106
|
/** A Command's class: what a service is registered for, by exact constructor identity. */
|
|
57
107
|
export type CommandClass<C extends AnyCommand> = abstract new (...args: never[]) => C;
|
|
108
|
+
/** What a Command class that travels declares: its identity, `<context>.<Command>`. */
|
|
109
|
+
export type CommandNamed = {
|
|
110
|
+
readonly commandName?: string;
|
|
111
|
+
};
|
|
58
112
|
/** A domain event's class, with its wire name. */
|
|
59
113
|
export type EventClass<E extends DomainEvent> = (abstract new (...args: never[]) => E) & EventNamed;
|
|
60
114
|
/** A Response's class: the backend's `return_type`, accepted for the same reading. */
|
|
61
115
|
export type ResponseClass<R> = abstract new (...args: never[]) => R;
|
|
116
|
+
/**
|
|
117
|
+
* What a service is registered for — a Command's class, a domain event's class, or a generated
|
|
118
|
+
* one's entry in the contract (`operations.X`, `events.X`) —, one or a list of them, as the
|
|
119
|
+
* backend's `DTORegistration`.
|
|
120
|
+
*/
|
|
121
|
+
export type DtoKey = CommandClass<AnyCommand> | EventClass<DomainEvent> | EventDescriptor | CommandDescriptor;
|
|
122
|
+
/** What a service registered for `K` receives. */
|
|
123
|
+
export type ExecutableOf<K> = K extends abstract new (...args: never[]) => infer E ? E extends Executable ? E : never : K extends CommandDescriptor ? RemoteCommand : K extends EventDescriptor ? RemoteEvent : never;
|
|
62
124
|
export {};
|
package/dist/core/domain/dto.js
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* class CommandCancelIssue extends Command<ResponseCancelIssue> {
|
|
6
6
|
* constructor(readonly issue_id: string) { super(); }
|
|
7
7
|
* }
|
|
8
|
-
* const answer = await
|
|
8
|
+
* const answer = await projectBoundedContext.execute(new CommandCancelIssue("x")); // ResponseCancelIssue
|
|
9
9
|
*
|
|
10
10
|
* Context: the backend asks `bus(dto, ResponseType)` for the type; here the Command carries it, so
|
|
11
11
|
* `execute` infers it with no second argument. The association is type-only: absent from a
|
|
@@ -16,6 +16,12 @@ export class DataTransferObject {
|
|
|
16
16
|
}
|
|
17
17
|
export class Response extends DataTransferObject {
|
|
18
18
|
}
|
|
19
|
+
/**
|
|
20
|
+
* A request for work, to one bus. Its identity is the framework's, taken when a bounded context
|
|
21
|
+
* registers it — `<context>.<Command>`, as the backend's `registered_name` —: what its message
|
|
22
|
+
* says (`type`), what the receiving bus finds its class by, and what a generated contract's
|
|
23
|
+
* `operations` carry. A production build keeps class names; `static commandName` fixes another.
|
|
24
|
+
*/
|
|
19
25
|
export class Command extends DataTransferObject {
|
|
20
26
|
}
|
|
21
27
|
/**
|
|
@@ -27,12 +33,12 @@ export class Command extends DataTransferObject {
|
|
|
27
33
|
* static readonly eventName = "shop.order.v1.paid";
|
|
28
34
|
* constructor(readonly order_id: string) { super(); }
|
|
29
35
|
* }
|
|
30
|
-
*
|
|
31
|
-
* await
|
|
36
|
+
* projectBoundedContext.feature(OrderPaid)(NotifyCustomer);
|
|
37
|
+
* await projectBoundedContext.execute(new OrderPaid("o-1"));
|
|
32
38
|
*
|
|
33
39
|
* Context: the backend names an event on the wire by `name`, `<context>.<aggregate>.vN.<fact>`,
|
|
34
|
-
* and defaults it to the class's name;
|
|
35
|
-
*
|
|
40
|
+
* and defaults it to the class's name; here it is declared: it is the contract other processes
|
|
41
|
+
* read, with its version, not a class's name. `name` is reserved, as the backend reserves it: every event reads its class's
|
|
36
42
|
* `eventName` there, and a subclass cannot make it a field.
|
|
37
43
|
*/
|
|
38
44
|
export class DomainEvent extends DataTransferObject {
|
|
@@ -41,3 +47,17 @@ export class DomainEvent extends DataTransferObject {
|
|
|
41
47
|
return this.constructor.eventName ?? "";
|
|
42
48
|
}
|
|
43
49
|
}
|
|
50
|
+
/** An event's wire name: dotted, as the backend's `<context>.<aggregate>.vN.<fact>`. */
|
|
51
|
+
export const EVENT_NAME = /^[\w-]+(\.[\w-]+)+$/;
|
|
52
|
+
/** What a generated Command carries beside its input: the contract's identity. Never serialized with its data. */
|
|
53
|
+
export const COMMAND_IDENTITY = Symbol.for("sincpro.client-framework.command");
|
|
54
|
+
/**
|
|
55
|
+
* The generated Command `operation` names, with `input`, as the bus executes it:
|
|
56
|
+
*
|
|
57
|
+
* await projectBoundedContext.execute(commandOf(operations.CommandSendIssue, input));
|
|
58
|
+
*/
|
|
59
|
+
export function commandOf(operation, input) {
|
|
60
|
+
const command = { ...input };
|
|
61
|
+
Object.defineProperty(command, COMMAND_IDENTITY, { value: operation.identity });
|
|
62
|
+
return command;
|
|
63
|
+
}
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* What answers a use case's failure instead of raising it — sincpro_framework's error handlers.
|
|
3
3
|
*
|
|
4
|
-
*
|
|
4
|
+
* projectBoundedContext.addFeatureErrorHandler((error) => {
|
|
5
5
|
* if (error instanceof IssueNotFound) return new ResponseEmpty(); // the answer
|
|
6
6
|
* throw error; // the next handler
|
|
7
7
|
* });
|
|
8
8
|
*
|
|
9
|
-
* Context: three chains per bounded context — global (around `
|
|
9
|
+
* Context: three chains per bounded context — global (around `projectBoundedContext.execute`), feature and
|
|
10
10
|
* application service —, each run first-registered-first. A handler that throws hands what it
|
|
11
11
|
* threw to the next; the last one's throw reaches the caller. The failure was already recorded
|
|
12
12
|
* where it happened (its span, its log line and, when unexpected, its report): a handler decides
|
|
@@ -8,11 +8,11 @@ import type { Outcome } from "../../observability/domain/tracing.js";
|
|
|
8
8
|
* control interceptors wrap the service: look, veto, adjust or answer
|
|
9
9
|
* observation listeners a run started, answered, or a failure escaped: heard, never changed
|
|
10
10
|
*
|
|
11
|
-
*
|
|
11
|
+
* projectBoundedContext.interceptor(CommandPay)(async (command, next, run) => {
|
|
12
12
|
* if (!credit.allows(command.customer_id)) throw new NoCredit(); // veto
|
|
13
13
|
* return next(command); // the next, or the service
|
|
14
14
|
* });
|
|
15
|
-
*
|
|
15
|
+
* projectBoundedContext.onCompletion((done) => activity.succeeded(done.execution.useCase));
|
|
16
16
|
*
|
|
17
17
|
* Context: the core owns the flow — context, span, log lines, outcome, report, handlers — and
|
|
18
18
|
* these are the places an application changes it from outside, without touching a service. An
|
|
@@ -23,16 +23,19 @@ import type { Outcome } from "../../observability/domain/tracing.js";
|
|
|
23
23
|
export type PipelineLayer = "feature" | "application_service";
|
|
24
24
|
/** What caused a run from outside: an event delivered with its envelope continues its chain. */
|
|
25
25
|
export type Cause = {
|
|
26
|
-
/** The id of what caused it — a delivered
|
|
26
|
+
/** The id of what caused it — a delivered message's. */
|
|
27
27
|
readonly id: string;
|
|
28
28
|
/** The chain it continues. */
|
|
29
29
|
readonly correlationId: string;
|
|
30
|
+
/** The trace it continues (W3C), when the message carried one: the run's span is its child. */
|
|
31
|
+
readonly traceparent?: string;
|
|
32
|
+
readonly tracestate?: string;
|
|
30
33
|
};
|
|
31
34
|
/**
|
|
32
35
|
* How a Command or an event is executed: what cancels it, and — for a delivered event — what
|
|
33
36
|
* caused it.
|
|
34
37
|
*
|
|
35
|
-
* await
|
|
38
|
+
* await salesBoundedContext.execute(new CommandQuote(3), { signal: screen.signal });
|
|
36
39
|
*
|
|
37
40
|
* Context: a screen that closes, a navigation that leaves, a user who gives up stop waiting; the
|
|
38
41
|
* run ends `aborted` (not reported), its Features inherit the signal, and the requests its
|
|
@@ -45,7 +48,7 @@ export type ExecuteOptions = {
|
|
|
45
48
|
/** The run an interceptor wraps: who runs, inside which execution, writing where. */
|
|
46
49
|
export type InterceptedRun = {
|
|
47
50
|
readonly boundedContext: string;
|
|
48
|
-
/** `<bounded context>.<
|
|
51
|
+
/** `<bounded context>.<Service>`: the service's name as registered. */
|
|
49
52
|
readonly useCase: string;
|
|
50
53
|
readonly layer: PipelineLayer;
|
|
51
54
|
/** Its id, span, context and chain. */
|
|
@@ -1,17 +1,18 @@
|
|
|
1
1
|
import type { Execution } from "../../context/domain/execution.js";
|
|
2
2
|
import type { ContextValues } from "../../context/domain/store.js";
|
|
3
3
|
import type { FeatureBus } from "./bus.js";
|
|
4
|
-
import type { Command, DomainEvent } from "./dto.js";
|
|
4
|
+
import type { Command, DomainEvent, RemoteCommand, RemoteEvent } from "./dto.js";
|
|
5
5
|
import type { LayerLogger } from "../../observability/domain/logging.js";
|
|
6
6
|
import type { Span } from "../../observability/domain/tracing.js";
|
|
7
|
+
/** What a service executes: a Command, a domain event, or a generated one. */
|
|
8
|
+
export type ServiceInput<R> = Command<R> | DomainEvent | RemoteEvent | RemoteCommand;
|
|
7
9
|
/**
|
|
8
|
-
* The services of the application layer, as sincpro_framework's: a Feature executes
|
|
9
|
-
* registered
|
|
10
|
-
* context's Features.
|
|
10
|
+
* The services of the application layer, as sincpro_framework's: a Feature executes the DTOs it
|
|
11
|
+
* is registered for — Commands, domain events, generated ones —; an ApplicationService composes
|
|
12
|
+
* its context's Features.
|
|
11
13
|
*
|
|
12
|
-
* @
|
|
14
|
+
* @projectBoundedContext.feature(CommandCancelIssue)
|
|
13
15
|
* class CancelIssue extends Feature<CommandCancelIssue, ResponseCancelIssue> {
|
|
14
|
-
* static readonly serviceName = "CancelIssue";
|
|
15
16
|
* async execute(command: CommandCancelIssue) {
|
|
16
17
|
* this.logger.info("Cancelling"); // bound to this run
|
|
17
18
|
* return new ResponseCancelIssue(await this.client.cancelIssue(command));
|
|
@@ -19,10 +20,10 @@ import type { Span } from "../../observability/domain/tracing.js";
|
|
|
19
20
|
* }
|
|
20
21
|
*
|
|
21
22
|
* Context: while a method runs, `this.context`, `this.span`, `this.logger`, `this.featureBus` and
|
|
22
|
-
* every dependency are that run's (`executeService`).
|
|
23
|
-
*
|
|
23
|
+
* every dependency are that run's (`executeService`). Its name — its use case in spans and
|
|
24
|
+
* logs — is the framework's to take when it is registered: nothing on the class declares it.
|
|
24
25
|
*/
|
|
25
|
-
export declare abstract class Feature<C extends
|
|
26
|
+
export declare abstract class Feature<C extends ServiceInput<R>, R, Context extends object = ContextValues> {
|
|
26
27
|
/** What this run reads: the process's context, its bounded context's, its scope's. */
|
|
27
28
|
readonly context: Readonly<Partial<Context>>;
|
|
28
29
|
/** This run's span: `this.span.set({...})` says more about the use case. */
|
|
@@ -32,16 +33,42 @@ export declare abstract class Feature<C extends Command<R> | DomainEvent, R, Con
|
|
|
32
33
|
/** What cancels this run, when it was executed with one: check it in a long loop. */
|
|
33
34
|
readonly signal: AbortSignal | undefined;
|
|
34
35
|
/**
|
|
35
|
-
* This run: its id, use case, flow (`correlationId`) and cause. A
|
|
36
|
-
*
|
|
37
|
-
* harmless.
|
|
36
|
+
* This run: its id, use case, flow (`correlationId`) and cause. A delivered event or Command
|
|
37
|
+
* finds its message's id as `causationId` — the key that makes executing it twice harmless.
|
|
38
38
|
*/
|
|
39
39
|
readonly execution: Execution;
|
|
40
40
|
abstract execute(command: C): R | Promise<R>;
|
|
41
41
|
}
|
|
42
|
-
export declare abstract class ApplicationService<C extends
|
|
42
|
+
export declare abstract class ApplicationService<C extends ServiceInput<R>, R, Context extends object = ContextValues> extends Feature<C, R, Context> {
|
|
43
43
|
/** The context's Features, run as children of this execution. */
|
|
44
44
|
protected readonly featureBus: FeatureBus;
|
|
45
45
|
}
|
|
46
|
+
/** A Feature base whose instances find every dependency of `D` on `this`, typed. */
|
|
47
|
+
export type FeatureWith<D extends object, Context extends object = ContextValues> = abstract new <C extends ServiceInput<R>, R>() => Feature<C, R, Context> & Readonly<D>;
|
|
48
|
+
/** An ApplicationService base whose instances find every dependency of `D` on `this`, typed. */
|
|
49
|
+
export type ApplicationServiceWith<D extends object, Context extends object = ContextValues> = abstract new <C extends ServiceInput<R>, R>() => ApplicationService<C, R, Context> & Readonly<D>;
|
|
50
|
+
/**
|
|
51
|
+
* The Feature and ApplicationService of a bounded context, typed with its dependencies — the
|
|
52
|
+
* backend's `class Feature(_Feature, DependencyContextType)`, in its `infrastructure/framework.py`:
|
|
53
|
+
*
|
|
54
|
+
* // infrastructure/framework.ts
|
|
55
|
+
* export const { Feature, ApplicationService } = servicesWith<HelpdeskDependencies>();
|
|
56
|
+
*
|
|
57
|
+
* // services/ticket/update_stage.feature.ts
|
|
58
|
+
* @helpdeskBoundedContext.feature(CommandUpdateStage)
|
|
59
|
+
* export class UpdateStage extends Feature<CommandUpdateStage, ResponseUpdateStage> {
|
|
60
|
+
* async execute(command: CommandUpdateStage) {
|
|
61
|
+
* return this.odooWrite.stage(command); // typed, declared nowhere else
|
|
62
|
+
* }
|
|
63
|
+
* }
|
|
64
|
+
*
|
|
65
|
+
* Context: a service never declares its dependencies again: they are the bounded context's, and
|
|
66
|
+
* `addDependency` supplies them at runtime (a missing one fails explicitly). This is the same
|
|
67
|
+
* class, typed — no other runtime behaviour.
|
|
68
|
+
*/
|
|
69
|
+
export declare function servicesWith<D extends object, Context extends object = ContextValues>(): {
|
|
70
|
+
Feature: FeatureWith<D, Context>;
|
|
71
|
+
ApplicationService: ApplicationServiceWith<D, Context>;
|
|
72
|
+
};
|
|
46
73
|
/** @internal — what a run finds on `this` that no dependency may take. */
|
|
47
74
|
export declare const RESERVED_MEMBERS: ReadonlySet<string>;
|
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* The services of the application layer, as sincpro_framework's: a Feature executes
|
|
3
|
-
* registered
|
|
4
|
-
* context's Features.
|
|
2
|
+
* The services of the application layer, as sincpro_framework's: a Feature executes the DTOs it
|
|
3
|
+
* is registered for — Commands, domain events, generated ones —; an ApplicationService composes
|
|
4
|
+
* its context's Features.
|
|
5
5
|
*
|
|
6
|
-
* @
|
|
6
|
+
* @projectBoundedContext.feature(CommandCancelIssue)
|
|
7
7
|
* class CancelIssue extends Feature<CommandCancelIssue, ResponseCancelIssue> {
|
|
8
|
-
* static readonly serviceName = "CancelIssue";
|
|
9
8
|
* async execute(command: CommandCancelIssue) {
|
|
10
9
|
* this.logger.info("Cancelling"); // bound to this run
|
|
11
10
|
* return new ResponseCancelIssue(await this.client.cancelIssue(command));
|
|
@@ -13,13 +12,38 @@
|
|
|
13
12
|
* }
|
|
14
13
|
*
|
|
15
14
|
* Context: while a method runs, `this.context`, `this.span`, `this.logger`, `this.featureBus` and
|
|
16
|
-
* every dependency are that run's (`executeService`).
|
|
17
|
-
*
|
|
15
|
+
* every dependency are that run's (`executeService`). Its name — its use case in spans and
|
|
16
|
+
* logs — is the framework's to take when it is registered: nothing on the class declares it.
|
|
18
17
|
*/
|
|
19
18
|
export class Feature {
|
|
20
19
|
}
|
|
21
20
|
export class ApplicationService extends Feature {
|
|
22
21
|
}
|
|
22
|
+
/**
|
|
23
|
+
* The Feature and ApplicationService of a bounded context, typed with its dependencies — the
|
|
24
|
+
* backend's `class Feature(_Feature, DependencyContextType)`, in its `infrastructure/framework.py`:
|
|
25
|
+
*
|
|
26
|
+
* // infrastructure/framework.ts
|
|
27
|
+
* export const { Feature, ApplicationService } = servicesWith<HelpdeskDependencies>();
|
|
28
|
+
*
|
|
29
|
+
* // services/ticket/update_stage.feature.ts
|
|
30
|
+
* @helpdeskBoundedContext.feature(CommandUpdateStage)
|
|
31
|
+
* export class UpdateStage extends Feature<CommandUpdateStage, ResponseUpdateStage> {
|
|
32
|
+
* async execute(command: CommandUpdateStage) {
|
|
33
|
+
* return this.odooWrite.stage(command); // typed, declared nowhere else
|
|
34
|
+
* }
|
|
35
|
+
* }
|
|
36
|
+
*
|
|
37
|
+
* Context: a service never declares its dependencies again: they are the bounded context's, and
|
|
38
|
+
* `addDependency` supplies them at runtime (a missing one fails explicitly). This is the same
|
|
39
|
+
* class, typed — no other runtime behaviour.
|
|
40
|
+
*/
|
|
41
|
+
export function servicesWith() {
|
|
42
|
+
return {
|
|
43
|
+
Feature: Feature,
|
|
44
|
+
ApplicationService: ApplicationService,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
23
47
|
/** @internal — what a run finds on `this` that no dependency may take. */
|
|
24
48
|
export const RESERVED_MEMBERS = new Set([
|
|
25
49
|
"featureBus",
|