@wooksjs/event-core 0.4.11 → 0.4.13
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 +0 -36
- package/dist/index.d.ts +104 -110
- package/dist/index.mjs +0 -36
- package/package.json +10 -2
package/dist/index.cjs
CHANGED
|
@@ -12,24 +12,11 @@ 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
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
21
|
if (!currentContext) {
|
|
35
22
|
throw new Error('Event context does not exist. Use event context synchronously within the runtime of the event.');
|
|
@@ -46,12 +33,6 @@ function useEventContext(expectedTypes) {
|
|
|
46
33
|
return _getCtxHelpers(cc);
|
|
47
34
|
}
|
|
48
35
|
function _getCtxHelpers(cc) {
|
|
49
|
-
/**
|
|
50
|
-
* Hook to an event store property
|
|
51
|
-
*
|
|
52
|
-
* @param key store property key
|
|
53
|
-
* @returns a hook { value: <prop value>, hook: (key2: keyof <prop value>) => { value: <nested prop value> }, ... }
|
|
54
|
-
*/
|
|
55
36
|
function store(key) {
|
|
56
37
|
const obj = {
|
|
57
38
|
value: null,
|
|
@@ -111,29 +92,12 @@ function _getCtxHelpers(cc) {
|
|
|
111
92
|
}
|
|
112
93
|
return obj;
|
|
113
94
|
}
|
|
114
|
-
/**
|
|
115
|
-
* Get event context object
|
|
116
|
-
*
|
|
117
|
-
* @returns whole context object
|
|
118
|
-
*/
|
|
119
95
|
function getCtx() {
|
|
120
96
|
return cc;
|
|
121
97
|
}
|
|
122
|
-
/**
|
|
123
|
-
* Get value of event store property
|
|
124
|
-
*
|
|
125
|
-
* @param key property name
|
|
126
|
-
* @returns value of property by name
|
|
127
|
-
*/
|
|
128
98
|
function get(key) {
|
|
129
99
|
return getCtx()[key];
|
|
130
100
|
}
|
|
131
|
-
/**
|
|
132
|
-
* Set value of event store property
|
|
133
|
-
*
|
|
134
|
-
* @param key property name
|
|
135
|
-
* @param v property value
|
|
136
|
-
*/
|
|
137
101
|
function set(key, v) {
|
|
138
102
|
getCtx()[key] = v;
|
|
139
103
|
}
|
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,24 +10,11 @@ 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
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
19
|
if (!currentContext) {
|
|
33
20
|
throw new Error('Event context does not exist. Use event context synchronously within the runtime of the event.');
|
|
@@ -44,12 +31,6 @@ function useEventContext(expectedTypes) {
|
|
|
44
31
|
return _getCtxHelpers(cc);
|
|
45
32
|
}
|
|
46
33
|
function _getCtxHelpers(cc) {
|
|
47
|
-
/**
|
|
48
|
-
* Hook to an event store property
|
|
49
|
-
*
|
|
50
|
-
* @param key store property key
|
|
51
|
-
* @returns a hook { value: <prop value>, hook: (key2: keyof <prop value>) => { value: <nested prop value> }, ... }
|
|
52
|
-
*/
|
|
53
34
|
function store(key) {
|
|
54
35
|
const obj = {
|
|
55
36
|
value: null,
|
|
@@ -109,29 +90,12 @@ function _getCtxHelpers(cc) {
|
|
|
109
90
|
}
|
|
110
91
|
return obj;
|
|
111
92
|
}
|
|
112
|
-
/**
|
|
113
|
-
* Get event context object
|
|
114
|
-
*
|
|
115
|
-
* @returns whole context object
|
|
116
|
-
*/
|
|
117
93
|
function getCtx() {
|
|
118
94
|
return cc;
|
|
119
95
|
}
|
|
120
|
-
/**
|
|
121
|
-
* Get value of event store property
|
|
122
|
-
*
|
|
123
|
-
* @param key property name
|
|
124
|
-
* @returns value of property by name
|
|
125
|
-
*/
|
|
126
96
|
function get(key) {
|
|
127
97
|
return getCtx()[key];
|
|
128
98
|
}
|
|
129
|
-
/**
|
|
130
|
-
* Set value of event store property
|
|
131
|
-
*
|
|
132
|
-
* @param key property name
|
|
133
|
-
* @param v property value
|
|
134
|
-
*/
|
|
135
99
|
function set(key, v) {
|
|
136
100
|
getCtx()[key] = v;
|
|
137
101
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wooksjs/event-core",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.13",
|
|
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
|
}
|