index.js 1.3 KB

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