https://github.com/seppevs/interceptable
Easily intercept calls to function properties
https://github.com/seppevs/interceptable
Last synced: 3 months ago
JSON representation
Easily intercept calls to function properties
- Host: GitHub
- URL: https://github.com/seppevs/interceptable
- Owner: seppevs
- License: mit
- Created: 2019-05-21T12:44:33.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2021-07-12T04:22:24.000Z (about 5 years ago)
- Last Synced: 2025-10-06T13:29:56.242Z (10 months ago)
- Language: JavaScript
- Size: 741 KB
- Stars: 2
- Watchers: 0
- Forks: 0
- Open Issues: 10
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# interceptable
Easily intercept calls to function properties
✨ [](https://travis-ci.org/seppevs/interceptable) [](https://coveralls.io/r/seppevs/interceptable) [](https://www.npmjs.org/package/interceptable) [](https://www.npmjs.org/package/interceptable) [](https://david-dm.org/seppevs/interceptable) [](https://snyk.io/test/github/seppevs/interceptable) ✨
## 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
```bash
$ npm install interceptable --save
```
## Demo's
### Intercept synchronous functions that return a value (sync / success)
```javascript
const interceptable = require('interceptable');
const obj = {
sayHello(name) {
console.log('... inside sayHello');
return `Hello ${name}!`;
}
};
const interceptor = ({ fn, args }) => {
console.log(`BEFORE call to ${fn} with args ${args}`);
return {
onSuccess(result) {
console.log(`AFTER call to ${fn} with args ${args} -> result: ${result}`);
},
};
};
const interceptableObj = interceptable(obj, interceptor);
interceptableObj.sayHello('Sam');
// 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)
```javascript
const interceptable = require('interceptable');
const obj = {
sayHello(name) {
console.log('... inside sayHello');
throw new Error(`Cannot say hello to ${name}!`);
}
};
const interceptor = ({ fn, args }) => {
console.log(`BEFORE call to ${fn} with args ${args}`);
return {
onError(err) {
console.log(`AFTER call to ${fn} with args ${args} -> error: ${err.message}`);
},
};
};
const interceptableObj = interceptable(obj, interceptor);
interceptableObj.sayHello('Sam');
// 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)
```javascript
const interceptable = require('interceptable');
const obj = {
sayHello(name) {
console.log('... inside sayHello');
return Promise.resolve(`Hello ${name}!`);
}
};
const interceptor = ({ fn, args }) => {
console.log(`BEFORE call to ${fn} with args ${args}`);
return {
onSuccess(result) {
console.log(`AFTER call to ${fn} with args ${args} -> result: ${result}`);
},
};
};
const interceptableObj = interceptable(obj, interceptor);
interceptableObj.sayHello('Sam');
// 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)
```javascript
const interceptable = require('interceptable');
const obj = {
sayHello(name) {
console.log('... inside sayHello');
return Promise.reject(new Error(`Cannot say hello to ${name}!`));
}
};
const interceptor = ({ fn, args }) => {
console.log(`BEFORE call to ${fn} with args ${args}`);
return {
onError(result) {
console.log(`AFTER call to ${fn} with args ${args} -> result: ${result}`);
},
};
};
const interceptableObj = interceptable(obj, interceptor);
interceptableObj.sayHello('Sam');
// 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)
// ...
```