change-string-case/node_modules/@octokit/auth-token/dist-web/index.js

53 lines
1.5 KiB
JavaScript
Raw Normal View History

2023-10-26 04:27:07 +08:00
// pkg/dist-src/auth.js
var REGEX_IS_INSTALLATION_LEGACY = /^v1\./;
var REGEX_IS_INSTALLATION = /^ghs_/;
var REGEX_IS_USER_TO_SERVER = /^ghu_/;
2022-11-04 00:55:07 +08:00
async function auth(token) {
2023-10-26 04:27:07 +08:00
const isApp = token.split(/\./).length === 3;
const isInstallation = REGEX_IS_INSTALLATION_LEGACY.test(token) || REGEX_IS_INSTALLATION.test(token);
const isUserToServer = REGEX_IS_USER_TO_SERVER.test(token);
const tokenType = isApp ? "app" : isInstallation ? "installation" : isUserToServer ? "user-to-server" : "oauth";
return {
type: "token",
token,
tokenType
};
2022-11-04 00:55:07 +08:00
}
2023-10-26 04:27:07 +08:00
// pkg/dist-src/with-authorization-prefix.js
2022-11-04 00:55:07 +08:00
function withAuthorizationPrefix(token) {
2023-10-26 04:27:07 +08:00
if (token.split(/\./).length === 3) {
return `bearer ${token}`;
}
return `token ${token}`;
2022-11-04 00:55:07 +08:00
}
2023-10-26 04:27:07 +08:00
// pkg/dist-src/hook.js
2022-11-04 00:55:07 +08:00
async function hook(token, request, route, parameters) {
2023-10-26 04:27:07 +08:00
const endpoint = request.endpoint.merge(
route,
parameters
);
endpoint.headers.authorization = withAuthorizationPrefix(token);
return request(endpoint);
2022-11-04 00:55:07 +08:00
}
2023-10-26 04:27:07 +08:00
// pkg/dist-src/index.js
var createTokenAuth = function createTokenAuth2(token) {
if (!token) {
throw new Error("[@octokit/auth-token] No token passed to createTokenAuth");
}
if (typeof token !== "string") {
throw new Error(
"[@octokit/auth-token] Token passed to createTokenAuth is not a string"
);
}
token = token.replace(/^(token|bearer) +/i, "");
return Object.assign(auth.bind(null, token), {
hook: hook.bind(null, token)
});
};
export {
createTokenAuth
2022-11-04 00:55:07 +08:00
};