Skip to content

Commit c71731f

Browse files
committed
feat: Base.defaults(options)
1 parent 0750b53 commit c71731f

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

src/index.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1+
type Options = {
2+
[key: string]: unknown;
3+
};
4+
15
type ApiExtension = { [key: string]: any };
2-
type TestPlugin = (instance: Base) => ApiExtension | undefined;
6+
type TestPlugin = (
7+
instance: Base,
8+
options: Options
9+
) => ApiExtension | undefined;
310
type Constructor<T> = new (...args: any[]) => T;
411

512
/**
@@ -33,12 +40,24 @@ export class Base {
3340
return BaseWithPlugins as typeof BaseWithPlugins & Constructor<Extension>;
3441
}
3542

36-
constructor() {
43+
static defaults(defaults: Options) {
44+
return class OctokitWithDefaults extends this {
45+
constructor(options: Options = {}) {
46+
super(Object.assign({}, defaults, options));
47+
}
48+
};
49+
}
50+
51+
constructor(options: Options = {}) {
52+
this.options = options;
53+
3754
// apply plugins
3855
// https://stackoverflow.com/a/16345172
3956
const classConstructor = this.constructor as typeof Base;
4057
classConstructor.plugins.forEach(plugin => {
41-
Object.assign(this, plugin(this));
58+
Object.assign(this, plugin(this, options));
4259
});
4360
}
61+
62+
options: Options;
4463
}

0 commit comments

Comments
 (0)