@wooksjs/event-core 0.4.26 → 0.4.27

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 CHANGED
@@ -23,12 +23,13 @@ function useEventContext(expectedTypes) {
23
23
  }
24
24
  const cc = currentContext;
25
25
  if (expectedTypes || typeof expectedTypes === 'string') {
26
- const type = cc.event?.type;
26
+ const type = cc.event.type;
27
27
  const types = typeof expectedTypes === 'string' ? [expectedTypes] : expectedTypes;
28
- if (!types.includes(type))
29
- new Error(`Event context type mismatch: expected ${types
30
- .map((t) => `"${t}"`)
28
+ if (!types.includes(type)) {
29
+ throw new Error(`Event context type mismatch: expected ${types
30
+ .map(t => `"${t}"`)
31
31
  .join(', ')}, received "${type}"`);
32
+ }
32
33
  }
33
34
  return _getCtxHelpers(cc);
34
35
  }
@@ -46,12 +47,15 @@ function _getCtxHelpers(cc) {
46
47
  clear,
47
48
  };
48
49
  attachHook(obj, {
49
- set: (v) => set(key, v),
50
+ set: v => {
51
+ set(key, v);
52
+ },
50
53
  get: () => get(key),
51
54
  });
52
55
  function init(key2, getter) {
53
- if (hasNested(key2))
56
+ if (hasNested(key2)) {
54
57
  return getNested(key2);
58
+ }
55
59
  return setNested(key2, getter());
56
60
  }
57
61
  function hook(key2) {
@@ -60,7 +64,7 @@ function _getCtxHelpers(cc) {
60
64
  isDefined: null,
61
65
  };
62
66
  attachHook(obj, {
63
- set: (v) => setNested(key2, v),
67
+ set: v => setNested(key2, v),
64
68
  get: () => getNested(key2),
65
69
  });
66
70
  attachHook(obj, {
@@ -69,7 +73,7 @@ function _getCtxHelpers(cc) {
69
73
  return obj;
70
74
  }
71
75
  function setNested(key2, v) {
72
- if (typeof obj.value === 'undefined') {
76
+ if (obj.value === undefined) {
73
77
  obj.value = {};
74
78
  }
75
79
  obj.value[key2] = v;
@@ -82,7 +86,7 @@ function _getCtxHelpers(cc) {
82
86
  return (obj.value || {})[key2];
83
87
  }
84
88
  function hasNested(key2) {
85
- return typeof (obj.value || {})[key2] !== 'undefined';
89
+ return (obj.value || {})[key2] !== undefined;
86
90
  }
87
91
  function entries() {
88
92
  return Object.entries(obj.value || {});
@@ -104,25 +108,13 @@ function _getCtxHelpers(cc) {
104
108
  return {
105
109
  getCtx,
106
110
  restoreCtx: () => (currentContext = cc),
107
- clearCtx: () => cc === currentContext ? (currentContext = null) : null,
111
+ clearCtx: () => (cc === currentContext ? (currentContext = null) : null),
108
112
  store,
109
113
  getStore: get,
110
114
  setStore: set,
111
115
  };
112
116
  }
113
117
 
114
- function useRouteParams() {
115
- const { store } = useEventContext();
116
- const params = (store('routeParams').value || {});
117
- function get(name) {
118
- return params[name];
119
- }
120
- return {
121
- params,
122
- get,
123
- };
124
- }
125
-
126
118
  function useEventId() {
127
119
  const { store } = useEventContext();
128
120
  const { init } = store('event');
@@ -136,7 +128,7 @@ class EventLogger extends logger.ProstoLogger {
136
128
  level: 4,
137
129
  };
138
130
  if (!_opts.mapper) {
139
- _opts.mapper = (msg) => ({
131
+ _opts.mapper = msg => ({
140
132
  ...msg,
141
133
  eventId,
142
134
  });
@@ -157,10 +149,22 @@ function useEventLogger(topic) {
157
149
  const { store, getCtx } = useEventContext();
158
150
  const { init } = store('event');
159
151
  const ctx = getCtx();
160
- const get = () => init('logger', () => new EventLogger(getId(), ctx.options?.eventLogger));
152
+ const get = () => init('logger', () => new EventLogger(getId(), ctx.options.eventLogger));
161
153
  return topic ? get().createTopic(topic) : get();
162
154
  }
163
155
 
156
+ function useRouteParams() {
157
+ const { store } = useEventContext();
158
+ const params = (store('routeParams').value || {});
159
+ function get(name) {
160
+ return params[name];
161
+ }
162
+ return {
163
+ params,
164
+ get,
165
+ };
166
+ }
167
+
164
168
  exports.EventLogger = EventLogger;
165
169
  exports.attachHook = attachHook;
166
170
  exports.createEventContext = createEventContext;
package/dist/index.d.ts CHANGED
@@ -1,12 +1,9 @@
1
1
  import * as _prostojs_logger from '@prostojs/logger';
2
- import { ProstoLogger, TProstoLoggerOptions } from '@prostojs/logger';
2
+ import { TProstoLoggerOptions, ProstoLogger } from '@prostojs/logger';
3
3
 
4
- interface TEventLoggerData {
5
- eventId: string;
6
- }
7
- declare class EventLogger extends ProstoLogger<TEventLoggerData> {
8
- constructor(eventId: string, opts?: TEventOptions['eventLogger']);
9
- }
4
+ declare function useEventId(): {
5
+ getId: () => string;
6
+ };
10
7
 
11
8
  interface TGenericEvent {
12
9
  type: string;
@@ -21,11 +18,11 @@ interface TEventOptions {
21
18
  topic?: string;
22
19
  } & TProstoLoggerOptions<TEventLoggerData>;
23
20
  }
24
- type TGenericContextStore<CustomEventType = TEmpty> = {
21
+ interface TGenericContextStore<CustomEventType = TEmpty> {
25
22
  event: CustomEventType & TGenericEvent;
26
23
  options: TEventOptions;
27
24
  routeParams?: Record<string, string | string[]>;
28
- };
25
+ }
29
26
  /**
30
27
  * Create a new event context
31
28
  *
@@ -37,7 +34,7 @@ declare function createEventContext<S = TEmpty, EventTypeToCreate = TEmpty>(data
37
34
  restoreCtx: () => TGenericContextStore<TEmpty>;
38
35
  clearCtx: () => null;
39
36
  store: <K extends keyof S | keyof TGenericContextStore<EventTypeToCreate>>(key: K) => {
40
- value: (S & TGenericContextStore<EventTypeToCreate>)[K];
37
+ value: (S & TGenericContextStore<EventTypeToCreate>)[K] | undefined;
41
38
  hook: <K2 extends keyof Required<S & TGenericContextStore<EventTypeToCreate>>[K]>(key2: K2) => {
42
39
  value: Required<S & TGenericContextStore<EventTypeToCreate>>[K][K2];
43
40
  isDefined: boolean;
@@ -65,7 +62,7 @@ declare function useEventContext<S = TEmpty, EventType = TEmpty>(expectedTypes?:
65
62
  restoreCtx: () => TGenericContextStore<TEmpty>;
66
63
  clearCtx: () => null;
67
64
  store: <K extends keyof S | keyof TGenericContextStore<EventType>>(key: K) => {
68
- value: (S & TGenericContextStore<EventType>)[K];
65
+ value: (S & TGenericContextStore<EventType>)[K] | undefined;
69
66
  hook: <K2 extends keyof Required<S & TGenericContextStore<EventType>>[K]>(key2: K2) => {
70
67
  value: Required<S & TGenericContextStore<EventType>>[K][K2];
71
68
  isDefined: boolean;
@@ -82,23 +79,26 @@ declare function useEventContext<S = TEmpty, EventType = TEmpty>(expectedTypes?:
82
79
  setStore: <K_2 extends keyof S | keyof TGenericContextStore<EventType>>(key: K_2, v: (S & TGenericContextStore<EventType>)[K_2]) => void;
83
80
  };
84
81
 
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
- };
82
+ interface TEventLoggerData {
83
+ eventId: string;
84
+ }
85
+ declare class EventLogger extends ProstoLogger<TEventLoggerData> {
86
+ constructor(eventId: string, opts?: TEventOptions['eventLogger']);
87
+ }
88
+
89
+ declare function useEventLogger(topic?: string): _prostojs_logger.ProstoLogger<TEventLoggerData>;
92
90
 
93
91
  declare function useRouteParams<T extends object = Record<string, string | string[]>>(): {
94
92
  params: T;
95
93
  get: <K extends keyof T>(name: K) => T[K];
96
94
  };
97
95
 
98
- declare function useEventId(): {
99
- getId: () => string;
96
+ declare function attachHook<V = unknown, T extends object | Function = object, P extends PropertyKey = 'value'>(target: T, opts: {
97
+ get: () => V | undefined;
98
+ set?: (value: V) => void;
99
+ }, name?: P): T & THook<V, P>;
100
+ type THook<T = string, K extends PropertyKey = 'value'> = {
101
+ [key in K]: T;
100
102
  };
101
103
 
102
- declare function useEventLogger(topic?: string): _prostojs_logger.ProstoLogger<TEventLoggerData>;
103
-
104
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
@@ -21,12 +21,13 @@ function useEventContext(expectedTypes) {
21
21
  }
22
22
  const cc = currentContext;
23
23
  if (expectedTypes || typeof expectedTypes === 'string') {
24
- const type = cc.event?.type;
24
+ const type = cc.event.type;
25
25
  const types = typeof expectedTypes === 'string' ? [expectedTypes] : expectedTypes;
26
- if (!types.includes(type))
27
- new Error(`Event context type mismatch: expected ${types
28
- .map((t) => `"${t}"`)
26
+ if (!types.includes(type)) {
27
+ throw new Error(`Event context type mismatch: expected ${types
28
+ .map(t => `"${t}"`)
29
29
  .join(', ')}, received "${type}"`);
30
+ }
30
31
  }
31
32
  return _getCtxHelpers(cc);
32
33
  }
@@ -44,12 +45,15 @@ function _getCtxHelpers(cc) {
44
45
  clear,
45
46
  };
46
47
  attachHook(obj, {
47
- set: (v) => set(key, v),
48
+ set: v => {
49
+ set(key, v);
50
+ },
48
51
  get: () => get(key),
49
52
  });
50
53
  function init(key2, getter) {
51
- if (hasNested(key2))
54
+ if (hasNested(key2)) {
52
55
  return getNested(key2);
56
+ }
53
57
  return setNested(key2, getter());
54
58
  }
55
59
  function hook(key2) {
@@ -58,7 +62,7 @@ function _getCtxHelpers(cc) {
58
62
  isDefined: null,
59
63
  };
60
64
  attachHook(obj, {
61
- set: (v) => setNested(key2, v),
65
+ set: v => setNested(key2, v),
62
66
  get: () => getNested(key2),
63
67
  });
64
68
  attachHook(obj, {
@@ -67,7 +71,7 @@ function _getCtxHelpers(cc) {
67
71
  return obj;
68
72
  }
69
73
  function setNested(key2, v) {
70
- if (typeof obj.value === 'undefined') {
74
+ if (obj.value === undefined) {
71
75
  obj.value = {};
72
76
  }
73
77
  obj.value[key2] = v;
@@ -80,7 +84,7 @@ function _getCtxHelpers(cc) {
80
84
  return (obj.value || {})[key2];
81
85
  }
82
86
  function hasNested(key2) {
83
- return typeof (obj.value || {})[key2] !== 'undefined';
87
+ return (obj.value || {})[key2] !== undefined;
84
88
  }
85
89
  function entries() {
86
90
  return Object.entries(obj.value || {});
@@ -102,25 +106,13 @@ function _getCtxHelpers(cc) {
102
106
  return {
103
107
  getCtx,
104
108
  restoreCtx: () => (currentContext = cc),
105
- clearCtx: () => cc === currentContext ? (currentContext = null) : null,
109
+ clearCtx: () => (cc === currentContext ? (currentContext = null) : null),
106
110
  store,
107
111
  getStore: get,
108
112
  setStore: set,
109
113
  };
110
114
  }
111
115
 
112
- function useRouteParams() {
113
- const { store } = useEventContext();
114
- const params = (store('routeParams').value || {});
115
- function get(name) {
116
- return params[name];
117
- }
118
- return {
119
- params,
120
- get,
121
- };
122
- }
123
-
124
116
  function useEventId() {
125
117
  const { store } = useEventContext();
126
118
  const { init } = store('event');
@@ -134,7 +126,7 @@ class EventLogger extends ProstoLogger {
134
126
  level: 4,
135
127
  };
136
128
  if (!_opts.mapper) {
137
- _opts.mapper = (msg) => ({
129
+ _opts.mapper = msg => ({
138
130
  ...msg,
139
131
  eventId,
140
132
  });
@@ -155,8 +147,20 @@ function useEventLogger(topic) {
155
147
  const { store, getCtx } = useEventContext();
156
148
  const { init } = store('event');
157
149
  const ctx = getCtx();
158
- const get = () => init('logger', () => new EventLogger(getId(), ctx.options?.eventLogger));
150
+ const get = () => init('logger', () => new EventLogger(getId(), ctx.options.eventLogger));
159
151
  return topic ? get().createTopic(topic) : get();
160
152
  }
161
153
 
154
+ function useRouteParams() {
155
+ const { store } = useEventContext();
156
+ const params = (store('routeParams').value || {});
157
+ function get(name) {
158
+ return params[name];
159
+ }
160
+ return {
161
+ params,
162
+ get,
163
+ };
164
+ }
165
+
162
166
  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.26",
3
+ "version": "0.4.27",
4
4
  "description": "@wooksjs/event-core",
5
5
  "main": "dist/index.cjs",
6
6
  "module": "dist/index.mjs",