Skip to content

Commit ff6d487

Browse files
committed
feat(typescript): add workaround to make it work with more .plugin() chaining
1 parent be81ab1 commit ff6d487

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

src/index.ts

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,45 @@
11
import { Octokit as Core } from "@octokit/core";
22
import { createActionAuth } from "@octokit/auth-action";
3+
import { paginateRest } from "@octokit/plugin-paginate-rest";
34

45
import { VERSION } from "./version";
56

6-
export const Octokit = Core.defaults({
7+
const CoreWithPlugins = Core.plugin([paginateRest]);
8+
9+
// Workaround for TypeScript bug, see https://github.com/octokit/core.js#a-note-on-typescript
10+
import {
11+
OctokitOptions,
12+
OctokitPlugin,
13+
ReturnTypeOf,
14+
Constructor
15+
} from "@octokit/core";
16+
17+
class CoreWithPluginsWorkaround extends CoreWithPlugins {
18+
static plugin<T extends OctokitPlugin | OctokitPlugin[]>(pluginOrPlugins: T) {
19+
const currentPlugins = this.plugins;
20+
const newPlugins = Array.isArray(pluginOrPlugins)
21+
? pluginOrPlugins
22+
: [pluginOrPlugins];
23+
24+
const NewOctokit = class extends this {
25+
static plugins = currentPlugins.concat(
26+
newPlugins.filter(plugin => !currentPlugins.includes(plugin))
27+
);
28+
};
29+
30+
return NewOctokit as typeof NewOctokit & Constructor<ReturnTypeOf<T>>;
31+
}
32+
33+
static defaults(defaults: OctokitOptions) {
34+
return class OctokitWithDefaults extends this {
35+
constructor(options: OctokitOptions = {}) {
36+
super(Object.assign({}, defaults, options));
37+
}
38+
};
39+
}
40+
}
41+
42+
export const Octokit = CoreWithPluginsWorkaround.defaults({
743
authStrategy: createActionAuth,
844
userAgent: `octokit-action.js/${VERSION}`
945
});

0 commit comments

Comments
 (0)