index.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. async function auth(token) {
  4. const tokenType = token.split(/\./).length === 3 ? "app" : /^v\d+\./.test(token) ? "installation" : "oauth";
  5. return {
  6. type: "token",
  7. token: token,
  8. tokenType
  9. };
  10. }
  11. /**
  12. * Prefix token for usage in the Authorization header
  13. *
  14. * @param token OAuth token or JSON Web Token
  15. */
  16. function withAuthorizationPrefix(token) {
  17. if (token.split(/\./).length === 3) {
  18. return `bearer ${token}`;
  19. }
  20. return `token ${token}`;
  21. }
  22. async function hook(token, request, route, parameters) {
  23. const endpoint = request.endpoint.merge(route, parameters);
  24. endpoint.headers.authorization = withAuthorizationPrefix(token);
  25. return request(endpoint);
  26. }
  27. const createTokenAuth = function createTokenAuth(token) {
  28. if (!token) {
  29. throw new Error("[@octokit/auth-token] No token passed to createTokenAuth");
  30. }
  31. if (typeof token !== "string") {
  32. throw new Error("[@octokit/auth-token] Token passed to createTokenAuth is not a string");
  33. }
  34. token = token.replace(/^(token|bearer) +/i, "");
  35. return Object.assign(auth.bind(null, token), {
  36. hook: hook.bind(null, token)
  37. });
  38. };
  39. exports.createTokenAuth = createTokenAuth;
  40. //# sourceMappingURL=index.js.map