1.0.6 • Published 3 years ago

enhancify v1.0.6

Weekly downloads
-
License
ISC
Repository
github
Last release
3 years ago

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
1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago