UNPKG

4.13 kBJavaScriptView Raw
1import { getUserAgent } from "universal-user-agent";
2import Hook from "before-after-hook";
3import { request } from "@octokit/request";
4import { withCustomRequest } from "@octokit/graphql";
5import { createTokenAuth } from "@octokit/auth-token";
6import { VERSION } from "./version.js";
7const noop = () => {
8};
9const consoleWarn = console.warn.bind(console);
10const consoleError = console.error.bind(console);
11const userAgentTrail = `octokit-core.js/${VERSION} ${getUserAgent()}`;
12class 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 * Attach a plugin (or many) to your Octokit instance.
39 *
40 * @example
41 * const API = Octokit.plugin(plugin1, plugin2, plugin3, ...)
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 // @ts-ignore internal usage only, no need to type
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 // we pass the current octokit instance as well as its constructor options
106 // to allow for authentication strategies that return a new octokit instance
107 // that shares the same internal state as the current one. The original
108 // requirement for this was the "event-octokit" authentication strategy
109 // of https://github.com/probot/octokit-auth-probot.
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 // assigned during constructor
125 request;
126 graphql;
127 log;
128 hook;
129 // TODO: type `octokit.auth` based on passed options.authStrategy
130 auth;
131}
132export {
133 Octokit
134};