@wooksjs/event-core 0.4.9 → 0.4.11

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.
Files changed (3) hide show
  1. package/dist/index.cjs +182 -180
  2. package/dist/index.mjs +182 -180
  3. package/package.json +1 -1
package/dist/index.cjs CHANGED
@@ -3,196 +3,198 @@
3
3
  var crypto = require('crypto');
4
4
  var logger = require('@prostojs/logger');
5
5
 
6
- function attachHook(target, opts, name) {
7
- Object.defineProperty(target, name || 'value', {
8
- get: opts.get,
9
- set: opts.set,
10
- });
11
- return target;
6
+ function attachHook(target, opts, name) {
7
+ Object.defineProperty(target, name || 'value', {
8
+ get: opts.get,
9
+ set: opts.set,
10
+ });
11
+ return target;
12
12
  }
13
13
 
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
- function createEventContext(data) {
22
- const newContext = Object.assign({}, data);
23
- currentContext = newContext;
24
- return _getCtxHelpers(newContext);
25
- }
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
- function useEventContext(expectedTypes) {
34
- var _a;
35
- if (!currentContext) {
36
- throw new Error('Event context does not exist. Use event context synchronously within the runtime of the event.');
37
- }
38
- const cc = currentContext;
39
- if (expectedTypes || typeof expectedTypes === 'string') {
40
- const type = (_a = cc.event) === null || _a === void 0 ? void 0 : _a.type;
41
- const types = typeof expectedTypes === 'string' ? [expectedTypes] : expectedTypes;
42
- if (!types.includes(type))
43
- new Error(`Event context type mismatch: expected ${types
44
- .map((t) => `"${t}"`)
45
- .join(', ')}, received "${type}"`);
46
- }
47
- return _getCtxHelpers(cc);
48
- }
49
- 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
- function store(key) {
57
- const obj = {
58
- value: null,
59
- hook,
60
- init,
61
- set: setNested,
62
- get: getNested,
63
- has: hasNested,
64
- del: delNested,
65
- entries,
66
- clear,
67
- };
68
- attachHook(obj, {
69
- set: (v) => set(key, v),
70
- get: () => get(key),
71
- });
72
- function init(key2, getter) {
73
- if (hasNested(key2))
74
- return getNested(key2);
75
- return setNested(key2, getter());
76
- }
77
- function hook(key2) {
78
- const obj = {
79
- value: null,
80
- isDefined: null,
81
- };
82
- attachHook(obj, {
83
- set: (v) => setNested(key2, v),
84
- get: () => getNested(key2),
85
- });
86
- attachHook(obj, {
87
- get: () => hasNested(key2),
88
- }, 'isDefined');
89
- return obj;
90
- }
91
- function setNested(key2, v) {
92
- if (typeof obj.value === 'undefined') {
93
- obj.value = {};
94
- }
95
- obj.value[key2] = v;
96
- return v;
97
- }
98
- function delNested(key2) {
99
- setNested(key2, undefined);
100
- }
101
- function getNested(key2) {
102
- return (obj.value || {})[key2];
103
- }
104
- function hasNested(key2) {
105
- return typeof (obj.value || {})[key2] !== 'undefined';
106
- }
107
- function entries() {
108
- return Object.entries(obj.value || {});
109
- }
110
- function clear() {
111
- obj.value = {};
112
- }
113
- return obj;
114
- }
115
- /**
116
- * Get event context object
117
- *
118
- * @returns whole context object
119
- */
120
- function getCtx() {
121
- return cc;
122
- }
123
- /**
124
- * Get value of event store property
125
- *
126
- * @param key property name
127
- * @returns value of property by name
128
- */
129
- function get(key) {
130
- return getCtx()[key];
131
- }
132
- /**
133
- * Set value of event store property
134
- *
135
- * @param key property name
136
- * @param v property value
137
- */
138
- function set(key, v) {
139
- getCtx()[key] = v;
140
- }
141
- return {
142
- getCtx,
143
- restoreCtx: () => (currentContext = cc),
144
- clearCtx: () => cc === currentContext ? (currentContext = null) : null,
145
- store,
146
- getStore: get,
147
- setStore: set,
148
- };
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
+ function createEventContext(data) {
22
+ const newContext = { ...data };
23
+ currentContext = newContext;
24
+ return _getCtxHelpers(newContext);
25
+ }
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
+ function useEventContext(expectedTypes) {
34
+ if (!currentContext) {
35
+ throw new Error('Event context does not exist. Use event context synchronously within the runtime of the event.');
36
+ }
37
+ const cc = currentContext;
38
+ if (expectedTypes || typeof expectedTypes === 'string') {
39
+ const type = cc.event?.type;
40
+ const types = typeof expectedTypes === 'string' ? [expectedTypes] : expectedTypes;
41
+ if (!types.includes(type))
42
+ new Error(`Event context type mismatch: expected ${types
43
+ .map((t) => `"${t}"`)
44
+ .join(', ')}, received "${type}"`);
45
+ }
46
+ return _getCtxHelpers(cc);
47
+ }
48
+ 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
+ function store(key) {
56
+ const obj = {
57
+ value: null,
58
+ hook,
59
+ init,
60
+ set: setNested,
61
+ get: getNested,
62
+ has: hasNested,
63
+ del: delNested,
64
+ entries,
65
+ clear,
66
+ };
67
+ attachHook(obj, {
68
+ set: (v) => set(key, v),
69
+ get: () => get(key),
70
+ });
71
+ function init(key2, getter) {
72
+ if (hasNested(key2))
73
+ return getNested(key2);
74
+ return setNested(key2, getter());
75
+ }
76
+ function hook(key2) {
77
+ const obj = {
78
+ value: null,
79
+ isDefined: null,
80
+ };
81
+ attachHook(obj, {
82
+ set: (v) => setNested(key2, v),
83
+ get: () => getNested(key2),
84
+ });
85
+ attachHook(obj, {
86
+ get: () => hasNested(key2),
87
+ }, 'isDefined');
88
+ return obj;
89
+ }
90
+ function setNested(key2, v) {
91
+ if (typeof obj.value === 'undefined') {
92
+ obj.value = {};
93
+ }
94
+ obj.value[key2] = v;
95
+ return v;
96
+ }
97
+ function delNested(key2) {
98
+ setNested(key2, undefined);
99
+ }
100
+ function getNested(key2) {
101
+ return (obj.value || {})[key2];
102
+ }
103
+ function hasNested(key2) {
104
+ return typeof (obj.value || {})[key2] !== 'undefined';
105
+ }
106
+ function entries() {
107
+ return Object.entries(obj.value || {});
108
+ }
109
+ function clear() {
110
+ obj.value = {};
111
+ }
112
+ return obj;
113
+ }
114
+ /**
115
+ * Get event context object
116
+ *
117
+ * @returns whole context object
118
+ */
119
+ function getCtx() {
120
+ return cc;
121
+ }
122
+ /**
123
+ * Get value of event store property
124
+ *
125
+ * @param key property name
126
+ * @returns value of property by name
127
+ */
128
+ function get(key) {
129
+ return getCtx()[key];
130
+ }
131
+ /**
132
+ * Set value of event store property
133
+ *
134
+ * @param key property name
135
+ * @param v property value
136
+ */
137
+ function set(key, v) {
138
+ getCtx()[key] = v;
139
+ }
140
+ return {
141
+ getCtx,
142
+ restoreCtx: () => (currentContext = cc),
143
+ clearCtx: () => cc === currentContext ? (currentContext = null) : null,
144
+ store,
145
+ getStore: get,
146
+ setStore: set,
147
+ };
149
148
  }
150
149
 
151
- function useRouteParams() {
152
- const { store } = useEventContext();
153
- const params = (store('routeParams').value || {});
154
- function get(name) {
155
- return params[name];
156
- }
157
- return {
158
- params,
159
- get,
160
- };
150
+ function useRouteParams() {
151
+ const { store } = useEventContext();
152
+ const params = (store('routeParams').value || {});
153
+ function get(name) {
154
+ return params[name];
155
+ }
156
+ return {
157
+ params,
158
+ get,
159
+ };
161
160
  }
162
161
 
163
- function useEventId() {
164
- const { store } = useEventContext();
165
- const { init } = store('event');
166
- const getId = () => init('id', () => crypto.randomUUID());
167
- return { getId };
162
+ function useEventId() {
163
+ const { store } = useEventContext();
164
+ const { init } = store('event');
165
+ const getId = () => init('id', () => crypto.randomUUID());
166
+ return { getId };
168
167
  }
169
168
 
170
- class EventLogger extends logger.ProstoLogger {
171
- constructor(eventId, opts) {
172
- const _opts = opts || {
173
- level: 4,
174
- };
175
- if (!_opts.mapper) {
176
- _opts.mapper = (msg) => (Object.assign(Object.assign({}, msg), { eventId }));
177
- }
178
- if (!_opts.transports) {
179
- _opts.transports = [
180
- logger.createConsoleTransort({
181
- format: logger.coloredConsole,
182
- }),
183
- ];
184
- }
185
- super(_opts, (opts === null || opts === void 0 ? void 0 : opts.topic) || 'event');
186
- }
169
+ class EventLogger extends logger.ProstoLogger {
170
+ constructor(eventId, opts) {
171
+ const _opts = opts || {
172
+ level: 4,
173
+ };
174
+ if (!_opts.mapper) {
175
+ _opts.mapper = (msg) => ({
176
+ ...msg,
177
+ eventId,
178
+ });
179
+ }
180
+ if (!_opts.transports) {
181
+ _opts.transports = [
182
+ logger.createConsoleTransort({
183
+ format: logger.coloredConsole,
184
+ }),
185
+ ];
186
+ }
187
+ super(_opts, opts?.topic || 'event');
188
+ }
187
189
  }
188
190
 
189
- function useEventLogger(topic) {
190
- const { getId } = useEventId();
191
- const { store, getCtx } = useEventContext();
192
- const { init } = store('event');
193
- const ctx = getCtx();
194
- const get = () => init('logger', () => { var _a; return new EventLogger(getId(), (_a = ctx.options) === null || _a === void 0 ? void 0 : _a.eventLogger); });
195
- return topic ? get().createTopic(topic) : get();
191
+ function useEventLogger(topic) {
192
+ const { getId } = useEventId();
193
+ const { store, getCtx } = useEventContext();
194
+ const { init } = store('event');
195
+ const ctx = getCtx();
196
+ const get = () => init('logger', () => new EventLogger(getId(), ctx.options?.eventLogger));
197
+ return topic ? get().createTopic(topic) : get();
196
198
  }
197
199
 
198
200
  exports.EventLogger = EventLogger;
package/dist/index.mjs CHANGED
@@ -1,196 +1,198 @@
1
1
  import { randomUUID } from 'crypto';
2
2
  import { ProstoLogger, createConsoleTransort, coloredConsole } from '@prostojs/logger';
3
3
 
4
- function attachHook(target, opts, name) {
5
- Object.defineProperty(target, name || 'value', {
6
- get: opts.get,
7
- set: opts.set,
8
- });
9
- return target;
4
+ function attachHook(target, opts, name) {
5
+ Object.defineProperty(target, name || 'value', {
6
+ get: opts.get,
7
+ set: opts.set,
8
+ });
9
+ return target;
10
10
  }
11
11
 
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
- function createEventContext(data) {
20
- const newContext = Object.assign({}, data);
21
- currentContext = newContext;
22
- return _getCtxHelpers(newContext);
23
- }
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
- function useEventContext(expectedTypes) {
32
- var _a;
33
- if (!currentContext) {
34
- throw new Error('Event context does not exist. Use event context synchronously within the runtime of the event.');
35
- }
36
- const cc = currentContext;
37
- if (expectedTypes || typeof expectedTypes === 'string') {
38
- const type = (_a = cc.event) === null || _a === void 0 ? void 0 : _a.type;
39
- const types = typeof expectedTypes === 'string' ? [expectedTypes] : expectedTypes;
40
- if (!types.includes(type))
41
- new Error(`Event context type mismatch: expected ${types
42
- .map((t) => `"${t}"`)
43
- .join(', ')}, received "${type}"`);
44
- }
45
- return _getCtxHelpers(cc);
46
- }
47
- 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
- function store(key) {
55
- const obj = {
56
- value: null,
57
- hook,
58
- init,
59
- set: setNested,
60
- get: getNested,
61
- has: hasNested,
62
- del: delNested,
63
- entries,
64
- clear,
65
- };
66
- attachHook(obj, {
67
- set: (v) => set(key, v),
68
- get: () => get(key),
69
- });
70
- function init(key2, getter) {
71
- if (hasNested(key2))
72
- return getNested(key2);
73
- return setNested(key2, getter());
74
- }
75
- function hook(key2) {
76
- const obj = {
77
- value: null,
78
- isDefined: null,
79
- };
80
- attachHook(obj, {
81
- set: (v) => setNested(key2, v),
82
- get: () => getNested(key2),
83
- });
84
- attachHook(obj, {
85
- get: () => hasNested(key2),
86
- }, 'isDefined');
87
- return obj;
88
- }
89
- function setNested(key2, v) {
90
- if (typeof obj.value === 'undefined') {
91
- obj.value = {};
92
- }
93
- obj.value[key2] = v;
94
- return v;
95
- }
96
- function delNested(key2) {
97
- setNested(key2, undefined);
98
- }
99
- function getNested(key2) {
100
- return (obj.value || {})[key2];
101
- }
102
- function hasNested(key2) {
103
- return typeof (obj.value || {})[key2] !== 'undefined';
104
- }
105
- function entries() {
106
- return Object.entries(obj.value || {});
107
- }
108
- function clear() {
109
- obj.value = {};
110
- }
111
- return obj;
112
- }
113
- /**
114
- * Get event context object
115
- *
116
- * @returns whole context object
117
- */
118
- function getCtx() {
119
- return cc;
120
- }
121
- /**
122
- * Get value of event store property
123
- *
124
- * @param key property name
125
- * @returns value of property by name
126
- */
127
- function get(key) {
128
- return getCtx()[key];
129
- }
130
- /**
131
- * Set value of event store property
132
- *
133
- * @param key property name
134
- * @param v property value
135
- */
136
- function set(key, v) {
137
- getCtx()[key] = v;
138
- }
139
- return {
140
- getCtx,
141
- restoreCtx: () => (currentContext = cc),
142
- clearCtx: () => cc === currentContext ? (currentContext = null) : null,
143
- store,
144
- getStore: get,
145
- setStore: set,
146
- };
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
+ function createEventContext(data) {
20
+ const newContext = { ...data };
21
+ currentContext = newContext;
22
+ return _getCtxHelpers(newContext);
23
+ }
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
+ function useEventContext(expectedTypes) {
32
+ if (!currentContext) {
33
+ throw new Error('Event context does not exist. Use event context synchronously within the runtime of the event.');
34
+ }
35
+ const cc = currentContext;
36
+ if (expectedTypes || typeof expectedTypes === 'string') {
37
+ const type = cc.event?.type;
38
+ const types = typeof expectedTypes === 'string' ? [expectedTypes] : expectedTypes;
39
+ if (!types.includes(type))
40
+ new Error(`Event context type mismatch: expected ${types
41
+ .map((t) => `"${t}"`)
42
+ .join(', ')}, received "${type}"`);
43
+ }
44
+ return _getCtxHelpers(cc);
45
+ }
46
+ 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
+ function store(key) {
54
+ const obj = {
55
+ value: null,
56
+ hook,
57
+ init,
58
+ set: setNested,
59
+ get: getNested,
60
+ has: hasNested,
61
+ del: delNested,
62
+ entries,
63
+ clear,
64
+ };
65
+ attachHook(obj, {
66
+ set: (v) => set(key, v),
67
+ get: () => get(key),
68
+ });
69
+ function init(key2, getter) {
70
+ if (hasNested(key2))
71
+ return getNested(key2);
72
+ return setNested(key2, getter());
73
+ }
74
+ function hook(key2) {
75
+ const obj = {
76
+ value: null,
77
+ isDefined: null,
78
+ };
79
+ attachHook(obj, {
80
+ set: (v) => setNested(key2, v),
81
+ get: () => getNested(key2),
82
+ });
83
+ attachHook(obj, {
84
+ get: () => hasNested(key2),
85
+ }, 'isDefined');
86
+ return obj;
87
+ }
88
+ function setNested(key2, v) {
89
+ if (typeof obj.value === 'undefined') {
90
+ obj.value = {};
91
+ }
92
+ obj.value[key2] = v;
93
+ return v;
94
+ }
95
+ function delNested(key2) {
96
+ setNested(key2, undefined);
97
+ }
98
+ function getNested(key2) {
99
+ return (obj.value || {})[key2];
100
+ }
101
+ function hasNested(key2) {
102
+ return typeof (obj.value || {})[key2] !== 'undefined';
103
+ }
104
+ function entries() {
105
+ return Object.entries(obj.value || {});
106
+ }
107
+ function clear() {
108
+ obj.value = {};
109
+ }
110
+ return obj;
111
+ }
112
+ /**
113
+ * Get event context object
114
+ *
115
+ * @returns whole context object
116
+ */
117
+ function getCtx() {
118
+ return cc;
119
+ }
120
+ /**
121
+ * Get value of event store property
122
+ *
123
+ * @param key property name
124
+ * @returns value of property by name
125
+ */
126
+ function get(key) {
127
+ return getCtx()[key];
128
+ }
129
+ /**
130
+ * Set value of event store property
131
+ *
132
+ * @param key property name
133
+ * @param v property value
134
+ */
135
+ function set(key, v) {
136
+ getCtx()[key] = v;
137
+ }
138
+ return {
139
+ getCtx,
140
+ restoreCtx: () => (currentContext = cc),
141
+ clearCtx: () => cc === currentContext ? (currentContext = null) : null,
142
+ store,
143
+ getStore: get,
144
+ setStore: set,
145
+ };
147
146
  }
148
147
 
149
- function useRouteParams() {
150
- const { store } = useEventContext();
151
- const params = (store('routeParams').value || {});
152
- function get(name) {
153
- return params[name];
154
- }
155
- return {
156
- params,
157
- get,
158
- };
148
+ function useRouteParams() {
149
+ const { store } = useEventContext();
150
+ const params = (store('routeParams').value || {});
151
+ function get(name) {
152
+ return params[name];
153
+ }
154
+ return {
155
+ params,
156
+ get,
157
+ };
159
158
  }
160
159
 
161
- function useEventId() {
162
- const { store } = useEventContext();
163
- const { init } = store('event');
164
- const getId = () => init('id', () => randomUUID());
165
- return { getId };
160
+ function useEventId() {
161
+ const { store } = useEventContext();
162
+ const { init } = store('event');
163
+ const getId = () => init('id', () => randomUUID());
164
+ return { getId };
166
165
  }
167
166
 
168
- class EventLogger extends ProstoLogger {
169
- constructor(eventId, opts) {
170
- const _opts = opts || {
171
- level: 4,
172
- };
173
- if (!_opts.mapper) {
174
- _opts.mapper = (msg) => (Object.assign(Object.assign({}, msg), { eventId }));
175
- }
176
- if (!_opts.transports) {
177
- _opts.transports = [
178
- createConsoleTransort({
179
- format: coloredConsole,
180
- }),
181
- ];
182
- }
183
- super(_opts, (opts === null || opts === void 0 ? void 0 : opts.topic) || 'event');
184
- }
167
+ class EventLogger extends ProstoLogger {
168
+ constructor(eventId, opts) {
169
+ const _opts = opts || {
170
+ level: 4,
171
+ };
172
+ if (!_opts.mapper) {
173
+ _opts.mapper = (msg) => ({
174
+ ...msg,
175
+ eventId,
176
+ });
177
+ }
178
+ if (!_opts.transports) {
179
+ _opts.transports = [
180
+ createConsoleTransort({
181
+ format: coloredConsole,
182
+ }),
183
+ ];
184
+ }
185
+ super(_opts, opts?.topic || 'event');
186
+ }
185
187
  }
186
188
 
187
- function useEventLogger(topic) {
188
- const { getId } = useEventId();
189
- const { store, getCtx } = useEventContext();
190
- const { init } = store('event');
191
- const ctx = getCtx();
192
- const get = () => init('logger', () => { var _a; return new EventLogger(getId(), (_a = ctx.options) === null || _a === void 0 ? void 0 : _a.eventLogger); });
193
- return topic ? get().createTopic(topic) : get();
189
+ function useEventLogger(topic) {
190
+ const { getId } = useEventId();
191
+ const { store, getCtx } = useEventContext();
192
+ const { init } = store('event');
193
+ const ctx = getCtx();
194
+ const get = () => init('logger', () => new EventLogger(getId(), ctx.options?.eventLogger));
195
+ return topic ? get().createTopic(topic) : get();
194
196
  }
195
197
 
196
198
  export { EventLogger, attachHook, createEventContext, useEventContext, useEventId, useEventLogger, useRouteParams };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wooksjs/event-core",
3
- "version": "0.4.9",
3
+ "version": "0.4.11",
4
4
  "description": "@wooksjs/event-core",
5
5
  "main": "dist/index.cjs",
6
6
  "module": "dist/index.mjs",