Skip to main content

javascript-plugin-architecture-with-typescript-definitions

Plugin architecture example with full TypeScript support

@latest Build Status Greenkeeper

The goal of this repository is to provide a template of a simple plugin Architecture which allows plugins to created and authored as separate npm modules and shared as official or 3rd party plugins.

Usage

Try it in TypeScript’s playground editor

import { Base } from "javascript-plugin-architecture-with-typescript-definitions";

function myFooPlugin(instance: Base) {
  return {
    foo: () => "foo",
  };
}

function myBarPlugin(instance: Base) {
  return {
    bar: () => "bar",
  };
}

const FooTest = Base.plugin(myFooPlugin);
const fooTest = new FooTest();
fooTest.foo(); // has full TypeScript intellisense

const FooBarTest = Base.plugin(myFooPlugin, myBarPlugin);
const fooBarTest = new FooBarTest();
fooBarTest.foo(); // has full TypeScript intellisense
fooBarTest.bar(); // has full TypeScript intellisense

The constructor accepts an optional options object which is passed to the plugins as second argument and stored in instance.options. Default options can be set using Base.defaults(options)

const BaseWithOptions = Base.defaults({ foo: "bar" });
const instance = new BaseWithOptions();
instance.options; // {foo: 'bar'}

Credit

This plugin architecture was extracted from @octokit/core. The implementation was made possible by help from @karol-majewski, @dragomirtitian, and StackOverflow user “hackape”.

LICENSE

ISC