@schemeless/event-store-types 3.2.2 → 5.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.
- package/dist/Errors.d.ts +41 -0
- package/dist/Errors.js +67 -1
- package/dist/EventStore.types.d.ts +43 -13
- package/package.json +2 -2
package/dist/Errors.d.ts
CHANGED
|
@@ -4,3 +4,44 @@ export declare class ConcurrencyError extends Error {
|
|
|
4
4
|
readonly actualSequence: number;
|
|
5
5
|
constructor(streamKey: string, expectedSequence: number, actualSequence: number);
|
|
6
6
|
}
|
|
7
|
+
export declare class EventStoreError extends Error {
|
|
8
|
+
constructor(message: string);
|
|
9
|
+
}
|
|
10
|
+
export declare class ValidationError extends EventStoreError {
|
|
11
|
+
readonly flow: {
|
|
12
|
+
domain: string;
|
|
13
|
+
type: string;
|
|
14
|
+
};
|
|
15
|
+
readonly eventId: string;
|
|
16
|
+
readonly cause: Error;
|
|
17
|
+
constructor(flow: {
|
|
18
|
+
domain: string;
|
|
19
|
+
type: string;
|
|
20
|
+
}, eventId: string, cause: Error);
|
|
21
|
+
}
|
|
22
|
+
export declare class FlowNotFoundError extends EventStoreError {
|
|
23
|
+
readonly domain: string;
|
|
24
|
+
readonly type: string;
|
|
25
|
+
constructor(domain: string, type: string);
|
|
26
|
+
}
|
|
27
|
+
export declare class AggregateError extends EventStoreError {
|
|
28
|
+
readonly flow: {
|
|
29
|
+
domain: string;
|
|
30
|
+
type: string;
|
|
31
|
+
};
|
|
32
|
+
readonly reason: 'no_loader' | 'no_identifier' | 'apply_must_return_state' | 'capability_missing';
|
|
33
|
+
constructor(flow: {
|
|
34
|
+
domain: string;
|
|
35
|
+
type: string;
|
|
36
|
+
}, reason: 'no_loader' | 'no_identifier' | 'apply_must_return_state' | 'capability_missing');
|
|
37
|
+
}
|
|
38
|
+
export declare class ShutdownTimeoutError extends EventStoreError {
|
|
39
|
+
readonly timeoutMs: number;
|
|
40
|
+
constructor(timeoutMs: number);
|
|
41
|
+
}
|
|
42
|
+
export declare class RevertError extends EventStoreError {
|
|
43
|
+
readonly eventId: string;
|
|
44
|
+
readonly reason: 'not_found' | 'not_root' | 'missing_compensate' | 'repo_not_supported';
|
|
45
|
+
readonly details?: string;
|
|
46
|
+
constructor(eventId: string, reason: 'not_found' | 'not_root' | 'missing_compensate' | 'repo_not_supported', details?: string);
|
|
47
|
+
}
|
package/dist/Errors.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ConcurrencyError = void 0;
|
|
3
|
+
exports.RevertError = exports.ShutdownTimeoutError = exports.AggregateError = exports.FlowNotFoundError = exports.ValidationError = exports.EventStoreError = exports.ConcurrencyError = void 0;
|
|
4
4
|
class ConcurrencyError extends Error {
|
|
5
5
|
constructor(streamKey, expectedSequence, actualSequence) {
|
|
6
6
|
super(`Concurrency conflict on stream "${streamKey}": expected sequence ${expectedSequence}, but found ${actualSequence}`);
|
|
@@ -11,3 +11,69 @@ class ConcurrencyError extends Error {
|
|
|
11
11
|
}
|
|
12
12
|
}
|
|
13
13
|
exports.ConcurrencyError = ConcurrencyError;
|
|
14
|
+
class EventStoreError extends Error {
|
|
15
|
+
constructor(message) {
|
|
16
|
+
super(message);
|
|
17
|
+
this.name = 'EventStoreError';
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
exports.EventStoreError = EventStoreError;
|
|
21
|
+
class ValidationError extends EventStoreError {
|
|
22
|
+
constructor(flow, eventId, cause) {
|
|
23
|
+
super(`Validation failed for ${flow.domain}/${flow.type} (event ${eventId}): ${cause.message}`);
|
|
24
|
+
this.flow = flow;
|
|
25
|
+
this.eventId = eventId;
|
|
26
|
+
this.cause = cause;
|
|
27
|
+
this.name = 'ValidationError';
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
exports.ValidationError = ValidationError;
|
|
31
|
+
class FlowNotFoundError extends EventStoreError {
|
|
32
|
+
constructor(domain, type) {
|
|
33
|
+
super(`No EventFlow registered for "${domain}/${type}". ` +
|
|
34
|
+
`Make sure the EventFlow is included in the eventFlows array passed to makeEventStore.`);
|
|
35
|
+
this.domain = domain;
|
|
36
|
+
this.type = type;
|
|
37
|
+
this.name = 'FlowNotFoundError';
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
exports.FlowNotFoundError = FlowNotFoundError;
|
|
41
|
+
class AggregateError extends EventStoreError {
|
|
42
|
+
constructor(flow, reason) {
|
|
43
|
+
const messages = {
|
|
44
|
+
no_loader: 'requires getAggregate() support to load state',
|
|
45
|
+
no_identifier: 'requires an identifier (set event.identifier or aggregate.getIdentifier)',
|
|
46
|
+
apply_must_return_state: 'apply() must return the new state object',
|
|
47
|
+
capability_missing: 'requires adapter with getStreamEvents() support',
|
|
48
|
+
};
|
|
49
|
+
super(`AggregateEventFlow ${flow.domain}/${flow.type}: ${messages[reason]}`);
|
|
50
|
+
this.flow = flow;
|
|
51
|
+
this.reason = reason;
|
|
52
|
+
this.name = 'AggregateError';
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
exports.AggregateError = AggregateError;
|
|
56
|
+
class ShutdownTimeoutError extends EventStoreError {
|
|
57
|
+
constructor(timeoutMs) {
|
|
58
|
+
super(`EventStore shutdown timed out after ${timeoutMs}ms`);
|
|
59
|
+
this.timeoutMs = timeoutMs;
|
|
60
|
+
this.name = 'ShutdownTimeoutError';
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
exports.ShutdownTimeoutError = ShutdownTimeoutError;
|
|
64
|
+
class RevertError extends EventStoreError {
|
|
65
|
+
constructor(eventId, reason, details) {
|
|
66
|
+
const messages = {
|
|
67
|
+
not_found: `Event not found: ${eventId}`,
|
|
68
|
+
not_root: `Event ${eventId} is not a root event. Revert the root event instead.`,
|
|
69
|
+
missing_compensate: `Some events in the tree lack a 'compensate' hook`,
|
|
70
|
+
repo_not_supported: 'Repository must implement getEventById and findByCausationId',
|
|
71
|
+
};
|
|
72
|
+
super(messages[reason] + (details ? `. ${details}` : ''));
|
|
73
|
+
this.eventId = eventId;
|
|
74
|
+
this.reason = reason;
|
|
75
|
+
this.details = details;
|
|
76
|
+
this.name = 'RevertError';
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
exports.RevertError = RevertError;
|
|
@@ -43,14 +43,15 @@ export interface CreatedEvent<Payload, META extends EventMeta = EventMeta> exten
|
|
|
43
43
|
export interface StoredEvent<Payload, META extends EventMeta = EventMeta> extends CreatedEvent<Payload, META> {
|
|
44
44
|
}
|
|
45
45
|
export type Event<Payload, META extends EventMeta = EventMeta> = StoredEvent<Payload, META>;
|
|
46
|
-
export interface EventFlow<PartialPayload = any, Payload extends PartialPayload = PartialPayload> {
|
|
46
|
+
export interface EventFlow<PartialPayload = any, Payload extends PartialPayload = PartialPayload, META extends EventMeta = EventMeta> {
|
|
47
|
+
readonly kind?: 'simple';
|
|
47
48
|
readonly domain: string;
|
|
48
49
|
readonly type: string;
|
|
49
50
|
readonly description?: string;
|
|
50
51
|
readonly meta?: {
|
|
51
52
|
readonly sideEffectFailedRetryAllowed?: number;
|
|
52
53
|
};
|
|
53
|
-
readonly eventType?: CreatedEvent<Payload>;
|
|
54
|
+
readonly eventType?: CreatedEvent<Payload, META>;
|
|
54
55
|
readonly payloadType?: PartialPayload | Payload;
|
|
55
56
|
readonly samplePayload?: PartialPayload | Payload;
|
|
56
57
|
/**
|
|
@@ -67,7 +68,7 @@ export interface EventFlow<PartialPayload = any, Payload extends PartialPayload
|
|
|
67
68
|
* @param fromVersion - The version stored in event.meta.schemaVersion (defaults to 1)
|
|
68
69
|
* @returns Updated event with migrated payload, or void to use the original
|
|
69
70
|
*/
|
|
70
|
-
readonly upcast?: (event: CreatedEvent<any>, fromVersion: number) => CreatedEvent<Payload> | Promise<CreatedEvent<Payload>> | void;
|
|
71
|
+
readonly upcast?: (event: CreatedEvent<any, META>, fromVersion: number) => CreatedEvent<Payload, META> | Promise<CreatedEvent<Payload, META>> | void;
|
|
71
72
|
/**
|
|
72
73
|
* Extract the shard key for event routing.
|
|
73
74
|
* Events with the same shard key will be processed sequentially in the same partition.
|
|
@@ -78,16 +79,21 @@ export interface EventFlow<PartialPayload = any, Payload extends PartialPayload
|
|
|
78
79
|
* @example
|
|
79
80
|
* getShardKey: (event) => event.payload.userId
|
|
80
81
|
*/
|
|
81
|
-
readonly getShardKey?: (event: BaseEvent<Payload>) => string | undefined;
|
|
82
|
+
readonly getShardKey?: (event: BaseEvent<Payload, META>) => string | undefined;
|
|
83
|
+
/**
|
|
84
|
+
* @deprecated The `receive` pattern will be replaced in v5.
|
|
85
|
+
* Instead of `const send = MyEvent.receive(eventStore); await send(input);`
|
|
86
|
+
* use `await eventStore.submit(MyEvent, input);` directly.
|
|
87
|
+
*/
|
|
82
88
|
readonly receive: (eventStore: {
|
|
83
|
-
receive: (eventFlow: EventFlow<PartialPayload, Payload>) => (eventInput: BaseEventInput<PartialPayload>) => Promise<[CreatedEvent<Payload>, ...Array<CreatedEvent<any>>]>;
|
|
84
|
-
}) => (eventInputArgs: BaseEventInput<PartialPayload>) => Promise<[CreatedEvent<Payload>, ...Array<CreatedEvent<any>>]>;
|
|
85
|
-
readonly validate?: (event: CreatedEvent<Payload>) => Promise<Error | void> | Error | void;
|
|
86
|
-
readonly preApply?: (event: CreatedEvent<PartialPayload>) => Promise<CreatedEvent<Payload> | void> | CreatedEvent<Payload> | void;
|
|
87
|
-
readonly apply?: (event: CreatedEvent<Payload>) => Promise<void> | void;
|
|
88
|
-
readonly sideEffect?: (event: CreatedEvent<Payload>) => Promise<void | BaseEvent<any>[]> | void | BaseEvent<any>[];
|
|
89
|
-
readonly cancelApply?: (event: CreatedEvent<Payload>) => Promise<void> | void;
|
|
90
|
-
readonly createConsequentEvents?: (causalEvent: CreatedEvent<Payload>) => Promise<BaseEvent<any>[]> | BaseEvent<any>[];
|
|
89
|
+
receive: (eventFlow: EventFlow<PartialPayload, Payload, META>) => (eventInput: BaseEventInput<PartialPayload, META>) => Promise<[CreatedEvent<Payload, META>, ...Array<CreatedEvent<any>>]>;
|
|
90
|
+
}) => (eventInputArgs: BaseEventInput<PartialPayload, META>) => Promise<[CreatedEvent<Payload, META>, ...Array<CreatedEvent<any>>]>;
|
|
91
|
+
readonly validate?: (event: CreatedEvent<Payload, META>) => Promise<Error | void> | Error | void;
|
|
92
|
+
readonly preApply?: (event: CreatedEvent<PartialPayload, META>) => Promise<CreatedEvent<Payload, META> | void> | CreatedEvent<Payload, META> | void;
|
|
93
|
+
readonly apply?: (event: CreatedEvent<Payload, META>) => Promise<void> | void;
|
|
94
|
+
readonly sideEffect?: (event: CreatedEvent<Payload, META>) => Promise<void | BaseEvent<any>[]> | void | BaseEvent<any>[];
|
|
95
|
+
readonly cancelApply?: (event: CreatedEvent<Payload, META>) => Promise<void> | void;
|
|
96
|
+
readonly createConsequentEvents?: (causalEvent: CreatedEvent<Payload, META>) => Promise<BaseEvent<any>[]> | BaseEvent<any>[];
|
|
91
97
|
/**
|
|
92
98
|
* Generates compensating event(s) to reverse this event's effects.
|
|
93
99
|
* Called by the framework during revert operations.
|
|
@@ -99,7 +105,22 @@ export interface EventFlow<PartialPayload = any, Payload extends PartialPayload
|
|
|
99
105
|
* @param originalEvent - The event being reverted
|
|
100
106
|
* @returns Compensating event(s) to persist
|
|
101
107
|
*/
|
|
102
|
-
readonly compensate?: (originalEvent: CreatedEvent<Payload>) => BaseEvent<any> | BaseEvent<any>[];
|
|
108
|
+
readonly compensate?: (originalEvent: CreatedEvent<Payload, META>) => BaseEvent<any> | BaseEvent<any>[];
|
|
109
|
+
}
|
|
110
|
+
export interface AggregateConfig<State> {
|
|
111
|
+
readonly reducer: (state: State, event: CreatedEvent<any>) => State;
|
|
112
|
+
readonly initialState: State;
|
|
113
|
+
readonly getIdentifier?: (event: BaseEvent<any>) => string | undefined;
|
|
114
|
+
}
|
|
115
|
+
export interface AggregateEventFlow<PartialPayload = any, Payload extends PartialPayload = PartialPayload, State = any, META extends EventMeta = EventMeta> extends Omit<EventFlow<PartialPayload, Payload, META>, 'validate' | 'apply' | 'kind'> {
|
|
116
|
+
readonly kind?: 'aggregate';
|
|
117
|
+
readonly aggregate: AggregateConfig<State>;
|
|
118
|
+
readonly validate?: (event: CreatedEvent<Payload, META>, state: State) => Promise<Error | void> | Error | void;
|
|
119
|
+
readonly apply?: (event: CreatedEvent<Payload, META>, state: State) => State | Promise<State>;
|
|
120
|
+
}
|
|
121
|
+
export interface AggregateEventObserver<Payload = any, State = any> extends Omit<SuccessEventObserver<Payload>, 'apply'> {
|
|
122
|
+
readonly aggregate: true;
|
|
123
|
+
readonly apply?: (event: CreatedEvent<Payload>, state: State) => Promise<void> | void;
|
|
103
124
|
}
|
|
104
125
|
export type EventTaskAndError = {
|
|
105
126
|
task: CreatedEvent<any>;
|
|
@@ -139,4 +160,13 @@ export declare enum EventOutputState {
|
|
|
139
160
|
export declare enum EventObserverState {
|
|
140
161
|
success = "Observer:success"
|
|
141
162
|
}
|
|
163
|
+
/**
|
|
164
|
+
* Use this type when defining new EventFlows.
|
|
165
|
+
* The `kind` field enables TypeScript to narrow the type automatically.
|
|
166
|
+
*/
|
|
167
|
+
export type TypedEventFlow<P = any, S = any> = (EventFlow<P, P> & {
|
|
168
|
+
readonly kind: 'simple';
|
|
169
|
+
}) | (AggregateEventFlow<P, P, S> & {
|
|
170
|
+
readonly kind: 'aggregate';
|
|
171
|
+
});
|
|
142
172
|
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@schemeless/event-store-types",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "5.0.0",
|
|
4
4
|
"typescript:main": "src/index.ts",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -34,5 +34,5 @@
|
|
|
34
34
|
"publishConfig": {
|
|
35
35
|
"access": "public"
|
|
36
36
|
},
|
|
37
|
-
"gitHead": "
|
|
37
|
+
"gitHead": "b391c1569bd4416b1aca13037d1a20a4d5899f25"
|
|
38
38
|
}
|