UNPKG

5.11 kBJavaScriptView Raw
1import { getUserAgent } from 'universal-user-agent';
2import { Collection } from 'before-after-hook';
3import { request } from '@octokit/request';
4import { withCustomRequest } from '@octokit/graphql';
5import { createTokenAuth } from '@octokit/auth-token';
6
7const VERSION = "4.2.0";
8
9class Octokit {
10 constructor(options = {}) {
11 const hook = new Collection();
12 const requestDefaults = {
13 baseUrl: request.endpoint.DEFAULTS.baseUrl,
14 headers: {},
15 request: Object.assign({}, options.request, {
16 // @ts-ignore internal usage only, no need to type
17 hook: hook.bind(null, "request"),
18 }),
19 mediaType: {
20 previews: [],
21 format: "",
22 },
23 };
24 // prepend default user agent with `options.userAgent` if set
25 requestDefaults.headers["user-agent"] = [
26 options.userAgent,
27 `octokit-core.js/${VERSION} ${getUserAgent()}`,
28 ]
29 .filter(Boolean)
30 .join(" ");
31 if (options.baseUrl) {
32 requestDefaults.baseUrl = options.baseUrl;
33 }
34 if (options.previews) {
35 requestDefaults.mediaType.previews = options.previews;
36 }
37 if (options.timeZone) {
38 requestDefaults.headers["time-zone"] = options.timeZone;
39 }
40 this.request = request.defaults(requestDefaults);
41 this.graphql = withCustomRequest(this.request).defaults(requestDefaults);
42 this.log = Object.assign({
43 debug: () => { },
44 info: () => { },
45 warn: console.warn.bind(console),
46 error: console.error.bind(console),
47 }, options.log);
48 this.hook = hook;
49 // (1) If neither `options.authStrategy` nor `options.auth` are set, the `octokit` instance
50 // is unauthenticated. The `this.auth()` method is a no-op and no request hook is registered.
51 // (2) If only `options.auth` is set, use the default token authentication strategy.
52 // (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.
53 // TODO: type `options.auth` based on `options.authStrategy`.
54 if (!options.authStrategy) {
55 if (!options.auth) {
56 // (1)
57 this.auth = async () => ({
58 type: "unauthenticated",
59 });
60 }
61 else {
62 // (2)
63 const auth = createTokenAuth(options.auth);
64 // @ts-ignore ¯\_(ツ)_/¯
65 hook.wrap("request", auth.hook);
66 this.auth = auth;
67 }
68 }
69 else {
70 const { authStrategy, ...otherOptions } = options;
71 const auth = authStrategy(Object.assign({
72 request: this.request,
73 log: this.log,
74 // we pass the current octokit instance as well as its constructor options
75 // to allow for authentication strategies that return a new octokit instance
76 // that shares the same internal state as the current one. The original
77 // requirement for this was the "event-octokit" authentication strategy
78 // of https://github.com/probot/octokit-auth-probot.
79 octokit: this,
80 octokitOptions: otherOptions,
81 }, options.auth));
82 // @ts-ignore ¯\_(ツ)_/¯
83 hook.wrap("request", auth.hook);
84 this.auth = auth;
85 }
86 // apply plugins
87 // https://stackoverflow.com/a/16345172
88 const classConstructor = this.constructor;
89 classConstructor.plugins.forEach((plugin) => {
90 Object.assign(this, plugin(this, options));
91 });
92 }
93 static defaults(defaults) {
94 const OctokitWithDefaults = class extends this {
95 constructor(...args) {
96 const options = args[0] || {};
97 if (typeof defaults === "function") {
98 super(defaults(options));
99 return;
100 }
101 super(Object.assign({}, defaults, options, options.userAgent && defaults.userAgent
102 ? {
103 userAgent: `${options.userAgent} ${defaults.userAgent}`,
104 }
105 : null));
106 }
107 };
108 return OctokitWithDefaults;
109 }
110 /**
111 * Attach a plugin (or many) to your Octokit instance.
112 *
113 * @example
114 * const API = Octokit.plugin(plugin1, plugin2, plugin3, ...)
115 */
116 static plugin(...newPlugins) {
117 var _a;
118 const currentPlugins = this.plugins;
119 const NewOctokit = (_a = class extends this {
120 },
121 _a.plugins = currentPlugins.concat(newPlugins.filter((plugin) => !currentPlugins.includes(plugin))),
122 _a);
123 return NewOctokit;
124 }
125}
126Octokit.VERSION = VERSION;
127Octokit.plugins = [];
128
129export { Octokit };
130//# sourceMappingURL=index.js.map