-
Notifications
You must be signed in to change notification settings - Fork 310
/
index.ts
178 lines (160 loc) · 5.04 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import { getUserAgent } from "universal-user-agent";
import { Collection, HookCollection } from "before-after-hook";
import { request } from "@octokit/request";
import { graphql, withCustomRequest } from "@octokit/graphql";
import { createTokenAuth } from "@octokit/auth-token";
import {
Constructor,
OctokitOptions,
OctokitPlugin,
RequestParameters,
ReturnTypeOf,
UnionToIntersection,
} from "./types";
import { VERSION } from "./version";
export class Octokit {
static VERSION = VERSION;
static defaults<S extends Constructor<any>>(
this: S,
defaults: OctokitOptions | Function
) {
const OctokitWithDefaults = class extends this {
constructor(...args: any[]) {
const options = args[0] || {};
if (typeof defaults === "function") {
super(defaults(options));
return;
}
super(
Object.assign(
{},
defaults,
options,
options.userAgent && defaults.userAgent
? {
userAgent: `${options.userAgent} ${defaults.userAgent}`,
}
: null
)
);
}
};
return OctokitWithDefaults;
}
static plugins: OctokitPlugin[] = [];
/**
* Attach a plugin (or many) to your Octokit instance.
*
* @example
* const API = Octokit.plugin(plugin1, plugin2, plugin3, ...)
*/
static plugin<
S extends Constructor<any> & { plugins: any[] },
T extends OctokitPlugin[]
>(this: S, ...newPlugins: T) {
const currentPlugins = this.plugins;
const NewOctokit = class extends this {
static plugins = currentPlugins.concat(
newPlugins.filter((plugin) => !currentPlugins.includes(plugin))
);
};
return NewOctokit as typeof NewOctokit &
Constructor<UnionToIntersection<ReturnTypeOf<T>>>;
}
constructor(options: OctokitOptions = {}) {
const hook = new Collection();
const requestDefaults: Required<RequestParameters> = {
baseUrl: request.endpoint.DEFAULTS.baseUrl,
headers: {},
request: Object.assign({}, options.request, {
hook: hook.bind(null, "request"),
}),
mediaType: {
previews: [],
format: "",
},
};
// prepend default user agent with `options.userAgent` if set
requestDefaults.headers["user-agent"] = [
options.userAgent,
`octokit-core.js/${VERSION} ${getUserAgent()}`,
]
.filter(Boolean)
.join(" ");
if (options.baseUrl) {
requestDefaults.baseUrl = options.baseUrl;
}
if (options.previews) {
requestDefaults.mediaType.previews = options.previews;
}
if (options.timeZone) {
requestDefaults.headers["time-zone"] = options.timeZone;
}
this.request = request.defaults(requestDefaults);
this.graphql = withCustomRequest(this.request).defaults({
...requestDefaults,
baseUrl: requestDefaults.baseUrl.replace(/\/api\/v3$/, "/api"),
});
this.log = Object.assign(
{
debug: () => {},
info: () => {},
warn: console.warn.bind(console),
error: console.error.bind(console),
},
options.log
);
this.hook = hook;
// (1) If neither `options.authStrategy` nor `options.auth` are set, the `octokit` instance
// is unauthenticated. The `this.auth()` method is a no-op and no request hook is registered.
// (2) If only `options.auth` is set, use the default token authentication strategy.
// (3) If `options.authStrategy` is set then use it and pass in `options.auth`. Always pass own request as many strategies accept a custom request instance.
// TODO: type `options.auth` based on `options.authStrategy`.
if (!options.authStrategy) {
if (!options.auth) {
// (1)
this.auth = async () => ({
type: "unauthenticated",
});
} else {
// (2)
const auth = createTokenAuth(options.auth as string);
// @ts-ignore ¯\_(ツ)_/¯
hook.wrap("request", auth.hook);
this.auth = auth;
}
} else {
const auth = options.authStrategy(
Object.assign(
{
request: this.request,
},
options.auth
)
);
// @ts-ignore ¯\_(ツ)_/¯
hook.wrap("request", auth.hook);
this.auth = auth;
}
// apply plugins
// https://stackoverflow.com/a/16345172
const classConstructor = this.constructor as typeof Octokit;
classConstructor.plugins.forEach((plugin) => {
Object.assign(this, plugin(this, options));
});
}
// assigned during constructor
request: typeof request;
graphql: typeof graphql;
log: {
debug: (message: string, additionalInfo?: object) => any;
info: (message: string, additionalInfo?: object) => any;
warn: (message: string, additionalInfo?: object) => any;
error: (message: string, additionalInfo?: object) => any;
[key: string]: any;
};
hook: HookCollection;
// TODO: type `octokit.auth` based on passed options.authStrategy
auth: (...args: unknown[]) => Promise<unknown>;
[key: string]: any;
}