interceptable
Easily intercept calls to function properties
Introduction
With this module, you can intercept calls to function properties on an object
- You hook in before and/or after a function is called
- Both sync and async function calls are interceptable
Installation
$ npm install interceptable --save
Demo's
Intercept synchronous functions that return a value (sync / success)
const interceptable = ; const obj = { console; return `Hello !`; }; const interceptor = { console; return { console; } ;}; const interceptableObj = ;interceptableObj; // OUTPUT:// BEFORE call to sayHello with args Sam// ... inside sayHello// AFTER call to sayHello with args Sam -> result: Hello Sam!
Intercept synchronous functions that throw an error (sync / error)
const interceptable = ; const obj = { console; throw `Cannot say hello to !`; }; const interceptor = { console; return { console; } ;}; const interceptableObj = ;interceptableObj; // OUTPUT:// BEFORE call to sayHello with args Sam// ... inside sayHello// AFTER call to sayHello with args Sam -> error: Cannot say hello to Sam!// /Users/seb/Work/Github/interceptable/src/interceptable.js:20// throw err;// ^// // Error: Cannot say hello to Sam!// at Object.sayHello (/Users/seb/Work/Github/interceptable/manual.js:6:11)// ...
Intercept asynchronous functions that resolve a value (async / success)
const interceptable = ; const obj = { console; return Promise; }; const interceptor = { console; return { console; } ;}; const interceptableObj = ;interceptableObj; // OUTPUT:// BEFORE call to sayHello with args Sam// ... inside sayHello// AFTER call to sayHello with args Sam -> result: Hello Sam!
Intercept asynchronous functions that rejects an error (async / error)
const interceptable = ; const obj = { console; return Promise; }; const interceptor = { console; return { console; } ;}; const interceptableObj = ;interceptableObj; // OUTPUT:// BEFORE call to sayHello with args Sam// ... inside sayHello// AFTER call to sayHello with args Sam -> result: Hello Sam!// (node:26506) UnhandledPromiseRejectionWarning: Error: Cannot say hello to Sam!// at Object.sayHello (/Users/seb/Work/Github/interceptable/manual.js:6:27)// ...