@signaltree/events 7.3.5 → 7.4.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/angular/handlers.cjs +38 -0
- package/dist/angular/handlers.js +35 -0
- package/dist/angular/index.cjs +15 -0
- package/dist/angular/index.js +3 -0
- package/dist/angular/optimistic-updates.cjs +161 -0
- package/dist/angular/optimistic-updates.js +159 -0
- package/dist/angular/websocket.service.cjs +357 -0
- package/{angular.esm.js → dist/angular/websocket.service.js} +1 -191
- package/dist/core/error-classification.cjs +282 -0
- package/dist/core/error-classification.js +276 -0
- package/dist/core/factory.cjs +148 -0
- package/{factory.esm.js → dist/core/factory.js} +2 -37
- package/dist/core/idempotency.cjs +252 -0
- package/dist/core/idempotency.js +247 -0
- package/dist/core/registry.cjs +183 -0
- package/dist/core/registry.js +180 -0
- package/dist/core/types.cjs +41 -0
- package/dist/core/types.js +38 -0
- package/dist/core/validation.cjs +185 -0
- package/{index.esm.js → dist/core/validation.js} +1 -4
- package/dist/index.cjs +43 -0
- package/dist/index.js +7 -0
- package/dist/nestjs/base.subscriber.cjs +287 -0
- package/dist/nestjs/base.subscriber.js +287 -0
- package/dist/nestjs/decorators.cjs +35 -0
- package/dist/nestjs/decorators.js +32 -0
- package/dist/nestjs/dlq.service.cjs +249 -0
- package/dist/nestjs/dlq.service.js +249 -0
- package/dist/nestjs/event-bus.module.cjs +152 -0
- package/dist/nestjs/event-bus.module.js +152 -0
- package/dist/nestjs/event-bus.service.cjs +243 -0
- package/dist/nestjs/event-bus.service.js +243 -0
- package/dist/nestjs/index.cjs +33 -0
- package/dist/nestjs/index.js +6 -0
- package/dist/nestjs/tokens.cjs +14 -0
- package/dist/nestjs/tokens.js +9 -0
- package/dist/testing/assertions.cjs +172 -0
- package/dist/testing/assertions.js +169 -0
- package/dist/testing/factories.cjs +122 -0
- package/dist/testing/factories.js +119 -0
- package/dist/testing/helpers.cjs +233 -0
- package/dist/testing/helpers.js +227 -0
- package/dist/testing/index.cjs +20 -0
- package/dist/testing/index.js +4 -0
- package/dist/testing/mock-event-bus.cjs +237 -0
- package/dist/testing/mock-event-bus.js +234 -0
- package/package.json +22 -22
- package/angular.d.ts +0 -1
- package/idempotency.esm.js +0 -701
- package/index.d.ts +0 -1
- package/nestjs.d.ts +0 -1
- package/nestjs.esm.js +0 -944
- package/testing.d.ts +0 -1
- package/testing.esm.js +0 -743
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Event Registry class - manages all registered event types
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```typescript
|
|
8
|
+
* const registry = createEventRegistry();
|
|
9
|
+
*
|
|
10
|
+
* // Register events
|
|
11
|
+
* registry.register({
|
|
12
|
+
* type: 'TradeProposalCreated',
|
|
13
|
+
* schema: TradeProposalCreatedSchema,
|
|
14
|
+
* priority: 'high',
|
|
15
|
+
* description: 'Emitted when a user proposes a trade',
|
|
16
|
+
* category: 'trade',
|
|
17
|
+
* });
|
|
18
|
+
*
|
|
19
|
+
* // Validate events
|
|
20
|
+
* const validated = registry.validate(rawEvent);
|
|
21
|
+
*
|
|
22
|
+
* // Get schema for event type
|
|
23
|
+
* const schema = registry.getSchema('TradeProposalCreated');
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
class EventRegistry {
|
|
27
|
+
events = new Map();
|
|
28
|
+
config;
|
|
29
|
+
constructor(config = {}) {
|
|
30
|
+
this.config = {
|
|
31
|
+
strict: config.strict ?? false,
|
|
32
|
+
warnOnDeprecated: config.warnOnDeprecated ?? true
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Register an event type with its schema
|
|
37
|
+
*/
|
|
38
|
+
register(event) {
|
|
39
|
+
if (this.events.has(event.type)) {
|
|
40
|
+
throw new Error(`Event type '${event.type}' is already registered`);
|
|
41
|
+
}
|
|
42
|
+
this.events.set(event.type, event);
|
|
43
|
+
return this;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Register multiple events at once
|
|
47
|
+
*/
|
|
48
|
+
registerMany(events) {
|
|
49
|
+
for (const event of events) {
|
|
50
|
+
this.register(event);
|
|
51
|
+
}
|
|
52
|
+
return this;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Get schema for an event type
|
|
56
|
+
*/
|
|
57
|
+
getSchema(type) {
|
|
58
|
+
return this.events.get(type)?.schema;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Get registered event info
|
|
62
|
+
*/
|
|
63
|
+
getEvent(type) {
|
|
64
|
+
return this.events.get(type);
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Check if event type is registered
|
|
68
|
+
*/
|
|
69
|
+
has(type) {
|
|
70
|
+
return this.events.has(type);
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Get default priority for event type
|
|
74
|
+
*/
|
|
75
|
+
getPriority(type) {
|
|
76
|
+
return this.events.get(type)?.priority ?? 'normal';
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Validate an event against its registered schema
|
|
80
|
+
*
|
|
81
|
+
* @throws Error if event type is unknown (in strict mode) or validation fails
|
|
82
|
+
*/
|
|
83
|
+
validate(event) {
|
|
84
|
+
if (typeof event !== 'object' || event === null) {
|
|
85
|
+
throw new Error('Event must be an object');
|
|
86
|
+
}
|
|
87
|
+
const eventObj = event;
|
|
88
|
+
const type = eventObj['type'];
|
|
89
|
+
if (!type) {
|
|
90
|
+
throw new Error('Event must have a type field');
|
|
91
|
+
}
|
|
92
|
+
const registered = this.events.get(type);
|
|
93
|
+
if (!registered) {
|
|
94
|
+
if (this.config.strict) {
|
|
95
|
+
throw new Error(`Unknown event type: ${type}`);
|
|
96
|
+
}
|
|
97
|
+
// In non-strict mode, return as-is (no validation)
|
|
98
|
+
return event;
|
|
99
|
+
}
|
|
100
|
+
// Warn about deprecated events
|
|
101
|
+
if (registered.deprecated && this.config.warnOnDeprecated) {
|
|
102
|
+
console.warn(`[EventRegistry] Event '${type}' is deprecated. ${registered.deprecationMessage ?? ''}`);
|
|
103
|
+
}
|
|
104
|
+
const result = registered.schema.safeParse(event);
|
|
105
|
+
if (!result.success) {
|
|
106
|
+
const errors = result.error.issues.map(i => `${i.path.join('.')}: ${i.message}`);
|
|
107
|
+
throw new Error(`Event validation failed for '${type}': ${errors.join(', ')}`);
|
|
108
|
+
}
|
|
109
|
+
return result.data;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Check if event is valid without throwing
|
|
113
|
+
*/
|
|
114
|
+
isValid(event) {
|
|
115
|
+
try {
|
|
116
|
+
this.validate(event);
|
|
117
|
+
return true;
|
|
118
|
+
} catch {
|
|
119
|
+
return false;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Get all registered event types
|
|
124
|
+
*/
|
|
125
|
+
getAllTypes() {
|
|
126
|
+
return Array.from(this.events.keys());
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Get all registered events
|
|
130
|
+
*/
|
|
131
|
+
getAll() {
|
|
132
|
+
return Array.from(this.events.values());
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Get events by category
|
|
136
|
+
*/
|
|
137
|
+
getByCategory(category) {
|
|
138
|
+
return this.getAll().filter(e => e.category === category);
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Get event catalog for documentation
|
|
142
|
+
*/
|
|
143
|
+
getCatalog() {
|
|
144
|
+
return this.getAll().map(e => ({
|
|
145
|
+
type: e.type,
|
|
146
|
+
category: e.category,
|
|
147
|
+
priority: e.priority,
|
|
148
|
+
description: e.description,
|
|
149
|
+
deprecated: e.deprecated ?? false
|
|
150
|
+
}));
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Export registry as JSON schema (for external tools)
|
|
154
|
+
*/
|
|
155
|
+
toJSONSchema() {
|
|
156
|
+
const schemas = {};
|
|
157
|
+
for (const [type, event] of Array.from(this.events.entries())) {
|
|
158
|
+
// Note: This is a simplified JSON schema export
|
|
159
|
+
// For full compatibility, use zod-to-json-schema package
|
|
160
|
+
schemas[type] = {
|
|
161
|
+
type: 'object',
|
|
162
|
+
description: event.description,
|
|
163
|
+
deprecated: event.deprecated,
|
|
164
|
+
priority: event.priority,
|
|
165
|
+
category: event.category
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
return {
|
|
169
|
+
$schema: 'http://json-schema.org/draft-07/schema#',
|
|
170
|
+
title: 'Event Registry',
|
|
171
|
+
definitions: schemas
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Create a new event registry
|
|
177
|
+
*/
|
|
178
|
+
function createEventRegistry(config) {
|
|
179
|
+
return new EventRegistry(config);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
exports.EventRegistry = EventRegistry;
|
|
183
|
+
exports.createEventRegistry = createEventRegistry;
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Event Registry class - manages all registered event types
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* ```typescript
|
|
6
|
+
* const registry = createEventRegistry();
|
|
7
|
+
*
|
|
8
|
+
* // Register events
|
|
9
|
+
* registry.register({
|
|
10
|
+
* type: 'TradeProposalCreated',
|
|
11
|
+
* schema: TradeProposalCreatedSchema,
|
|
12
|
+
* priority: 'high',
|
|
13
|
+
* description: 'Emitted when a user proposes a trade',
|
|
14
|
+
* category: 'trade',
|
|
15
|
+
* });
|
|
16
|
+
*
|
|
17
|
+
* // Validate events
|
|
18
|
+
* const validated = registry.validate(rawEvent);
|
|
19
|
+
*
|
|
20
|
+
* // Get schema for event type
|
|
21
|
+
* const schema = registry.getSchema('TradeProposalCreated');
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
class EventRegistry {
|
|
25
|
+
events = new Map();
|
|
26
|
+
config;
|
|
27
|
+
constructor(config = {}) {
|
|
28
|
+
this.config = {
|
|
29
|
+
strict: config.strict ?? false,
|
|
30
|
+
warnOnDeprecated: config.warnOnDeprecated ?? true
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Register an event type with its schema
|
|
35
|
+
*/
|
|
36
|
+
register(event) {
|
|
37
|
+
if (this.events.has(event.type)) {
|
|
38
|
+
throw new Error(`Event type '${event.type}' is already registered`);
|
|
39
|
+
}
|
|
40
|
+
this.events.set(event.type, event);
|
|
41
|
+
return this;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Register multiple events at once
|
|
45
|
+
*/
|
|
46
|
+
registerMany(events) {
|
|
47
|
+
for (const event of events) {
|
|
48
|
+
this.register(event);
|
|
49
|
+
}
|
|
50
|
+
return this;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Get schema for an event type
|
|
54
|
+
*/
|
|
55
|
+
getSchema(type) {
|
|
56
|
+
return this.events.get(type)?.schema;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Get registered event info
|
|
60
|
+
*/
|
|
61
|
+
getEvent(type) {
|
|
62
|
+
return this.events.get(type);
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Check if event type is registered
|
|
66
|
+
*/
|
|
67
|
+
has(type) {
|
|
68
|
+
return this.events.has(type);
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Get default priority for event type
|
|
72
|
+
*/
|
|
73
|
+
getPriority(type) {
|
|
74
|
+
return this.events.get(type)?.priority ?? 'normal';
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Validate an event against its registered schema
|
|
78
|
+
*
|
|
79
|
+
* @throws Error if event type is unknown (in strict mode) or validation fails
|
|
80
|
+
*/
|
|
81
|
+
validate(event) {
|
|
82
|
+
if (typeof event !== 'object' || event === null) {
|
|
83
|
+
throw new Error('Event must be an object');
|
|
84
|
+
}
|
|
85
|
+
const eventObj = event;
|
|
86
|
+
const type = eventObj['type'];
|
|
87
|
+
if (!type) {
|
|
88
|
+
throw new Error('Event must have a type field');
|
|
89
|
+
}
|
|
90
|
+
const registered = this.events.get(type);
|
|
91
|
+
if (!registered) {
|
|
92
|
+
if (this.config.strict) {
|
|
93
|
+
throw new Error(`Unknown event type: ${type}`);
|
|
94
|
+
}
|
|
95
|
+
// In non-strict mode, return as-is (no validation)
|
|
96
|
+
return event;
|
|
97
|
+
}
|
|
98
|
+
// Warn about deprecated events
|
|
99
|
+
if (registered.deprecated && this.config.warnOnDeprecated) {
|
|
100
|
+
console.warn(`[EventRegistry] Event '${type}' is deprecated. ${registered.deprecationMessage ?? ''}`);
|
|
101
|
+
}
|
|
102
|
+
const result = registered.schema.safeParse(event);
|
|
103
|
+
if (!result.success) {
|
|
104
|
+
const errors = result.error.issues.map(i => `${i.path.join('.')}: ${i.message}`);
|
|
105
|
+
throw new Error(`Event validation failed for '${type}': ${errors.join(', ')}`);
|
|
106
|
+
}
|
|
107
|
+
return result.data;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Check if event is valid without throwing
|
|
111
|
+
*/
|
|
112
|
+
isValid(event) {
|
|
113
|
+
try {
|
|
114
|
+
this.validate(event);
|
|
115
|
+
return true;
|
|
116
|
+
} catch {
|
|
117
|
+
return false;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Get all registered event types
|
|
122
|
+
*/
|
|
123
|
+
getAllTypes() {
|
|
124
|
+
return Array.from(this.events.keys());
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Get all registered events
|
|
128
|
+
*/
|
|
129
|
+
getAll() {
|
|
130
|
+
return Array.from(this.events.values());
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Get events by category
|
|
134
|
+
*/
|
|
135
|
+
getByCategory(category) {
|
|
136
|
+
return this.getAll().filter(e => e.category === category);
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Get event catalog for documentation
|
|
140
|
+
*/
|
|
141
|
+
getCatalog() {
|
|
142
|
+
return this.getAll().map(e => ({
|
|
143
|
+
type: e.type,
|
|
144
|
+
category: e.category,
|
|
145
|
+
priority: e.priority,
|
|
146
|
+
description: e.description,
|
|
147
|
+
deprecated: e.deprecated ?? false
|
|
148
|
+
}));
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Export registry as JSON schema (for external tools)
|
|
152
|
+
*/
|
|
153
|
+
toJSONSchema() {
|
|
154
|
+
const schemas = {};
|
|
155
|
+
for (const [type, event] of Array.from(this.events.entries())) {
|
|
156
|
+
// Note: This is a simplified JSON schema export
|
|
157
|
+
// For full compatibility, use zod-to-json-schema package
|
|
158
|
+
schemas[type] = {
|
|
159
|
+
type: 'object',
|
|
160
|
+
description: event.description,
|
|
161
|
+
deprecated: event.deprecated,
|
|
162
|
+
priority: event.priority,
|
|
163
|
+
category: event.category
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
return {
|
|
167
|
+
$schema: 'http://json-schema.org/draft-07/schema#',
|
|
168
|
+
title: 'Event Registry',
|
|
169
|
+
definitions: schemas
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Create a new event registry
|
|
175
|
+
*/
|
|
176
|
+
function createEventRegistry(config) {
|
|
177
|
+
return new EventRegistry(config);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export { EventRegistry, createEventRegistry };
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Core event types - framework-agnostic definitions
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Priority configuration with SLA targets
|
|
8
|
+
*/
|
|
9
|
+
const EVENT_PRIORITIES = {
|
|
10
|
+
critical: {
|
|
11
|
+
sla: 100,
|
|
12
|
+
weight: 10
|
|
13
|
+
},
|
|
14
|
+
// < 100ms
|
|
15
|
+
high: {
|
|
16
|
+
sla: 500,
|
|
17
|
+
weight: 7
|
|
18
|
+
},
|
|
19
|
+
// < 500ms
|
|
20
|
+
normal: {
|
|
21
|
+
sla: 2000,
|
|
22
|
+
weight: 5
|
|
23
|
+
},
|
|
24
|
+
// < 2s
|
|
25
|
+
low: {
|
|
26
|
+
sla: 30000,
|
|
27
|
+
weight: 3
|
|
28
|
+
},
|
|
29
|
+
// < 30s
|
|
30
|
+
bulk: {
|
|
31
|
+
sla: 300000,
|
|
32
|
+
weight: 1
|
|
33
|
+
} // < 5min
|
|
34
|
+
};
|
|
35
|
+
const DEFAULT_EVENT_VERSION = {
|
|
36
|
+
major: 1,
|
|
37
|
+
minor: 0
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
exports.DEFAULT_EVENT_VERSION = DEFAULT_EVENT_VERSION;
|
|
41
|
+
exports.EVENT_PRIORITIES = EVENT_PRIORITIES;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Core event types - framework-agnostic definitions
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Priority configuration with SLA targets
|
|
6
|
+
*/
|
|
7
|
+
const EVENT_PRIORITIES = {
|
|
8
|
+
critical: {
|
|
9
|
+
sla: 100,
|
|
10
|
+
weight: 10
|
|
11
|
+
},
|
|
12
|
+
// < 100ms
|
|
13
|
+
high: {
|
|
14
|
+
sla: 500,
|
|
15
|
+
weight: 7
|
|
16
|
+
},
|
|
17
|
+
// < 500ms
|
|
18
|
+
normal: {
|
|
19
|
+
sla: 2000,
|
|
20
|
+
weight: 5
|
|
21
|
+
},
|
|
22
|
+
// < 2s
|
|
23
|
+
low: {
|
|
24
|
+
sla: 30000,
|
|
25
|
+
weight: 3
|
|
26
|
+
},
|
|
27
|
+
// < 30s
|
|
28
|
+
bulk: {
|
|
29
|
+
sla: 300000,
|
|
30
|
+
weight: 1
|
|
31
|
+
} // < 5min
|
|
32
|
+
};
|
|
33
|
+
const DEFAULT_EVENT_VERSION = {
|
|
34
|
+
major: 1,
|
|
35
|
+
minor: 0
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export { DEFAULT_EVENT_VERSION, EVENT_PRIORITIES };
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var zod = require('zod');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Event validation using Zod schemas
|
|
7
|
+
*
|
|
8
|
+
* Provides:
|
|
9
|
+
* - Base schemas for common event fields
|
|
10
|
+
* - Factory function to create event schemas
|
|
11
|
+
* - Runtime validation with detailed errors
|
|
12
|
+
*/
|
|
13
|
+
// ============================================================================
|
|
14
|
+
// Base Schemas
|
|
15
|
+
// ============================================================================
|
|
16
|
+
/**
|
|
17
|
+
* Schema for event actor
|
|
18
|
+
*/
|
|
19
|
+
const EventActorSchema = zod.z.object({
|
|
20
|
+
id: zod.z.string().min(1),
|
|
21
|
+
type: zod.z.enum(['user', 'system', 'admin', 'webhook']),
|
|
22
|
+
name: zod.z.string().optional()
|
|
23
|
+
});
|
|
24
|
+
/**
|
|
25
|
+
* Schema for event version
|
|
26
|
+
*/
|
|
27
|
+
const EventVersionSchema = zod.z.object({
|
|
28
|
+
major: zod.z.number().int().min(1),
|
|
29
|
+
minor: zod.z.number().int().min(0)
|
|
30
|
+
});
|
|
31
|
+
/**
|
|
32
|
+
* Schema for event metadata
|
|
33
|
+
*/
|
|
34
|
+
const EventMetadataSchema = zod.z.object({
|
|
35
|
+
source: zod.z.string().min(1),
|
|
36
|
+
environment: zod.z.string().min(1),
|
|
37
|
+
ip: zod.z.string().optional(),
|
|
38
|
+
userAgent: zod.z.string().optional()
|
|
39
|
+
}).passthrough(); // Allow additional properties
|
|
40
|
+
/**
|
|
41
|
+
* Schema for aggregate info
|
|
42
|
+
*/
|
|
43
|
+
const AggregateSchema = zod.z.object({
|
|
44
|
+
type: zod.z.string().min(1),
|
|
45
|
+
id: zod.z.string().min(1)
|
|
46
|
+
}).optional();
|
|
47
|
+
/**
|
|
48
|
+
* Base event schema without data field
|
|
49
|
+
*/
|
|
50
|
+
const BaseEventSchema = zod.z.object({
|
|
51
|
+
id: zod.z.string().uuid(),
|
|
52
|
+
type: zod.z.string().min(1),
|
|
53
|
+
version: EventVersionSchema,
|
|
54
|
+
timestamp: zod.z.string().datetime(),
|
|
55
|
+
correlationId: zod.z.string().uuid(),
|
|
56
|
+
causationId: zod.z.string().uuid().optional(),
|
|
57
|
+
actor: EventActorSchema,
|
|
58
|
+
metadata: EventMetadataSchema,
|
|
59
|
+
priority: zod.z.enum(['critical', 'high', 'normal', 'low', 'bulk']).optional(),
|
|
60
|
+
aggregate: AggregateSchema
|
|
61
|
+
});
|
|
62
|
+
// ============================================================================
|
|
63
|
+
// Schema Factory
|
|
64
|
+
// ============================================================================
|
|
65
|
+
/**
|
|
66
|
+
* Create a typed event schema with Zod validation
|
|
67
|
+
*
|
|
68
|
+
* @param eventType - The event type string (e.g., 'TradeProposalCreated')
|
|
69
|
+
* @param dataSchema - Zod schema for the event's data field
|
|
70
|
+
* @returns Complete event schema
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* ```typescript
|
|
74
|
+
* const TradeProposalCreatedSchema = createEventSchema('TradeProposalCreated', {
|
|
75
|
+
* tradeId: z.string().uuid(),
|
|
76
|
+
* initiatorId: z.string().uuid(),
|
|
77
|
+
* recipientId: z.string().uuid(),
|
|
78
|
+
* vehicleOfferedId: z.string().uuid(),
|
|
79
|
+
* terms: z.object({
|
|
80
|
+
* cashDifference: z.number().optional(),
|
|
81
|
+
* }),
|
|
82
|
+
* });
|
|
83
|
+
*
|
|
84
|
+
* type TradeProposalCreated = z.infer<typeof TradeProposalCreatedSchema>;
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
function createEventSchema(eventType, dataSchema) {
|
|
88
|
+
return BaseEventSchema.extend({
|
|
89
|
+
type: zod.z.literal(eventType),
|
|
90
|
+
data: zod.z.object(dataSchema)
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Create event schema from existing Zod object
|
|
95
|
+
*/
|
|
96
|
+
function createEventSchemaFromZod(eventType, dataSchema) {
|
|
97
|
+
return BaseEventSchema.extend({
|
|
98
|
+
type: zod.z.literal(eventType),
|
|
99
|
+
data: dataSchema
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
// ============================================================================
|
|
103
|
+
// Validation Functions
|
|
104
|
+
// ============================================================================
|
|
105
|
+
/**
|
|
106
|
+
* Custom error class for event validation failures
|
|
107
|
+
*/
|
|
108
|
+
class EventValidationError extends Error {
|
|
109
|
+
zodError;
|
|
110
|
+
event;
|
|
111
|
+
constructor(message, zodError, event) {
|
|
112
|
+
super(message);
|
|
113
|
+
this.zodError = zodError;
|
|
114
|
+
this.event = event;
|
|
115
|
+
this.name = 'EventValidationError';
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Get formatted error messages
|
|
119
|
+
*/
|
|
120
|
+
get issues() {
|
|
121
|
+
return this.zodError.issues.map(issue => {
|
|
122
|
+
const path = issue.path.join('.');
|
|
123
|
+
return path ? `${path}: ${issue.message}` : issue.message;
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Validate an event against a schema, throwing on failure
|
|
129
|
+
*
|
|
130
|
+
* @param schema - Zod schema to validate against
|
|
131
|
+
* @param event - Event to validate
|
|
132
|
+
* @returns Validated and typed event
|
|
133
|
+
* @throws EventValidationError if validation fails
|
|
134
|
+
*
|
|
135
|
+
* @example
|
|
136
|
+
* ```typescript
|
|
137
|
+
* try {
|
|
138
|
+
* const validEvent = validateEvent(TradeProposalCreatedSchema, rawEvent);
|
|
139
|
+
* // validEvent is typed as TradeProposalCreated
|
|
140
|
+
* } catch (error) {
|
|
141
|
+
* if (error instanceof EventValidationError) {
|
|
142
|
+
* console.error('Validation failed:', error.issues);
|
|
143
|
+
* }
|
|
144
|
+
* }
|
|
145
|
+
* ```
|
|
146
|
+
*/
|
|
147
|
+
function validateEvent(schema, event) {
|
|
148
|
+
const result = schema.safeParse(event);
|
|
149
|
+
if (!result.success) {
|
|
150
|
+
throw new EventValidationError(`Event validation failed: ${result.error.issues.map(i => i.message).join(', ')}`, result.error, event);
|
|
151
|
+
}
|
|
152
|
+
return result.data;
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Check if an event is valid without throwing
|
|
156
|
+
*
|
|
157
|
+
* @param schema - Zod schema to validate against
|
|
158
|
+
* @param event - Event to check
|
|
159
|
+
* @returns true if valid, false otherwise
|
|
160
|
+
*/
|
|
161
|
+
function isValidEvent(schema, event) {
|
|
162
|
+
return schema.safeParse(event).success;
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Parse an event, returning result with success/error
|
|
166
|
+
*
|
|
167
|
+
* @param schema - Zod schema to parse with
|
|
168
|
+
* @param event - Event to parse
|
|
169
|
+
* @returns SafeParseResult with data or error
|
|
170
|
+
*/
|
|
171
|
+
function parseEvent(schema, event) {
|
|
172
|
+
return schema.safeParse(event);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
exports.AggregateSchema = AggregateSchema;
|
|
176
|
+
exports.BaseEventSchema = BaseEventSchema;
|
|
177
|
+
exports.EventActorSchema = EventActorSchema;
|
|
178
|
+
exports.EventMetadataSchema = EventMetadataSchema;
|
|
179
|
+
exports.EventValidationError = EventValidationError;
|
|
180
|
+
exports.EventVersionSchema = EventVersionSchema;
|
|
181
|
+
exports.createEventSchema = createEventSchema;
|
|
182
|
+
exports.createEventSchemaFromZod = createEventSchemaFromZod;
|
|
183
|
+
exports.isValidEvent = isValidEvent;
|
|
184
|
+
exports.parseEvent = parseEvent;
|
|
185
|
+
exports.validateEvent = validateEvent;
|
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
export { D as DEFAULT_EVENT_VERSION, E as EVENT_PRIORITIES, c as createEvent, a as createEventFactory, b as generateCorrelationId, g as generateEventId } from './factory.esm.js';
|
|
2
1
|
import { z } from 'zod';
|
|
3
|
-
export { z } from 'zod';
|
|
4
|
-
export { D as DEFAULT_RETRY_CONFIGS, E as EventRegistry, I as InMemoryIdempotencyStore, a as classifyError, b as createErrorClassifier, c as createEventRegistry, e as createInMemoryIdempotencyStore, d as defaultErrorClassifier, f as generateCorrelationKey, g as generateIdempotencyKey, i as isRetryableError } from './idempotency.esm.js';
|
|
5
2
|
|
|
6
3
|
/**
|
|
7
4
|
* Event validation using Zod schemas
|
|
@@ -173,4 +170,4 @@ function parseEvent(schema, event) {
|
|
|
173
170
|
return schema.safeParse(event);
|
|
174
171
|
}
|
|
175
172
|
|
|
176
|
-
export { BaseEventSchema, EventActorSchema, EventMetadataSchema, EventValidationError, EventVersionSchema, createEventSchema, createEventSchemaFromZod, isValidEvent, parseEvent, validateEvent };
|
|
173
|
+
export { AggregateSchema, BaseEventSchema, EventActorSchema, EventMetadataSchema, EventValidationError, EventVersionSchema, createEventSchema, createEventSchemaFromZod, isValidEvent, parseEvent, validateEvent };
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var types = require('./core/types.cjs');
|
|
4
|
+
var validation = require('./core/validation.cjs');
|
|
5
|
+
var registry = require('./core/registry.cjs');
|
|
6
|
+
var factory = require('./core/factory.cjs');
|
|
7
|
+
var errorClassification = require('./core/error-classification.cjs');
|
|
8
|
+
var idempotency = require('./core/idempotency.cjs');
|
|
9
|
+
var zod = require('zod');
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
exports.DEFAULT_EVENT_VERSION = types.DEFAULT_EVENT_VERSION;
|
|
14
|
+
exports.EVENT_PRIORITIES = types.EVENT_PRIORITIES;
|
|
15
|
+
exports.BaseEventSchema = validation.BaseEventSchema;
|
|
16
|
+
exports.EventActorSchema = validation.EventActorSchema;
|
|
17
|
+
exports.EventMetadataSchema = validation.EventMetadataSchema;
|
|
18
|
+
exports.EventValidationError = validation.EventValidationError;
|
|
19
|
+
exports.EventVersionSchema = validation.EventVersionSchema;
|
|
20
|
+
exports.createEventSchema = validation.createEventSchema;
|
|
21
|
+
exports.createEventSchemaFromZod = validation.createEventSchemaFromZod;
|
|
22
|
+
exports.isValidEvent = validation.isValidEvent;
|
|
23
|
+
exports.parseEvent = validation.parseEvent;
|
|
24
|
+
exports.validateEvent = validation.validateEvent;
|
|
25
|
+
exports.EventRegistry = registry.EventRegistry;
|
|
26
|
+
exports.createEventRegistry = registry.createEventRegistry;
|
|
27
|
+
exports.createEvent = factory.createEvent;
|
|
28
|
+
exports.createEventFactory = factory.createEventFactory;
|
|
29
|
+
exports.generateCorrelationId = factory.generateCorrelationId;
|
|
30
|
+
exports.generateEventId = factory.generateEventId;
|
|
31
|
+
exports.DEFAULT_RETRY_CONFIGS = errorClassification.DEFAULT_RETRY_CONFIGS;
|
|
32
|
+
exports.classifyError = errorClassification.classifyError;
|
|
33
|
+
exports.createErrorClassifier = errorClassification.createErrorClassifier;
|
|
34
|
+
exports.defaultErrorClassifier = errorClassification.defaultErrorClassifier;
|
|
35
|
+
exports.isRetryableError = errorClassification.isRetryableError;
|
|
36
|
+
exports.InMemoryIdempotencyStore = idempotency.InMemoryIdempotencyStore;
|
|
37
|
+
exports.createInMemoryIdempotencyStore = idempotency.createInMemoryIdempotencyStore;
|
|
38
|
+
exports.generateCorrelationKey = idempotency.generateCorrelationKey;
|
|
39
|
+
exports.generateIdempotencyKey = idempotency.generateIdempotencyKey;
|
|
40
|
+
Object.defineProperty(exports, "z", {
|
|
41
|
+
enumerable: true,
|
|
42
|
+
get: function () { return zod.z; }
|
|
43
|
+
});
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { DEFAULT_EVENT_VERSION, EVENT_PRIORITIES } from './core/types.js';
|
|
2
|
+
export { BaseEventSchema, EventActorSchema, EventMetadataSchema, EventValidationError, EventVersionSchema, createEventSchema, createEventSchemaFromZod, isValidEvent, parseEvent, validateEvent } from './core/validation.js';
|
|
3
|
+
export { EventRegistry, createEventRegistry } from './core/registry.js';
|
|
4
|
+
export { createEvent, createEventFactory, generateCorrelationId, generateEventId } from './core/factory.js';
|
|
5
|
+
export { DEFAULT_RETRY_CONFIGS, classifyError, createErrorClassifier, defaultErrorClassifier, isRetryableError } from './core/error-classification.js';
|
|
6
|
+
export { InMemoryIdempotencyStore, createInMemoryIdempotencyStore, generateCorrelationKey, generateIdempotencyKey } from './core/idempotency.js';
|
|
7
|
+
export { z } from 'zod';
|