Socket
Socket
Sign inDemoInstall

@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.1.0 to 1.1.1

7

build/src/Hooks/index.d.ts

@@ -25,2 +25,6 @@ import { IocResolverContract } from '@adonisjs/fold';

/**
* Returns handlers set for a given action or undefined
*/
private getActionHandlers;
/**
* Adds the resolved handler to the actions set

@@ -42,3 +46,4 @@ */

/**
* Remove a pre-registered handler
* Remove all handlers for a given action or lifecycle. If action is not
* defined, then all actions for that given lifecycle are removed
*/

@@ -45,0 +50,0 @@ clear(lifecycle: 'before' | 'after', action?: string): void;

24

build/src/Hooks/index.js

@@ -48,6 +48,12 @@ "use strict";

/**
* Returns handlers set for a given action or undefined
*/
getActionHandlers(lifecycle, action) {
return this.hooks[lifecycle].get(action);
}
/**
* Adds the resolved handler to the actions set
*/
addResolvedHandler(lifecycle, action, handler) {
const handlers = this.hooks[lifecycle].get(action);
const handlers = this.getActionHandlers(lifecycle, action);
if (handlers) {

@@ -64,8 +70,7 @@ handlers.add(handler);

has(lifecycle, action, handler) {
const handlers = this.hooks[lifecycle].get(action);
const resolvedHandler = this.resolveHandler(handler);
const handlers = this.getActionHandlers(lifecycle, action);
if (!handlers) {
return false;
}
return handlers.has(resolvedHandler);
return handlers.has(this.resolveHandler(handler));
}

@@ -83,11 +88,11 @@ /**

remove(lifecycle, action, handler) {
const handlers = this.hooks[lifecycle].get(action);
const handlers = this.getActionHandlers(lifecycle, action);
if (!handlers) {
return;
}
const resolvedHandler = this.resolveHandler(handler);
handlers.delete(resolvedHandler);
handlers.delete(this.resolveHandler(handler));
}
/**
* Remove a pre-registered handler
* Remove all handlers for a given action or lifecycle. If action is not
* defined, then all actions for that given lifecycle are removed
*/

@@ -121,3 +126,3 @@ clear(lifecycle, action) {

async exec(lifecycle, action, ...data) {
const handlers = this.hooks[lifecycle].get(action);
const handlers = this.getActionHandlers(lifecycle, action);
if (!handlers) {

@@ -131,3 +136,2 @@ return;

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

@@ -134,0 +138,0 @@ }

# The MIT License
Copyright 2019 Harminder virk, contributors
Copyright 2020 Harminder virk, contributors

@@ -5,0 +5,0 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

{
"name": "@poppinss/hooks",
"version": "1.1.0",
"version": "1.1.1",
"description": "A no brainer hooks module for execute before/after lifecycle hooks",

@@ -15,3 +15,2 @@ "main": "build/index.js",

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

@@ -23,3 +22,6 @@ "compile": "npm run lint && npm run clean && tsc",

"version": "npm run build",
"format": "prettier --write ."
"format": "prettier --write .",
"prepublishOnly": "npm run build",
"lint": "eslint . --ext=.ts",
"sync-labels": "github-label-sync --labels ./node_modules/@adonisjs/mrm-preset/gh-labels.json poppinss/hooks"
},

@@ -37,4 +39,4 @@ "dependencies": {},

"@adonisjs/fold": "^6.3.5",
"@adonisjs/mrm-preset": "^2.3.7",
"@types/node": "^14.0.20",
"@adonisjs/mrm-preset": "^2.4.0",
"@types/node": "^14.0.23",
"commitizen": "^4.1.2",

@@ -48,9 +50,11 @@ "cz-conventional-changelog": "^3.2.0",

"eslint-plugin-prettier": "^3.1.4",
"github-label-sync": "^2.0.0",
"husky": "^4.2.5",
"japa": "^3.1.1",
"mrm": "^2.3.3",
"np": "^6.3.1",
"np": "^6.3.2",
"npm-audit-html": "^1.4.1",
"prettier": "^2.0.5",
"ts-node": "^8.10.2",
"typescript": "^3.9.6"
"typescript": "^3.9.7"
},

@@ -81,3 +85,3 @@ "repository": {

"hooks": {
"pre-commit": "doctoc README.md --title='## Table of contents' && git add README.md",
"pre-commit": "doctoc README.md --title='## Table of contents' && git add README.md && npm audit --production --json | ./node_modules/.bin/npm-audit-html && git add npm-audit.html",
"commit-msg": "node ./node_modules/@adonisjs/mrm-preset/validateCommit/conventional/validate.js"

@@ -84,0 +88,0 @@ }

@@ -7,7 +7,6 @@ <div align="center"><img src="https://res.cloudinary.com/adonisjs/image/upload/q_100/v1557762307/

[![circleci-image]][circleci-url] [![typescript-image]][typescript-url] [![npm-image]][npm-url] [![license-image]][license-url]
[![circleci-image]][circleci-url] [![typescript-image]][typescript-url] [![npm-image]][npm-url] [![license-image]][license-url] [![audit-report-image]][audit-report-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.
<!-- START doctoc generated TOC please keep comment here to allow auto update -->

@@ -30,3 +29,3 @@ <!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO 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 and not by the 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 and this gives you the chance to improve the hooks DX.

@@ -61,3 +60,3 @@ 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.

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.
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.

@@ -147,1 +146,4 @@ ```ts

[license-url]: LICENSE.md "license"
[audit-report-image]: https://img.shields.io/badge/-Audit%20Report-blueviolet?style=for-the-badge
[audit-report-url]: https://htmlpreview.github.io/?https://github.com/poppinss/hooks/blob/develop/npm-audit.html "audit-report"
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