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