@my-nestjs-kit/events 0.0.0-stage → 0.1.0-dev.20261129010337
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/LICENSE +21 -0
- package/README.md +35 -2
- package/dist/base-event.factory.d.ts +25 -0
- package/dist/base-event.factory.js +52 -0
- package/dist/default-events.map.d.ts +18 -0
- package/dist/default-events.map.js +2 -0
- package/dist/event-bus.module.d.ts +16 -0
- package/dist/event-bus.module.js +63 -0
- package/dist/event-handler.decorator.d.ts +15 -0
- package/dist/event-handler.decorator.js +23 -0
- package/dist/events-registry.d.ts +13 -0
- package/dist/events-registry.js +47 -0
- package/dist/events-service.base.d.ts +31 -0
- package/dist/events-service.base.js +56 -0
- package/dist/events.types.d.ts +51 -0
- package/dist/events.types.js +2 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +26 -0
- package/dist/pipeline/event-enhancers.resolver.d.ts +23 -0
- package/dist/pipeline/event-enhancers.resolver.js +56 -0
- package/dist/pipeline/event-execution-context.d.ts +19 -0
- package/dist/pipeline/event-execution-context.js +27 -0
- package/dist/pipeline/event-handlers.explorer.d.ts +27 -0
- package/dist/pipeline/event-handlers.explorer.js +116 -0
- package/dist/pipeline/event-pipeline.constants.d.ts +5 -0
- package/dist/pipeline/event-pipeline.constants.js +8 -0
- package/dist/pipeline/event-pipeline.decorators.d.ts +7 -0
- package/dist/pipeline/event-pipeline.decorators.js +21 -0
- package/dist/pipeline/event-pipeline.interfaces.d.ts +34 -0
- package/dist/pipeline/event-pipeline.interfaces.js +2 -0
- package/dist/pipeline/event-pipeline.runner.d.ts +37 -0
- package/dist/pipeline/event-pipeline.runner.js +67 -0
- package/dist/pipeline/events-listener.decorator.d.ts +31 -0
- package/dist/pipeline/events-listener.decorator.js +28 -0
- package/dist/pipeline/index.d.ts +6 -0
- package/dist/pipeline/index.js +24 -0
- package/package.json +104 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 sabationok
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,36 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @my-nestjs-kit/events
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Typed in-process events for NestJS over `EventEmitter2`. No other kit package.
|
|
4
|
+
|
|
5
|
+
```sh
|
|
6
|
+
npm i @my-nestjs-kit/events @nestjs/event-emitter
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
Peers: `@nestjs/common`, `@nestjs/core`, `reflect-metadata`, `axios` (types of `WebhookEventInstance`) and
|
|
10
|
+
`@nestjs/event-emitter`, which is optional only because `@my-nestjs-kit/events/pipeline` doesn't need it; the root
|
|
11
|
+
entry does.
|
|
12
|
+
|
|
13
|
+
## Entries
|
|
14
|
+
|
|
15
|
+
| Import | What |
|
|
16
|
+
|---|---|
|
|
17
|
+
| `@my-nestjs-kit/events` | event classes keyed by `static type` (`createEventClass`), `EventBusModule`, `EventsRegistry`, `EventsServiceBase`, `@EventsListener` / `@EventHandler`, plus the pipeline |
|
|
18
|
+
| `@my-nestjs-kit/events/pipeline` | the guards / interceptors / filters pipeline (`UseEventGuards`, `EventExecutionContext`, ...), shared with `@my-nestjs-kit/channels` |
|
|
19
|
+
|
|
20
|
+
Each module extends the map next to its classes and registers them at runtime:
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
export const orderEvents = [OrderRemoved, OrderPaid] as const;
|
|
24
|
+
declare module '@my-nestjs-kit/events' {
|
|
25
|
+
interface DefaultEventsMap extends EventsMapOf<typeof orderEvents> {}
|
|
26
|
+
}
|
|
27
|
+
EventBusModule.forFeature(orderEvents);
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
`EventBusModule.forRoot({ guards?, interceptors?, filters?, suppressErrors? })` once (needs `EventEmitterModule`).
|
|
31
|
+
`emit` publishes, `emitAsync` awaits every handler. Handlers of one pattern run sequentially by `priority`, each
|
|
32
|
+
through guards, interceptors and filters; errors are logged unless `suppressErrors: false`.
|
|
33
|
+
|
|
34
|
+
## License
|
|
35
|
+
|
|
36
|
+
MIT
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { EventInit, EventTransport } from './events.types';
|
|
2
|
+
export declare function createEventClass<Payload = any, Namespace extends string = string, Action extends string = string>(namespace: Namespace, action: Action): {
|
|
3
|
+
new (payload: EventInit<Payload>): {
|
|
4
|
+
timestamp: number;
|
|
5
|
+
readonly namespace: Namespace;
|
|
6
|
+
readonly action: Action;
|
|
7
|
+
readonly type: `${Namespace}.${Action}`;
|
|
8
|
+
transports: EventTransport[];
|
|
9
|
+
};
|
|
10
|
+
readonly namespace: Namespace;
|
|
11
|
+
readonly action: Action;
|
|
12
|
+
readonly type: `${Namespace}.${Action}`;
|
|
13
|
+
};
|
|
14
|
+
/** Same as `createEventClass` but without a constructor — subclasses assign the payload themselves. */
|
|
15
|
+
export declare function createAbstractEventClass<Namespace extends string = string, Action extends string = string>(namespace: Namespace, action: Action): (abstract new () => {
|
|
16
|
+
timestamp: number;
|
|
17
|
+
transports: EventTransport[];
|
|
18
|
+
readonly namespace: Namespace;
|
|
19
|
+
readonly action: Action;
|
|
20
|
+
readonly type: `${Namespace}.${Action}`;
|
|
21
|
+
}) & {
|
|
22
|
+
readonly action: Action;
|
|
23
|
+
readonly namespace: Namespace;
|
|
24
|
+
readonly type: `${Namespace}.${Action}`;
|
|
25
|
+
};
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createAbstractEventClass = exports.createEventClass = void 0;
|
|
4
|
+
function createEventClass(namespace, action) {
|
|
5
|
+
class Base {
|
|
6
|
+
static get type() {
|
|
7
|
+
return `${this.namespace}.${this.action}`;
|
|
8
|
+
}
|
|
9
|
+
get namespace() {
|
|
10
|
+
return Base.namespace;
|
|
11
|
+
}
|
|
12
|
+
get action() {
|
|
13
|
+
return Base.action;
|
|
14
|
+
}
|
|
15
|
+
get type() {
|
|
16
|
+
return Base.type;
|
|
17
|
+
}
|
|
18
|
+
constructor(payload) {
|
|
19
|
+
this.transports = [];
|
|
20
|
+
Object.assign(this, payload);
|
|
21
|
+
this.timestamp = Date.now();
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
Base.namespace = namespace;
|
|
25
|
+
Base.action = action;
|
|
26
|
+
return Base;
|
|
27
|
+
}
|
|
28
|
+
exports.createEventClass = createEventClass;
|
|
29
|
+
/** Same as `createEventClass` but without a constructor — subclasses assign the payload themselves. */
|
|
30
|
+
function createAbstractEventClass(namespace, action) {
|
|
31
|
+
class Event {
|
|
32
|
+
constructor() {
|
|
33
|
+
this.transports = [];
|
|
34
|
+
}
|
|
35
|
+
static get type() {
|
|
36
|
+
return `${this.namespace}.${this.action}`;
|
|
37
|
+
}
|
|
38
|
+
get namespace() {
|
|
39
|
+
return Event.namespace;
|
|
40
|
+
}
|
|
41
|
+
get action() {
|
|
42
|
+
return Event.action;
|
|
43
|
+
}
|
|
44
|
+
get type() {
|
|
45
|
+
return Event.type;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
Event.action = action;
|
|
49
|
+
Event.namespace = namespace;
|
|
50
|
+
return Event;
|
|
51
|
+
}
|
|
52
|
+
exports.createAbstractEventClass = createAbstractEventClass;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { EventsMap } from './events.types';
|
|
2
|
+
/**
|
|
3
|
+
* The app's events map — empty in the kit. Each module extends it next to its event classes
|
|
4
|
+
* through declaration merging (no file imports every class):
|
|
5
|
+
*
|
|
6
|
+
* ```ts
|
|
7
|
+
* export const ordersEvents = [OrdersRemoveEvent, OrderStatusChangedEvent] as const;
|
|
8
|
+
*
|
|
9
|
+
* declare module '@my-nestjs-kit/events' {
|
|
10
|
+
* interface DefaultEventsMap extends EventsMapOf<typeof ordersEvents> {}
|
|
11
|
+
* }
|
|
12
|
+
* ```
|
|
13
|
+
*
|
|
14
|
+
* and registers the constructors at runtime with `EventBusModule.forFeature(ordersEvents)`.
|
|
15
|
+
* `EventsServiceBase`, `@EventHandler` and `@EventsListener` default to this map.
|
|
16
|
+
*/
|
|
17
|
+
export interface DefaultEventsMap extends EventsMap {
|
|
18
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { DynamicModule } from '@nestjs/common';
|
|
2
|
+
import type { EventCtor } from './events.types';
|
|
3
|
+
import type { EventBusModuleOptions } from './pipeline/event-pipeline.interfaces';
|
|
4
|
+
/**
|
|
5
|
+
* - `forRoot()` once in the app: the global `EventsRegistry` and the handlers pipeline
|
|
6
|
+
* (`@EventsListener` classes → guards / interceptors / filters → EventEmitter2).
|
|
7
|
+
* - `forFeature([...classes])` in each domain module: registers its event constructors.
|
|
8
|
+
* The types come from the same module via `declare module '@my-nestjs-kit/events'
|
|
9
|
+
* { interface DefaultEventsMap extends EventsMapOf<typeof xEvents> {} }`.
|
|
10
|
+
*
|
|
11
|
+
* Needs `EventEmitterModule.forRoot()` in the app (it provides `EventEmitter2`).
|
|
12
|
+
*/
|
|
13
|
+
export declare class EventBusModule {
|
|
14
|
+
static forRoot(options?: EventBusModuleOptions): DynamicModule;
|
|
15
|
+
static forFeature(classes: readonly EventCtor[]): DynamicModule;
|
|
16
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var EventBusModule_1;
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.EventBusModule = void 0;
|
|
11
|
+
const common_1 = require("@nestjs/common");
|
|
12
|
+
const core_1 = require("@nestjs/core");
|
|
13
|
+
const events_registry_1 = require("./events-registry");
|
|
14
|
+
const event_pipeline_constants_1 = require("./pipeline/event-pipeline.constants");
|
|
15
|
+
const event_handlers_explorer_1 = require("./pipeline/event-handlers.explorer");
|
|
16
|
+
let featureSeq = 0;
|
|
17
|
+
let EventBusFeatureModule = class EventBusFeatureModule {
|
|
18
|
+
};
|
|
19
|
+
EventBusFeatureModule = __decorate([
|
|
20
|
+
(0, common_1.Module)({})
|
|
21
|
+
], EventBusFeatureModule);
|
|
22
|
+
/**
|
|
23
|
+
* - `forRoot()` once in the app: the global `EventsRegistry` and the handlers pipeline
|
|
24
|
+
* (`@EventsListener` classes → guards / interceptors / filters → EventEmitter2).
|
|
25
|
+
* - `forFeature([...classes])` in each domain module: registers its event constructors.
|
|
26
|
+
* The types come from the same module via `declare module '@my-nestjs-kit/events'
|
|
27
|
+
* { interface DefaultEventsMap extends EventsMapOf<typeof xEvents> {} }`.
|
|
28
|
+
*
|
|
29
|
+
* Needs `EventEmitterModule.forRoot()` in the app (it provides `EventEmitter2`).
|
|
30
|
+
*/
|
|
31
|
+
let EventBusModule = EventBusModule_1 = class EventBusModule {
|
|
32
|
+
static forRoot(options = {}) {
|
|
33
|
+
return {
|
|
34
|
+
module: EventBusModule_1,
|
|
35
|
+
global: true,
|
|
36
|
+
imports: [core_1.DiscoveryModule],
|
|
37
|
+
providers: [
|
|
38
|
+
{ provide: event_pipeline_constants_1.EVENT_BUS_MODULE_OPTIONS, useValue: options },
|
|
39
|
+
events_registry_1.EventsRegistry,
|
|
40
|
+
event_handlers_explorer_1.EventHandlersExplorer,
|
|
41
|
+
],
|
|
42
|
+
exports: [events_registry_1.EventsRegistry],
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
static forFeature(classes) {
|
|
46
|
+
// a unique token per call, so Nest doesn't dedupe two forFeature() modules into one
|
|
47
|
+
const token = `EVENT_BUS_FEATURE_${++featureSeq}`;
|
|
48
|
+
return {
|
|
49
|
+
module: EventBusFeatureModule,
|
|
50
|
+
providers: [
|
|
51
|
+
{
|
|
52
|
+
provide: token,
|
|
53
|
+
useFactory: (registry) => registry.add(...classes),
|
|
54
|
+
inject: [events_registry_1.EventsRegistry],
|
|
55
|
+
},
|
|
56
|
+
],
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
exports.EventBusModule = EventBusModule;
|
|
61
|
+
exports.EventBusModule = EventBusModule = EventBusModule_1 = __decorate([
|
|
62
|
+
(0, common_1.Module)({})
|
|
63
|
+
], EventBusModule);
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import 'reflect-metadata';
|
|
2
|
+
import type { EventHandlerOptions } from './pipeline/event-pipeline.interfaces';
|
|
3
|
+
import type { EventName, EventOf, EventsMap } from './events.types';
|
|
4
|
+
import type { DefaultEventsMap } from './default-events.map';
|
|
5
|
+
import type { EventAction } from './pipeline/events-listener.decorator';
|
|
6
|
+
export type EventHandlerDescriptor<E> = TypedPropertyDescriptor<(event: E, ...args: any[]) => unknown>;
|
|
7
|
+
/**
|
|
8
|
+
* Marks a method of an `@EventsListener()` class as an event handler.
|
|
9
|
+
* - In a namespaced class (`@EventsListener('orders')`) the key is relative: `'remove'` → `'orders.remove'`.
|
|
10
|
+
* - Without a namespace it is a full key (a class' `static type`, or a wildcard pattern), and the
|
|
11
|
+
* handler's argument is checked against that event class.
|
|
12
|
+
* Subscribed by `EventBusModule` through the pipeline: `@UseEventGuards` /
|
|
13
|
+
* `@UseEventInterceptors` / `@UseEventFilters` apply. Extra method decorators can follow the options.
|
|
14
|
+
*/
|
|
15
|
+
export declare function EventHandler<K extends EventName<M> | EventAction<M>, M extends EventsMap = DefaultEventsMap>(event: K, options?: EventHandlerOptions, ...decorators: MethodDecorator[]): <D extends EventHandlerDescriptor<K extends EventName<M> ? EventOf<M, K> : any>>(target: object, propertyKey: string | symbol, descriptor: D) => void;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.EventHandler = void 0;
|
|
4
|
+
require("reflect-metadata");
|
|
5
|
+
const event_pipeline_constants_1 = require("./pipeline/event-pipeline.constants");
|
|
6
|
+
/**
|
|
7
|
+
* Marks a method of an `@EventsListener()` class as an event handler.
|
|
8
|
+
* - In a namespaced class (`@EventsListener('orders')`) the key is relative: `'remove'` → `'orders.remove'`.
|
|
9
|
+
* - Without a namespace it is a full key (a class' `static type`, or a wildcard pattern), and the
|
|
10
|
+
* handler's argument is checked against that event class.
|
|
11
|
+
* Subscribed by `EventBusModule` through the pipeline: `@UseEventGuards` /
|
|
12
|
+
* `@UseEventInterceptors` / `@UseEventFilters` apply. Extra method decorators can follow the options.
|
|
13
|
+
*/
|
|
14
|
+
function EventHandler(event, options = {}, ...decorators) {
|
|
15
|
+
return (target, propertyKey, descriptor) => {
|
|
16
|
+
// extra decorators first: if one wraps the method, the metadata lands on the final function
|
|
17
|
+
for (const decorate of decorators)
|
|
18
|
+
decorate(target, propertyKey, descriptor);
|
|
19
|
+
const current = Reflect.getMetadata(event_pipeline_constants_1.EVENT_HANDLER_METADATA, descriptor.value) ?? [];
|
|
20
|
+
Reflect.defineMetadata(event_pipeline_constants_1.EVENT_HANDLER_METADATA, [...current, { event, options }], descriptor.value);
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
exports.EventHandler = EventHandler;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { EventCtor } from './events.types';
|
|
2
|
+
/**
|
|
3
|
+
* App-wide registry of event constructors, keyed by each class' `static type`.
|
|
4
|
+
* Modules fill it themselves with `EventBusModule.forFeature([...classes])`, so no file
|
|
5
|
+
* has to import every event class.
|
|
6
|
+
*/
|
|
7
|
+
export declare class EventsRegistry {
|
|
8
|
+
private readonly ctors;
|
|
9
|
+
add(...classes: readonly EventCtor[]): this;
|
|
10
|
+
has(type: string): boolean;
|
|
11
|
+
getOrFail(type: string): EventCtor;
|
|
12
|
+
types(): string[];
|
|
13
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.EventsRegistry = void 0;
|
|
10
|
+
const common_1 = require("@nestjs/common");
|
|
11
|
+
/**
|
|
12
|
+
* App-wide registry of event constructors, keyed by each class' `static type`.
|
|
13
|
+
* Modules fill it themselves with `EventBusModule.forFeature([...classes])`, so no file
|
|
14
|
+
* has to import every event class.
|
|
15
|
+
*/
|
|
16
|
+
let EventsRegistry = class EventsRegistry {
|
|
17
|
+
constructor() {
|
|
18
|
+
this.ctors = new Map();
|
|
19
|
+
}
|
|
20
|
+
add(...classes) {
|
|
21
|
+
for (const C of classes) {
|
|
22
|
+
const existing = this.ctors.get(C.type);
|
|
23
|
+
if (existing && existing !== C) {
|
|
24
|
+
throw new Error(`Event "${C.type}" is registered twice: ${existing.name} and ${C.name}`);
|
|
25
|
+
}
|
|
26
|
+
this.ctors.set(C.type, C);
|
|
27
|
+
}
|
|
28
|
+
return this;
|
|
29
|
+
}
|
|
30
|
+
has(type) {
|
|
31
|
+
return this.ctors.has(type);
|
|
32
|
+
}
|
|
33
|
+
getOrFail(type) {
|
|
34
|
+
const C = this.ctors.get(type);
|
|
35
|
+
if (!C) {
|
|
36
|
+
throw new Error(`No events constructor for: ${type} — register it with EventBusModule.forFeature()`);
|
|
37
|
+
}
|
|
38
|
+
return C;
|
|
39
|
+
}
|
|
40
|
+
types() {
|
|
41
|
+
return [...this.ctors.keys()];
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
exports.EventsRegistry = EventsRegistry;
|
|
45
|
+
exports.EventsRegistry = EventsRegistry = __decorate([
|
|
46
|
+
(0, common_1.Injectable)()
|
|
47
|
+
], EventsRegistry);
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { Logger } from '@nestjs/common';
|
|
2
|
+
import type { EventEmitter2 } from '@nestjs/event-emitter';
|
|
3
|
+
import type { EventsRegistry } from './events-registry';
|
|
4
|
+
import type { EventArgs, EventListenerFn, EventName, EventOf, EventsMap } from './events.types';
|
|
5
|
+
import type { DefaultEventsMap } from './default-events.map';
|
|
6
|
+
/**
|
|
7
|
+
* Typed facade over `EventEmitter2`. `emit(type, ...args)` looks up the constructor
|
|
8
|
+
* registered under `type`, runs `new Ctor(...args)` and publishes the instance.
|
|
9
|
+
*/
|
|
10
|
+
export declare abstract class EventsServiceBase<M extends EventsMap = DefaultEventsMap> {
|
|
11
|
+
protected readonly ee: EventEmitter2;
|
|
12
|
+
protected readonly registry: EventsRegistry;
|
|
13
|
+
protected readonly _logger: Logger;
|
|
14
|
+
protected constructor(ee: EventEmitter2, registry: EventsRegistry);
|
|
15
|
+
get emitterRef(): () => EventEmitter2;
|
|
16
|
+
/** Fire-and-forget: handlers run, nobody waits for them. */
|
|
17
|
+
emit<K extends EventName<M>>(type: K, ...args: EventArgs<M, K>): unknown;
|
|
18
|
+
/**
|
|
19
|
+
* Waits for every handler (guards and interceptors included); resolves with the event.
|
|
20
|
+
* Use it when a listener emits the next event of a chain and the order matters.
|
|
21
|
+
*/
|
|
22
|
+
emitAsync<K extends EventName<M>>(type: K, ...args: EventArgs<M, K>): Promise<EventOf<M, K>>;
|
|
23
|
+
on<K extends EventName<M>>(type: K, listener: EventListenerFn<M, K>): ReturnType<EventEmitter2['on']>;
|
|
24
|
+
once<K extends EventName<M>>(type: K, listener: EventListenerFn<M, K>): ReturnType<EventEmitter2['once']>;
|
|
25
|
+
off<K extends EventName<M>>(type: K, listener: EventListenerFn<M, K>): EventEmitter2;
|
|
26
|
+
protected build<K extends EventName<M>>(type: K, args: unknown[]): EventOf<M, K>;
|
|
27
|
+
/** Hands the built event to the emitter. Override to route it elsewhere. */
|
|
28
|
+
protected publish(type: string, event: unknown): unknown;
|
|
29
|
+
/** Async twin of `publish`, used by `emitAsync`. */
|
|
30
|
+
protected publishAsync(type: string, event: unknown): Promise<unknown>;
|
|
31
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.EventsServiceBase = void 0;
|
|
4
|
+
const common_1 = require("@nestjs/common");
|
|
5
|
+
// TODO(kit): a payload-map emitter for apps without event classes — `DomainEventEmitterBase<Map>` (typed
|
|
6
|
+
// `emit`/`emitAsync(name, payload)` over EventEmitter2) + `createDomainEventDecorator<Map>()` (typed `@OnEvent`), one
|
|
7
|
+
// `*.ev-emitter.ts` per domain. Exists in fintrack: apps/api/src/common/events/ (used by import/rules/module/import-rules.ev-emitter.ts).
|
|
8
|
+
/**
|
|
9
|
+
* Typed facade over `EventEmitter2`. `emit(type, ...args)` looks up the constructor
|
|
10
|
+
* registered under `type`, runs `new Ctor(...args)` and publishes the instance.
|
|
11
|
+
*/
|
|
12
|
+
class EventsServiceBase {
|
|
13
|
+
constructor(ee, registry) {
|
|
14
|
+
this.ee = ee;
|
|
15
|
+
this.registry = registry;
|
|
16
|
+
this._logger = new common_1.Logger(this.constructor.name);
|
|
17
|
+
}
|
|
18
|
+
get emitterRef() {
|
|
19
|
+
return () => this.ee;
|
|
20
|
+
}
|
|
21
|
+
/** Fire-and-forget: handlers run, nobody waits for them. */
|
|
22
|
+
emit(type, ...args) {
|
|
23
|
+
return this.publish(type, this.build(type, args));
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Waits for every handler (guards and interceptors included); resolves with the event.
|
|
27
|
+
* Use it when a listener emits the next event of a chain and the order matters.
|
|
28
|
+
*/
|
|
29
|
+
async emitAsync(type, ...args) {
|
|
30
|
+
const event = this.build(type, args);
|
|
31
|
+
await this.publishAsync(type, event);
|
|
32
|
+
return event;
|
|
33
|
+
}
|
|
34
|
+
on(type, listener) {
|
|
35
|
+
return this.ee.on(type, listener);
|
|
36
|
+
}
|
|
37
|
+
once(type, listener) {
|
|
38
|
+
return this.ee.once(type, listener);
|
|
39
|
+
}
|
|
40
|
+
off(type, listener) {
|
|
41
|
+
return this.ee.off(type, listener);
|
|
42
|
+
}
|
|
43
|
+
build(type, args) {
|
|
44
|
+
const Ctor = this.registry.getOrFail(type);
|
|
45
|
+
return new Ctor(...args);
|
|
46
|
+
}
|
|
47
|
+
/** Hands the built event to the emitter. Override to route it elsewhere. */
|
|
48
|
+
publish(type, event) {
|
|
49
|
+
return this.ee.emit(type, event);
|
|
50
|
+
}
|
|
51
|
+
/** Async twin of `publish`, used by `emitAsync`. */
|
|
52
|
+
publishAsync(type, event) {
|
|
53
|
+
return this.ee.emitAsync(type, event);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
exports.EventsServiceBase = EventsServiceBase;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import type { AxiosRequestConfig } from 'axios';
|
|
3
|
+
/** Same as core's `UUID`; kept local so `events` doesn't depend on `@my-nestjs-kit/core` for one type. */
|
|
4
|
+
type UUID = string | import('node:crypto').UUID;
|
|
5
|
+
export type EventTransport = 'sse' | 'webhooks' | 'ws';
|
|
6
|
+
/** Wire name of an in-process event: `${namespace}.${action}`. */
|
|
7
|
+
export type EventKey<N extends string = string, A extends string = string> = `${N}.${A}`;
|
|
8
|
+
export interface EventInstance<Payload = any, Namespace extends string = string, Action extends string = string, Type extends string = string> {
|
|
9
|
+
payload?: Payload;
|
|
10
|
+
get namespace(): Namespace;
|
|
11
|
+
get action(): Action;
|
|
12
|
+
get type(): Type;
|
|
13
|
+
timestamp: number | Date;
|
|
14
|
+
transports: EventTransport[];
|
|
15
|
+
}
|
|
16
|
+
/** Constructor argument of an event class: the event minus its framework-owned fields. */
|
|
17
|
+
export type EventInit<E> = Omit<E, keyof EventInstance>;
|
|
18
|
+
export type EventMessageData<Data = Record<string, any>> = Data & {
|
|
19
|
+
_id: UUID;
|
|
20
|
+
};
|
|
21
|
+
/** Body of an outbound webhook / SSE channel message. */
|
|
22
|
+
export interface EventMessage<Data = Record<string, any>, Namespace extends string = string, Action extends string = string, Type extends string = string> {
|
|
23
|
+
namespace: Namespace;
|
|
24
|
+
action: Action;
|
|
25
|
+
type?: Type;
|
|
26
|
+
data: EventMessageData<Data>;
|
|
27
|
+
}
|
|
28
|
+
export interface WebhookEventInstance<Payload = any, Namespace extends string = string, Action extends string = string, Type extends string = string, Data = any> {
|
|
29
|
+
payload?: Payload;
|
|
30
|
+
config: AxiosRequestConfig<EventMessage<Data, Namespace, Action, Type>>;
|
|
31
|
+
get namespace(): Namespace;
|
|
32
|
+
get action(): Action;
|
|
33
|
+
get type(): Type;
|
|
34
|
+
timestamp: number | Date;
|
|
35
|
+
}
|
|
36
|
+
export type EventCtor = (abstract new (...args: any[]) => object) & {
|
|
37
|
+
readonly type: string;
|
|
38
|
+
};
|
|
39
|
+
export interface EventsMap {
|
|
40
|
+
}
|
|
41
|
+
/** `{ [C.type]: C }` — classes whose `type` is a bare `string` are dropped, so keys stay literal. */
|
|
42
|
+
export type EventsMapOf<Cs extends readonly EventCtor[]> = {
|
|
43
|
+
[C in Cs[number] as string extends C['type'] ? never : C['type']]: C;
|
|
44
|
+
};
|
|
45
|
+
export type EventName<M extends EventsMap> = Extract<keyof M, string>;
|
|
46
|
+
/** Arguments of `emit(type, ...args)` — the constructor's parameters. */
|
|
47
|
+
export type EventArgs<M extends EventsMap, K extends EventName<M>> = M[K] extends abstract new (...args: infer A) => any ? A : never;
|
|
48
|
+
/** What listeners receive — the constructed event. */
|
|
49
|
+
export type EventOf<M extends EventsMap, K extends EventName<M>> = M[K] extends abstract new (...args: any[]) => infer E ? E : never;
|
|
50
|
+
export type EventListenerFn<M extends EventsMap, K extends EventName<M>> = (event: EventOf<M, K>) => unknown;
|
|
51
|
+
export {};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export * from './events.types';
|
|
2
|
+
export * from './default-events.map';
|
|
3
|
+
export * from './base-event.factory';
|
|
4
|
+
export * from './events-registry';
|
|
5
|
+
export * from './event-bus.module';
|
|
6
|
+
export * from './events-service.base';
|
|
7
|
+
export * from './event-handler.decorator';
|
|
8
|
+
export * from './pipeline';
|
|
9
|
+
export * from './pipeline/events-listener.decorator';
|
|
10
|
+
export * from './pipeline/event-handlers.explorer';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./events.types"), exports);
|
|
18
|
+
__exportStar(require("./default-events.map"), exports);
|
|
19
|
+
__exportStar(require("./base-event.factory"), exports);
|
|
20
|
+
__exportStar(require("./events-registry"), exports);
|
|
21
|
+
__exportStar(require("./event-bus.module"), exports);
|
|
22
|
+
__exportStar(require("./events-service.base"), exports);
|
|
23
|
+
__exportStar(require("./event-handler.decorator"), exports);
|
|
24
|
+
__exportStar(require("./pipeline"), exports);
|
|
25
|
+
__exportStar(require("./pipeline/events-listener.decorator"), exports);
|
|
26
|
+
__exportStar(require("./pipeline/event-handlers.explorer"), exports);
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { ModuleRef } from '@nestjs/core';
|
|
2
|
+
import type { EventEnhancer, EventExceptionFilter, EventGuard, EventInterceptor } from './event-pipeline.interfaces';
|
|
3
|
+
import type { ResolvedEventPipeline } from './event-pipeline.runner';
|
|
4
|
+
export interface GlobalEventEnhancers {
|
|
5
|
+
guards?: EventEnhancer<EventGuard>[];
|
|
6
|
+
interceptors?: EventEnhancer<EventInterceptor>[];
|
|
7
|
+
filters?: EventEnhancer<EventExceptionFilter>[];
|
|
8
|
+
suppressErrors?: boolean;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Builds a handler's pipeline from global + class + method enhancers
|
|
12
|
+
* (`@UseEventGuards` / `@UseEventInterceptors` / `@UseEventFilters`).
|
|
13
|
+
* Shared by every listener kind (in-process events, Redis channels, …).
|
|
14
|
+
*/
|
|
15
|
+
export declare class EventEnhancersResolver {
|
|
16
|
+
private readonly moduleRef;
|
|
17
|
+
private readonly globals;
|
|
18
|
+
private readonly instances;
|
|
19
|
+
constructor(moduleRef: ModuleRef, globals?: GlobalEventEnhancers);
|
|
20
|
+
pipelineFor(classRef: object, handler: object, suppressErrors?: boolean): Promise<ResolvedEventPipeline>;
|
|
21
|
+
/** A provider in DI wins; otherwise the class is instantiated with its own deps (`ModuleRef.create`). */
|
|
22
|
+
resolve<T>(enhancer: EventEnhancer<T>): Promise<T>;
|
|
23
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.EventEnhancersResolver = void 0;
|
|
4
|
+
const event_pipeline_constants_1 = require("./event-pipeline.constants");
|
|
5
|
+
/**
|
|
6
|
+
* Builds a handler's pipeline from global + class + method enhancers
|
|
7
|
+
* (`@UseEventGuards` / `@UseEventInterceptors` / `@UseEventFilters`).
|
|
8
|
+
* Shared by every listener kind (in-process events, Redis channels, …).
|
|
9
|
+
*/
|
|
10
|
+
class EventEnhancersResolver {
|
|
11
|
+
constructor(moduleRef, globals = {}) {
|
|
12
|
+
this.moduleRef = moduleRef;
|
|
13
|
+
this.globals = globals;
|
|
14
|
+
this.instances = new Map();
|
|
15
|
+
}
|
|
16
|
+
async pipelineFor(classRef, handler, suppressErrors) {
|
|
17
|
+
const read = (key, target) => Reflect.getMetadata(key, target) ?? [];
|
|
18
|
+
const all = (items) => Promise.all(items.map((e) => this.resolve(e)));
|
|
19
|
+
return {
|
|
20
|
+
guards: await all([
|
|
21
|
+
...(this.globals.guards ?? []),
|
|
22
|
+
...read(event_pipeline_constants_1.EVENT_GUARDS_METADATA, classRef),
|
|
23
|
+
...read(event_pipeline_constants_1.EVENT_GUARDS_METADATA, handler),
|
|
24
|
+
]),
|
|
25
|
+
interceptors: await all([
|
|
26
|
+
...(this.globals.interceptors ?? []),
|
|
27
|
+
...read(event_pipeline_constants_1.EVENT_INTERCEPTORS_METADATA, classRef),
|
|
28
|
+
...read(event_pipeline_constants_1.EVENT_INTERCEPTORS_METADATA, handler),
|
|
29
|
+
]),
|
|
30
|
+
filters: await all([
|
|
31
|
+
...read(event_pipeline_constants_1.EVENT_FILTERS_METADATA, handler),
|
|
32
|
+
...read(event_pipeline_constants_1.EVENT_FILTERS_METADATA, classRef),
|
|
33
|
+
...(this.globals.filters ?? []),
|
|
34
|
+
]),
|
|
35
|
+
suppressErrors: suppressErrors ?? this.globals.suppressErrors ?? true,
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
/** A provider in DI wins; otherwise the class is instantiated with its own deps (`ModuleRef.create`). */
|
|
39
|
+
async resolve(enhancer) {
|
|
40
|
+
if (typeof enhancer !== 'function')
|
|
41
|
+
return enhancer;
|
|
42
|
+
const classRef = enhancer;
|
|
43
|
+
if (!this.instances.has(classRef)) {
|
|
44
|
+
let instance;
|
|
45
|
+
try {
|
|
46
|
+
instance = this.moduleRef.get(classRef, { strict: false });
|
|
47
|
+
}
|
|
48
|
+
catch {
|
|
49
|
+
instance = await this.moduleRef.create(classRef);
|
|
50
|
+
}
|
|
51
|
+
this.instances.set(classRef, instance);
|
|
52
|
+
}
|
|
53
|
+
return this.instances.get(classRef);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
exports.EventEnhancersResolver = EventEnhancersResolver;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { Type } from '@nestjs/common';
|
|
2
|
+
/** What guards, interceptors and filters see — the event-listener twin of Nest's `ExecutionContext`. */
|
|
3
|
+
export declare class EventExecutionContext<E = unknown> {
|
|
4
|
+
/** The emitted event name (the concrete one, also for wildcard handlers). */
|
|
5
|
+
readonly type: string;
|
|
6
|
+
/** The registered pattern this handler listens on. */
|
|
7
|
+
readonly pattern: string;
|
|
8
|
+
readonly args: unknown[];
|
|
9
|
+
readonly instance: object;
|
|
10
|
+
private readonly handler;
|
|
11
|
+
constructor(
|
|
12
|
+
/** The emitted event name (the concrete one, also for wildcard handlers). */
|
|
13
|
+
type: string,
|
|
14
|
+
/** The registered pattern this handler listens on. */
|
|
15
|
+
pattern: string, args: unknown[], instance: object, handler: (...args: unknown[]) => unknown);
|
|
16
|
+
getEvent<T = E>(): T;
|
|
17
|
+
getHandler(): (...args: unknown[]) => unknown;
|
|
18
|
+
getClass<T = unknown>(): Type<T>;
|
|
19
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.EventExecutionContext = void 0;
|
|
4
|
+
/** What guards, interceptors and filters see — the event-listener twin of Nest's `ExecutionContext`. */
|
|
5
|
+
class EventExecutionContext {
|
|
6
|
+
constructor(
|
|
7
|
+
/** The emitted event name (the concrete one, also for wildcard handlers). */
|
|
8
|
+
type,
|
|
9
|
+
/** The registered pattern this handler listens on. */
|
|
10
|
+
pattern, args, instance, handler) {
|
|
11
|
+
this.type = type;
|
|
12
|
+
this.pattern = pattern;
|
|
13
|
+
this.args = args;
|
|
14
|
+
this.instance = instance;
|
|
15
|
+
this.handler = handler;
|
|
16
|
+
}
|
|
17
|
+
getEvent() {
|
|
18
|
+
return this.args[0];
|
|
19
|
+
}
|
|
20
|
+
getHandler() {
|
|
21
|
+
return this.handler;
|
|
22
|
+
}
|
|
23
|
+
getClass() {
|
|
24
|
+
return this.instance.constructor;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
exports.EventExecutionContext = EventExecutionContext;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { OnApplicationBootstrap, OnApplicationShutdown } from '@nestjs/common';
|
|
2
|
+
import { DiscoveryService, MetadataScanner, ModuleRef } from '@nestjs/core';
|
|
3
|
+
import { EventEmitter2 } from '@nestjs/event-emitter';
|
|
4
|
+
import type { EventBusModuleOptions } from './event-pipeline.interfaces';
|
|
5
|
+
/**
|
|
6
|
+
* Replaces `@nestjs/event-emitter`'s loader for `@EventHandler` methods: finds the
|
|
7
|
+
* `@EventsListener()` providers via `DiscoveryService`, resolves their handlers' guards /
|
|
8
|
+
* interceptors / filters and subscribes one dispatcher per pattern to EventEmitter2.
|
|
9
|
+
*/
|
|
10
|
+
export declare class EventHandlersExplorer implements OnApplicationBootstrap, OnApplicationShutdown {
|
|
11
|
+
private readonly discovery;
|
|
12
|
+
private readonly scanner;
|
|
13
|
+
private readonly moduleRef;
|
|
14
|
+
private readonly ee;
|
|
15
|
+
private readonly options;
|
|
16
|
+
private readonly logger;
|
|
17
|
+
private readonly subscriptions;
|
|
18
|
+
private readonly enhancers;
|
|
19
|
+
constructor(discovery: DiscoveryService, scanner: MetadataScanner, moduleRef: ModuleRef, ee: EventEmitter2, options: EventBusModuleOptions);
|
|
20
|
+
onApplicationBootstrap(): Promise<void>;
|
|
21
|
+
onApplicationShutdown(): void;
|
|
22
|
+
private metasOf;
|
|
23
|
+
/** `[methodKey, method]` of every `@EventHandler` method on the instance's prototype chain. */
|
|
24
|
+
private handlersOf;
|
|
25
|
+
/** An `@EventHandler` outside an `@EventsListener()` class would be silently dead — say so. */
|
|
26
|
+
private reportStrayHandlers;
|
|
27
|
+
}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
12
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.EventHandlersExplorer = void 0;
|
|
16
|
+
const common_1 = require("@nestjs/common");
|
|
17
|
+
const core_1 = require("@nestjs/core");
|
|
18
|
+
const event_emitter_1 = require("@nestjs/event-emitter");
|
|
19
|
+
const event_pipeline_constants_1 = require("./event-pipeline.constants");
|
|
20
|
+
const events_listener_decorator_1 = require("./events-listener.decorator");
|
|
21
|
+
const event_enhancers_resolver_1 = require("./event-enhancers.resolver");
|
|
22
|
+
const event_pipeline_runner_1 = require("./event-pipeline.runner");
|
|
23
|
+
/**
|
|
24
|
+
* Replaces `@nestjs/event-emitter`'s loader for `@EventHandler` methods: finds the
|
|
25
|
+
* `@EventsListener()` providers via `DiscoveryService`, resolves their handlers' guards /
|
|
26
|
+
* interceptors / filters and subscribes one dispatcher per pattern to EventEmitter2.
|
|
27
|
+
*/
|
|
28
|
+
let EventHandlersExplorer = class EventHandlersExplorer {
|
|
29
|
+
constructor(discovery, scanner, moduleRef, ee, options) {
|
|
30
|
+
this.discovery = discovery;
|
|
31
|
+
this.scanner = scanner;
|
|
32
|
+
this.moduleRef = moduleRef;
|
|
33
|
+
this.ee = ee;
|
|
34
|
+
this.options = options;
|
|
35
|
+
this.logger = new common_1.Logger('EventHandlers');
|
|
36
|
+
this.subscriptions = [];
|
|
37
|
+
this.enhancers = new event_enhancers_resolver_1.EventEnhancersResolver(moduleRef, options);
|
|
38
|
+
}
|
|
39
|
+
async onApplicationBootstrap() {
|
|
40
|
+
const byPattern = new Map();
|
|
41
|
+
const listeners = new Set(this.discovery.getProviders({ metadataKey: events_listener_decorator_1.EventsListener.KEY }));
|
|
42
|
+
this.reportStrayHandlers(listeners);
|
|
43
|
+
for (const wrapper of listeners) {
|
|
44
|
+
const { instance } = wrapper;
|
|
45
|
+
if (!instance || wrapper.isAlias || typeof instance !== 'object')
|
|
46
|
+
continue;
|
|
47
|
+
const { namespace } = Reflect.getMetadata(events_listener_decorator_1.EventsListener.KEY, instance.constructor) ?? {};
|
|
48
|
+
for (const [methodKey, handler] of this.handlersOf(instance)) {
|
|
49
|
+
if (!wrapper.isDependencyTreeStatic()) {
|
|
50
|
+
this.logger.warn(`${instance.constructor.name}.${methodKey}: request-scoped event handlers are not supported, skipped`);
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
for (const meta of this.metasOf(handler)) {
|
|
54
|
+
const pattern = (0, events_listener_decorator_1.resolveEventKey)(namespace, meta.event);
|
|
55
|
+
const pipeline = await this.enhancers.pipelineFor(instance.constructor, handler, meta.options.suppressErrors);
|
|
56
|
+
const list = byPattern.get(pattern) ?? [];
|
|
57
|
+
list.push({
|
|
58
|
+
pattern,
|
|
59
|
+
instance,
|
|
60
|
+
handler,
|
|
61
|
+
options: meta.options,
|
|
62
|
+
pipeline,
|
|
63
|
+
});
|
|
64
|
+
byPattern.set(pattern, list);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
for (const [pattern, handlers] of byPattern) {
|
|
69
|
+
// stable sort: higher priority first, ties keep registration order
|
|
70
|
+
handlers.sort((a, b) => (b.options.priority ?? 0) - (a.options.priority ?? 0));
|
|
71
|
+
const dispatcher = (0, event_pipeline_runner_1.createEventDispatcher)(pattern, handlers, this.logger);
|
|
72
|
+
this.ee.on(pattern, dispatcher);
|
|
73
|
+
this.subscriptions.push([pattern, dispatcher]);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
onApplicationShutdown() {
|
|
77
|
+
for (const [pattern, dispatcher] of this.subscriptions)
|
|
78
|
+
this.ee.off(pattern, dispatcher);
|
|
79
|
+
this.subscriptions.length = 0;
|
|
80
|
+
}
|
|
81
|
+
metasOf(handler) {
|
|
82
|
+
return ((typeof handler === 'function' &&
|
|
83
|
+
Reflect.getMetadata(event_pipeline_constants_1.EVENT_HANDLER_METADATA, handler)) ||
|
|
84
|
+
[]);
|
|
85
|
+
}
|
|
86
|
+
/** `[methodKey, method]` of every `@EventHandler` method on the instance's prototype chain. */
|
|
87
|
+
handlersOf(instance) {
|
|
88
|
+
const prototype = Object.getPrototypeOf(instance);
|
|
89
|
+
if (!prototype)
|
|
90
|
+
return [];
|
|
91
|
+
return this.scanner
|
|
92
|
+
.getAllMethodNames(prototype)
|
|
93
|
+
.map((key) => [key, prototype[key]])
|
|
94
|
+
.filter(([, method]) => this.metasOf(method).length > 0);
|
|
95
|
+
}
|
|
96
|
+
/** An `@EventHandler` outside an `@EventsListener()` class would be silently dead — say so. */
|
|
97
|
+
reportStrayHandlers(listeners) {
|
|
98
|
+
for (const wrapper of this.discovery.getProviders()) {
|
|
99
|
+
const { instance } = wrapper;
|
|
100
|
+
if (listeners.has(wrapper) || !instance || typeof instance !== 'object')
|
|
101
|
+
continue;
|
|
102
|
+
for (const [methodKey] of this.handlersOf(instance)) {
|
|
103
|
+
this.logger.warn(`${instance.constructor.name}.${methodKey}: @EventHandler outside an @EventsListener() class — not subscribed`);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
exports.EventHandlersExplorer = EventHandlersExplorer;
|
|
109
|
+
exports.EventHandlersExplorer = EventHandlersExplorer = __decorate([
|
|
110
|
+
(0, common_1.Injectable)(),
|
|
111
|
+
__param(4, (0, common_1.Inject)(event_pipeline_constants_1.EVENT_BUS_MODULE_OPTIONS)),
|
|
112
|
+
__metadata("design:paramtypes", [core_1.DiscoveryService,
|
|
113
|
+
core_1.MetadataScanner,
|
|
114
|
+
core_1.ModuleRef,
|
|
115
|
+
event_emitter_1.EventEmitter2, Object])
|
|
116
|
+
], EventHandlersExplorer);
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export declare const EVENT_HANDLER_METADATA = "nest-kit:event-handler";
|
|
2
|
+
export declare const EVENT_GUARDS_METADATA = "nest-kit:event-guards";
|
|
3
|
+
export declare const EVENT_INTERCEPTORS_METADATA = "nest-kit:event-interceptors";
|
|
4
|
+
export declare const EVENT_FILTERS_METADATA = "nest-kit:event-filters";
|
|
5
|
+
export declare const EVENT_BUS_MODULE_OPTIONS: unique symbol;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.EVENT_BUS_MODULE_OPTIONS = exports.EVENT_FILTERS_METADATA = exports.EVENT_INTERCEPTORS_METADATA = exports.EVENT_GUARDS_METADATA = exports.EVENT_HANDLER_METADATA = void 0;
|
|
4
|
+
exports.EVENT_HANDLER_METADATA = 'nest-kit:event-handler';
|
|
5
|
+
exports.EVENT_GUARDS_METADATA = 'nest-kit:event-guards';
|
|
6
|
+
exports.EVENT_INTERCEPTORS_METADATA = 'nest-kit:event-interceptors';
|
|
7
|
+
exports.EVENT_FILTERS_METADATA = 'nest-kit:event-filters';
|
|
8
|
+
exports.EVENT_BUS_MODULE_OPTIONS = Symbol('EVENT_BUS_MODULE_OPTIONS');
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import 'reflect-metadata';
|
|
2
|
+
import type { EventEnhancer, EventExceptionFilter, EventGuard, EventInterceptor } from './event-pipeline.interfaces';
|
|
3
|
+
/** Guards for event handlers — on a class (all its handlers) or a method. Run class → method. */
|
|
4
|
+
export declare const UseEventGuards: (...guards: EventEnhancer<EventGuard>[]) => ClassDecorator & MethodDecorator;
|
|
5
|
+
export declare const UseEventInterceptors: (...interceptors: EventEnhancer<EventInterceptor>[]) => ClassDecorator & MethodDecorator;
|
|
6
|
+
/** Filters are tried method → class → global; the first one that doesn't rethrow handles the error. */
|
|
7
|
+
export declare const UseEventFilters: (...filters: EventEnhancer<EventExceptionFilter>[]) => ClassDecorator & MethodDecorator;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.UseEventFilters = exports.UseEventInterceptors = exports.UseEventGuards = void 0;
|
|
4
|
+
require("reflect-metadata");
|
|
5
|
+
const event_pipeline_constants_1 = require("./event-pipeline.constants");
|
|
6
|
+
function appendMetadata(key, items) {
|
|
7
|
+
return (target, _key, descriptor) => {
|
|
8
|
+
const holder = descriptor ? descriptor.value : target;
|
|
9
|
+
const current = Reflect.getMetadata(key, holder) ?? [];
|
|
10
|
+
Reflect.defineMetadata(key, [...current, ...items], holder);
|
|
11
|
+
return descriptor;
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
/** Guards for event handlers — on a class (all its handlers) or a method. Run class → method. */
|
|
15
|
+
const UseEventGuards = (...guards) => appendMetadata(event_pipeline_constants_1.EVENT_GUARDS_METADATA, guards);
|
|
16
|
+
exports.UseEventGuards = UseEventGuards;
|
|
17
|
+
const UseEventInterceptors = (...interceptors) => appendMetadata(event_pipeline_constants_1.EVENT_INTERCEPTORS_METADATA, interceptors);
|
|
18
|
+
exports.UseEventInterceptors = UseEventInterceptors;
|
|
19
|
+
/** Filters are tried method → class → global; the first one that doesn't rethrow handles the error. */
|
|
20
|
+
const UseEventFilters = (...filters) => appendMetadata(event_pipeline_constants_1.EVENT_FILTERS_METADATA, filters);
|
|
21
|
+
exports.UseEventFilters = UseEventFilters;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { Type } from '@nestjs/common';
|
|
2
|
+
import type { EventExecutionContext } from './event-execution-context';
|
|
3
|
+
/** `false` skips this handler only; other handlers of the event still run. */
|
|
4
|
+
export interface EventGuard<E = any> {
|
|
5
|
+
canActivate(ctx: EventExecutionContext<E>): boolean | Promise<boolean>;
|
|
6
|
+
}
|
|
7
|
+
export type EventCallHandler = () => Promise<unknown>;
|
|
8
|
+
/** Onion around the handler: call `next()` to continue, or don't to short-circuit. */
|
|
9
|
+
export interface EventInterceptor<E = any> {
|
|
10
|
+
intercept(ctx: EventExecutionContext<E>, next: EventCallHandler): Promise<unknown>;
|
|
11
|
+
}
|
|
12
|
+
/** Gets errors thrown by guards, interceptors or the handler. Rethrow to pass it on to the next filter. */
|
|
13
|
+
export interface EventExceptionFilter<E = any> {
|
|
14
|
+
catch(error: unknown, ctx: EventExecutionContext<E>): unknown;
|
|
15
|
+
}
|
|
16
|
+
/** A class (resolved from DI, or instantiated if it isn't a provider) or a ready instance. */
|
|
17
|
+
export type EventEnhancer<T> = Type<T> | T;
|
|
18
|
+
export interface EventHandlerOptions {
|
|
19
|
+
/** Higher runs first among handlers of the same pattern. Default 0. */
|
|
20
|
+
priority?: number;
|
|
21
|
+
/** Swallow (and log) errors no filter handled. Default true, like `@OnEvent`. */
|
|
22
|
+
suppressErrors?: boolean;
|
|
23
|
+
}
|
|
24
|
+
export interface EventHandlerMeta {
|
|
25
|
+
event: string;
|
|
26
|
+
options: EventHandlerOptions;
|
|
27
|
+
}
|
|
28
|
+
export interface EventBusModuleOptions {
|
|
29
|
+
guards?: EventEnhancer<EventGuard>[];
|
|
30
|
+
interceptors?: EventEnhancer<EventInterceptor>[];
|
|
31
|
+
filters?: EventEnhancer<EventExceptionFilter>[];
|
|
32
|
+
/** Default for handlers that don't set it. Default true. */
|
|
33
|
+
suppressErrors?: boolean;
|
|
34
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { Logger } from '@nestjs/common';
|
|
2
|
+
import { EventExecutionContext } from './event-execution-context';
|
|
3
|
+
import type { EventExceptionFilter, EventGuard, EventHandlerOptions, EventInterceptor } from './event-pipeline.interfaces';
|
|
4
|
+
export interface ResolvedEventPipeline {
|
|
5
|
+
guards: EventGuard[];
|
|
6
|
+
interceptors: EventInterceptor[];
|
|
7
|
+
filters: EventExceptionFilter[];
|
|
8
|
+
suppressErrors: boolean;
|
|
9
|
+
}
|
|
10
|
+
export type EventPipelineResult = {
|
|
11
|
+
status: 'done';
|
|
12
|
+
result: unknown;
|
|
13
|
+
} | {
|
|
14
|
+
status: 'skipped';
|
|
15
|
+
guard: string;
|
|
16
|
+
} | {
|
|
17
|
+
status: 'failed';
|
|
18
|
+
error: unknown;
|
|
19
|
+
};
|
|
20
|
+
/** guards → interceptors (onion) → handler; errors go through the filters. Never re-emits. */
|
|
21
|
+
export declare function runEventPipeline(ctx: EventExecutionContext, call: () => unknown, pipeline: ResolvedEventPipeline, logger?: Pick<Logger, 'error' | 'debug'>): Promise<EventPipelineResult>;
|
|
22
|
+
export interface RegisteredEventHandler {
|
|
23
|
+
pattern: string;
|
|
24
|
+
instance: object;
|
|
25
|
+
handler: (...args: unknown[]) => unknown;
|
|
26
|
+
options: EventHandlerOptions;
|
|
27
|
+
pipeline: ResolvedEventPipeline;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* One EventEmitter2 listener per pattern. Runs that pattern's handlers one after another
|
|
31
|
+
* (by `priority`, then registration order) and awaits each, each through its own pipeline,
|
|
32
|
+
* so a guard only ever skips its own handler and the order is deterministic.
|
|
33
|
+
*/
|
|
34
|
+
export declare function createEventDispatcher(pattern: string, handlers: RegisteredEventHandler[], logger?: Pick<Logger, 'error' | 'debug'>): (this: {
|
|
35
|
+
event?: string | string[];
|
|
36
|
+
delimiter?: string;
|
|
37
|
+
} | undefined, ...args: unknown[]) => Promise<EventPipelineResult[]>;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createEventDispatcher = exports.runEventPipeline = void 0;
|
|
4
|
+
const event_execution_context_1 = require("./event-execution-context");
|
|
5
|
+
/** guards → interceptors (onion) → handler; errors go through the filters. Never re-emits. */
|
|
6
|
+
async function runEventPipeline(ctx, call, pipeline, logger) {
|
|
7
|
+
try {
|
|
8
|
+
for (const guard of pipeline.guards) {
|
|
9
|
+
if (!(await guard.canActivate(ctx))) {
|
|
10
|
+
const name = guard.constructor?.name ?? 'guard';
|
|
11
|
+
logger?.debug(`${ctx.type}: ${ctx.getClass().name} skipped by ${name}`);
|
|
12
|
+
return { status: 'skipped', guard: name };
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
const invoke = pipeline.interceptors.reduceRight((next, interceptor) => () => interceptor.intercept(ctx, next), async () => call());
|
|
16
|
+
return { status: 'done', result: await invoke() };
|
|
17
|
+
}
|
|
18
|
+
catch (error) {
|
|
19
|
+
let current = error;
|
|
20
|
+
for (const filter of pipeline.filters) {
|
|
21
|
+
try {
|
|
22
|
+
return { status: 'done', result: await filter.catch(current, ctx) };
|
|
23
|
+
}
|
|
24
|
+
catch (rethrown) {
|
|
25
|
+
current = rethrown;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
if (!pipeline.suppressErrors)
|
|
29
|
+
throw current;
|
|
30
|
+
logger?.error(current);
|
|
31
|
+
return { status: 'failed', error: current };
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
exports.runEventPipeline = runEventPipeline;
|
|
35
|
+
/**
|
|
36
|
+
* One EventEmitter2 listener per pattern. Runs that pattern's handlers one after another
|
|
37
|
+
* (by `priority`, then registration order) and awaits each, each through its own pipeline,
|
|
38
|
+
* so a guard only ever skips its own handler and the order is deterministic.
|
|
39
|
+
*/
|
|
40
|
+
function createEventDispatcher(pattern, handlers, logger) {
|
|
41
|
+
return function dispatch(...args) {
|
|
42
|
+
// EventEmitter2 calls listeners with `this` = emitter, whose `event` is the emitted name
|
|
43
|
+
const emitted = this?.event;
|
|
44
|
+
const type = Array.isArray(emitted)
|
|
45
|
+
? emitted.join(this?.delimiter ?? '.')
|
|
46
|
+
: emitted ?? pattern;
|
|
47
|
+
return (async () => {
|
|
48
|
+
const results = [];
|
|
49
|
+
let firstError;
|
|
50
|
+
for (const h of handlers) {
|
|
51
|
+
const ctx = new event_execution_context_1.EventExecutionContext(type, pattern, args, h.instance, h.handler);
|
|
52
|
+
try {
|
|
53
|
+
results.push(await runEventPipeline(ctx, () => h.handler.apply(h.instance, args), h.pipeline, logger));
|
|
54
|
+
}
|
|
55
|
+
catch (error) {
|
|
56
|
+
// suppressErrors: false — keep the other handlers running, surface the first error
|
|
57
|
+
firstError ??= error;
|
|
58
|
+
results.push({ status: 'failed', error });
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
if (firstError !== undefined)
|
|
62
|
+
throw firstError;
|
|
63
|
+
return results;
|
|
64
|
+
})();
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
exports.createEventDispatcher = createEventDispatcher;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { EventName, EventsMap } from '../events.types';
|
|
2
|
+
import type { DefaultEventsMap } from '../default-events.map';
|
|
3
|
+
/** Namespace part of the map's keys: `'orders.status.changed'` → `'orders'`. */
|
|
4
|
+
export type EventNamespace<M extends EventsMap = DefaultEventsMap> = EventName<M> extends infer K ? K extends `${infer N}.${string}` ? N : never : never;
|
|
5
|
+
/** Action part of the map's keys: `'orders.status.changed'` → `'status.changed'`. */
|
|
6
|
+
export type EventAction<M extends EventsMap = DefaultEventsMap> = EventName<M> extends infer K ? K extends `${string}.${infer A}` ? A : never : never;
|
|
7
|
+
export interface EventsListenerOptions {
|
|
8
|
+
/** Prefix for the class' `@EventHandler` keys, like a controller's path: `'orders'` + `'remove'` → `'orders.remove'`. */
|
|
9
|
+
namespace?: string;
|
|
10
|
+
/** Label used in logs; defaults to the class name. */
|
|
11
|
+
name?: string;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Marks a provider as a domain events listener (`<Domain>Listener`), like `@Controller` for routes.
|
|
15
|
+
* `EventBusModule` discovers only these classes. With a namespace the class' `@EventHandler`
|
|
16
|
+
* keys are relative to it; without one they are full keys.
|
|
17
|
+
*
|
|
18
|
+
* ```ts
|
|
19
|
+
* @EventsListener('orders')
|
|
20
|
+
* export class OrdersListener {
|
|
21
|
+
* @EventHandler('remove') // → 'orders.remove'
|
|
22
|
+
* onRemoved(e: OrdersRemoveEvent) {}
|
|
23
|
+
* }
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export declare function EventsListener<N extends EventNamespace<M>, M extends EventsMap = DefaultEventsMap>(namespace?: N, options?: Omit<EventsListenerOptions, 'namespace'>): ClassDecorator;
|
|
27
|
+
export declare namespace EventsListener {
|
|
28
|
+
var KEY: string;
|
|
29
|
+
}
|
|
30
|
+
/** Full event key of a handler: the class namespace joined with the handler key. */
|
|
31
|
+
export declare function resolveEventKey(namespace: string | undefined, key: string): string;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.resolveEventKey = exports.EventsListener = void 0;
|
|
4
|
+
const core_1 = require("@nestjs/core");
|
|
5
|
+
const discoverable = core_1.DiscoveryService.createDecorator();
|
|
6
|
+
/**
|
|
7
|
+
* Marks a provider as a domain events listener (`<Domain>Listener`), like `@Controller` for routes.
|
|
8
|
+
* `EventBusModule` discovers only these classes. With a namespace the class' `@EventHandler`
|
|
9
|
+
* keys are relative to it; without one they are full keys.
|
|
10
|
+
*
|
|
11
|
+
* ```ts
|
|
12
|
+
* @EventsListener('orders')
|
|
13
|
+
* export class OrdersListener {
|
|
14
|
+
* @EventHandler('remove') // → 'orders.remove'
|
|
15
|
+
* onRemoved(e: OrdersRemoveEvent) {}
|
|
16
|
+
* }
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
function EventsListener(namespace, options = {}) {
|
|
20
|
+
return discoverable({ ...options, namespace });
|
|
21
|
+
}
|
|
22
|
+
exports.EventsListener = EventsListener;
|
|
23
|
+
EventsListener.KEY = discoverable.KEY;
|
|
24
|
+
/** Full event key of a handler: the class namespace joined with the handler key. */
|
|
25
|
+
function resolveEventKey(namespace, key) {
|
|
26
|
+
return namespace ? `${namespace}.${key}` : key;
|
|
27
|
+
}
|
|
28
|
+
exports.resolveEventKey = resolveEventKey;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export * from './event-execution-context';
|
|
2
|
+
export * from './event-pipeline.interfaces';
|
|
3
|
+
export * from './event-pipeline.constants';
|
|
4
|
+
export * from './event-pipeline.decorators';
|
|
5
|
+
export * from './event-pipeline.runner';
|
|
6
|
+
export * from './event-enhancers.resolver';
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
// @my-nestjs-kit/events/pipeline — the guards / interceptors / filters pipeline shared by the events bus and
|
|
18
|
+
// @my-nestjs-kit/channels. Loads no @nestjs/event-emitter code, so channels works without it.
|
|
19
|
+
__exportStar(require("./event-execution-context"), exports);
|
|
20
|
+
__exportStar(require("./event-pipeline.interfaces"), exports);
|
|
21
|
+
__exportStar(require("./event-pipeline.constants"), exports);
|
|
22
|
+
__exportStar(require("./event-pipeline.decorators"), exports);
|
|
23
|
+
__exportStar(require("./event-pipeline.runner"), exports);
|
|
24
|
+
__exportStar(require("./event-enhancers.resolver"), exports);
|
package/package.json
CHANGED
|
@@ -1,6 +1,107 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@my-nestjs-kit/events",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0-dev.20261129010337",
|
|
4
|
+
"description": "Typed in-process events for NestJS over EventEmitter2: event classes keyed by type, EventsServiceBase, @EventsListener / @EventHandler, guards / interceptors / filters pipeline (also at /pipeline).",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"nestjs",
|
|
7
|
+
"nest",
|
|
8
|
+
"events",
|
|
9
|
+
"event-emitter",
|
|
10
|
+
"eventemitter2"
|
|
11
|
+
],
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"homepage": "https://github.com/sabationok/my-nestjs-kit/tree/main/packages/events#readme",
|
|
14
|
+
"bugs": {
|
|
15
|
+
"url": "https://github.com/sabationok/my-nestjs-kit/issues"
|
|
16
|
+
},
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/sabationok/my-nestjs-kit.git",
|
|
20
|
+
"directory": "packages/events"
|
|
21
|
+
},
|
|
22
|
+
"engines": {
|
|
23
|
+
"node": ">=18"
|
|
24
|
+
},
|
|
25
|
+
"publishConfig": {
|
|
26
|
+
"access": "public"
|
|
27
|
+
},
|
|
28
|
+
"main": "dist/index.js",
|
|
29
|
+
"types": "dist/index.d.ts",
|
|
30
|
+
"exports": {
|
|
31
|
+
".": {
|
|
32
|
+
"types": "./dist/index.d.ts",
|
|
33
|
+
"require": "./dist/index.js",
|
|
34
|
+
"default": "./dist/index.js"
|
|
35
|
+
},
|
|
36
|
+
"./pipeline": {
|
|
37
|
+
"types": "./dist/pipeline/index.d.ts",
|
|
38
|
+
"require": "./dist/pipeline/index.js",
|
|
39
|
+
"default": "./dist/pipeline/index.js"
|
|
40
|
+
},
|
|
41
|
+
"./package.json": "./package.json"
|
|
42
|
+
},
|
|
43
|
+
"typesVersions": {
|
|
44
|
+
"*": {
|
|
45
|
+
"pipeline": [
|
|
46
|
+
"dist/pipeline/index.d.ts"
|
|
47
|
+
]
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
"files": [
|
|
51
|
+
"dist",
|
|
52
|
+
"!dist/**/*.spec.*",
|
|
53
|
+
"!dist/**/*.tsbuildinfo",
|
|
54
|
+
"README.md",
|
|
55
|
+
"LICENSE"
|
|
56
|
+
],
|
|
57
|
+
"peerDependencies": {
|
|
58
|
+
"@nestjs/common": "^10.4.1",
|
|
59
|
+
"@nestjs/core": "^10.4.1",
|
|
60
|
+
"@nestjs/event-emitter": "^2.0.4",
|
|
61
|
+
"axios": "^1.7.5",
|
|
62
|
+
"reflect-metadata": "^0.2.2"
|
|
63
|
+
},
|
|
64
|
+
"peerDependenciesMeta": {
|
|
65
|
+
"@nestjs/event-emitter": {
|
|
66
|
+
"optional": true
|
|
67
|
+
}
|
|
68
|
+
},
|
|
69
|
+
"devDependencies": {
|
|
70
|
+
"@nestjs/common": "10.4.20",
|
|
71
|
+
"@nestjs/core": "10.4.1",
|
|
72
|
+
"@nestjs/event-emitter": "2.0.4",
|
|
73
|
+
"@nestjs/testing": "10.4.1",
|
|
74
|
+
"@types/jest": "29.5.12",
|
|
75
|
+
"@types/node": "20.11.28",
|
|
76
|
+
"axios": "1.13.2",
|
|
77
|
+
"jest": "29.7.0",
|
|
78
|
+
"reflect-metadata": "0.2.2",
|
|
79
|
+
"rxjs": "7.8.1",
|
|
80
|
+
"ts-jest": "29.1.2",
|
|
81
|
+
"typescript": "5.4.2"
|
|
82
|
+
},
|
|
83
|
+
"jest": {
|
|
84
|
+
"moduleFileExtensions": [
|
|
85
|
+
"js",
|
|
86
|
+
"json",
|
|
87
|
+
"ts"
|
|
88
|
+
],
|
|
89
|
+
"rootDir": "src",
|
|
90
|
+
"testRegex": ".*\\.spec\\.ts$",
|
|
91
|
+
"transform": {
|
|
92
|
+
"^.+\\.ts$": [
|
|
93
|
+
"ts-jest",
|
|
94
|
+
{
|
|
95
|
+
"tsconfig": "<rootDir>/../tsconfig.spec.json"
|
|
96
|
+
}
|
|
97
|
+
]
|
|
98
|
+
},
|
|
99
|
+
"testEnvironment": "node"
|
|
100
|
+
},
|
|
101
|
+
"scripts": {
|
|
102
|
+
"build": "rm -rf dist && tsc -p tsconfig.json",
|
|
103
|
+
"build:watch": "tsc -p tsconfig.json --watch",
|
|
104
|
+
"typecheck": "tsc -p tsconfig.spec.json",
|
|
105
|
+
"test": "jest"
|
|
106
|
+
}
|
|
6
107
|
}
|