1 | import { getUserAgent } from 'universal-user-agent';
|
2 | import { Collection } from 'before-after-hook';
|
3 | import { request } from '@octokit/request';
|
4 | import { withCustomRequest } from '@octokit/graphql';
|
5 | import { createTokenAuth } from '@octokit/auth-token';
|
6 |
|
7 | const VERSION = "4.2.0";
|
8 |
|
9 | class 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 |
|
17 | hook: hook.bind(null, "request"),
|
18 | }),
|
19 | mediaType: {
|
20 | previews: [],
|
21 | format: "",
|
22 | },
|
23 | };
|
24 |
|
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 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 | if (!options.authStrategy) {
|
55 | if (!options.auth) {
|
56 |
|
57 | this.auth = async () => ({
|
58 | type: "unauthenticated",
|
59 | });
|
60 | }
|
61 | else {
|
62 |
|
63 | const auth = createTokenAuth(options.auth);
|
64 |
|
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 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 | octokit: this,
|
80 | octokitOptions: otherOptions,
|
81 | }, options.auth));
|
82 |
|
83 | hook.wrap("request", auth.hook);
|
84 | this.auth = auth;
|
85 | }
|
86 |
|
87 |
|
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 |
|
112 |
|
113 |
|
114 |
|
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 | }
|
126 | Octokit.VERSION = VERSION;
|
127 | Octokit.plugins = [];
|
128 |
|
129 | export { Octokit };
|
130 |
|