@wooksjs/event-core 0.2.13 → 0.2.15

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
@@ -1,18 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  var crypto = require('crypto');
4
-
5
- const banner = () => `[${"@wooksjs/event-core"}][${new Date().toISOString().replace('T', ' ').replace(/\.\d{3}z$/i, '')}] `;
6
-
7
- /* istanbul ignore file */
8
- function logError(error) {
9
- console.error('' + '' + banner() + error + '');
10
- }
11
-
12
- function panic(error) {
13
- logError(error);
14
- return new Error(error);
15
- }
4
+ var logger = require('@prostojs/logger');
16
5
 
17
6
  // eslint-disable-next-line @typescript-eslint/ban-types
18
7
  function attachHook(target, opts, name) {
@@ -45,14 +34,14 @@ function createEventContext(data) {
45
34
  function useEventContext(expectedTypes) {
46
35
  var _a;
47
36
  if (!currentContext) {
48
- throw panic('Event context does not exist. Use event context synchronously within the runtime of the event.');
37
+ throw new Error('Event context does not exist. Use event context synchronously within the runtime of the event.');
49
38
  }
50
39
  const cc = currentContext;
51
40
  if (expectedTypes || typeof expectedTypes === 'string') {
52
41
  const type = (_a = cc.event) === null || _a === void 0 ? void 0 : _a.type;
53
42
  const types = typeof expectedTypes === 'string' ? [expectedTypes] : expectedTypes;
54
43
  if (!types.includes(type))
55
- panic(`Event context type mismatch: expected ${types.map(t => `"${t}"`).join(', ')}, received "${type}"`);
44
+ new Error(`Event context type mismatch: expected ${types.map(t => `"${t}"`).join(', ')}, received "${type}"`);
56
45
  }
57
46
  return _getCtxHelpers(cc);
58
47
  }
@@ -165,8 +154,38 @@ function useEventId() {
165
154
  return { getId };
166
155
  }
167
156
 
157
+ class EventLogger extends logger.ProstoLogger {
158
+ constructor(eventId, opts) {
159
+ const _opts = opts || {
160
+ level: 4,
161
+ };
162
+ if (!_opts.mapper) {
163
+ _opts.mapper = (msg) => (Object.assign(Object.assign({}, msg), { eventId }));
164
+ }
165
+ if (!_opts.transports) {
166
+ _opts.transports = [
167
+ logger.createConsoleTransort({
168
+ format: logger.coloredConsole,
169
+ }),
170
+ ];
171
+ }
172
+ super(_opts, (opts === null || opts === void 0 ? void 0 : opts.topic) || 'event');
173
+ }
174
+ }
175
+
176
+ function useEventLogger(topic) {
177
+ const { getId } = useEventId();
178
+ const { store, getCtx } = useEventContext();
179
+ const { init } = store('event');
180
+ const ctx = getCtx();
181
+ const get = () => init('logger', () => { var _a; return new EventLogger(getId(), (_a = ctx.options) === null || _a === void 0 ? void 0 : _a.eventLogger); });
182
+ return topic ? get().createTopic(topic) : get();
183
+ }
184
+
185
+ exports.EventLogger = EventLogger;
168
186
  exports.attachHook = attachHook;
169
187
  exports.createEventContext = createEventContext;
170
188
  exports.useEventContext = useEventContext;
171
189
  exports.useEventId = useEventId;
190
+ exports.useEventLogger = useEventLogger;
172
191
  exports.useRouteParams = useRouteParams;
package/dist/index.d.ts CHANGED
@@ -1,3 +1,6 @@
1
+ import { ProstoLogger } from '@prostojs/logger';
2
+ import { TProstoLoggerOptions } from '@prostojs/logger';
3
+
1
4
  export declare function attachHook<V = unknown, T extends (object | Function) = object, P extends PropertyKey = 'value'>(target: T, opts: {
2
5
  get: () => V | undefined;
3
6
  set?: (value: V) => void;
@@ -31,13 +34,29 @@ export declare function createEventContext<S extends TGenericContextStore>(data:
31
34
  setStore: <K_2 extends keyof S>(key: K_2, v: S[K_2]) => void;
32
35
  };
33
36
 
37
+ export declare class EventLogger extends ProstoLogger<TEventLoggerData> {
38
+ constructor(eventId: string, opts?: TEventOptions['eventLogger']);
39
+ }
40
+
41
+ export declare interface TEventLoggerData {
42
+ eventId: string;
43
+ }
44
+
45
+ export declare interface TEventOptions {
46
+ eventLogger?: {
47
+ topic?: string;
48
+ } & TProstoLoggerOptions<TEventLoggerData>;
49
+ }
50
+
34
51
  export declare type TGenericContextStore<E extends TGenericEvent = TGenericEvent> = {
35
52
  event: E;
53
+ options: TEventOptions;
36
54
  routeParams?: Record<string, string | string[]>;
37
55
  };
38
56
 
39
57
  export declare interface TGenericEvent {
40
58
  type: string;
59
+ logger?: EventLogger;
41
60
  id?: string;
42
61
  }
43
62
 
@@ -74,6 +93,8 @@ export declare function useEventId(): {
74
93
  getId: () => string;
75
94
  };
76
95
 
96
+ export declare function useEventLogger(topic?: string): ProstoLogger<TEventLoggerData>;
97
+
77
98
  export declare function useRouteParams<T extends object = Record<string, string | string[]>>(): {
78
99
  params: T;
79
100
  get: <K extends keyof T>(name: K) => T[K];
package/dist/index.mjs CHANGED
@@ -1,16 +1,5 @@
1
1
  import { randomUUID } from 'crypto';
2
-
3
- const banner = () => `[${"@wooksjs/event-core"}][${new Date().toISOString().replace('T', ' ').replace(/\.\d{3}z$/i, '')}] `;
4
-
5
- /* istanbul ignore file */
6
- function logError(error) {
7
- console.error('' + '' + banner() + error + '');
8
- }
9
-
10
- function panic(error) {
11
- logError(error);
12
- return new Error(error);
13
- }
2
+ import { ProstoLogger, createConsoleTransort, coloredConsole } from '@prostojs/logger';
14
3
 
15
4
  // eslint-disable-next-line @typescript-eslint/ban-types
16
5
  function attachHook(target, opts, name) {
@@ -43,14 +32,14 @@ function createEventContext(data) {
43
32
  function useEventContext(expectedTypes) {
44
33
  var _a;
45
34
  if (!currentContext) {
46
- throw panic('Event context does not exist. Use event context synchronously within the runtime of the event.');
35
+ throw new Error('Event context does not exist. Use event context synchronously within the runtime of the event.');
47
36
  }
48
37
  const cc = currentContext;
49
38
  if (expectedTypes || typeof expectedTypes === 'string') {
50
39
  const type = (_a = cc.event) === null || _a === void 0 ? void 0 : _a.type;
51
40
  const types = typeof expectedTypes === 'string' ? [expectedTypes] : expectedTypes;
52
41
  if (!types.includes(type))
53
- panic(`Event context type mismatch: expected ${types.map(t => `"${t}"`).join(', ')}, received "${type}"`);
42
+ new Error(`Event context type mismatch: expected ${types.map(t => `"${t}"`).join(', ')}, received "${type}"`);
54
43
  }
55
44
  return _getCtxHelpers(cc);
56
45
  }
@@ -163,4 +152,32 @@ function useEventId() {
163
152
  return { getId };
164
153
  }
165
154
 
166
- export { attachHook, createEventContext, useEventContext, useEventId, useRouteParams };
155
+ class EventLogger extends ProstoLogger {
156
+ constructor(eventId, opts) {
157
+ const _opts = opts || {
158
+ level: 4,
159
+ };
160
+ if (!_opts.mapper) {
161
+ _opts.mapper = (msg) => (Object.assign(Object.assign({}, msg), { eventId }));
162
+ }
163
+ if (!_opts.transports) {
164
+ _opts.transports = [
165
+ createConsoleTransort({
166
+ format: coloredConsole,
167
+ }),
168
+ ];
169
+ }
170
+ super(_opts, (opts === null || opts === void 0 ? void 0 : opts.topic) || 'event');
171
+ }
172
+ }
173
+
174
+ function useEventLogger(topic) {
175
+ const { getId } = useEventId();
176
+ const { store, getCtx } = useEventContext();
177
+ const { init } = store('event');
178
+ const ctx = getCtx();
179
+ const get = () => init('logger', () => { var _a; return new EventLogger(getId(), (_a = ctx.options) === null || _a === void 0 ? void 0 : _a.eventLogger); });
180
+ return topic ? get().createTopic(topic) : get();
181
+ }
182
+
183
+ 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.2.13",
3
+ "version": "0.2.15",
4
4
  "description": "@wooksjs/event-core",
5
5
  "main": "dist/index.cjs",
6
6
  "module": "dist/index.mjs",
@@ -30,5 +30,8 @@
30
30
  "bugs": {
31
31
  "url": "https://github.com/wooksjs/wooksjs/issues"
32
32
  },
33
+ "dependencies": {
34
+ "@prostojs/logger": "^0.3.3"
35
+ },
33
36
  "homepage": "https://github.com/wooksjs/wooksjs/tree/main/packages/event-core#readme"
34
37
  }