@wooksjs/event-core 0.5.19 → 0.5.25
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/dist/index.cjs +217 -184
- package/dist/index.d.ts +33 -254
- package/dist/index.mjs +184 -173
- package/package.json +13 -6
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 wooksjs
|
|
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/dist/index.cjs
CHANGED
|
@@ -1,213 +1,246 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
//#region rolldown:runtime
|
|
3
|
+
var __create = Object.create;
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
11
|
+
key = keys[i];
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
13
|
+
get: ((k) => from[k]).bind(null, key),
|
|
14
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
20
|
+
value: mod,
|
|
21
|
+
enumerable: true
|
|
22
|
+
}) : target, mod));
|
|
2
23
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
24
|
+
//#endregion
|
|
25
|
+
const crypto = __toESM(require("crypto"));
|
|
26
|
+
const node_async_hooks = __toESM(require("node:async_hooks"));
|
|
27
|
+
const __prostojs_logger = __toESM(require("@prostojs/logger"));
|
|
6
28
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
}
|
|
29
|
+
//#region packages/event-core/src/context-injector.ts
|
|
30
|
+
var ContextInjector = class {
|
|
31
|
+
with(name, attributes, cb) {
|
|
32
|
+
const fn = typeof attributes === "function" ? attributes : cb;
|
|
33
|
+
return fn();
|
|
34
|
+
}
|
|
35
|
+
hook(method, name, route) {}
|
|
36
|
+
};
|
|
15
37
|
let ci = new ContextInjector();
|
|
16
38
|
function getContextInjector() {
|
|
17
|
-
|
|
39
|
+
return ci;
|
|
18
40
|
}
|
|
19
41
|
function replaceContextInjector(newCi) {
|
|
20
|
-
|
|
42
|
+
ci = newCi;
|
|
21
43
|
}
|
|
22
44
|
|
|
45
|
+
//#endregion
|
|
46
|
+
//#region packages/event-core/src/hook.ts
|
|
23
47
|
function attachHook(target, opts, name) {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
48
|
+
Object.defineProperty(target, name || "value", {
|
|
49
|
+
get: opts.get,
|
|
50
|
+
set: opts.set
|
|
51
|
+
});
|
|
52
|
+
return target;
|
|
29
53
|
}
|
|
30
54
|
|
|
55
|
+
//#endregion
|
|
56
|
+
//#region packages/event-core/src/context.ts
|
|
31
57
|
const asyncStorage = new node_async_hooks.AsyncLocalStorage();
|
|
32
58
|
function createAsyncEventContext(data) {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
const ci = getContextInjector();
|
|
39
|
-
return (cb) => asyncStorage.run(newContext, () => ci.with('Event:start', { eventType: newContext.event.type }, cb));
|
|
59
|
+
const newContext = { ...data };
|
|
60
|
+
const cc = asyncStorage.getStore();
|
|
61
|
+
if (cc && typeof cc === "object" && cc.event?.type) newContext.parentCtx = cc;
|
|
62
|
+
const ci$1 = getContextInjector();
|
|
63
|
+
return (cb) => asyncStorage.run(newContext, () => ci$1.with("Event:start", { eventType: newContext.event.type }, cb));
|
|
40
64
|
}
|
|
41
65
|
function useAsyncEventContext(expectedTypes) {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
cc = cc.parentCtx;
|
|
52
|
-
}
|
|
53
|
-
else {
|
|
54
|
-
throw new Error(`Event context type mismatch: expected ${types
|
|
55
|
-
.map(t => `"${t}"`)
|
|
56
|
-
.join(', ')}, received "${type}"`);
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
return _getCtxHelpers(cc);
|
|
66
|
+
let cc = asyncStorage.getStore();
|
|
67
|
+
if (!cc) throw new Error("Event context does not exist at this point.");
|
|
68
|
+
if (expectedTypes || typeof expectedTypes === "string") {
|
|
69
|
+
const type = cc.event.type;
|
|
70
|
+
const types = typeof expectedTypes === "string" ? [expectedTypes] : expectedTypes;
|
|
71
|
+
if (!types.includes(type)) if (cc.parentCtx?.event.type && types.includes(cc.parentCtx.event.type)) cc = cc.parentCtx;
|
|
72
|
+
else throw new Error(`Event context type mismatch: expected ${types.map((t) => `"${t}"`).join(", ")}, received "${type}"`);
|
|
73
|
+
}
|
|
74
|
+
return _getCtxHelpers(cc);
|
|
61
75
|
}
|
|
76
|
+
/**
|
|
77
|
+
*
|
|
78
|
+
* @param cc
|
|
79
|
+
* @returns
|
|
80
|
+
*/
|
|
62
81
|
function _getCtxHelpers(cc) {
|
|
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
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
82
|
+
/**
|
|
83
|
+
* Hook to an event store property
|
|
84
|
+
*
|
|
85
|
+
* @param key store property key
|
|
86
|
+
* @returns a hook { value: <prop value>, hook: (key2: keyof <prop value>) => { value: <nested prop value> }, ... }
|
|
87
|
+
*/
|
|
88
|
+
function store(key) {
|
|
89
|
+
const obj = {
|
|
90
|
+
value: null,
|
|
91
|
+
hook,
|
|
92
|
+
init,
|
|
93
|
+
set: setNested,
|
|
94
|
+
get: getNested,
|
|
95
|
+
has: hasNested,
|
|
96
|
+
del: delNested,
|
|
97
|
+
entries,
|
|
98
|
+
clear
|
|
99
|
+
};
|
|
100
|
+
attachHook(obj, {
|
|
101
|
+
set: (v) => {
|
|
102
|
+
set(key, v);
|
|
103
|
+
},
|
|
104
|
+
get: () => get(key)
|
|
105
|
+
});
|
|
106
|
+
function init(key2, getter) {
|
|
107
|
+
if (hasNested(key2)) return getNested(key2);
|
|
108
|
+
return setNested(key2, getter());
|
|
109
|
+
}
|
|
110
|
+
function hook(key2) {
|
|
111
|
+
const obj$1 = {
|
|
112
|
+
value: null,
|
|
113
|
+
isDefined: null
|
|
114
|
+
};
|
|
115
|
+
attachHook(obj$1, {
|
|
116
|
+
set: (v) => setNested(key2, v),
|
|
117
|
+
get: () => getNested(key2)
|
|
118
|
+
});
|
|
119
|
+
attachHook(obj$1, { get: () => hasNested(key2) }, "isDefined");
|
|
120
|
+
return obj$1;
|
|
121
|
+
}
|
|
122
|
+
function setNested(key2, v) {
|
|
123
|
+
if (obj.value === undefined) obj.value = {};
|
|
124
|
+
obj.value[key2] = v;
|
|
125
|
+
return v;
|
|
126
|
+
}
|
|
127
|
+
function delNested(key2) {
|
|
128
|
+
setNested(key2, undefined);
|
|
129
|
+
}
|
|
130
|
+
function getNested(key2) {
|
|
131
|
+
return (obj.value || {})[key2];
|
|
132
|
+
}
|
|
133
|
+
function hasNested(key2) {
|
|
134
|
+
return (obj.value || {})[key2] !== undefined;
|
|
135
|
+
}
|
|
136
|
+
function entries() {
|
|
137
|
+
return Object.entries(obj.value || {});
|
|
138
|
+
}
|
|
139
|
+
function clear() {
|
|
140
|
+
obj.value = {};
|
|
141
|
+
}
|
|
142
|
+
return obj;
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Get event context object
|
|
146
|
+
*
|
|
147
|
+
* @returns whole context object
|
|
148
|
+
*/
|
|
149
|
+
function getCtx() {
|
|
150
|
+
return cc;
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Get value of event store property
|
|
154
|
+
*
|
|
155
|
+
* @param key property name
|
|
156
|
+
* @returns value of property by name
|
|
157
|
+
*/
|
|
158
|
+
function get(key) {
|
|
159
|
+
return getCtx()[key];
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Set value of event store property
|
|
163
|
+
*
|
|
164
|
+
* @param key property name
|
|
165
|
+
* @param v property value
|
|
166
|
+
*/
|
|
167
|
+
function set(key, v) {
|
|
168
|
+
getCtx()[key] = v;
|
|
169
|
+
}
|
|
170
|
+
const hasParentCtx = () => !!cc.parentCtx;
|
|
171
|
+
return {
|
|
172
|
+
getCtx,
|
|
173
|
+
store,
|
|
174
|
+
getStore: get,
|
|
175
|
+
setStore: set,
|
|
176
|
+
setParentCtx: (parentCtx) => {
|
|
177
|
+
cc.parentCtx = parentCtx;
|
|
178
|
+
},
|
|
179
|
+
hasParentCtx,
|
|
180
|
+
getParentCtx: () => {
|
|
181
|
+
if (!hasParentCtx()) throw new Error("Parent context is not available");
|
|
182
|
+
return _getCtxHelpers(cc.parentCtx);
|
|
183
|
+
}
|
|
184
|
+
};
|
|
151
185
|
}
|
|
152
186
|
|
|
187
|
+
//#endregion
|
|
188
|
+
//#region packages/event-core/src/composables/event-id.ts
|
|
153
189
|
function useEventId() {
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
190
|
+
const { store } = useAsyncEventContext();
|
|
191
|
+
const { init } = store("event");
|
|
192
|
+
const getId = () => init("id", () => (0, crypto.randomUUID)());
|
|
193
|
+
return { getId };
|
|
158
194
|
}
|
|
159
195
|
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
logger.createConsoleTransort({
|
|
174
|
-
format: logger.coloredConsole,
|
|
175
|
-
}),
|
|
176
|
-
];
|
|
177
|
-
}
|
|
178
|
-
super(_opts, opts?.topic || 'event');
|
|
179
|
-
}
|
|
180
|
-
}
|
|
196
|
+
//#endregion
|
|
197
|
+
//#region packages/event-core/src/event-logger.ts
|
|
198
|
+
var EventLogger = class extends __prostojs_logger.ProstoLogger {
|
|
199
|
+
constructor(eventId, opts) {
|
|
200
|
+
const _opts = opts || { level: 4 };
|
|
201
|
+
if (!_opts.mapper) _opts.mapper = (msg) => ({
|
|
202
|
+
...msg,
|
|
203
|
+
eventId
|
|
204
|
+
});
|
|
205
|
+
if (!_opts.transports) _opts.transports = [(0, __prostojs_logger.createConsoleTransort)({ format: __prostojs_logger.coloredConsole })];
|
|
206
|
+
super(_opts, opts?.topic || "event");
|
|
207
|
+
}
|
|
208
|
+
};
|
|
181
209
|
|
|
210
|
+
//#endregion
|
|
211
|
+
//#region packages/event-core/src/composables/event-logger.ts
|
|
182
212
|
function useEventLogger(topic) {
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
213
|
+
const { getId } = useEventId();
|
|
214
|
+
const { store, getCtx } = useAsyncEventContext();
|
|
215
|
+
const { init } = store("event");
|
|
216
|
+
const ctx = getCtx();
|
|
217
|
+
const get = () => init("logger", () => new EventLogger(getId(), ctx.options.eventLogger));
|
|
218
|
+
return topic ? get().createTopic(topic) : get();
|
|
189
219
|
}
|
|
190
220
|
|
|
221
|
+
//#endregion
|
|
222
|
+
//#region packages/event-core/src/composables/route-params.ts
|
|
191
223
|
function useRouteParams() {
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
224
|
+
const { store } = useAsyncEventContext();
|
|
225
|
+
const params = store("routeParams").value || {};
|
|
226
|
+
function get(name) {
|
|
227
|
+
return params[name];
|
|
228
|
+
}
|
|
229
|
+
return {
|
|
230
|
+
params,
|
|
231
|
+
get
|
|
232
|
+
};
|
|
201
233
|
}
|
|
202
234
|
|
|
203
|
-
|
|
204
|
-
exports.
|
|
205
|
-
exports.
|
|
206
|
-
exports.
|
|
207
|
-
exports.
|
|
208
|
-
exports.
|
|
209
|
-
exports.
|
|
210
|
-
exports.
|
|
211
|
-
exports.
|
|
212
|
-
exports.
|
|
213
|
-
exports.
|
|
235
|
+
//#endregion
|
|
236
|
+
exports.ContextInjector = ContextInjector
|
|
237
|
+
exports.EventLogger = EventLogger
|
|
238
|
+
exports.asyncStorage = asyncStorage
|
|
239
|
+
exports.attachHook = attachHook
|
|
240
|
+
exports.createAsyncEventContext = createAsyncEventContext
|
|
241
|
+
exports.getContextInjector = getContextInjector
|
|
242
|
+
exports.replaceContextInjector = replaceContextInjector
|
|
243
|
+
exports.useAsyncEventContext = useAsyncEventContext
|
|
244
|
+
exports.useEventId = useEventId
|
|
245
|
+
exports.useEventLogger = useEventLogger
|
|
246
|
+
exports.useRouteParams = useRouteParams
|
package/dist/index.d.ts
CHANGED
|
@@ -1,11 +1,24 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { TProstoLoggerOptions, ProstoLogger } from '@prostojs/logger';
|
|
1
|
+
import { TConsoleBase, ProstoLogger, TProstoLoggerOptions } from '@prostojs/logger';
|
|
3
2
|
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
4
3
|
|
|
5
4
|
declare function useEventId(): {
|
|
6
5
|
getId: () => string;
|
|
7
6
|
};
|
|
8
7
|
|
|
8
|
+
declare function useEventLogger(topic?: string): TConsoleBase;
|
|
9
|
+
|
|
10
|
+
declare function useRouteParams<T extends object = Record<string, string | string[]>>(): {
|
|
11
|
+
params: T;
|
|
12
|
+
get: <K extends keyof T>(name: K) => T[K];
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
interface TEventLoggerData {
|
|
16
|
+
eventId: string;
|
|
17
|
+
}
|
|
18
|
+
declare class EventLogger extends ProstoLogger<TEventLoggerData> {
|
|
19
|
+
constructor(eventId: string, opts?: TEventOptions['eventLogger']);
|
|
20
|
+
}
|
|
21
|
+
|
|
9
22
|
interface TGenericEvent {
|
|
10
23
|
type: string;
|
|
11
24
|
logger?: EventLogger;
|
|
@@ -26,266 +39,32 @@ interface TGenericContextStore<CustomEventType = TEmpty> {
|
|
|
26
39
|
routeParams?: Record<string, string | string[]>;
|
|
27
40
|
_ended?: boolean;
|
|
28
41
|
}
|
|
29
|
-
|
|
30
|
-
* AsyncLocalStorage instance
|
|
31
|
-
*
|
|
32
|
-
* Use on your own risk only if you know what you're doing
|
|
33
|
-
*/
|
|
34
|
-
declare const asyncStorage: AsyncLocalStorage<unknown>;
|
|
42
|
+
declare const asyncStorage: AsyncLocalStorage<TGenericContextStore>;
|
|
35
43
|
declare function createAsyncEventContext<S = TEmpty, EventTypeToCreate = TEmpty>(data: S & TGenericContextStore<EventTypeToCreate>): <T>(cb: (...a: any[]) => T) => T;
|
|
36
|
-
declare function useAsyncEventContext<S = TEmpty, EventType = TEmpty>(expectedTypes?: string | string[]):
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
44
|
+
declare function useAsyncEventContext<S = TEmpty, EventType = TEmpty>(expectedTypes?: string | string[]): TCtxHelpers<S & TGenericContextStore<EventType>>;
|
|
45
|
+
interface TCtxHelpers<T> {
|
|
46
|
+
getCtx: () => T;
|
|
47
|
+
store: <K extends keyof Required<T>>(key: K) => {
|
|
48
|
+
value: T[K];
|
|
49
|
+
hook: <K2 extends keyof Required<T>[K]>(key2: K2) => {
|
|
50
|
+
value: Required<T>[K][K2];
|
|
42
51
|
isDefined: boolean;
|
|
43
52
|
};
|
|
44
|
-
init: <
|
|
45
|
-
set: <
|
|
46
|
-
get: <
|
|
47
|
-
has: <
|
|
48
|
-
del: <
|
|
49
|
-
entries: () => [string, unknown]
|
|
53
|
+
init: <K2 extends keyof Required<T>[K]>(key2: K2, getter: () => Required<Required<T>[K]>[K2]) => Required<Required<T>[K]>[K2];
|
|
54
|
+
set: <K2 extends keyof Required<T>[K]>(key2: K2, v: Required<T[K]>[K2]) => Required<T[K]>[K2];
|
|
55
|
+
get: <K2 extends keyof Required<T>[K]>(key2: K2) => Required<T>[K][K2] | undefined;
|
|
56
|
+
has: <K2 extends keyof Required<T>[K]>(key2: K2) => boolean;
|
|
57
|
+
del: <K2 extends keyof Required<T>[K]>(key2: K2) => void;
|
|
58
|
+
entries: () => Array<[string, unknown]>;
|
|
50
59
|
clear: () => void;
|
|
51
60
|
};
|
|
52
|
-
getStore: <
|
|
53
|
-
setStore: <
|
|
61
|
+
getStore: <K extends keyof T>(key: K) => T[K];
|
|
62
|
+
setStore: <K extends keyof T>(key: K, v: T[K]) => void;
|
|
54
63
|
setParentCtx: (parentCtx: unknown) => void;
|
|
55
64
|
hasParentCtx: () => boolean;
|
|
56
|
-
getParentCtx: <T2 =
|
|
57
|
-
getCtx: () => T2;
|
|
58
|
-
store: <K_3 extends keyof T2>(key: K_3) => {
|
|
59
|
-
value: T2[K_3];
|
|
60
|
-
hook: <K2_6 extends keyof Required<T2>[K_3]>(key2: K2_6) => {
|
|
61
|
-
value: Required<T2>[K_3][K2_6];
|
|
62
|
-
isDefined: boolean;
|
|
63
|
-
};
|
|
64
|
-
init: <K2_7 extends keyof Required<T2>[K_3]>(key2: K2_7, getter: () => Required<Required<T2>[K_3]>[K2_7]) => Required<Required<T2>[K_3]>[K2_7];
|
|
65
|
-
set: <K2_8 extends keyof Required<T2>[K_3]>(key2: K2_8, v: Required<T2[K_3]>[K2_8]) => Required<T2[K_3]>[K2_8];
|
|
66
|
-
get: <K2_9 extends keyof Required<T2>[K_3]>(key2: K2_9) => Required<T2>[K_3][K2_9] | undefined;
|
|
67
|
-
has: <K2_10 extends keyof Required<T2>[K_3]>(key2: K2_10) => boolean;
|
|
68
|
-
del: <K2_11 extends keyof Required<T2>[K_3]>(key2: K2_11) => void;
|
|
69
|
-
entries: () => [string, unknown][];
|
|
70
|
-
clear: () => void;
|
|
71
|
-
};
|
|
72
|
-
getStore: <K_4 extends keyof T2>(key: K_4) => T2[K_4];
|
|
73
|
-
setStore: <K_5 extends keyof T2>(key: K_5, v: T2[K_5]) => void;
|
|
74
|
-
setParentCtx: (parentCtx: unknown) => void;
|
|
75
|
-
hasParentCtx: () => boolean;
|
|
76
|
-
getParentCtx: <T2_1 = T2>() => {
|
|
77
|
-
getCtx: () => T2_1;
|
|
78
|
-
store: <K_6 extends keyof T2_1>(key: K_6) => {
|
|
79
|
-
value: T2_1[K_6];
|
|
80
|
-
hook: <K2_12 extends keyof Required<T2_1>[K_6]>(key2: K2_12) => {
|
|
81
|
-
value: Required<T2_1>[K_6][K2_12];
|
|
82
|
-
isDefined: boolean;
|
|
83
|
-
};
|
|
84
|
-
init: <K2_13 extends keyof Required<T2_1>[K_6]>(key2: K2_13, getter: () => Required<Required<T2_1>[K_6]>[K2_13]) => Required<Required<T2_1>[K_6]>[K2_13];
|
|
85
|
-
set: <K2_14 extends keyof Required<T2_1>[K_6]>(key2: K2_14, v: Required<T2_1[K_6]>[K2_14]) => Required<T2_1[K_6]>[K2_14];
|
|
86
|
-
get: <K2_15 extends keyof Required<T2_1>[K_6]>(key2: K2_15) => Required<T2_1>[K_6][K2_15] | undefined;
|
|
87
|
-
has: <K2_16 extends keyof Required<T2_1>[K_6]>(key2: K2_16) => boolean;
|
|
88
|
-
del: <K2_17 extends keyof Required<T2_1>[K_6]>(key2: K2_17) => void;
|
|
89
|
-
entries: () => [string, unknown][];
|
|
90
|
-
clear: () => void;
|
|
91
|
-
};
|
|
92
|
-
getStore: <K_7 extends keyof T2_1>(key: K_7) => T2_1[K_7];
|
|
93
|
-
setStore: <K_8 extends keyof T2_1>(key: K_8, v: T2_1[K_8]) => void;
|
|
94
|
-
setParentCtx: (parentCtx: unknown) => void;
|
|
95
|
-
hasParentCtx: () => boolean;
|
|
96
|
-
getParentCtx: <T2_2 = T2_1>() => {
|
|
97
|
-
getCtx: () => T2_2;
|
|
98
|
-
store: <K_9 extends keyof T2_2>(key: K_9) => {
|
|
99
|
-
value: T2_2[K_9];
|
|
100
|
-
hook: <K2_18 extends keyof Required<T2_2>[K_9]>(key2: K2_18) => {
|
|
101
|
-
value: Required<T2_2>[K_9][K2_18];
|
|
102
|
-
isDefined: boolean;
|
|
103
|
-
};
|
|
104
|
-
init: <K2_19 extends keyof Required<T2_2>[K_9]>(key2: K2_19, getter: () => Required<Required<T2_2>[K_9]>[K2_19]) => Required<Required<T2_2>[K_9]>[K2_19];
|
|
105
|
-
set: <K2_20 extends keyof Required<T2_2>[K_9]>(key2: K2_20, v: Required<T2_2[K_9]>[K2_20]) => Required<T2_2[K_9]>[K2_20];
|
|
106
|
-
get: <K2_21 extends keyof Required<T2_2>[K_9]>(key2: K2_21) => Required<T2_2>[K_9][K2_21] | undefined;
|
|
107
|
-
has: <K2_22 extends keyof Required<T2_2>[K_9]>(key2: K2_22) => boolean;
|
|
108
|
-
del: <K2_23 extends keyof Required<T2_2>[K_9]>(key2: K2_23) => void;
|
|
109
|
-
entries: () => [string, unknown][];
|
|
110
|
-
clear: () => void;
|
|
111
|
-
};
|
|
112
|
-
getStore: <K_10 extends keyof T2_2>(key: K_10) => T2_2[K_10];
|
|
113
|
-
setStore: <K_11 extends keyof T2_2>(key: K_11, v: T2_2[K_11]) => void;
|
|
114
|
-
setParentCtx: (parentCtx: unknown) => void;
|
|
115
|
-
hasParentCtx: () => boolean;
|
|
116
|
-
getParentCtx: <T2_3 = T2_2>() => {
|
|
117
|
-
getCtx: () => T2_3;
|
|
118
|
-
store: <K_12 extends keyof T2_3>(key: K_12) => {
|
|
119
|
-
value: T2_3[K_12];
|
|
120
|
-
hook: <K2_24 extends keyof Required<T2_3>[K_12]>(key2: K2_24) => {
|
|
121
|
-
value: Required<T2_3>[K_12][K2_24];
|
|
122
|
-
isDefined: boolean;
|
|
123
|
-
};
|
|
124
|
-
init: <K2_25 extends keyof Required<T2_3>[K_12]>(key2: K2_25, getter: () => Required<Required<T2_3>[K_12]>[K2_25]) => Required<Required<T2_3>[K_12]>[K2_25];
|
|
125
|
-
set: <K2_26 extends keyof Required<T2_3>[K_12]>(key2: K2_26, v: Required<T2_3[K_12]>[K2_26]) => Required<T2_3[K_12]>[K2_26];
|
|
126
|
-
get: <K2_27 extends keyof Required<T2_3>[K_12]>(key2: K2_27) => Required<T2_3>[K_12][K2_27] | undefined;
|
|
127
|
-
has: <K2_28 extends keyof Required<T2_3>[K_12]>(key2: K2_28) => boolean;
|
|
128
|
-
del: <K2_29 extends keyof Required<T2_3>[K_12]>(key2: K2_29) => void;
|
|
129
|
-
entries: () => [string, unknown][];
|
|
130
|
-
clear: () => void;
|
|
131
|
-
};
|
|
132
|
-
getStore: <K_13 extends keyof T2_3>(key: K_13) => T2_3[K_13];
|
|
133
|
-
setStore: <K_14 extends keyof T2_3>(key: K_14, v: T2_3[K_14]) => void;
|
|
134
|
-
setParentCtx: (parentCtx: unknown) => void;
|
|
135
|
-
hasParentCtx: () => boolean;
|
|
136
|
-
getParentCtx: <T2_4 = T2_3>() => {
|
|
137
|
-
getCtx: () => T2_4;
|
|
138
|
-
store: <K_15 extends keyof T2_4>(key: K_15) => {
|
|
139
|
-
value: T2_4[K_15];
|
|
140
|
-
hook: <K2_30 extends keyof Required<T2_4>[K_15]>(key2: K2_30) => {
|
|
141
|
-
value: Required<T2_4>[K_15][K2_30];
|
|
142
|
-
isDefined: boolean;
|
|
143
|
-
};
|
|
144
|
-
init: <K2_31 extends keyof Required<T2_4>[K_15]>(key2: K2_31, getter: () => Required<Required<T2_4>[K_15]>[K2_31]) => Required<Required<T2_4>[K_15]>[K2_31];
|
|
145
|
-
set: <K2_32 extends keyof Required<T2_4>[K_15]>(key2: K2_32, v: Required<T2_4[K_15]>[K2_32]) => Required<T2_4[K_15]>[K2_32];
|
|
146
|
-
get: <K2_33 extends keyof Required<T2_4>[K_15]>(key2: K2_33) => Required<T2_4>[K_15][K2_33] | undefined;
|
|
147
|
-
has: <K2_34 extends keyof Required<T2_4>[K_15]>(key2: K2_34) => boolean;
|
|
148
|
-
del: <K2_35 extends keyof Required<T2_4>[K_15]>(key2: K2_35) => void;
|
|
149
|
-
entries: () => [string, unknown][];
|
|
150
|
-
clear: () => void;
|
|
151
|
-
};
|
|
152
|
-
getStore: <K_16 extends keyof T2_4>(key: K_16) => T2_4[K_16];
|
|
153
|
-
setStore: <K_17 extends keyof T2_4>(key: K_17, v: T2_4[K_17]) => void;
|
|
154
|
-
setParentCtx: (parentCtx: unknown) => void;
|
|
155
|
-
hasParentCtx: () => boolean;
|
|
156
|
-
getParentCtx: <T2_5 = T2_4>() => {
|
|
157
|
-
getCtx: () => T2_5;
|
|
158
|
-
store: <K_18 extends keyof T2_5>(key: K_18) => {
|
|
159
|
-
value: T2_5[K_18];
|
|
160
|
-
hook: <K2_36 extends keyof Required<T2_5>[K_18]>(key2: K2_36) => {
|
|
161
|
-
value: Required<T2_5>[K_18][K2_36];
|
|
162
|
-
isDefined: boolean;
|
|
163
|
-
};
|
|
164
|
-
init: <K2_37 extends keyof Required<T2_5>[K_18]>(key2: K2_37, getter: () => Required<Required<T2_5>[K_18]>[K2_37]) => Required<Required<T2_5>[K_18]>[K2_37];
|
|
165
|
-
set: <K2_38 extends keyof Required<T2_5>[K_18]>(key2: K2_38, v: Required<T2_5[K_18]>[K2_38]) => Required<T2_5[K_18]>[K2_38];
|
|
166
|
-
get: <K2_39 extends keyof Required<T2_5>[K_18]>(key2: K2_39) => Required<T2_5>[K_18][K2_39] | undefined;
|
|
167
|
-
has: <K2_40 extends keyof Required<T2_5>[K_18]>(key2: K2_40) => boolean;
|
|
168
|
-
del: <K2_41 extends keyof Required<T2_5>[K_18]>(key2: K2_41) => void;
|
|
169
|
-
entries: () => [string, unknown][];
|
|
170
|
-
clear: () => void;
|
|
171
|
-
};
|
|
172
|
-
getStore: <K_19 extends keyof T2_5>(key: K_19) => T2_5[K_19];
|
|
173
|
-
setStore: <K_20 extends keyof T2_5>(key: K_20, v: T2_5[K_20]) => void;
|
|
174
|
-
setParentCtx: (parentCtx: unknown) => void;
|
|
175
|
-
hasParentCtx: () => boolean;
|
|
176
|
-
getParentCtx: <T2_6 = T2_5>() => {
|
|
177
|
-
getCtx: () => T2_6;
|
|
178
|
-
store: <K_21 extends keyof T2_6>(key: K_21) => {
|
|
179
|
-
value: T2_6[K_21];
|
|
180
|
-
hook: <K2_42 extends keyof Required<T2_6>[K_21]>(key2: K2_42) => {
|
|
181
|
-
value: Required<T2_6>[K_21][K2_42];
|
|
182
|
-
isDefined: boolean;
|
|
183
|
-
};
|
|
184
|
-
init: <K2_43 extends keyof Required<T2_6>[K_21]>(key2: K2_43, getter: () => Required<Required<T2_6>[K_21]>[K2_43]) => Required<Required<T2_6>[K_21]>[K2_43];
|
|
185
|
-
set: <K2_44 extends keyof Required<T2_6>[K_21]>(key2: K2_44, v: Required<T2_6[K_21]>[K2_44]) => Required<T2_6[K_21]>[K2_44];
|
|
186
|
-
get: <K2_45 extends keyof Required<T2_6>[K_21]>(key2: K2_45) => Required<T2_6>[K_21][K2_45] | undefined;
|
|
187
|
-
has: <K2_46 extends keyof Required<T2_6>[K_21]>(key2: K2_46) => boolean;
|
|
188
|
-
del: <K2_47 extends keyof Required<T2_6>[K_21]>(key2: K2_47) => void;
|
|
189
|
-
entries: () => [string, unknown][];
|
|
190
|
-
clear: () => void;
|
|
191
|
-
};
|
|
192
|
-
getStore: <K_22 extends keyof T2_6>(key: K_22) => T2_6[K_22];
|
|
193
|
-
setStore: <K_23 extends keyof T2_6>(key: K_23, v: T2_6[K_23]) => void;
|
|
194
|
-
setParentCtx: (parentCtx: unknown) => void;
|
|
195
|
-
hasParentCtx: () => boolean;
|
|
196
|
-
getParentCtx: <T2_7 = T2_6>() => {
|
|
197
|
-
getCtx: () => T2_7;
|
|
198
|
-
store: <K_24 extends keyof T2_7>(key: K_24) => {
|
|
199
|
-
value: T2_7[K_24];
|
|
200
|
-
hook: <K2_48 extends keyof Required<T2_7>[K_24]>(key2: K2_48) => {
|
|
201
|
-
value: Required<T2_7>[K_24][K2_48];
|
|
202
|
-
isDefined: boolean;
|
|
203
|
-
};
|
|
204
|
-
init: <K2_49 extends keyof Required<T2_7>[K_24]>(key2: K2_49, getter: () => Required<Required<T2_7>[K_24]>[K2_49]) => Required<Required<T2_7>[K_24]>[K2_49];
|
|
205
|
-
set: <K2_50 extends keyof Required<T2_7>[K_24]>(key2: K2_50, v: Required<T2_7[K_24]>[K2_50]) => Required<T2_7[K_24]>[K2_50];
|
|
206
|
-
get: <K2_51 extends keyof Required<T2_7>[K_24]>(key2: K2_51) => Required<T2_7>[K_24][K2_51] | undefined;
|
|
207
|
-
has: <K2_52 extends keyof Required<T2_7>[K_24]>(key2: K2_52) => boolean;
|
|
208
|
-
del: <K2_53 extends keyof Required<T2_7>[K_24]>(key2: K2_53) => void;
|
|
209
|
-
entries: () => [string, unknown][];
|
|
210
|
-
clear: () => void;
|
|
211
|
-
};
|
|
212
|
-
getStore: <K_25 extends keyof T2_7>(key: K_25) => T2_7[K_25];
|
|
213
|
-
setStore: <K_26 extends keyof T2_7>(key: K_26, v: T2_7[K_26]) => void;
|
|
214
|
-
setParentCtx: (parentCtx: unknown) => void;
|
|
215
|
-
hasParentCtx: () => boolean;
|
|
216
|
-
getParentCtx: <T2_8 = T2_7>() => {
|
|
217
|
-
getCtx: () => T2_8;
|
|
218
|
-
store: <K_27 extends keyof T2_8>(key: K_27) => {
|
|
219
|
-
value: T2_8[K_27];
|
|
220
|
-
hook: <K2_54 extends keyof Required<T2_8>[K_27]>(key2: K2_54) => {
|
|
221
|
-
value: Required<T2_8>[K_27][K2_54];
|
|
222
|
-
isDefined: boolean;
|
|
223
|
-
};
|
|
224
|
-
init: <K2_55 extends keyof Required<T2_8>[K_27]>(key2: K2_55, getter: () => Required<Required<T2_8>[K_27]>[K2_55]) => Required<Required<T2_8>[K_27]>[K2_55];
|
|
225
|
-
set: <K2_56 extends keyof Required<T2_8>[K_27]>(key2: K2_56, v: Required<T2_8[K_27]>[K2_56]) => Required<T2_8[K_27]>[K2_56];
|
|
226
|
-
get: <K2_57 extends keyof Required<T2_8>[K_27]>(key2: K2_57) => Required<T2_8>[K_27][K2_57] | undefined;
|
|
227
|
-
has: <K2_58 extends keyof Required<T2_8>[K_27]>(key2: K2_58) => boolean;
|
|
228
|
-
del: <K2_59 extends keyof Required<T2_8>[K_27]>(key2: K2_59) => void;
|
|
229
|
-
entries: () => [string, unknown][];
|
|
230
|
-
clear: () => void;
|
|
231
|
-
};
|
|
232
|
-
getStore: <K_28 extends keyof T2_8>(key: K_28) => T2_8[K_28];
|
|
233
|
-
setStore: <K_29 extends keyof T2_8>(key: K_29, v: T2_8[K_29]) => void;
|
|
234
|
-
setParentCtx: (parentCtx: unknown) => void;
|
|
235
|
-
hasParentCtx: () => boolean;
|
|
236
|
-
getParentCtx: <T2_9 = T2_8>() => {
|
|
237
|
-
getCtx: () => T2_9;
|
|
238
|
-
store: <K_30 extends keyof T2_9>(key: K_30) => {
|
|
239
|
-
value: T2_9[K_30];
|
|
240
|
-
hook: <K2_60 extends keyof Required<T2_9>[K_30]>(key2: K2_60) => {
|
|
241
|
-
value: Required<T2_9>[K_30][K2_60];
|
|
242
|
-
isDefined: boolean;
|
|
243
|
-
};
|
|
244
|
-
init: <K2_61 extends keyof Required<T2_9>[K_30]>(key2: K2_61, getter: () => Required<Required<T2_9>[K_30]>[K2_61]) => Required<Required<T2_9>[K_30]>[K2_61];
|
|
245
|
-
set: <K2_62 extends keyof Required<T2_9>[K_30]>(key2: K2_62, v: Required<T2_9[K_30]>[K2_62]) => Required<T2_9[K_30]>[K2_62];
|
|
246
|
-
get: <K2_63 extends keyof Required<T2_9>[K_30]>(key2: K2_63) => Required<T2_9>[K_30][K2_63] | undefined;
|
|
247
|
-
has: <K2_64 extends keyof Required<T2_9>[K_30]>(key2: K2_64) => boolean;
|
|
248
|
-
del: <K2_65 extends keyof Required<T2_9>[K_30]>(key2: K2_65) => void;
|
|
249
|
-
entries: () => [string, unknown][];
|
|
250
|
-
clear: () => void;
|
|
251
|
-
};
|
|
252
|
-
getStore: <K_31 extends keyof T2_9>(key: K_31) => T2_9[K_31];
|
|
253
|
-
setStore: <K_32 extends keyof T2_9>(key: K_32, v: T2_9[K_32]) => void;
|
|
254
|
-
setParentCtx: (parentCtx: unknown) => void;
|
|
255
|
-
hasParentCtx: () => boolean;
|
|
256
|
-
getParentCtx: <T2_10 = T2_9>() => any;
|
|
257
|
-
};
|
|
258
|
-
};
|
|
259
|
-
};
|
|
260
|
-
};
|
|
261
|
-
};
|
|
262
|
-
};
|
|
263
|
-
};
|
|
264
|
-
};
|
|
265
|
-
};
|
|
266
|
-
};
|
|
267
|
-
};
|
|
268
|
-
|
|
269
|
-
interface TEventLoggerData {
|
|
270
|
-
eventId: string;
|
|
271
|
-
}
|
|
272
|
-
declare class EventLogger extends ProstoLogger<TEventLoggerData> {
|
|
273
|
-
constructor(eventId: string, opts?: TEventOptions['eventLogger']);
|
|
65
|
+
getParentCtx: <T2 = T>() => TCtxHelpers<T2>;
|
|
274
66
|
}
|
|
275
67
|
|
|
276
|
-
declare function useEventLogger(topic?: string): _prostojs_logger.ProstoLogger<TEventLoggerData>;
|
|
277
|
-
|
|
278
|
-
declare function useRouteParams<T extends object = Record<string, string | string[]>>(): {
|
|
279
|
-
params: T;
|
|
280
|
-
get: <K extends keyof T>(name: K) => T[K];
|
|
281
|
-
};
|
|
282
|
-
|
|
283
|
-
/**
|
|
284
|
-
* ContextInjector
|
|
285
|
-
*
|
|
286
|
-
* Provides a way to inject context
|
|
287
|
-
* Usefull when working with opentelemetry spans
|
|
288
|
-
*/
|
|
289
68
|
declare class ContextInjector<N> {
|
|
290
69
|
with<T>(name: N, attributes: Record<string, string | number | boolean>, cb: TContextInjectorCallback<T>): T;
|
|
291
70
|
with<T>(name: N, cb: TContextInjectorCallback<T>): T;
|
|
@@ -304,4 +83,4 @@ type THook<T = string, K extends PropertyKey = 'value'> = {
|
|
|
304
83
|
[key in K]: T;
|
|
305
84
|
};
|
|
306
85
|
|
|
307
|
-
export { ContextInjector, EventLogger, type TContextInjectorHooks, type TEmpty, type TEventLoggerData, type TEventOptions, type TGenericContextStore, type TGenericEvent, type THook, asyncStorage, attachHook, createAsyncEventContext, getContextInjector, replaceContextInjector, useAsyncEventContext, useEventId, useEventLogger, useRouteParams };
|
|
86
|
+
export { ContextInjector, EventLogger, type TContextInjectorHooks, type TCtxHelpers, type TEmpty, type TEventLoggerData, type TEventOptions, type TGenericContextStore, type TGenericEvent, type THook, asyncStorage, attachHook, createAsyncEventContext, getContextInjector, replaceContextInjector, useAsyncEventContext, useEventId, useEventLogger, useRouteParams };
|
package/dist/index.mjs
CHANGED
|
@@ -1,201 +1,212 @@
|
|
|
1
|
-
import { randomUUID } from
|
|
2
|
-
import { AsyncLocalStorage } from
|
|
3
|
-
import { ProstoLogger,
|
|
1
|
+
import { randomUUID } from "crypto";
|
|
2
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
3
|
+
import { ProstoLogger, coloredConsole, createConsoleTransort } from "@prostojs/logger";
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
}
|
|
5
|
+
//#region packages/event-core/src/context-injector.ts
|
|
6
|
+
var ContextInjector = class {
|
|
7
|
+
with(name, attributes, cb) {
|
|
8
|
+
const fn = typeof attributes === "function" ? attributes : cb;
|
|
9
|
+
return fn();
|
|
10
|
+
}
|
|
11
|
+
hook(method, name, route) {}
|
|
12
|
+
};
|
|
13
13
|
let ci = new ContextInjector();
|
|
14
14
|
function getContextInjector() {
|
|
15
|
-
|
|
15
|
+
return ci;
|
|
16
16
|
}
|
|
17
17
|
function replaceContextInjector(newCi) {
|
|
18
|
-
|
|
18
|
+
ci = newCi;
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
+
//#endregion
|
|
22
|
+
//#region packages/event-core/src/hook.ts
|
|
21
23
|
function attachHook(target, opts, name) {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
24
|
+
Object.defineProperty(target, name || "value", {
|
|
25
|
+
get: opts.get,
|
|
26
|
+
set: opts.set
|
|
27
|
+
});
|
|
28
|
+
return target;
|
|
27
29
|
}
|
|
28
30
|
|
|
31
|
+
//#endregion
|
|
32
|
+
//#region packages/event-core/src/context.ts
|
|
29
33
|
const asyncStorage = new AsyncLocalStorage();
|
|
30
34
|
function createAsyncEventContext(data) {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
const ci = getContextInjector();
|
|
37
|
-
return (cb) => asyncStorage.run(newContext, () => ci.with('Event:start', { eventType: newContext.event.type }, cb));
|
|
35
|
+
const newContext = { ...data };
|
|
36
|
+
const cc = asyncStorage.getStore();
|
|
37
|
+
if (cc && typeof cc === "object" && cc.event?.type) newContext.parentCtx = cc;
|
|
38
|
+
const ci$1 = getContextInjector();
|
|
39
|
+
return (cb) => asyncStorage.run(newContext, () => ci$1.with("Event:start", { eventType: newContext.event.type }, cb));
|
|
38
40
|
}
|
|
39
41
|
function useAsyncEventContext(expectedTypes) {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
cc = cc.parentCtx;
|
|
50
|
-
}
|
|
51
|
-
else {
|
|
52
|
-
throw new Error(`Event context type mismatch: expected ${types
|
|
53
|
-
.map(t => `"${t}"`)
|
|
54
|
-
.join(', ')}, received "${type}"`);
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
return _getCtxHelpers(cc);
|
|
42
|
+
let cc = asyncStorage.getStore();
|
|
43
|
+
if (!cc) throw new Error("Event context does not exist at this point.");
|
|
44
|
+
if (expectedTypes || typeof expectedTypes === "string") {
|
|
45
|
+
const type = cc.event.type;
|
|
46
|
+
const types = typeof expectedTypes === "string" ? [expectedTypes] : expectedTypes;
|
|
47
|
+
if (!types.includes(type)) if (cc.parentCtx?.event.type && types.includes(cc.parentCtx.event.type)) cc = cc.parentCtx;
|
|
48
|
+
else throw new Error(`Event context type mismatch: expected ${types.map((t) => `"${t}"`).join(", ")}, received "${type}"`);
|
|
49
|
+
}
|
|
50
|
+
return _getCtxHelpers(cc);
|
|
59
51
|
}
|
|
52
|
+
/**
|
|
53
|
+
*
|
|
54
|
+
* @param cc
|
|
55
|
+
* @returns
|
|
56
|
+
*/
|
|
60
57
|
function _getCtxHelpers(cc) {
|
|
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
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
58
|
+
/**
|
|
59
|
+
* Hook to an event store property
|
|
60
|
+
*
|
|
61
|
+
* @param key store property key
|
|
62
|
+
* @returns a hook { value: <prop value>, hook: (key2: keyof <prop value>) => { value: <nested prop value> }, ... }
|
|
63
|
+
*/
|
|
64
|
+
function store(key) {
|
|
65
|
+
const obj = {
|
|
66
|
+
value: null,
|
|
67
|
+
hook,
|
|
68
|
+
init,
|
|
69
|
+
set: setNested,
|
|
70
|
+
get: getNested,
|
|
71
|
+
has: hasNested,
|
|
72
|
+
del: delNested,
|
|
73
|
+
entries,
|
|
74
|
+
clear
|
|
75
|
+
};
|
|
76
|
+
attachHook(obj, {
|
|
77
|
+
set: (v) => {
|
|
78
|
+
set(key, v);
|
|
79
|
+
},
|
|
80
|
+
get: () => get(key)
|
|
81
|
+
});
|
|
82
|
+
function init(key2, getter) {
|
|
83
|
+
if (hasNested(key2)) return getNested(key2);
|
|
84
|
+
return setNested(key2, getter());
|
|
85
|
+
}
|
|
86
|
+
function hook(key2) {
|
|
87
|
+
const obj$1 = {
|
|
88
|
+
value: null,
|
|
89
|
+
isDefined: null
|
|
90
|
+
};
|
|
91
|
+
attachHook(obj$1, {
|
|
92
|
+
set: (v) => setNested(key2, v),
|
|
93
|
+
get: () => getNested(key2)
|
|
94
|
+
});
|
|
95
|
+
attachHook(obj$1, { get: () => hasNested(key2) }, "isDefined");
|
|
96
|
+
return obj$1;
|
|
97
|
+
}
|
|
98
|
+
function setNested(key2, v) {
|
|
99
|
+
if (obj.value === undefined) obj.value = {};
|
|
100
|
+
obj.value[key2] = v;
|
|
101
|
+
return v;
|
|
102
|
+
}
|
|
103
|
+
function delNested(key2) {
|
|
104
|
+
setNested(key2, undefined);
|
|
105
|
+
}
|
|
106
|
+
function getNested(key2) {
|
|
107
|
+
return (obj.value || {})[key2];
|
|
108
|
+
}
|
|
109
|
+
function hasNested(key2) {
|
|
110
|
+
return (obj.value || {})[key2] !== undefined;
|
|
111
|
+
}
|
|
112
|
+
function entries() {
|
|
113
|
+
return Object.entries(obj.value || {});
|
|
114
|
+
}
|
|
115
|
+
function clear() {
|
|
116
|
+
obj.value = {};
|
|
117
|
+
}
|
|
118
|
+
return obj;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Get event context object
|
|
122
|
+
*
|
|
123
|
+
* @returns whole context object
|
|
124
|
+
*/
|
|
125
|
+
function getCtx() {
|
|
126
|
+
return cc;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Get value of event store property
|
|
130
|
+
*
|
|
131
|
+
* @param key property name
|
|
132
|
+
* @returns value of property by name
|
|
133
|
+
*/
|
|
134
|
+
function get(key) {
|
|
135
|
+
return getCtx()[key];
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Set value of event store property
|
|
139
|
+
*
|
|
140
|
+
* @param key property name
|
|
141
|
+
* @param v property value
|
|
142
|
+
*/
|
|
143
|
+
function set(key, v) {
|
|
144
|
+
getCtx()[key] = v;
|
|
145
|
+
}
|
|
146
|
+
const hasParentCtx = () => !!cc.parentCtx;
|
|
147
|
+
return {
|
|
148
|
+
getCtx,
|
|
149
|
+
store,
|
|
150
|
+
getStore: get,
|
|
151
|
+
setStore: set,
|
|
152
|
+
setParentCtx: (parentCtx) => {
|
|
153
|
+
cc.parentCtx = parentCtx;
|
|
154
|
+
},
|
|
155
|
+
hasParentCtx,
|
|
156
|
+
getParentCtx: () => {
|
|
157
|
+
if (!hasParentCtx()) throw new Error("Parent context is not available");
|
|
158
|
+
return _getCtxHelpers(cc.parentCtx);
|
|
159
|
+
}
|
|
160
|
+
};
|
|
149
161
|
}
|
|
150
162
|
|
|
163
|
+
//#endregion
|
|
164
|
+
//#region packages/event-core/src/composables/event-id.ts
|
|
151
165
|
function useEventId() {
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
166
|
+
const { store } = useAsyncEventContext();
|
|
167
|
+
const { init } = store("event");
|
|
168
|
+
const getId = () => init("id", () => randomUUID());
|
|
169
|
+
return { getId };
|
|
156
170
|
}
|
|
157
171
|
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
createConsoleTransort({
|
|
172
|
-
format: coloredConsole,
|
|
173
|
-
}),
|
|
174
|
-
];
|
|
175
|
-
}
|
|
176
|
-
super(_opts, opts?.topic || 'event');
|
|
177
|
-
}
|
|
178
|
-
}
|
|
172
|
+
//#endregion
|
|
173
|
+
//#region packages/event-core/src/event-logger.ts
|
|
174
|
+
var EventLogger = class extends ProstoLogger {
|
|
175
|
+
constructor(eventId, opts) {
|
|
176
|
+
const _opts = opts || { level: 4 };
|
|
177
|
+
if (!_opts.mapper) _opts.mapper = (msg) => ({
|
|
178
|
+
...msg,
|
|
179
|
+
eventId
|
|
180
|
+
});
|
|
181
|
+
if (!_opts.transports) _opts.transports = [createConsoleTransort({ format: coloredConsole })];
|
|
182
|
+
super(_opts, opts?.topic || "event");
|
|
183
|
+
}
|
|
184
|
+
};
|
|
179
185
|
|
|
186
|
+
//#endregion
|
|
187
|
+
//#region packages/event-core/src/composables/event-logger.ts
|
|
180
188
|
function useEventLogger(topic) {
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
189
|
+
const { getId } = useEventId();
|
|
190
|
+
const { store, getCtx } = useAsyncEventContext();
|
|
191
|
+
const { init } = store("event");
|
|
192
|
+
const ctx = getCtx();
|
|
193
|
+
const get = () => init("logger", () => new EventLogger(getId(), ctx.options.eventLogger));
|
|
194
|
+
return topic ? get().createTopic(topic) : get();
|
|
187
195
|
}
|
|
188
196
|
|
|
197
|
+
//#endregion
|
|
198
|
+
//#region packages/event-core/src/composables/route-params.ts
|
|
189
199
|
function useRouteParams() {
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
200
|
+
const { store } = useAsyncEventContext();
|
|
201
|
+
const params = store("routeParams").value || {};
|
|
202
|
+
function get(name) {
|
|
203
|
+
return params[name];
|
|
204
|
+
}
|
|
205
|
+
return {
|
|
206
|
+
params,
|
|
207
|
+
get
|
|
208
|
+
};
|
|
199
209
|
}
|
|
200
210
|
|
|
201
|
-
|
|
211
|
+
//#endregion
|
|
212
|
+
export { ContextInjector, EventLogger, asyncStorage, attachHook, createAsyncEventContext, getContextInjector, replaceContextInjector, useAsyncEventContext, useEventId, useEventLogger, useRouteParams };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wooksjs/event-core",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.25",
|
|
4
4
|
"description": "@wooksjs/event-core",
|
|
5
5
|
"main": "dist/index.cjs",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -11,9 +11,9 @@
|
|
|
11
11
|
"exports": {
|
|
12
12
|
"./package.json": "./package.json",
|
|
13
13
|
".": {
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
14
15
|
"require": "./dist/index.cjs",
|
|
15
|
-
"import": "./dist/index.mjs"
|
|
16
|
-
"types": "./dist/index.d.ts"
|
|
16
|
+
"import": "./dist/index.mjs"
|
|
17
17
|
}
|
|
18
18
|
},
|
|
19
19
|
"repository": {
|
|
@@ -39,8 +39,15 @@
|
|
|
39
39
|
"url": "https://github.com/wooksjs/wooksjs/issues"
|
|
40
40
|
},
|
|
41
41
|
"dependencies": {
|
|
42
|
-
"hookable": "^5.5.3",
|
|
43
42
|
"@prostojs/logger": "^0.4.3"
|
|
44
43
|
},
|
|
45
|
-
"homepage": "https://github.com/wooksjs/wooksjs/tree/main/packages/event-core#readme"
|
|
46
|
-
|
|
44
|
+
"homepage": "https://github.com/wooksjs/wooksjs/tree/main/packages/event-core#readme",
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"dts-bundle-generator": "^9.5.1",
|
|
47
|
+
"typescript": "^5.7.3",
|
|
48
|
+
"vitest": "^2.1.8"
|
|
49
|
+
},
|
|
50
|
+
"scripts": {
|
|
51
|
+
"build": "rolldown -c ../../rolldown.config.mjs"
|
|
52
|
+
}
|
|
53
|
+
}
|