New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

enhancify

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

enhancify

```js

latest
Source
npmnpm
Version
1.0.6
Version published
Maintainers
1
Created
Source

Enhance object methods with hooks.

Hook into any method call in an object. Using JavaScript Proxy spec.

Before method is called:

const data = { sayHello: () => {} };

const wrapped = enhancify(data)
                  .before('sayHello', ({ propKey, args }) => { 
                    logger.info('calling ', propKey, 'with args', args);
                  })
                  .build();

wrapped.sayHello('hi', 'there'); // calling sayHello with args ['hi', 'there']

Usefull for

  • Logging
  • Elastic search
  • Event dispatching
  • Analytics
  • Metrics
  • Caching

Example

const enhancify = require('enhancify');

const contactService = { 
  create: (record) => { 
    return contactRepository.create(record);
  },
  update: (record) => {
    return contactRepository.update(record);
  }
};

const contact = enhancify(contactService)
                .before('*', (data) => {
                   logger.info('invoking', data.propKey, 'with args', data.args);
                })
                .after('*', ({ result }) => {
                   cache.set(result.id, result);
                })
                .before('create', ({ args }) => {
                   metrics.createContact(userId, 'applying_discount_code');
                })
                .after('create', () => {
                   metrics.createContact(userId, 'discount_code_applied');
                })
                .before('update', (data) => {
                   pubsub.emit('contact_updated', data.result);
                })
                .after('update', (data) => {
                   elasticSearch.indexPost(data.result);
                })
                .build();

contact.create({ name: "Paul" });
contact.update({ name: "Paul Vocker", id: "1" });

TODO

  • [] On error hook
  • [] On missing method
  • [] Ossify / Freeze object
  • [] Regex pattern

Keywords

hooks

FAQs

Package last updated on 04 May 2021

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