Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

interceptable

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

interceptable

Easily intercept calls to function properties

  • 1.0.7
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

interceptable

Easily intercept calls to function properties

Build Status Coverage Status NPM Downloads Dependencies Known Vulnerabilities

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 = 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)

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)

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)

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

Keywords

FAQs

Package last updated on 24 Apr 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

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