Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@poppinss/hooks

Package Overview
Dependencies
Maintainers
1
Versions
40
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@poppinss/hooks - npm Package Compare versions

Comparing version 1.0.0 to 1.0.1

8

build/index.js
"use strict";
/*
* @poppinss/hooks
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
Object.defineProperty(exports, "__esModule", { value: true });
var Hooks_1 = require("./src/Hooks");
exports.Hooks = Hooks_1.Hooks;

32

build/src/Hooks/index.d.ts
import { IocResolverContract } from '@adonisjs/fold';
declare type HooksHandler = (...args: any[]) => void | Promise<void>;
/**
* Exposes the API to register before/after lifecycle hooks for a given action
* with option to resolve handlers from the IoC container.
*
* The hooks class doesn't provide autocomplete for actions and the arguments
* the handler will receive, since we expect this class to be used internally
* for user facing objects.
*/
export declare class Hooks {
private _resolver?;
private _hooks;
constructor(_resolver?: IocResolverContract | undefined);
private _resolveHandler;
private resolver?;
private hooks;
constructor(resolver?: IocResolverContract | undefined);
/**
* Resolves the hook handler using the resolver when it is defined as string
* or returns the function reference back
*/
private resolveHandler;
/**
* Register hook handler for a given event and lifecycle
*/
add(lifecycle: 'before' | 'after', action: string, handler: HooksHandler | string): this;
/**
* Remove a pre-registered handler
*/
remove(lifecycle: 'before' | 'after', action: string, handler: HooksHandler | string): void;
/**
* Remove a pre-registered handler
*/
clear(lifecycle: 'before' | 'after', action?: string): void;
/**
* Executes the hook handler for a given action and lifecycle
*/
exec(lifecycle: 'before' | 'after', action: string, ...data: any[]): Promise<void>;
}
export {};

62

build/src/Hooks/index.js
"use strict";
/*
* @poppinss/hooks
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
Object.defineProperty(exports, "__esModule", { value: true });
/**
* Exposes the API to register before/after lifecycle hooks for a given action
* with option to resolve handlers from the IoC container.
*
* The hooks class doesn't provide autocomplete for actions and the arguments
* the handler will receive, since we expect this class to be used internally
* for user facing objects.
*/
class Hooks {
constructor(_resolver) {
this._resolver = _resolver;
this._hooks = {
constructor(resolver) {
this.resolver = resolver;
this.hooks = {
before: new Map(),

@@ -11,14 +27,21 @@ after: new Map(),

}
_resolveHandler(handler) {
if (typeof (handler) === 'string' && !this._resolver) {
/**
* Resolves the hook handler using the resolver when it is defined as string
* or returns the function reference back
*/
resolveHandler(handler) {
if (typeof (handler) === 'string' && !this.resolver) {
throw new Error('Cannot register string based hooks handlers without ioc resolver');
}
if (typeof (handler) === 'string') {
return this._resolver.resolve(handler);
return this.resolver.resolve(handler);
}
return handler;
}
/**
* Register hook handler for a given event and lifecycle
*/
add(lifecycle, action, handler) {
const handlers = this._hooks[lifecycle].get(action);
const resolvedHandler = this._resolveHandler(handler);
const handlers = this.hooks[lifecycle].get(action);
const resolvedHandler = this.resolveHandler(handler);
if (handlers) {

@@ -28,23 +51,32 @@ handlers.add(resolvedHandler);

else {
this._hooks[lifecycle].set(action, new Set([resolvedHandler]));
this.hooks[lifecycle].set(action, new Set([resolvedHandler]));
}
return this;
}
/**
* Remove a pre-registered handler
*/
remove(lifecycle, action, handler) {
const handlers = this._hooks[lifecycle].get(action);
const handlers = this.hooks[lifecycle].get(action);
if (!handlers) {
return;
}
const resolvedHandler = this._resolveHandler(handler);
const resolvedHandler = this.resolveHandler(handler);
handlers.delete(resolvedHandler);
}
/**
* Remove a pre-registered handler
*/
clear(lifecycle, action) {
if (!action) {
this._hooks[lifecycle].clear();
this.hooks[lifecycle].clear();
return;
}
this._hooks[lifecycle].delete(action);
this.hooks[lifecycle].delete(action);
}
/**
* Executes the hook handler for a given action and lifecycle
*/
async exec(lifecycle, action, ...data) {
const handlers = this._hooks[lifecycle].get(action);
const handlers = this.hooks[lifecycle].get(action);
if (!handlers) {

@@ -58,3 +90,3 @@ return;

else {
await this._resolver.call(handler, undefined, data);
await this.resolver.call(handler, undefined, data);
}

@@ -61,0 +93,0 @@ }

{
"name": "@poppinss/hooks",
"version": "1.0.0",
"version": "1.0.1",
"description": "A no brainer hooks module for execute before/after lifecycle hooks",
"main": "build/index.js",
"files": [
"build/src",
"build/index.d.ts",
"build/index.js"
],
"dependencies": {},
"devDependencies": {
"@adonisjs/fold": "^6.2.2",
"@adonisjs/mrm-preset": "^2.1.0",
"@types/node": "^12.11.1",
"@adonisjs/fold": "^6.2.3",
"@adonisjs/mrm-preset": "^2.2.3",
"@types/node": "^12.12.21",
"commitizen": "^4.0.3",

@@ -14,13 +20,13 @@ "cz-conventional-changelog": "^3.0.2",

"doctoc": "^1.4.0",
"husky": "^3.0.9",
"eslint": "^6.7.2",
"eslint-plugin-adonis": "^1.0.4",
"husky": "^3.1.0",
"japa": "^3.0.1",
"mrm": "^1.2.2",
"np": "^5.1.1",
"ts-node": "^8.4.1",
"tslint": "^5.20.0",
"tslint-eslint-rules": "^5.4.0",
"typedoc": "^0.15.0",
"mrm": "^2.0.2",
"np": "^5.2.1",
"ts-node": "^8.5.4",
"typedoc": "^0.15.5",
"typedoc-plugin-external-module-name": "^2.1.0",
"typedoc-plugin-markdown": "^2.2.11",
"typescript": "^3.6.4"
"typedoc-plugin-markdown": "^2.2.14",
"typescript": "^3.7.3"
},

@@ -31,3 +37,3 @@ "scripts": {

"test": "node japaFile.js",
"lint": "tslint --project tsconfig.json",
"lint": "eslint . --ext=.ts",
"clean": "del build",

@@ -62,8 +68,2 @@ "compile": "npm run lint && npm run clean && tsc",

},
"main": "build/index.js",
"files": [
"build/src",
"build/index.d.ts",
"build/index.js"
],
"husky": {

@@ -70,0 +70,0 @@ "hooks": {

@@ -1,4 +0,3 @@

<div align="center">
<img src="https://res.cloudinary.com/adonisjs/image/upload/q_100/v1557762307/poppinss_iftxlt.jpg" width="600px">
</div>
<div align="center"><img src="https://res.cloudinary.com/adonisjs/image/upload/q_100/v1557762307/
poppinss_iftxlt.jpg" width="600px"></div>

@@ -8,6 +7,7 @@ # Hooks

[![circleci-image]][circleci-url] [![npm-image]][npm-url] ![][typescript-image] [![license-image]][license-url]
[![circleci-image]][circleci-url] [![typescript-image]][typescript-url] [![npm-image]][npm-url] [![license-image]][license-url]
I find myself re-writing the code for hooks in multiple packages, so decided to extract it to it's own module, that can be re-used by other modules of AdonisJs.
I find myself re-writing the code for hooks in multiple packages, so decided to extract it to it's own module, that can be re-used by other modules of AdonisJS.
<!-- START doctoc generated TOC please keep comment here to allow auto update -->

@@ -18,5 +18,9 @@ <!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->

- [How it works?](#how-it-works)
- [Installation](#installation)
- [Usage](#usage)
- [API Docs](#api-docs)
- [Maintainers](#maintainers)
- [API](#api)
- [add(lifecycle: 'before' | 'after', action: string, handler: Function | string)](#addlifecycle-before--after-action-string-handler-function--string)
- [exec(lifecycle: 'before' | 'after', action: string, ...data: any[])](#execlifecycle-before--after-action-string-data-any)
- [remove (lifecycle: 'before' | 'after', action: string, handler: HooksHandler | string)](#remove-lifecycle-before--after-action-string-handler-hookshandler--string)
- [clear(lifecycle: 'before' | 'after', action?: string)](#clearlifecycle-before--after-action-string)

@@ -26,9 +30,9 @@ <!-- END doctoc generated TOC please keep comment here to allow auto update -->

## How it works?
The hooks class exposes the API to `register`, `remove` and `exec` lifecycle hooks for any number of actions or events. The class API is meant to be used internally by user facing code, so that you can improve the autocomplete UX.
The hooks class exposes the API to `register`, `remove` and `exec` lifecycle hooks for any number of actions or events. The class API is meant to be used internally and not by the user facing code, so that you can improve the autocomplete UX.
For example: The Lucid models uses this class internally and expose `before` and `after` methods on the model itself. Doing this, Lucid can control the autocomplete, type checking for the `before` and `after` methods itself, without relying on this package to expose the generics API.
> Also generics increases the number of types, Typescript has to generate. So it's better to avoid them whenever possible.
> Also generics increases the number of types Typescript has to generate and it's better to avoid them whenever possible.
## Usage
## Installation
Install the package from npm registry as follows:

@@ -43,3 +47,4 @@

and use it as follows
## Usage
Use it as follows

@@ -73,19 +78,57 @@ ```ts

## API Docs
Following are the autogenerated files via Typedoc
## API
* [API](docs/README.md)
#### add(lifecycle: 'before' | 'after', action: string, handler: Function | string)
## Maintainers
[Harminder virk](https://github.com/thetutlage)
Add a new hook handler.
```ts
hooks.add('before', 'save', (data) => {
console.log(data)
})
```
#### exec(lifecycle: 'before' | 'after', action: string, ...data: any[])
Execute a given hook for a selected lifecycle.
```ts
hooks.exec('before', 'save', { username: 'virk' })
```
#### remove (lifecycle: 'before' | 'after', action: string, handler: HooksHandler | string)
Remove an earlier registered hook. If you are using the IoC container bindings, then passing the binding string is enough, otherwise you need to store the reference of the function.
```ts
function onSave () {}
hooks.add('before', 'save', onSave)
// Later remove it
hooks.remove('before', 'save', onSave)
```
#### clear(lifecycle: 'before' | 'after', action?: string)
Clear all hooks for a given lifecycle and optionally an action.
```ts
hooks.clear('before')
// Clear just for the save action
hooks.clear('before', 'save')
```
[circleci-image]: https://img.shields.io/circleci/project/github/poppinss/hooks/master.svg?style=for-the-badge&logo=circleci
[circleci-url]: https://circleci.com/gh/poppinss/hooks "circleci"
[typescript-image]: https://img.shields.io/badge/Typescript-294E80.svg?style=for-the-badge&logo=typescript
[typescript-url]: "typescript"
[npm-image]: https://img.shields.io/npm/v/@poppinss/hooks.svg?style=for-the-badge&logo=npm
[npm-url]: https://npmjs.org/package/@poppinss/hooks "npm"
[typescript-image]: https://img.shields.io/badge/Typescript-294E80.svg?style=for-the-badge&logo=typescript
[license-url]: LICENSE.md
[license-image]: https://img.shields.io/aur/license/pac.svg?style=for-the-badge
[license-image]: https://img.shields.io/npm/l/@poppinss/hooks?color=blueviolet&style=for-the-badge
[license-url]: LICENSE.md "license"
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc