hookified 1.12.2 → 1.14.0
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/README.md +139 -13
- package/dist/browser/index.global.js +103 -14
- package/dist/browser/index.global.js.map +1 -1
- package/dist/browser/index.js +103 -14
- package/dist/browser/index.js.map +1 -1
- package/dist/node/index.cjs +103 -14
- package/dist/node/index.d.cts +83 -43
- package/dist/node/index.d.ts +83 -43
- package/dist/node/index.js +103 -14
- package/package.json +25 -23
package/dist/node/index.d.cts
CHANGED
|
@@ -6,7 +6,21 @@ type Logger = {
|
|
|
6
6
|
error: (message: string, ...arguments_: unknown[]) => void;
|
|
7
7
|
fatal: (message: string, ...arguments_: unknown[]) => void;
|
|
8
8
|
};
|
|
9
|
-
|
|
9
|
+
type EventEmitterOptions = {
|
|
10
|
+
/**
|
|
11
|
+
* Logger instance for logging errors.
|
|
12
|
+
*/
|
|
13
|
+
logger?: Logger;
|
|
14
|
+
/**
|
|
15
|
+
* Whether to throw an error when emit 'error' and there are no listeners. Default is false and only emits an error event.
|
|
16
|
+
*/
|
|
17
|
+
throwOnEmitError?: boolean;
|
|
18
|
+
/**
|
|
19
|
+
* Whether to throw on 'error' when there are no listeners. This is the standard functionality in EventEmitter
|
|
20
|
+
* @default false - in v2 this will be set to true by default
|
|
21
|
+
*/
|
|
22
|
+
throwOnEmptyListeners?: boolean;
|
|
23
|
+
};
|
|
10
24
|
type IEventEmitter = {
|
|
11
25
|
/**
|
|
12
26
|
* Registers a listener for the specified event.
|
|
@@ -152,21 +166,54 @@ type IEventEmitter = {
|
|
|
152
166
|
prependOnceListener(eventName: string | symbol, listener: (...arguments_: any[]) => void): IEventEmitter;
|
|
153
167
|
};
|
|
154
168
|
type EventListener = (...arguments_: any[]) => void;
|
|
155
|
-
type
|
|
169
|
+
type Hook = (...arguments_: any[]) => Promise<void> | void;
|
|
170
|
+
type HookEntry = {
|
|
156
171
|
/**
|
|
157
|
-
*
|
|
172
|
+
* The event name for the hook
|
|
158
173
|
*/
|
|
159
|
-
|
|
174
|
+
event: string;
|
|
160
175
|
/**
|
|
161
|
-
*
|
|
176
|
+
* The handler function for the hook
|
|
162
177
|
*/
|
|
163
|
-
|
|
178
|
+
handler: Hook;
|
|
164
179
|
};
|
|
180
|
+
type HookifiedOptions = {
|
|
181
|
+
/**
|
|
182
|
+
* Whether an error should be thrown when a hook throws an error. Default is false and only emits an error event.
|
|
183
|
+
* @deprecated - this will be deprecated in version 2. Please use throwOnHookError.
|
|
184
|
+
*/
|
|
185
|
+
throwHookErrors?: boolean;
|
|
186
|
+
/**
|
|
187
|
+
* Whether an error should be thrown when a hook throws an error. Default is false and only emits an error event.
|
|
188
|
+
*/
|
|
189
|
+
throwOnHookError?: boolean;
|
|
190
|
+
/**
|
|
191
|
+
* Whether to enforce that all hook names start with 'before' or 'after'. Default is false.
|
|
192
|
+
* @type {boolean}
|
|
193
|
+
* @default false
|
|
194
|
+
*/
|
|
195
|
+
enforceBeforeAfter?: boolean;
|
|
196
|
+
/**
|
|
197
|
+
* Map of deprecated hook names to deprecation messages. When a deprecated hook is used, a warning will be emitted.
|
|
198
|
+
* @type {Map<string, string>}
|
|
199
|
+
* @default new Map()
|
|
200
|
+
*/
|
|
201
|
+
deprecatedHooks?: Map<string, string>;
|
|
202
|
+
/**
|
|
203
|
+
* Whether to allow deprecated hooks to be registered and executed. Default is true.
|
|
204
|
+
* @type {boolean}
|
|
205
|
+
* @default true
|
|
206
|
+
*/
|
|
207
|
+
allowDeprecated?: boolean;
|
|
208
|
+
} & EventEmitterOptions;
|
|
209
|
+
|
|
165
210
|
declare class Eventified implements IEventEmitter {
|
|
166
211
|
private readonly _eventListeners;
|
|
167
212
|
private _maxListeners;
|
|
168
213
|
private _logger?;
|
|
169
214
|
private _throwOnEmitError;
|
|
215
|
+
private _throwOnEmptyListeners;
|
|
216
|
+
private _errorEvent;
|
|
170
217
|
constructor(options?: EventEmitterOptions);
|
|
171
218
|
/**
|
|
172
219
|
* Gets the logger
|
|
@@ -188,6 +235,16 @@ declare class Eventified implements IEventEmitter {
|
|
|
188
235
|
* @param {boolean} value
|
|
189
236
|
*/
|
|
190
237
|
set throwOnEmitError(value: boolean);
|
|
238
|
+
/**
|
|
239
|
+
* Gets whether an error should be thrown when emitting 'error' event with no listeners. Default is false.
|
|
240
|
+
* @returns {boolean}
|
|
241
|
+
*/
|
|
242
|
+
get throwOnEmptyListeners(): boolean;
|
|
243
|
+
/**
|
|
244
|
+
* Sets whether an error should be thrown when emitting 'error' event with no listeners. Default is false.
|
|
245
|
+
* @param {boolean} value
|
|
246
|
+
*/
|
|
247
|
+
set throwOnEmptyListeners(value: boolean);
|
|
191
248
|
/**
|
|
192
249
|
* Adds a handler function for a specific event that will run only once
|
|
193
250
|
* @param {string | symbol} eventName
|
|
@@ -289,46 +346,17 @@ declare class Eventified implements IEventEmitter {
|
|
|
289
346
|
* @returns {EventListener[]} An array of listeners
|
|
290
347
|
*/
|
|
291
348
|
getAllListeners(): EventListener[];
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
type Hook = (...arguments_: any[]) => Promise<void> | void;
|
|
295
|
-
type HookEntry = {
|
|
296
|
-
/**
|
|
297
|
-
* The event name for the hook
|
|
298
|
-
*/
|
|
299
|
-
event: string;
|
|
300
349
|
/**
|
|
301
|
-
*
|
|
350
|
+
* Sends a log message using the configured logger based on the event name
|
|
351
|
+
* @param {string | symbol} eventName - The event name that determines the log level
|
|
352
|
+
* @param {unknown} data - The data to log
|
|
302
353
|
*/
|
|
303
|
-
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
/**
|
|
307
|
-
* Whether an error should be thrown when a hook throws an error. Default is false and only emits an error event.
|
|
308
|
-
*/
|
|
309
|
-
throwHookErrors?: boolean;
|
|
310
|
-
/**
|
|
311
|
-
* Whether to enforce that all hook names start with 'before' or 'after'. Default is false.
|
|
312
|
-
* @type {boolean}
|
|
313
|
-
* @default false
|
|
314
|
-
*/
|
|
315
|
-
enforceBeforeAfter?: boolean;
|
|
316
|
-
/**
|
|
317
|
-
* Map of deprecated hook names to deprecation messages. When a deprecated hook is used, a warning will be emitted.
|
|
318
|
-
* @type {Map<string, string>}
|
|
319
|
-
* @default new Map()
|
|
320
|
-
*/
|
|
321
|
-
deprecatedHooks?: Map<string, string>;
|
|
322
|
-
/**
|
|
323
|
-
* Whether to allow deprecated hooks to be registered and executed. Default is true.
|
|
324
|
-
* @type {boolean}
|
|
325
|
-
* @default true
|
|
326
|
-
*/
|
|
327
|
-
allowDeprecated?: boolean;
|
|
328
|
-
} & EventEmitterOptions;
|
|
354
|
+
private sendLog;
|
|
355
|
+
}
|
|
356
|
+
|
|
329
357
|
declare class Hookified extends Eventified {
|
|
330
358
|
private readonly _hooks;
|
|
331
|
-
private
|
|
359
|
+
private _throwOnHookError;
|
|
332
360
|
private _enforceBeforeAfter;
|
|
333
361
|
private _deprecatedHooks;
|
|
334
362
|
private _allowDeprecated;
|
|
@@ -341,13 +369,25 @@ declare class Hookified extends Eventified {
|
|
|
341
369
|
/**
|
|
342
370
|
* Gets whether an error should be thrown when a hook throws an error. Default is false and only emits an error event.
|
|
343
371
|
* @returns {boolean}
|
|
372
|
+
* @deprecated - this will be deprecated in version 2. Please use throwOnHookError.
|
|
344
373
|
*/
|
|
345
374
|
get throwHookErrors(): boolean;
|
|
346
375
|
/**
|
|
347
376
|
* Sets whether an error should be thrown when a hook throws an error. Default is false and only emits an error event.
|
|
348
377
|
* @param {boolean} value
|
|
378
|
+
* @deprecated - this will be deprecated in version 2. Please use throwOnHookError.
|
|
349
379
|
*/
|
|
350
380
|
set throwHookErrors(value: boolean);
|
|
381
|
+
/**
|
|
382
|
+
* Gets whether an error should be thrown when a hook throws an error. Default is false and only emits an error event.
|
|
383
|
+
* @returns {boolean}
|
|
384
|
+
*/
|
|
385
|
+
get throwOnHookError(): boolean;
|
|
386
|
+
/**
|
|
387
|
+
* Sets whether an error should be thrown when a hook throws an error. Default is false and only emits an error event.
|
|
388
|
+
* @param {boolean} value
|
|
389
|
+
*/
|
|
390
|
+
set throwOnHookError(value: boolean);
|
|
351
391
|
/**
|
|
352
392
|
* Gets whether to enforce that all hook names start with 'before' or 'after'. Default is false.
|
|
353
393
|
* @returns {boolean}
|
|
@@ -489,4 +529,4 @@ declare class Hookified extends Eventified {
|
|
|
489
529
|
clearHooks(): void;
|
|
490
530
|
}
|
|
491
531
|
|
|
492
|
-
export { type EventListener, Eventified, type Hook, type HookEntry, Hookified, type HookifiedOptions, type Logger };
|
|
532
|
+
export { type EventEmitterOptions, type EventListener, Eventified, type Hook, type HookEntry, Hookified, type HookifiedOptions, type IEventEmitter, type Logger };
|
package/dist/node/index.d.ts
CHANGED
|
@@ -6,7 +6,21 @@ type Logger = {
|
|
|
6
6
|
error: (message: string, ...arguments_: unknown[]) => void;
|
|
7
7
|
fatal: (message: string, ...arguments_: unknown[]) => void;
|
|
8
8
|
};
|
|
9
|
-
|
|
9
|
+
type EventEmitterOptions = {
|
|
10
|
+
/**
|
|
11
|
+
* Logger instance for logging errors.
|
|
12
|
+
*/
|
|
13
|
+
logger?: Logger;
|
|
14
|
+
/**
|
|
15
|
+
* Whether to throw an error when emit 'error' and there are no listeners. Default is false and only emits an error event.
|
|
16
|
+
*/
|
|
17
|
+
throwOnEmitError?: boolean;
|
|
18
|
+
/**
|
|
19
|
+
* Whether to throw on 'error' when there are no listeners. This is the standard functionality in EventEmitter
|
|
20
|
+
* @default false - in v2 this will be set to true by default
|
|
21
|
+
*/
|
|
22
|
+
throwOnEmptyListeners?: boolean;
|
|
23
|
+
};
|
|
10
24
|
type IEventEmitter = {
|
|
11
25
|
/**
|
|
12
26
|
* Registers a listener for the specified event.
|
|
@@ -152,21 +166,54 @@ type IEventEmitter = {
|
|
|
152
166
|
prependOnceListener(eventName: string | symbol, listener: (...arguments_: any[]) => void): IEventEmitter;
|
|
153
167
|
};
|
|
154
168
|
type EventListener = (...arguments_: any[]) => void;
|
|
155
|
-
type
|
|
169
|
+
type Hook = (...arguments_: any[]) => Promise<void> | void;
|
|
170
|
+
type HookEntry = {
|
|
156
171
|
/**
|
|
157
|
-
*
|
|
172
|
+
* The event name for the hook
|
|
158
173
|
*/
|
|
159
|
-
|
|
174
|
+
event: string;
|
|
160
175
|
/**
|
|
161
|
-
*
|
|
176
|
+
* The handler function for the hook
|
|
162
177
|
*/
|
|
163
|
-
|
|
178
|
+
handler: Hook;
|
|
164
179
|
};
|
|
180
|
+
type HookifiedOptions = {
|
|
181
|
+
/**
|
|
182
|
+
* Whether an error should be thrown when a hook throws an error. Default is false and only emits an error event.
|
|
183
|
+
* @deprecated - this will be deprecated in version 2. Please use throwOnHookError.
|
|
184
|
+
*/
|
|
185
|
+
throwHookErrors?: boolean;
|
|
186
|
+
/**
|
|
187
|
+
* Whether an error should be thrown when a hook throws an error. Default is false and only emits an error event.
|
|
188
|
+
*/
|
|
189
|
+
throwOnHookError?: boolean;
|
|
190
|
+
/**
|
|
191
|
+
* Whether to enforce that all hook names start with 'before' or 'after'. Default is false.
|
|
192
|
+
* @type {boolean}
|
|
193
|
+
* @default false
|
|
194
|
+
*/
|
|
195
|
+
enforceBeforeAfter?: boolean;
|
|
196
|
+
/**
|
|
197
|
+
* Map of deprecated hook names to deprecation messages. When a deprecated hook is used, a warning will be emitted.
|
|
198
|
+
* @type {Map<string, string>}
|
|
199
|
+
* @default new Map()
|
|
200
|
+
*/
|
|
201
|
+
deprecatedHooks?: Map<string, string>;
|
|
202
|
+
/**
|
|
203
|
+
* Whether to allow deprecated hooks to be registered and executed. Default is true.
|
|
204
|
+
* @type {boolean}
|
|
205
|
+
* @default true
|
|
206
|
+
*/
|
|
207
|
+
allowDeprecated?: boolean;
|
|
208
|
+
} & EventEmitterOptions;
|
|
209
|
+
|
|
165
210
|
declare class Eventified implements IEventEmitter {
|
|
166
211
|
private readonly _eventListeners;
|
|
167
212
|
private _maxListeners;
|
|
168
213
|
private _logger?;
|
|
169
214
|
private _throwOnEmitError;
|
|
215
|
+
private _throwOnEmptyListeners;
|
|
216
|
+
private _errorEvent;
|
|
170
217
|
constructor(options?: EventEmitterOptions);
|
|
171
218
|
/**
|
|
172
219
|
* Gets the logger
|
|
@@ -188,6 +235,16 @@ declare class Eventified implements IEventEmitter {
|
|
|
188
235
|
* @param {boolean} value
|
|
189
236
|
*/
|
|
190
237
|
set throwOnEmitError(value: boolean);
|
|
238
|
+
/**
|
|
239
|
+
* Gets whether an error should be thrown when emitting 'error' event with no listeners. Default is false.
|
|
240
|
+
* @returns {boolean}
|
|
241
|
+
*/
|
|
242
|
+
get throwOnEmptyListeners(): boolean;
|
|
243
|
+
/**
|
|
244
|
+
* Sets whether an error should be thrown when emitting 'error' event with no listeners. Default is false.
|
|
245
|
+
* @param {boolean} value
|
|
246
|
+
*/
|
|
247
|
+
set throwOnEmptyListeners(value: boolean);
|
|
191
248
|
/**
|
|
192
249
|
* Adds a handler function for a specific event that will run only once
|
|
193
250
|
* @param {string | symbol} eventName
|
|
@@ -289,46 +346,17 @@ declare class Eventified implements IEventEmitter {
|
|
|
289
346
|
* @returns {EventListener[]} An array of listeners
|
|
290
347
|
*/
|
|
291
348
|
getAllListeners(): EventListener[];
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
type Hook = (...arguments_: any[]) => Promise<void> | void;
|
|
295
|
-
type HookEntry = {
|
|
296
|
-
/**
|
|
297
|
-
* The event name for the hook
|
|
298
|
-
*/
|
|
299
|
-
event: string;
|
|
300
349
|
/**
|
|
301
|
-
*
|
|
350
|
+
* Sends a log message using the configured logger based on the event name
|
|
351
|
+
* @param {string | symbol} eventName - The event name that determines the log level
|
|
352
|
+
* @param {unknown} data - The data to log
|
|
302
353
|
*/
|
|
303
|
-
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
/**
|
|
307
|
-
* Whether an error should be thrown when a hook throws an error. Default is false and only emits an error event.
|
|
308
|
-
*/
|
|
309
|
-
throwHookErrors?: boolean;
|
|
310
|
-
/**
|
|
311
|
-
* Whether to enforce that all hook names start with 'before' or 'after'. Default is false.
|
|
312
|
-
* @type {boolean}
|
|
313
|
-
* @default false
|
|
314
|
-
*/
|
|
315
|
-
enforceBeforeAfter?: boolean;
|
|
316
|
-
/**
|
|
317
|
-
* Map of deprecated hook names to deprecation messages. When a deprecated hook is used, a warning will be emitted.
|
|
318
|
-
* @type {Map<string, string>}
|
|
319
|
-
* @default new Map()
|
|
320
|
-
*/
|
|
321
|
-
deprecatedHooks?: Map<string, string>;
|
|
322
|
-
/**
|
|
323
|
-
* Whether to allow deprecated hooks to be registered and executed. Default is true.
|
|
324
|
-
* @type {boolean}
|
|
325
|
-
* @default true
|
|
326
|
-
*/
|
|
327
|
-
allowDeprecated?: boolean;
|
|
328
|
-
} & EventEmitterOptions;
|
|
354
|
+
private sendLog;
|
|
355
|
+
}
|
|
356
|
+
|
|
329
357
|
declare class Hookified extends Eventified {
|
|
330
358
|
private readonly _hooks;
|
|
331
|
-
private
|
|
359
|
+
private _throwOnHookError;
|
|
332
360
|
private _enforceBeforeAfter;
|
|
333
361
|
private _deprecatedHooks;
|
|
334
362
|
private _allowDeprecated;
|
|
@@ -341,13 +369,25 @@ declare class Hookified extends Eventified {
|
|
|
341
369
|
/**
|
|
342
370
|
* Gets whether an error should be thrown when a hook throws an error. Default is false and only emits an error event.
|
|
343
371
|
* @returns {boolean}
|
|
372
|
+
* @deprecated - this will be deprecated in version 2. Please use throwOnHookError.
|
|
344
373
|
*/
|
|
345
374
|
get throwHookErrors(): boolean;
|
|
346
375
|
/**
|
|
347
376
|
* Sets whether an error should be thrown when a hook throws an error. Default is false and only emits an error event.
|
|
348
377
|
* @param {boolean} value
|
|
378
|
+
* @deprecated - this will be deprecated in version 2. Please use throwOnHookError.
|
|
349
379
|
*/
|
|
350
380
|
set throwHookErrors(value: boolean);
|
|
381
|
+
/**
|
|
382
|
+
* Gets whether an error should be thrown when a hook throws an error. Default is false and only emits an error event.
|
|
383
|
+
* @returns {boolean}
|
|
384
|
+
*/
|
|
385
|
+
get throwOnHookError(): boolean;
|
|
386
|
+
/**
|
|
387
|
+
* Sets whether an error should be thrown when a hook throws an error. Default is false and only emits an error event.
|
|
388
|
+
* @param {boolean} value
|
|
389
|
+
*/
|
|
390
|
+
set throwOnHookError(value: boolean);
|
|
351
391
|
/**
|
|
352
392
|
* Gets whether to enforce that all hook names start with 'before' or 'after'. Default is false.
|
|
353
393
|
* @returns {boolean}
|
|
@@ -489,4 +529,4 @@ declare class Hookified extends Eventified {
|
|
|
489
529
|
clearHooks(): void;
|
|
490
530
|
}
|
|
491
531
|
|
|
492
|
-
export { type EventListener, Eventified, type Hook, type HookEntry, Hookified, type HookifiedOptions, type Logger };
|
|
532
|
+
export { type EventEmitterOptions, type EventListener, Eventified, type Hook, type HookEntry, Hookified, type HookifiedOptions, type IEventEmitter, type Logger };
|
package/dist/node/index.js
CHANGED
|
@@ -4,6 +4,8 @@ var Eventified = class {
|
|
|
4
4
|
_maxListeners;
|
|
5
5
|
_logger;
|
|
6
6
|
_throwOnEmitError = false;
|
|
7
|
+
_throwOnEmptyListeners = false;
|
|
8
|
+
_errorEvent = "error";
|
|
7
9
|
constructor(options) {
|
|
8
10
|
this._eventListeners = /* @__PURE__ */ new Map();
|
|
9
11
|
this._maxListeners = 100;
|
|
@@ -11,6 +13,9 @@ var Eventified = class {
|
|
|
11
13
|
if (options?.throwOnEmitError !== void 0) {
|
|
12
14
|
this._throwOnEmitError = options.throwOnEmitError;
|
|
13
15
|
}
|
|
16
|
+
if (options?.throwOnEmptyListeners !== void 0) {
|
|
17
|
+
this._throwOnEmptyListeners = options.throwOnEmptyListeners;
|
|
18
|
+
}
|
|
14
19
|
}
|
|
15
20
|
/**
|
|
16
21
|
* Gets the logger
|
|
@@ -40,6 +45,20 @@ var Eventified = class {
|
|
|
40
45
|
set throwOnEmitError(value) {
|
|
41
46
|
this._throwOnEmitError = value;
|
|
42
47
|
}
|
|
48
|
+
/**
|
|
49
|
+
* Gets whether an error should be thrown when emitting 'error' event with no listeners. Default is false.
|
|
50
|
+
* @returns {boolean}
|
|
51
|
+
*/
|
|
52
|
+
get throwOnEmptyListeners() {
|
|
53
|
+
return this._throwOnEmptyListeners;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Sets whether an error should be thrown when emitting 'error' event with no listeners. Default is false.
|
|
57
|
+
* @param {boolean} value
|
|
58
|
+
*/
|
|
59
|
+
set throwOnEmptyListeners(value) {
|
|
60
|
+
this._throwOnEmptyListeners = value;
|
|
61
|
+
}
|
|
43
62
|
/**
|
|
44
63
|
* Adds a handler function for a specific event that will run only once
|
|
45
64
|
* @param {string | symbol} eventName
|
|
@@ -190,12 +209,17 @@ var Eventified = class {
|
|
|
190
209
|
result = true;
|
|
191
210
|
}
|
|
192
211
|
}
|
|
193
|
-
if (event ===
|
|
212
|
+
if (event === this._errorEvent) {
|
|
194
213
|
const error = arguments_[0] instanceof Error ? arguments_[0] : new Error(`${arguments_[0]}`);
|
|
195
214
|
if (this._throwOnEmitError && !result) {
|
|
196
215
|
throw error;
|
|
216
|
+
} else {
|
|
217
|
+
if (this.listeners(this._errorEvent).length === 0 && this._throwOnEmptyListeners === true) {
|
|
218
|
+
throw error;
|
|
219
|
+
}
|
|
197
220
|
}
|
|
198
221
|
}
|
|
222
|
+
this.sendLog(event, arguments_);
|
|
199
223
|
return result;
|
|
200
224
|
}
|
|
201
225
|
/**
|
|
@@ -243,21 +267,75 @@ var Eventified = class {
|
|
|
243
267
|
}
|
|
244
268
|
return result;
|
|
245
269
|
}
|
|
270
|
+
/**
|
|
271
|
+
* Sends a log message using the configured logger based on the event name
|
|
272
|
+
* @param {string | symbol} eventName - The event name that determines the log level
|
|
273
|
+
* @param {unknown} data - The data to log
|
|
274
|
+
*/
|
|
275
|
+
sendLog(eventName, data) {
|
|
276
|
+
if (!this._logger) {
|
|
277
|
+
return;
|
|
278
|
+
}
|
|
279
|
+
let message;
|
|
280
|
+
if (typeof data === "string") {
|
|
281
|
+
message = data;
|
|
282
|
+
} else if (Array.isArray(data) && data.length > 0 && data[0] instanceof Error) {
|
|
283
|
+
message = data[0].message;
|
|
284
|
+
} else if (data instanceof Error) {
|
|
285
|
+
message = data.message;
|
|
286
|
+
} else if (Array.isArray(data) && data.length > 0 && typeof data[0]?.message === "string") {
|
|
287
|
+
message = data[0].message;
|
|
288
|
+
} else {
|
|
289
|
+
message = JSON.stringify(data);
|
|
290
|
+
}
|
|
291
|
+
switch (eventName) {
|
|
292
|
+
case "error": {
|
|
293
|
+
this._logger.error?.(message, { event: eventName, data });
|
|
294
|
+
break;
|
|
295
|
+
}
|
|
296
|
+
case "warn": {
|
|
297
|
+
this._logger.warn?.(message, { event: eventName, data });
|
|
298
|
+
break;
|
|
299
|
+
}
|
|
300
|
+
case "trace": {
|
|
301
|
+
this._logger.trace?.(message, { event: eventName, data });
|
|
302
|
+
break;
|
|
303
|
+
}
|
|
304
|
+
case "debug": {
|
|
305
|
+
this._logger.debug?.(message, { event: eventName, data });
|
|
306
|
+
break;
|
|
307
|
+
}
|
|
308
|
+
case "fatal": {
|
|
309
|
+
this._logger.fatal?.(message, { event: eventName, data });
|
|
310
|
+
break;
|
|
311
|
+
}
|
|
312
|
+
default: {
|
|
313
|
+
this._logger.info?.(message, { event: eventName, data });
|
|
314
|
+
break;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
}
|
|
246
318
|
};
|
|
247
319
|
|
|
248
320
|
// src/index.ts
|
|
249
321
|
var Hookified = class extends Eventified {
|
|
250
322
|
_hooks;
|
|
251
|
-
|
|
323
|
+
_throwOnHookError = false;
|
|
252
324
|
_enforceBeforeAfter = false;
|
|
253
325
|
_deprecatedHooks;
|
|
254
326
|
_allowDeprecated = true;
|
|
255
327
|
constructor(options) {
|
|
256
|
-
super({
|
|
328
|
+
super({
|
|
329
|
+
logger: options?.logger,
|
|
330
|
+
throwOnEmitError: options?.throwOnEmitError,
|
|
331
|
+
throwOnEmptyListeners: options?.throwOnEmptyListeners
|
|
332
|
+
});
|
|
257
333
|
this._hooks = /* @__PURE__ */ new Map();
|
|
258
334
|
this._deprecatedHooks = options?.deprecatedHooks ? new Map(options.deprecatedHooks) : /* @__PURE__ */ new Map();
|
|
259
|
-
if (options?.
|
|
260
|
-
this.
|
|
335
|
+
if (options?.throwOnHookError !== void 0) {
|
|
336
|
+
this._throwOnHookError = options.throwOnHookError;
|
|
337
|
+
} else if (options?.throwHookErrors !== void 0) {
|
|
338
|
+
this._throwOnHookError = options.throwHookErrors;
|
|
261
339
|
}
|
|
262
340
|
if (options?.enforceBeforeAfter !== void 0) {
|
|
263
341
|
this._enforceBeforeAfter = options.enforceBeforeAfter;
|
|
@@ -276,16 +354,32 @@ var Hookified = class extends Eventified {
|
|
|
276
354
|
/**
|
|
277
355
|
* Gets whether an error should be thrown when a hook throws an error. Default is false and only emits an error event.
|
|
278
356
|
* @returns {boolean}
|
|
357
|
+
* @deprecated - this will be deprecated in version 2. Please use throwOnHookError.
|
|
279
358
|
*/
|
|
280
359
|
get throwHookErrors() {
|
|
281
|
-
return this.
|
|
360
|
+
return this._throwOnHookError;
|
|
282
361
|
}
|
|
283
362
|
/**
|
|
284
363
|
* Sets whether an error should be thrown when a hook throws an error. Default is false and only emits an error event.
|
|
285
364
|
* @param {boolean} value
|
|
365
|
+
* @deprecated - this will be deprecated in version 2. Please use throwOnHookError.
|
|
286
366
|
*/
|
|
287
367
|
set throwHookErrors(value) {
|
|
288
|
-
this.
|
|
368
|
+
this._throwOnHookError = value;
|
|
369
|
+
}
|
|
370
|
+
/**
|
|
371
|
+
* Gets whether an error should be thrown when a hook throws an error. Default is false and only emits an error event.
|
|
372
|
+
* @returns {boolean}
|
|
373
|
+
*/
|
|
374
|
+
get throwOnHookError() {
|
|
375
|
+
return this._throwOnHookError;
|
|
376
|
+
}
|
|
377
|
+
/**
|
|
378
|
+
* Sets whether an error should be thrown when a hook throws an error. Default is false and only emits an error event.
|
|
379
|
+
* @param {boolean} value
|
|
380
|
+
*/
|
|
381
|
+
set throwOnHookError(value) {
|
|
382
|
+
this._throwOnHookError = value;
|
|
289
383
|
}
|
|
290
384
|
/**
|
|
291
385
|
* Gets whether to enforce that all hook names start with 'before' or 'after'. Default is false.
|
|
@@ -355,9 +449,6 @@ var Hookified = class extends Eventified {
|
|
|
355
449
|
const message = this._deprecatedHooks.get(event);
|
|
356
450
|
const warningMessage = `Hook "${event}" is deprecated${message ? `: ${message}` : ""}`;
|
|
357
451
|
this.emit("warn", { hook: event, message: warningMessage });
|
|
358
|
-
if (this.logger?.warn) {
|
|
359
|
-
this.logger.warn(warningMessage);
|
|
360
|
-
}
|
|
361
452
|
return this._allowDeprecated;
|
|
362
453
|
}
|
|
363
454
|
return true;
|
|
@@ -505,10 +596,7 @@ var Hookified = class extends Eventified {
|
|
|
505
596
|
} catch (error) {
|
|
506
597
|
const message = `${event}: ${error.message}`;
|
|
507
598
|
this.emit("error", new Error(message));
|
|
508
|
-
if (this.
|
|
509
|
-
this.logger.error(message);
|
|
510
|
-
}
|
|
511
|
-
if (this._throwHookErrors) {
|
|
599
|
+
if (this._throwOnHookError) {
|
|
512
600
|
throw new Error(message);
|
|
513
601
|
}
|
|
514
602
|
}
|
|
@@ -565,3 +653,4 @@ export {
|
|
|
565
653
|
Eventified,
|
|
566
654
|
Hookified
|
|
567
655
|
};
|
|
656
|
+
/* v8 ignore next -- @preserve */
|
package/package.json
CHANGED
|
@@ -1,21 +1,28 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hookified",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.14.0",
|
|
4
4
|
"description": "Event Emitting and Middleware Hooks",
|
|
5
5
|
"type": "module",
|
|
6
|
-
"main": "dist/node/index.
|
|
7
|
-
"module": "dist/node/index.js",
|
|
6
|
+
"main": "./dist/node/index.js",
|
|
7
|
+
"module": "./dist/node/index.js",
|
|
8
|
+
"types": "./dist/node/index.d.ts",
|
|
8
9
|
"exports": {
|
|
9
10
|
".": {
|
|
10
|
-
"import":
|
|
11
|
-
|
|
11
|
+
"import": {
|
|
12
|
+
"types": "./dist/node/index.d.ts",
|
|
13
|
+
"default": "./dist/node/index.js"
|
|
14
|
+
},
|
|
15
|
+
"require": {
|
|
16
|
+
"types": "./dist/node/index.d.cts",
|
|
17
|
+
"default": "./dist/node/index.cjs"
|
|
18
|
+
},
|
|
19
|
+
"default": "./dist/node/index.js"
|
|
12
20
|
},
|
|
13
21
|
"./browser": {
|
|
14
22
|
"import": "./dist/browser/index.js",
|
|
15
23
|
"default": "./dist/browser/index.global.js"
|
|
16
24
|
}
|
|
17
25
|
},
|
|
18
|
-
"types": "dist/node/index.d.ts",
|
|
19
26
|
"scripts": {
|
|
20
27
|
"lint": "biome check --write --error-on-warnings",
|
|
21
28
|
"test": "pnpm lint && vitest run --coverage",
|
|
@@ -61,29 +68,24 @@
|
|
|
61
68
|
},
|
|
62
69
|
"homepage": "https://github.com/jaredwray/hookified#readme",
|
|
63
70
|
"devDependencies": {
|
|
64
|
-
"@biomejs/biome": "^2.
|
|
65
|
-
"@monstermann/tinybench-pretty-printer": "^0.
|
|
66
|
-
"@types/node": "^24.
|
|
67
|
-
"@vitest/coverage-v8": "^
|
|
68
|
-
"docula": "^0.
|
|
71
|
+
"@biomejs/biome": "^2.3.8",
|
|
72
|
+
"@monstermann/tinybench-pretty-printer": "^0.3.0",
|
|
73
|
+
"@types/node": "^24.10.1",
|
|
74
|
+
"@vitest/coverage-v8": "^4.0.15",
|
|
75
|
+
"docula": "^0.31.1",
|
|
69
76
|
"emittery": "^1.2.0",
|
|
70
77
|
"eventemitter3": "^5.0.1",
|
|
71
78
|
"hookable": "^5.5.3",
|
|
72
|
-
"pino": "^10.
|
|
73
|
-
"rimraf": "^6.
|
|
74
|
-
"tinybench": "^
|
|
75
|
-
"tsup": "^8.5.
|
|
76
|
-
"tsx": "^4.
|
|
79
|
+
"pino": "^10.1.0",
|
|
80
|
+
"rimraf": "^6.1.2",
|
|
81
|
+
"tinybench": "^6.0.0",
|
|
82
|
+
"tsup": "^8.5.1",
|
|
83
|
+
"tsx": "^4.21.0",
|
|
77
84
|
"typescript": "^5.9.3",
|
|
78
|
-
"vitest": "^
|
|
85
|
+
"vitest": "^4.0.15"
|
|
79
86
|
},
|
|
80
87
|
"files": [
|
|
81
88
|
"dist",
|
|
82
89
|
"LICENSE"
|
|
83
|
-
]
|
|
84
|
-
"pnpm": {
|
|
85
|
-
"onlyBuiltDependencies": [
|
|
86
|
-
"unrs-resolver"
|
|
87
|
-
]
|
|
88
|
-
}
|
|
90
|
+
]
|
|
89
91
|
}
|