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

A no brainer hooks module for execute before/after lifecycle hooks

  • 3.0.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
50K
decreased by-5.78%
Maintainers
1
Weekly downloads
 
Created
Source

Hooks

A no brainer module to execute lifecycle hooks in sequence.

circleci-image typescript-image npm-image license-image synk-image

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.

Table of contents

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 and not by the user facing code and this gives you the chance to improve the hooks DX.

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 and it's better to avoid them whenever possible.

Installation

Install the package from npm registry as follows:

npm i @poppinss/hooks

# yarn
yarn add @poppinss/hooks

Usage

Use it as follows

import { Hooks } from '@poppinss/hooks'
const hooks = new Hooks()

hooks.add('before', 'save', function () {})

// Later invoke before save hooks
await hooks.exec('before', 'save', { id: 1 })

If you want the end user to define IoC container bindings as the hook handler, then you need to pass the IoC container resolver to the Hooks constructor. Following is the snippet from Lucid models.

import { Ioc } from '@adonisjs/fold'
const ioc = new Ioc()
const resolver = ioc.getResolver(undefined, 'modelHooks', 'App/Models/Hooks')

const hooks = new Hooks(resolver)

The resolver allows the end user to pass the hook reference as string and hooks must live inside App/Models/Hooks folder.

hooks.add('before', 'save', 'User.encryptPassword')

API

add(lifecycle: 'before' | 'after', action: string, handler: Function | string)

Add a new hook handler.

hooks.add('before', 'save', (data) => {
  console.log(data)
})
exec(lifecycle: 'before' | 'after', action: string, ...data: any[])

Execute a given hook for a selected lifecycle.

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.

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.

hooks.clear('before')

// Clear just for the save action
hooks.clear('before', 'save')
merge (hooks: Hooks): void

Merge hooks from an existing hooks instance. Useful during class inheritance.

const hooks = new Hooks()
hooks.add('before', 'save', function () {})

const hooks1 = new Hooks()
hooks1.merge(hooks)

await hooks1.exec('before', 'save', [])

Keywords

FAQs

Package last updated on 24 Mar 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

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