mirror of
https://github.com/ASzc/change-string-case-action.git
synced 2024-11-24 02:03:50 +08:00
119 lines
4.4 KiB
JavaScript
119 lines
4.4 KiB
JavaScript
import { getUserAgent } from "universal-user-agent";
|
|
import { Collection } from "before-after-hook";
|
|
import { request } from "@octokit/request";
|
|
import { withCustomRequest } from "@octokit/graphql";
|
|
import { createTokenAuth } from "@octokit/auth-token";
|
|
import { VERSION } from "./version";
|
|
export class Octokit {
|
|
constructor(options = {}) {
|
|
const hook = new Collection();
|
|
const requestDefaults = {
|
|
baseUrl: request.endpoint.DEFAULTS.baseUrl,
|
|
headers: {},
|
|
request: Object.assign({}, options.request, {
|
|
hook: hook.bind(null, "request"),
|
|
}),
|
|
mediaType: {
|
|
previews: [],
|
|
format: "",
|
|
},
|
|
};
|
|
// prepend default user agent with `options.userAgent` if set
|
|
requestDefaults.headers["user-agent"] = [
|
|
options.userAgent,
|
|
`octokit-core.js/${VERSION} ${getUserAgent()}`,
|
|
]
|
|
.filter(Boolean)
|
|
.join(" ");
|
|
if (options.baseUrl) {
|
|
requestDefaults.baseUrl = options.baseUrl;
|
|
}
|
|
if (options.previews) {
|
|
requestDefaults.mediaType.previews = options.previews;
|
|
}
|
|
if (options.timeZone) {
|
|
requestDefaults.headers["time-zone"] = options.timeZone;
|
|
}
|
|
this.request = request.defaults(requestDefaults);
|
|
this.graphql = withCustomRequest(this.request).defaults({
|
|
...requestDefaults,
|
|
baseUrl: requestDefaults.baseUrl.replace(/\/api\/v3$/, "/api"),
|
|
});
|
|
this.log = Object.assign({
|
|
debug: () => { },
|
|
info: () => { },
|
|
warn: console.warn.bind(console),
|
|
error: console.error.bind(console),
|
|
}, options.log);
|
|
this.hook = hook;
|
|
// (1) If neither `options.authStrategy` nor `options.auth` are set, the `octokit` instance
|
|
// is unauthenticated. The `this.auth()` method is a no-op and no request hook is registred.
|
|
// (2) If only `options.auth` is set, use the default token authentication strategy.
|
|
// (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.
|
|
// TODO: type `options.auth` based on `options.authStrategy`.
|
|
if (!options.authStrategy) {
|
|
if (!options.auth) {
|
|
// (1)
|
|
this.auth = async () => ({
|
|
type: "unauthenticated",
|
|
});
|
|
}
|
|
else {
|
|
// (2)
|
|
const auth = createTokenAuth(options.auth);
|
|
// @ts-ignore ¯\_(ツ)_/¯
|
|
hook.wrap("request", auth.hook);
|
|
this.auth = auth;
|
|
}
|
|
}
|
|
else {
|
|
const auth = options.authStrategy(Object.assign({
|
|
request: this.request,
|
|
}, options.auth));
|
|
// @ts-ignore ¯\_(ツ)_/¯
|
|
hook.wrap("request", auth.hook);
|
|
this.auth = auth;
|
|
}
|
|
// apply plugins
|
|
// https://stackoverflow.com/a/16345172
|
|
const classConstructor = this.constructor;
|
|
classConstructor.plugins.forEach((plugin) => {
|
|
Object.assign(this, plugin(this, options));
|
|
});
|
|
}
|
|
static defaults(defaults) {
|
|
const OctokitWithDefaults = class extends this {
|
|
constructor(...args) {
|
|
const options = args[0] || {};
|
|
if (typeof defaults === "function") {
|
|
super(defaults(options));
|
|
return;
|
|
}
|
|
super(Object.assign({}, defaults, options, options.userAgent && defaults.userAgent
|
|
? {
|
|
userAgent: `${options.userAgent} ${defaults.userAgent}`,
|
|
}
|
|
: null));
|
|
}
|
|
};
|
|
return OctokitWithDefaults;
|
|
}
|
|
/**
|
|
* Attach a plugin (or many) to your Octokit instance.
|
|
*
|
|
* @example
|
|
* const API = Octokit.plugin(plugin1, plugin2, plugin3, ...)
|
|
*/
|
|
static plugin(...newPlugins) {
|
|
var _a;
|
|
const currentPlugins = this.plugins;
|
|
const NewOctokit = (_a = class extends this {
|
|
},
|
|
_a.plugins = currentPlugins.concat(newPlugins.filter((plugin) => !currentPlugins.includes(plugin))),
|
|
_a);
|
|
return NewOctokit;
|
|
}
|
|
}
|
|
Octokit.VERSION = VERSION;
|
|
Octokit.plugins = [];
|