Skip to content

Commit c0d2a6e

Browse files
committed
docs(README): .plugin() is now .withPlugins(), and .defaults() is now .withDefaults(). Also give credit to @JoshuaKGoldberg 💖
1 parent c0e7bb0 commit c0d2a6e

File tree

1 file changed

+33
-8
lines changed

1 file changed

+33
-8
lines changed

README.md

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
[![@latest](https://img.shields.io/npm/v/javascript-plugin-architecture-with-typescript-definitions.svg)](https://www.npmjs.com/package/javascript-plugin-architecture-with-typescript-definitions)
66
[![Build Status](https://github.com/gr2m/javascript-plugin-architecture-with-typescript-definitions/workflows/Test/badge.svg)](https://github.com/gr2m/javascript-plugin-architecture-with-typescript-definitions/actions/workflows/test.yml)
77

8-
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.
8+
The goal of this repository is to provide a template of a simple plugin Architecture which allows plugins to be created and authored as separate npm modules and shared as official or 3rd party plugins. It also permits the plugins to extend the types for the constructor options.
99

1010
## Usage
1111

12-
[Try it in TypeScript's playground editor](https://www.typescriptlang.org/play?#code/JYWwDg9gTgLgBAbzgIQIYGcCmcC+cBmUEIcARAFaoBuGAxlMGDALRgA2ArgObAB2zqKLQAWwGJlowOUTMwDuY4cxgBPMJnT1GLACaZ8fMcAi90pANwAoS-g69Jx3nBAqAYhAgAFTj14AKPnQYVHtMAC4UDEwASkRLODgZKSgnBHiEgg8Iv1iAXgA+MnwPUgAadJwrHGtbexhHZxU0KG9uPgDTYNCItCxYtISk6VT0hIAjQWy8wtIJqDKKqpq7BxM4DCxYAGUYBl4uP3QOMfIJGAigva5+6staEyC4dwgAFQ14XMisADp2Nv8XM9Wr5olZ7p1Mq93nBPrxMHInh43kEclYNphtrs+AdilCgt9cTlQdZwY9ns1kR8vphfj52oCPMC+KVGs0mbxiaT4LiKdDYfDERBeSjiejMVc-DzBJSCR4iWj0JsYDsJVKoDK5vKgA)
12+
[Try it in TypeScript's playground editor](https://www.typescriptlang.org/play?#code/JYWwDg9gTgLgBAbzgIQIYGcCmcC+cBmUEIcARAFaoBuGAxlMGDALRgA2ArgObAB2zqKLQAWwGJlowOUTMwDuY4cxgBPMJnT1GLACaZ8fMcAi90pANwAoS-g69Jx3nBAqAYhAgAFTj14AKPnQYVHtMAC4UDEwASkRLODgZKSgnBHiEgg8Iv1iAXgA+MnwPUgAadJwrHGtbexhHZxU0KG9uPgDTYNCItCxYtISk6VT0hIAjQWy8wtIJqDKKqpq7BxM4DCxYAGUYBl4uP3QOMfIJGAigva5+6staEyC4dwgAFQ14XMisADoFGGFWr50H4ANouZ6AvgAXWiVnunUyr3ecE+vEwcieHjeQRyVg2mG2uz4B2KSKC31JOVh1nhj2ezWxHy+mF+ikhplB4I87NKjWa7JhcIe8FJDORqPRmIgYpx1PxhKuflFgkZFI8VLx6E2MB2iuVUFVcw1QA)
1313

1414
```ts
1515
import { Base } from "javascript-plugin-architecture-with-typescript-definitions";
@@ -26,31 +26,56 @@ function myBarPlugin(instance: Base) {
2626
};
2727
}
2828

29-
const FooTest = Base.plugin([myFooPlugin]);
29+
const FooTest = Base.withPlugins([myFooPlugin]);
3030
const fooTest = new FooTest();
3131
fooTest.foo(); // has full TypeScript intellisense
3232

33-
const FooBarTest = Base.plugin([myFooPlugin, myBarPlugin]);
33+
const FooBarTest = Base.withPlugins([myFooPlugin, myBarPlugin]);
3434
const fooBarTest = new FooBarTest();
3535
fooBarTest.foo(); // has full TypeScript intellisense
3636
fooBarTest.bar(); // has full TypeScript intellisense
3737
```
3838

39-
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)`
39+
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.withDefaults(options)`.
4040

4141
```js
42-
const BaseWithOptions = Base.defaults({ foo: "bar" });
42+
const BaseWithOptions = Base.withDefaults({ foo: "bar" });
4343
const instance = new BaseWithOptions();
4444
instance.options; // {foo: 'bar'}
4545
```
4646

47+
Note that in for TypeScript to recognize the new option, you have to extend the `Base.Option` intererface.
48+
49+
```ts
50+
declare module "javascript-plugin-architecture-with-typescript-definitions" {
51+
namespace Base {
52+
interface Options {
53+
foo: string;
54+
}
55+
}
56+
}
57+
```
58+
59+
See also the [`required-options` example](examples/required-options).
60+
61+
The `Base` class also has two static properties
62+
63+
- `.defaults`: the default options for all instances
64+
- `.plugins`: the list of plugins applied to all instances
65+
66+
When creating a new class with `.withPlugins()` and `.defaults()`, the static properties of the returned class are set accordingly.
67+
68+
```js
69+
const MyBase = Base.withDefaults({ foo: "bar" });
70+
```
71+
4772
### Defaults
4873

49-
TypeScript will not complain when chaining `.defaults()` calls endlessly: the static `.defaultOptions` property will be set correctly. However, when instantiating from a class with 4+ chained `.defaults()` calls, then only the defaults from the first 3 calls are supported. See [#57](https://github.com/gr2m/javascript-plugin-architecture-with-typescript-definitions/pull/57) for details.
74+
TypeScript will not complain when chaining `.withDefaults()` calls endlessly: the static `.defaults` property will be set correctly. However, when instantiating from a class with 4+ chained `.withDefaults()` calls, then only the defaults from the first 3 calls are supported. See [#57](https://github.com/gr2m/javascript-plugin-architecture-with-typescript-definitions/pull/57) for details.
5075

5176
## Credit
5277

53-
This plugin architecture was extracted from [`@octokit/core`](https://github.com/octokit/core.js). The implementation was made possible by help from [@karol-majewski](https://github.com/karol-majewski), [@dragomirtitian](https://github.com/dragomirtitian), and [StackOverflow user "hackape"](https://stackoverflow.com/a/58706699/206879).
78+
This plugin architecture was extracted from [`@octokit/core`](https://github.com/octokit/core.js). The implementation was made possible by help from [@karol-majewski](https://github.com/karol-majewski), [@dragomirtitian](https://github.com/dragomirtitian), [StackOverflow user "hackape"](https://stackoverflow.com/a/58706699/206879), and [@JoshuaKGoldberg](https://github.com/JoshuaKGoldberg).
5479

5580
## LICENSE
5681

0 commit comments

Comments
 (0)