1.0.0 • Published 5 years ago

abstract-decorator v1.0.0

Weekly downloads
7
License
MIT
Repository
github
Last release
5 years ago

abstract-decorator

JavaScript doesn't have a built in language feature for defining abstract classes and enforcing their contracts - but with decorators, we can create that functionality ourselves! The @abstract decorator allows you to define simple abstract classes, like so:

@abstract
class CookieService {
  @abstract headers;

  @abstract getValue() {}
  @abstract setValue() {}
}

You can then extend the class and provide the abstract values:

class BrowserCookieService extends CookieService {
  headers = new Map();

  getValue() {
    // get cookie value
  }

  setValue(value) {
    // set cookie value
  }
}

If a subclass fails to provide the correct values, then an error will be thrown the first time the class is instatiated. The two types of abstract values that can be defined are fields and methods, and they are defined by decorating a class field or a method respectively.

Dev Time Only

The errors thrown by @abstract are useful in development, but are pointless overhead in production applications. If you're consuming this package, you can remove the decorators using the filter-imports babel plugin:

{
  "plugins": [
    [
      "babel-plugin-filter-imports",
      {
        "imports": {
          "abstract-decorator": ["default"]
        }
      }
    ]
  ]
}

This setup is provided out-of-the-box in Ember apps and addons, zero configuration necessary!

Compatibility

This package is compatible with Node v10 and above out of the box.

Decorator Transforms

This package supports the stage 1/legacy decorators transform. This is transform that is the current recommendation of the champions of the decorators proposal.

Typescript

This package does not support Typescript. Typescript has first class support for abstract classes, so there's no need to include extra weight via this decorator.

Ember

  • Ember.js v2.18 or above
  • Ember CLI v2.13 or above
  • Node.js v8 or above

Installation

npm install --save abstract-decorator

# or with yarn
yarn add abstract-decorator

Contributing

See the Contributing guide for details.

License

This project is licensed under the MIT License.