Create hookable methods in typescript
npm install make-it-hookablenpm install make-it-hookable --save
typescript
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 = HookableComponent.returnableAll();
}
// create instance of class
let instance = new SomeHookableClass();
// create a pre hook
instance.hm.push((input: String, next: ReturnablePreParams) => {
// change value of input to allways be goat
next('goat');
});
// create an actor
instance.hm.actor = (input: String, next: ReturnablePostParams) => {
// change value of input to allways be goat
next(input, 10);
};
// create an post hook
instance.hm.post.push((input: String, next: ReturnablePostParams) => {
// 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 | actor | input : T, output: U, next: ArgumentableCb |
| ArugmentableAll | 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
`typescript
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 = 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);
});
``