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 | import { VERSION } from "./version";
|
7 | export class Octokit {
|
8 | constructor(options = {}) {
|
9 | const hook = new Collection();
|
10 | const requestDefaults = {
|
11 | baseUrl: request.endpoint.DEFAULTS.baseUrl,
|
12 | headers: {},
|
13 | request: Object.assign({}, options.request, {
|
14 |
|
15 | hook: hook.bind(null, "request"),
|
16 | }),
|
17 | mediaType: {
|
18 | previews: [],
|
19 | format: "",
|
20 | },
|
21 | };
|
22 |
|
23 | requestDefaults.headers["user-agent"] = [
|
24 | options.userAgent,
|
25 | `octokit-core.js/${VERSION} ${getUserAgent()}`,
|
26 | ]
|
27 | .filter(Boolean)
|
28 | .join(" ");
|
29 | if (options.baseUrl) {
|
30 | requestDefaults.baseUrl = options.baseUrl;
|
31 | }
|
32 | if (options.previews) {
|
33 | requestDefaults.mediaType.previews = options.previews;
|
34 | }
|
35 | if (options.timeZone) {
|
36 | requestDefaults.headers["time-zone"] = options.timeZone;
|
37 | }
|
38 | this.request = request.defaults(requestDefaults);
|
39 | this.graphql = withCustomRequest(this.request).defaults(requestDefaults);
|
40 | this.log = Object.assign({
|
41 | debug: () => { },
|
42 | info: () => { },
|
43 | warn: console.warn.bind(console),
|
44 | error: console.error.bind(console),
|
45 | }, options.log);
|
46 | this.hook = hook;
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 | if (!options.authStrategy) {
|
53 | if (!options.auth) {
|
54 |
|
55 | this.auth = async () => ({
|
56 | type: "unauthenticated",
|
57 | });
|
58 | }
|
59 | else {
|
60 |
|
61 | const auth = createTokenAuth(options.auth);
|
62 |
|
63 | hook.wrap("request", auth.hook);
|
64 | this.auth = auth;
|
65 | }
|
66 | }
|
67 | else {
|
68 | const { authStrategy, ...otherOptions } = options;
|
69 | const auth = authStrategy(Object.assign({
|
70 | request: this.request,
|
71 | log: this.log,
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 | octokit: this,
|
78 | octokitOptions: otherOptions,
|
79 | }, options.auth));
|
80 |
|
81 | hook.wrap("request", auth.hook);
|
82 | this.auth = auth;
|
83 | }
|
84 |
|
85 |
|
86 | const classConstructor = this.constructor;
|
87 | classConstructor.plugins.forEach((plugin) => {
|
88 | Object.assign(this, plugin(this, options));
|
89 | });
|
90 | }
|
91 | static defaults(defaults) {
|
92 | const OctokitWithDefaults = class extends this {
|
93 | constructor(...args) {
|
94 | const options = args[0] || {};
|
95 | if (typeof defaults === "function") {
|
96 | super(defaults(options));
|
97 | return;
|
98 | }
|
99 | super(Object.assign({}, defaults, options, options.userAgent && defaults.userAgent
|
100 | ? {
|
101 | userAgent: `${options.userAgent} ${defaults.userAgent}`,
|
102 | }
|
103 | : null));
|
104 | }
|
105 | };
|
106 | return OctokitWithDefaults;
|
107 | }
|
108 | |
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 | static plugin(...newPlugins) {
|
115 | var _a;
|
116 | const currentPlugins = this.plugins;
|
117 | const NewOctokit = (_a = class extends this {
|
118 | },
|
119 | _a.plugins = currentPlugins.concat(newPlugins.filter((plugin) => !currentPlugins.includes(plugin))),
|
120 | _a);
|
121 | return NewOctokit;
|
122 | }
|
123 | }
|
124 | Octokit.VERSION = VERSION;
|
125 | Octokit.plugins = [];
|