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

custom-decorators

Package Overview
Dependencies
Maintainers
0
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

custom-decorators

[![npm](https://img.shields.io/npm/v/custom-decorators.svg)](https://www.npmjs.com/package/custom-decorators) [![npm](https://img.shields.io/npm/dy/custom-decorators.svg)](https://www.npmjs.com/package/custom-decorators)

  • 0.0.4
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
0
Maintainers
0
Weekly downloads
 
Created
Source

custom Open Technical Architecture

npm npm

Easily extend and enhance existing code using a standard decorator syntax.

The extension plugin is written following the style of Koa Middleware.

Quick Start

import custom from 'custom-decorators';

class ClassA {

  // Just add a `@custom` decorator and the extension name.
  @custom('ClassA.method')

  // No need to modify something for the function
  async method(name) {
    console.log(`hello ${name}`);
  }
}

// Add a middleware to extend the method
custom('ClassA.method', async function(context, next) {

  // Get the arguments from the context
  const { args } = context;

  console.log(this, 'before', args[0]);
  await next();
  console.log(this, 'after', args[0]);
})


const instancea = new ClassA();

// invoke the original method as usual
instancea.method('world');

/* output
ClassA {} before world
hello world
ClassA {} after world
*/

Architecture

image

Features

  • Support both async and sync functions depending on your declaration.
class ClassA {

  @custom('ClassA.method')
  method(name) {
    console.log(`hello ${name}`);
  }
}

// Note that the original function is synchronous, so the plugin function should also be sync.
// DO NOT add the `async` keyword.
custom('ClassA.method', function(context, next) {

  const { args } = context;

  console.log(this, 'before', args[0]);
  next();
  console.log(this, 'after', args[0]);
})
  • Support the return value.
class ClassA {

  @custom('ClassA.method')
  async get(name) {
    return `hello ${name}`;
  }
}

// Get & modify the return value
custom('ClassA.method', async function(context, next) {

  const returnValue = await next();

  return `before ${returnValue} after`;
})

const instancea = new ClassA();
console.log(instancea.get('world'));

/* output
before hello world after
*/
  • Support for private extension (by not exports your symbol objects)
const PRIVATE_EXTENSION = Symbol('private_extension');

class ClassA {

  @custom(PRIVATE_EXTENSION)
  async method(name) {
    console.log(`hello ${name}`);
  }
}

custom(PRIVATE_EXTENSION, async function(context, next) {
  // code gos here
})

License

MIT

Copyright (c) 2024-present, Zhoukekestar

FAQs

Package last updated on 18 Sep 2024

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