npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

make-it-hookable

v3.0.0

Published

Create hookable methods in typescript

Downloads

3

Readme

Introduction: make-it-hookable

Create hookable methods in classes in TypeScript using this static HookableComponent class. There are four types of generic hookables:

  • Argumentable
  • ArgumentableAll
  • Returnable
  • ReturnableAll

One should notice that the hookables is separated into: Argumentable vs Returnable and All appended to their name or nothing appended to their name.

The types of hookables

The aforementioned appendence are further described here. For the All type it is possible to add three types of hooks: pre, actor and post. When nothing is added, only the actor are.

| Type | Definition | | ---------- | ---------------------------------------------- | | pre | Manipulate the input parameters to the actors | | actor | Actors carrying out the intended functionality of a hookable method | | post | Manipulate the output of the actors |

Notice that multiple pre and post hooks can be added for Returnable. Multiple pre, actor and post hooks can be added for Argumentable.

Usage

To use this in your project and save it in the package.json file do: npm install make-it-hookable --save

Please be aware that we use semantic versioning. This means that you should be able to safely subscribe to updates on this module for versions 1.x.x or 2.x.x etc. Major versions for example from 1.x.x to 2.x.x is not safe as the module API might change.

The component

In order to create hookable method the static methods from the HookableComponent should be used. These methods are described in the table below. Please notice something about the naming. Method returnable returns a Returnable model, argumentableAll returns a ArgumentableAll mode and so on.

| Method | Returned model | | -------------------- | ------------------ | | returnable<T, U> | Returnable<T,U> | | returnableAll<T, U> | ReturnableAll<T,U> | | argumentable<T, U> | Argumentable<T,U> | | argumentableAll<T, U> | ArgumentableAll<T,U> |

Notice this about the generics, T and U:

  • T: Type of input prams to hookable method
  • U: Type of output from hookable method

All models in this project is exposed in HookableModels in this module use as needed.

The returnable hookable model

The returnable model will return a es6-promise Promise that is resolved once all hooks has been fired or rejected if anything goes wrong.

Models involved in returnable hooks

Here the models are described involving hookables that are returnable. Returnable hooks a asynchronous, so they should call a next function with some parameters when they are done and they want the next hook to be fired. The parameters for the hooks are here described and then the the parameters that should be given to the next functions are described.

| Hook Model | Type | Called with | Returns | | -------------------- | ----- |----------------------------------------------- | ---------- | | Returnable<T,U> | actor | arg1 : T, next: ReturnableActorParams | Promise | | ReturnableAll<T,U> | pre | arg1 : T, next: ReturnablePreParams | Promise | | | actor | arg1 : T, next: ReturnableActorParams | | | | post | arg1 : T, arg2: U, next: ReturnablePostParams | |

The contents of the next functions are as follows:

| Next function | Called with | | -------------------- | ---------------- | | ReturnableActorParams | arg1: T | | ReturnablePreParams | arg1: T | | ReturnableActorParams<T, U> | arg1: T, arg2: U |

Example of returnable hooks

import {HookableComponent, HookableModels}  from    'make-it-hookable';

/**
* Some class that has a hookable method
*/
class SomeHookableClass {
    /**
    * Create a hookable method that returns the number of a given animal
    * Output should here be a number and input should be a string
    */
    public hm: HookableModels.ReturnableAll<String, Number> = HookableComponent.returnableAll();
}

// create instance of class
let instance = new SomeHookableClass();

// create a pre hook
instance.hm.push((input: String, next: ReturnablePreParams<String>) => {
    
    // change value of input to allways be goat
    next('goat');
});

// create an actor
instance.hm.actor = (input: String, next: ReturnablePostParams<String, Number>) => {
    
    // change value of input to allways be goat
    next(input, 10);
};

// create an post hook
instance.hm.post.push((input: String, next: ReturnablePostParams<String, Number>) => {
    
    // increase the number to 20
    next(input, 20);
});

// run the method (async)
instance.hm('Cow').then((result: Number) => {

    // the content is returned here. (20 goats)
    console.log('There are: ' + result);
});

The argumentable hookable model

The reason for this model to exists is to support the ability of creating hooks that can used with express. The basic idea here is that the type of the input and output are either some kind of objects or arrays.

Models involved in argumentable hooks

When an argumentable hookable is used an initial array or object like variable is created and passed along the call to the hookable along with a callback ArgumentableCb that is called once the all hooks are done.

| Hook Model | Type | Called with | | -------------------- | ----- |----------------------------------------------- | | Argumentable<T,U> | actor | input : T, output: U, next: ArgumentableCb | | ArugmentableAll<T,U> | pre | input : T, output: U, next: ArgumentableCb | | | actor | input : T, output: U, next: ArgumentableCb | | | post | input : T, output: U, next: ArgumentableCb |

The callback do not need any parameters as both the input and output are passed along the hooks as references (therefore object like or array like data types of these). It does however accept one input argument. This is to be understand as an error by the [express] framework and therefore also in this component.

Example of argumentable hooks

import {HookableComponent, HookableModels}  from    'make-it-hookable';
import * as express                         from    'express';

/**
* Some class that has a hookable method
*/
class SomeHookableClass {
    /**
    * Create a hookable method for express
    * Params should be objects or arrays
    */
    public hm: HookableModels.Argumentable<express.Request, express.Response> = HookableComponent.argumentable();
}

// create instance of class
let instance = new SomeHookableClass();

// create an actor
instance.hm.actor.push((req: express.Request, res: express.Response, cb: HookableModels.ArgumentableCb) => {
    
    // set some prop of response
    res.params.goat = true;

    // no errors is made
    cb();
});

// this would in express normally be invoked by an express router
let req: express.Request = {};
let res: express.Response = {};

instance.hm(req, res, (err: any) => {

    // do something about err if any?

    // otherwise perform action
    console.log('There are goats?: ' + res.params.goat);
});

License

The MIT License (MIT)