Skip to content

Commit 570d913

Browse files
committed
feat: Octokit.plugin
1 parent 667af51 commit 570d913

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/index.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,24 @@
1-
export function Octokit() {}
1+
import { OctokitOptions, Plugin } from "./types";
2+
3+
export class Octokit {
4+
static plugins: Plugin[] = [];
5+
static plugin(plugins: Plugin | Plugin[]) {
6+
const currentPlugins = this.plugins;
7+
const newPlugins = Array.isArray(plugins) ? plugins : [plugins];
8+
9+
return class NewOctokit extends this {
10+
static plugins = currentPlugins.concat(
11+
newPlugins.filter(plugin => !currentPlugins.includes(plugin))
12+
);
13+
};
14+
}
15+
16+
constructor(options?: OctokitOptions) {
17+
// https://stackoverflow.com/a/16345172
18+
const classConstructor = this.constructor as typeof Octokit;
19+
classConstructor.plugins.forEach(plugin => plugin(this, options));
20+
}
21+
22+
// allow for plugins to extend the Octokit instance
23+
[key: string]: any;
24+
}

src/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Octokit } from ".";
2+
3+
export type OctokitOptions = {
4+
[option: string]: any;
5+
};
6+
7+
export type Plugin = (octokit: Octokit, options?: OctokitOptions) => void;

0 commit comments

Comments
 (0)