@wooksjs/event-core 0.4.10 → 0.4.12
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/index.cjs +8 -42
- package/dist/index.d.ts +104 -110
- package/dist/index.mjs +8 -42
- package/package.json +10 -2
package/dist/index.cjs
CHANGED
|
@@ -12,32 +12,18 @@ function attachHook(target, opts, name) {
|
|
|
12
12
|
}
|
|
13
13
|
|
|
14
14
|
let currentContext = null;
|
|
15
|
-
/**
|
|
16
|
-
* Create a new event context
|
|
17
|
-
*
|
|
18
|
-
* @param data
|
|
19
|
-
* @returns set of hooks { getCtx, restoreCtx, clearCtx, hookStore, getStore, setStore }
|
|
20
|
-
*/
|
|
21
15
|
function createEventContext(data) {
|
|
22
|
-
const newContext =
|
|
16
|
+
const newContext = { ...data };
|
|
23
17
|
currentContext = newContext;
|
|
24
18
|
return _getCtxHelpers(newContext);
|
|
25
19
|
}
|
|
26
|
-
/**
|
|
27
|
-
* Use existing event context
|
|
28
|
-
*
|
|
29
|
-
* !Must be called syncronously while context is reachable
|
|
30
|
-
*
|
|
31
|
-
* @returns set of hooks { getCtx, restoreCtx, clearCtx, hookStore, getStore, setStore }
|
|
32
|
-
*/
|
|
33
20
|
function useEventContext(expectedTypes) {
|
|
34
|
-
var _a;
|
|
35
21
|
if (!currentContext) {
|
|
36
22
|
throw new Error('Event context does not exist. Use event context synchronously within the runtime of the event.');
|
|
37
23
|
}
|
|
38
24
|
const cc = currentContext;
|
|
39
25
|
if (expectedTypes || typeof expectedTypes === 'string') {
|
|
40
|
-
const type =
|
|
26
|
+
const type = cc.event?.type;
|
|
41
27
|
const types = typeof expectedTypes === 'string' ? [expectedTypes] : expectedTypes;
|
|
42
28
|
if (!types.includes(type))
|
|
43
29
|
new Error(`Event context type mismatch: expected ${types
|
|
@@ -47,12 +33,6 @@ function useEventContext(expectedTypes) {
|
|
|
47
33
|
return _getCtxHelpers(cc);
|
|
48
34
|
}
|
|
49
35
|
function _getCtxHelpers(cc) {
|
|
50
|
-
/**
|
|
51
|
-
* Hook to an event store property
|
|
52
|
-
*
|
|
53
|
-
* @param key store property key
|
|
54
|
-
* @returns a hook { value: <prop value>, hook: (key2: keyof <prop value>) => { value: <nested prop value> }, ... }
|
|
55
|
-
*/
|
|
56
36
|
function store(key) {
|
|
57
37
|
const obj = {
|
|
58
38
|
value: null,
|
|
@@ -112,29 +92,12 @@ function _getCtxHelpers(cc) {
|
|
|
112
92
|
}
|
|
113
93
|
return obj;
|
|
114
94
|
}
|
|
115
|
-
/**
|
|
116
|
-
* Get event context object
|
|
117
|
-
*
|
|
118
|
-
* @returns whole context object
|
|
119
|
-
*/
|
|
120
95
|
function getCtx() {
|
|
121
96
|
return cc;
|
|
122
97
|
}
|
|
123
|
-
/**
|
|
124
|
-
* Get value of event store property
|
|
125
|
-
*
|
|
126
|
-
* @param key property name
|
|
127
|
-
* @returns value of property by name
|
|
128
|
-
*/
|
|
129
98
|
function get(key) {
|
|
130
99
|
return getCtx()[key];
|
|
131
100
|
}
|
|
132
|
-
/**
|
|
133
|
-
* Set value of event store property
|
|
134
|
-
*
|
|
135
|
-
* @param key property name
|
|
136
|
-
* @param v property value
|
|
137
|
-
*/
|
|
138
101
|
function set(key, v) {
|
|
139
102
|
getCtx()[key] = v;
|
|
140
103
|
}
|
|
@@ -173,7 +136,10 @@ class EventLogger extends logger.ProstoLogger {
|
|
|
173
136
|
level: 4,
|
|
174
137
|
};
|
|
175
138
|
if (!_opts.mapper) {
|
|
176
|
-
_opts.mapper = (msg) => (
|
|
139
|
+
_opts.mapper = (msg) => ({
|
|
140
|
+
...msg,
|
|
141
|
+
eventId,
|
|
142
|
+
});
|
|
177
143
|
}
|
|
178
144
|
if (!_opts.transports) {
|
|
179
145
|
_opts.transports = [
|
|
@@ -182,7 +148,7 @@ class EventLogger extends logger.ProstoLogger {
|
|
|
182
148
|
}),
|
|
183
149
|
];
|
|
184
150
|
}
|
|
185
|
-
super(_opts,
|
|
151
|
+
super(_opts, opts?.topic || 'event');
|
|
186
152
|
}
|
|
187
153
|
}
|
|
188
154
|
|
|
@@ -191,7 +157,7 @@ function useEventLogger(topic) {
|
|
|
191
157
|
const { store, getCtx } = useEventContext();
|
|
192
158
|
const { init } = store('event');
|
|
193
159
|
const ctx = getCtx();
|
|
194
|
-
const get = () => init('logger', () =>
|
|
160
|
+
const get = () => init('logger', () => new EventLogger(getId(), ctx.options?.eventLogger));
|
|
195
161
|
return topic ? get().createTopic(topic) : get();
|
|
196
162
|
}
|
|
197
163
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,110 +1,104 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { TProstoLoggerOptions } from '@prostojs/logger';
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
export declare function useRouteParams<T extends object = Record<string, string | string[]>>(): {
|
|
106
|
-
params: T;
|
|
107
|
-
get: <K extends keyof T>(name: K) => T[K];
|
|
108
|
-
};
|
|
109
|
-
|
|
110
|
-
export { }
|
|
1
|
+
import * as _prostojs_logger from '@prostojs/logger';
|
|
2
|
+
import { ProstoLogger, TProstoLoggerOptions } from '@prostojs/logger';
|
|
3
|
+
|
|
4
|
+
interface TEventLoggerData {
|
|
5
|
+
eventId: string;
|
|
6
|
+
}
|
|
7
|
+
declare class EventLogger extends ProstoLogger<TEventLoggerData> {
|
|
8
|
+
constructor(eventId: string, opts?: TEventOptions['eventLogger']);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
interface TGenericEvent {
|
|
12
|
+
type: string;
|
|
13
|
+
logger?: EventLogger;
|
|
14
|
+
id?: string;
|
|
15
|
+
}
|
|
16
|
+
interface TEmpty {
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
interface TEventOptions {
|
|
20
|
+
eventLogger?: {
|
|
21
|
+
topic?: string;
|
|
22
|
+
} & TProstoLoggerOptions<TEventLoggerData>;
|
|
23
|
+
}
|
|
24
|
+
type TGenericContextStore<CustomEventType = TEmpty> = {
|
|
25
|
+
event: CustomEventType & TGenericEvent;
|
|
26
|
+
options: TEventOptions;
|
|
27
|
+
routeParams?: Record<string, string | string[]>;
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* Create a new event context
|
|
31
|
+
*
|
|
32
|
+
* @param data
|
|
33
|
+
* @returns set of hooks { getCtx, restoreCtx, clearCtx, hookStore, getStore, setStore }
|
|
34
|
+
*/
|
|
35
|
+
declare function createEventContext<S = TEmpty, EventTypeToCreate = TEmpty>(data: S & TGenericContextStore<EventTypeToCreate>): {
|
|
36
|
+
getCtx: () => S & TGenericContextStore<EventTypeToCreate>;
|
|
37
|
+
restoreCtx: () => TGenericContextStore<TEmpty>;
|
|
38
|
+
clearCtx: () => null;
|
|
39
|
+
store: <K extends keyof S | keyof TGenericContextStore<EventTypeToCreate>>(key: K) => {
|
|
40
|
+
value: (S & TGenericContextStore<EventTypeToCreate>)[K];
|
|
41
|
+
hook: <K2 extends keyof Required<S & TGenericContextStore<EventTypeToCreate>>[K]>(key2: K2) => {
|
|
42
|
+
value: Required<S & TGenericContextStore<EventTypeToCreate>>[K][K2];
|
|
43
|
+
isDefined: boolean;
|
|
44
|
+
};
|
|
45
|
+
init: <K2_1 extends keyof Required<S & TGenericContextStore<EventTypeToCreate>>[K]>(key2: K2_1, getter: () => Required<Required<S & TGenericContextStore<EventTypeToCreate>>[K]>[K2_1]) => Required<Required<S & TGenericContextStore<EventTypeToCreate>>[K]>[K2_1];
|
|
46
|
+
set: <K2_2 extends keyof Required<S & TGenericContextStore<EventTypeToCreate>>[K]>(key2: K2_2, v: Required<(S & TGenericContextStore<EventTypeToCreate>)[K]>[K2_2]) => Required<(S & TGenericContextStore<EventTypeToCreate>)[K]>[K2_2];
|
|
47
|
+
get: <K2_3 extends keyof Required<S & TGenericContextStore<EventTypeToCreate>>[K]>(key2: K2_3) => Required<S & TGenericContextStore<EventTypeToCreate>>[K][K2_3];
|
|
48
|
+
has: <K2_4 extends keyof Required<S & TGenericContextStore<EventTypeToCreate>>[K]>(key2: K2_4) => boolean;
|
|
49
|
+
del: <K2_5 extends keyof Required<S & TGenericContextStore<EventTypeToCreate>>[K]>(key2: K2_5) => void;
|
|
50
|
+
entries: () => [string, unknown][];
|
|
51
|
+
clear: () => void;
|
|
52
|
+
};
|
|
53
|
+
getStore: <K_1 extends keyof S | keyof TGenericContextStore<EventTypeToCreate>>(key: K_1) => (S & TGenericContextStore<EventTypeToCreate>)[K_1];
|
|
54
|
+
setStore: <K_2 extends keyof S | keyof TGenericContextStore<EventTypeToCreate>>(key: K_2, v: (S & TGenericContextStore<EventTypeToCreate>)[K_2]) => void;
|
|
55
|
+
};
|
|
56
|
+
/**
|
|
57
|
+
* Use existing event context
|
|
58
|
+
*
|
|
59
|
+
* !Must be called syncronously while context is reachable
|
|
60
|
+
*
|
|
61
|
+
* @returns set of hooks { getCtx, restoreCtx, clearCtx, hookStore, getStore, setStore }
|
|
62
|
+
*/
|
|
63
|
+
declare function useEventContext<S = TEmpty, EventType = TEmpty>(expectedTypes?: string | string[]): {
|
|
64
|
+
getCtx: () => S & TGenericContextStore<EventType>;
|
|
65
|
+
restoreCtx: () => TGenericContextStore<TEmpty>;
|
|
66
|
+
clearCtx: () => null;
|
|
67
|
+
store: <K extends keyof S | keyof TGenericContextStore<EventType>>(key: K) => {
|
|
68
|
+
value: (S & TGenericContextStore<EventType>)[K];
|
|
69
|
+
hook: <K2 extends keyof Required<S & TGenericContextStore<EventType>>[K]>(key2: K2) => {
|
|
70
|
+
value: Required<S & TGenericContextStore<EventType>>[K][K2];
|
|
71
|
+
isDefined: boolean;
|
|
72
|
+
};
|
|
73
|
+
init: <K2_1 extends keyof Required<S & TGenericContextStore<EventType>>[K]>(key2: K2_1, getter: () => Required<Required<S & TGenericContextStore<EventType>>[K]>[K2_1]) => Required<Required<S & TGenericContextStore<EventType>>[K]>[K2_1];
|
|
74
|
+
set: <K2_2 extends keyof Required<S & TGenericContextStore<EventType>>[K]>(key2: K2_2, v: Required<(S & TGenericContextStore<EventType>)[K]>[K2_2]) => Required<(S & TGenericContextStore<EventType>)[K]>[K2_2];
|
|
75
|
+
get: <K2_3 extends keyof Required<S & TGenericContextStore<EventType>>[K]>(key2: K2_3) => Required<S & TGenericContextStore<EventType>>[K][K2_3];
|
|
76
|
+
has: <K2_4 extends keyof Required<S & TGenericContextStore<EventType>>[K]>(key2: K2_4) => boolean;
|
|
77
|
+
del: <K2_5 extends keyof Required<S & TGenericContextStore<EventType>>[K]>(key2: K2_5) => void;
|
|
78
|
+
entries: () => [string, unknown][];
|
|
79
|
+
clear: () => void;
|
|
80
|
+
};
|
|
81
|
+
getStore: <K_1 extends keyof S | keyof TGenericContextStore<EventType>>(key: K_1) => (S & TGenericContextStore<EventType>)[K_1];
|
|
82
|
+
setStore: <K_2 extends keyof S | keyof TGenericContextStore<EventType>>(key: K_2, v: (S & TGenericContextStore<EventType>)[K_2]) => void;
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
declare function attachHook<V = unknown, T extends object | Function = object, P extends PropertyKey = 'value'>(target: T, opts: {
|
|
86
|
+
get: () => V | undefined;
|
|
87
|
+
set?: (value: V) => void;
|
|
88
|
+
}, name?: P): T & THook<V, P>;
|
|
89
|
+
type THook<T = string, K extends PropertyKey = 'value'> = {
|
|
90
|
+
[key in K]: T;
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
declare function useRouteParams<T extends object = Record<string, string | string[]>>(): {
|
|
94
|
+
params: T;
|
|
95
|
+
get: <K extends keyof T>(name: K) => T[K];
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
declare function useEventId(): {
|
|
99
|
+
getId: () => string;
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
declare function useEventLogger(topic?: string): _prostojs_logger.ProstoLogger<TEventLoggerData>;
|
|
103
|
+
|
|
104
|
+
export { EventLogger, type TEmpty, type TEventLoggerData, type TEventOptions, type TGenericContextStore, type TGenericEvent, type THook, attachHook, createEventContext, useEventContext, useEventId, useEventLogger, useRouteParams };
|
package/dist/index.mjs
CHANGED
|
@@ -10,32 +10,18 @@ function attachHook(target, opts, name) {
|
|
|
10
10
|
}
|
|
11
11
|
|
|
12
12
|
let currentContext = null;
|
|
13
|
-
/**
|
|
14
|
-
* Create a new event context
|
|
15
|
-
*
|
|
16
|
-
* @param data
|
|
17
|
-
* @returns set of hooks { getCtx, restoreCtx, clearCtx, hookStore, getStore, setStore }
|
|
18
|
-
*/
|
|
19
13
|
function createEventContext(data) {
|
|
20
|
-
const newContext =
|
|
14
|
+
const newContext = { ...data };
|
|
21
15
|
currentContext = newContext;
|
|
22
16
|
return _getCtxHelpers(newContext);
|
|
23
17
|
}
|
|
24
|
-
/**
|
|
25
|
-
* Use existing event context
|
|
26
|
-
*
|
|
27
|
-
* !Must be called syncronously while context is reachable
|
|
28
|
-
*
|
|
29
|
-
* @returns set of hooks { getCtx, restoreCtx, clearCtx, hookStore, getStore, setStore }
|
|
30
|
-
*/
|
|
31
18
|
function useEventContext(expectedTypes) {
|
|
32
|
-
var _a;
|
|
33
19
|
if (!currentContext) {
|
|
34
20
|
throw new Error('Event context does not exist. Use event context synchronously within the runtime of the event.');
|
|
35
21
|
}
|
|
36
22
|
const cc = currentContext;
|
|
37
23
|
if (expectedTypes || typeof expectedTypes === 'string') {
|
|
38
|
-
const type =
|
|
24
|
+
const type = cc.event?.type;
|
|
39
25
|
const types = typeof expectedTypes === 'string' ? [expectedTypes] : expectedTypes;
|
|
40
26
|
if (!types.includes(type))
|
|
41
27
|
new Error(`Event context type mismatch: expected ${types
|
|
@@ -45,12 +31,6 @@ function useEventContext(expectedTypes) {
|
|
|
45
31
|
return _getCtxHelpers(cc);
|
|
46
32
|
}
|
|
47
33
|
function _getCtxHelpers(cc) {
|
|
48
|
-
/**
|
|
49
|
-
* Hook to an event store property
|
|
50
|
-
*
|
|
51
|
-
* @param key store property key
|
|
52
|
-
* @returns a hook { value: <prop value>, hook: (key2: keyof <prop value>) => { value: <nested prop value> }, ... }
|
|
53
|
-
*/
|
|
54
34
|
function store(key) {
|
|
55
35
|
const obj = {
|
|
56
36
|
value: null,
|
|
@@ -110,29 +90,12 @@ function _getCtxHelpers(cc) {
|
|
|
110
90
|
}
|
|
111
91
|
return obj;
|
|
112
92
|
}
|
|
113
|
-
/**
|
|
114
|
-
* Get event context object
|
|
115
|
-
*
|
|
116
|
-
* @returns whole context object
|
|
117
|
-
*/
|
|
118
93
|
function getCtx() {
|
|
119
94
|
return cc;
|
|
120
95
|
}
|
|
121
|
-
/**
|
|
122
|
-
* Get value of event store property
|
|
123
|
-
*
|
|
124
|
-
* @param key property name
|
|
125
|
-
* @returns value of property by name
|
|
126
|
-
*/
|
|
127
96
|
function get(key) {
|
|
128
97
|
return getCtx()[key];
|
|
129
98
|
}
|
|
130
|
-
/**
|
|
131
|
-
* Set value of event store property
|
|
132
|
-
*
|
|
133
|
-
* @param key property name
|
|
134
|
-
* @param v property value
|
|
135
|
-
*/
|
|
136
99
|
function set(key, v) {
|
|
137
100
|
getCtx()[key] = v;
|
|
138
101
|
}
|
|
@@ -171,7 +134,10 @@ class EventLogger extends ProstoLogger {
|
|
|
171
134
|
level: 4,
|
|
172
135
|
};
|
|
173
136
|
if (!_opts.mapper) {
|
|
174
|
-
_opts.mapper = (msg) => (
|
|
137
|
+
_opts.mapper = (msg) => ({
|
|
138
|
+
...msg,
|
|
139
|
+
eventId,
|
|
140
|
+
});
|
|
175
141
|
}
|
|
176
142
|
if (!_opts.transports) {
|
|
177
143
|
_opts.transports = [
|
|
@@ -180,7 +146,7 @@ class EventLogger extends ProstoLogger {
|
|
|
180
146
|
}),
|
|
181
147
|
];
|
|
182
148
|
}
|
|
183
|
-
super(_opts,
|
|
149
|
+
super(_opts, opts?.topic || 'event');
|
|
184
150
|
}
|
|
185
151
|
}
|
|
186
152
|
|
|
@@ -189,7 +155,7 @@ function useEventLogger(topic) {
|
|
|
189
155
|
const { store, getCtx } = useEventContext();
|
|
190
156
|
const { init } = store('event');
|
|
191
157
|
const ctx = getCtx();
|
|
192
|
-
const get = () => init('logger', () =>
|
|
158
|
+
const get = () => init('logger', () => new EventLogger(getId(), ctx.options?.eventLogger));
|
|
193
159
|
return topic ? get().createTopic(topic) : get();
|
|
194
160
|
}
|
|
195
161
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wooksjs/event-core",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.12",
|
|
4
4
|
"description": "@wooksjs/event-core",
|
|
5
5
|
"main": "dist/index.cjs",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -8,6 +8,14 @@
|
|
|
8
8
|
"files": [
|
|
9
9
|
"dist"
|
|
10
10
|
],
|
|
11
|
+
"exports": {
|
|
12
|
+
"./package.json": "./package.json",
|
|
13
|
+
".": {
|
|
14
|
+
"require": "./dist/index.cjs",
|
|
15
|
+
"import": "./dist/index.mjs",
|
|
16
|
+
"types": "./dist/index.d.ts"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
11
19
|
"repository": {
|
|
12
20
|
"type": "git",
|
|
13
21
|
"url": "git+https://github.com/wooksjs/wooksjs.git",
|
|
@@ -31,7 +39,7 @@
|
|
|
31
39
|
"url": "https://github.com/wooksjs/wooksjs/issues"
|
|
32
40
|
},
|
|
33
41
|
"dependencies": {
|
|
34
|
-
"@prostojs/logger": "^0.
|
|
42
|
+
"@prostojs/logger": "^0.4.0"
|
|
35
43
|
},
|
|
36
44
|
"homepage": "https://github.com/wooksjs/wooksjs/tree/main/packages/event-core#readme"
|
|
37
45
|
}
|