Comparing version 0.7.1 to 1.0.0
import Emittery from 'emittery'; | ||
export type Hook = (...arguments_: any[]) => Promise<void> | void; | ||
export declare class Hookified extends Emittery { | ||
type Hook = (...arguments_: any[]) => Promise<void> | void; | ||
declare class Hookified extends Emittery { | ||
_hooks: Map<string, Hook[]>; | ||
@@ -13,1 +14,3 @@ constructor(); | ||
} | ||
export { type Hook, Hookified }; |
@@ -1,56 +0,54 @@ | ||
import Emittery from 'emittery'; | ||
export class Hookified extends Emittery { | ||
_hooks; | ||
constructor() { | ||
super(); | ||
this._hooks = new Map(); | ||
// src/index.ts | ||
import Emittery from "emittery"; | ||
var Hookified = class extends Emittery { | ||
_hooks; | ||
constructor() { | ||
super(); | ||
this._hooks = /* @__PURE__ */ new Map(); | ||
} | ||
// Adds a handler function for a specific event | ||
onHook(event, handler) { | ||
const eventHandlers = this._hooks.get(event); | ||
if (eventHandlers) { | ||
eventHandlers.push(handler); | ||
} else { | ||
this._hooks.set(event, [handler]); | ||
} | ||
// Adds a handler function for a specific event | ||
onHook(event, handler) { | ||
const eventHandlers = this._hooks.get(event); | ||
if (eventHandlers) { | ||
eventHandlers.push(handler); | ||
} | ||
else { | ||
this._hooks.set(event, [handler]); | ||
} | ||
} | ||
// Removes a specific handler function for a specific event | ||
removeHook(event, handler) { | ||
const eventHandlers = this._hooks.get(event); | ||
if (eventHandlers) { | ||
const index = eventHandlers.indexOf(handler); | ||
if (index !== -1) { | ||
eventHandlers.splice(index, 1); | ||
} | ||
} | ||
// Removes a specific handler function for a specific event | ||
removeHook(event, handler) { | ||
const eventHandlers = this._hooks.get(event); | ||
if (eventHandlers) { | ||
const index = eventHandlers.indexOf(handler); | ||
if (index !== -1) { | ||
eventHandlers.splice(index, 1); | ||
} | ||
} | ||
// Triggers all handlers for a specific event with provided data | ||
async hook(event, ...arguments_) { | ||
const eventHandlers = this._hooks.get(event); | ||
if (eventHandlers) { | ||
for (const handler of eventHandlers) { | ||
try { | ||
await handler(...arguments_); | ||
} catch (error) { | ||
await this.emit("error", new Error(`Error in hook handler for event "${event}": ${error.message}`)); | ||
} | ||
} | ||
} | ||
// Triggers all handlers for a specific event with provided data | ||
async hook(event, ...arguments_) { | ||
const eventHandlers = this._hooks.get(event); | ||
if (eventHandlers) { | ||
for (const handler of eventHandlers) { | ||
try { | ||
// eslint-disable-next-line no-await-in-loop | ||
await handler(...arguments_); | ||
} | ||
catch (error) { | ||
// eslint-disable-next-line no-await-in-loop | ||
await this.emit('error', new Error(`Error in hook handler for event "${event}": ${error.message}`)); | ||
} | ||
} | ||
} | ||
} | ||
// Provides read-only access to the current handlers | ||
get hooks() { | ||
// Creating a new map to prevent external modifications to the original map | ||
return this._hooks; | ||
} | ||
getHooks(event) { | ||
return this._hooks.get(event); | ||
} | ||
clearHooks() { | ||
this._hooks.clear(); | ||
} | ||
} | ||
//# sourceMappingURL=index.js.map | ||
} | ||
// Provides read-only access to the current handlers | ||
get hooks() { | ||
return this._hooks; | ||
} | ||
getHooks(event) { | ||
return this._hooks.get(event); | ||
} | ||
clearHooks() { | ||
this._hooks.clear(); | ||
} | ||
}; | ||
export { | ||
Hookified | ||
}; |
{ | ||
"name": "hookified", | ||
"version": "0.7.1", | ||
"version": "1.0.0", | ||
"description": "Event and Middleware Hooks", | ||
"type": "module", | ||
"main": "./dist/index.js", | ||
"exports": "./dist/index.js", | ||
"main": "./dist/index.cjs", | ||
"module": "./dist/index.js", | ||
"types": "./dist/index.d.ts", | ||
"exports": { | ||
".": { | ||
"require": "./dist/index.cjs", | ||
"import": "./dist/index.js" | ||
} | ||
}, | ||
"engines": { | ||
@@ -16,3 +22,3 @@ "node": ">=20" | ||
"clean": "rimraf ./dist ./coverage ./site/dist", | ||
"build": "rimraf ./dist && tsc", | ||
"build": "rimraf ./dist && tsup src/index.ts --format cjs,esm --dts --clean", | ||
"website:build": "docula build", | ||
@@ -59,7 +65,8 @@ "website:serve": "docula serve", | ||
"devDependencies": { | ||
"@vitest/coverage-v8": "^2.0.5", | ||
"@vitest/coverage-v8": "^2.1.1", | ||
"docula": "^0.9.0", | ||
"rimraf": "^6.0.1", | ||
"typescript": "^5.5.4", | ||
"vitest": "^2.0.5", | ||
"tsup": "^8.3.0", | ||
"typescript": "^5.6.2", | ||
"vitest": "^2.1.1", | ||
"xo": "^0.59.3" | ||
@@ -66,0 +73,0 @@ }, |
@@ -14,3 +14,3 @@ <img src="site/logo.svg" alt="Hookified" height="400" align="center"> | ||
- Async Middleware Hooks for Your Methods | ||
- ESM and Nodejs 20+ | ||
- ESM / CJS and Nodejs 20+ | ||
- Maintained on a regular basis! | ||
@@ -17,0 +17,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
11270
7
155
0
7