index.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import { getUserAgent } from 'universal-user-agent';
  2. import { Collection } from 'before-after-hook';
  3. import { request } from '@octokit/request';
  4. import { withCustomRequest } from '@octokit/graphql';
  5. import { createTokenAuth } from '@octokit/auth-token';
  6. const VERSION = "3.5.1";
  7. class Octokit {
  8. constructor(options = {}) {
  9. const hook = new Collection();
  10. const requestDefaults = {
  11. baseUrl: request.endpoint.DEFAULTS.baseUrl,
  12. headers: {},
  13. request: Object.assign({}, options.request, {
  14. // @ts-ignore internal usage only, no need to type
  15. hook: hook.bind(null, "request"),
  16. }),
  17. mediaType: {
  18. previews: [],
  19. format: "",
  20. },
  21. };
  22. // prepend default user agent with `options.userAgent` if set
  23. requestDefaults.headers["user-agent"] = [
  24. options.userAgent,
  25. `octokit-core.js/${VERSION} ${getUserAgent()}`,
  26. ]
  27. .filter(Boolean)
  28. .join(" ");
  29. if (options.baseUrl) {
  30. requestDefaults.baseUrl = options.baseUrl;
  31. }
  32. if (options.previews) {
  33. requestDefaults.mediaType.previews = options.previews;
  34. }
  35. if (options.timeZone) {
  36. requestDefaults.headers["time-zone"] = options.timeZone;
  37. }
  38. this.request = request.defaults(requestDefaults);
  39. this.graphql = withCustomRequest(this.request).defaults(requestDefaults);
  40. this.log = Object.assign({
  41. debug: () => { },
  42. info: () => { },
  43. warn: console.warn.bind(console),
  44. error: console.error.bind(console),
  45. }, options.log);
  46. this.hook = hook;
  47. // (1) If neither `options.authStrategy` nor `options.auth` are set, the `octokit` instance
  48. // is unauthenticated. The `this.auth()` method is a no-op and no request hook is registered.
  49. // (2) If only `options.auth` is set, use the default token authentication strategy.
  50. // (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.
  51. // TODO: type `options.auth` based on `options.authStrategy`.
  52. if (!options.authStrategy) {
  53. if (!options.auth) {
  54. // (1)
  55. this.auth = async () => ({
  56. type: "unauthenticated",
  57. });
  58. }
  59. else {
  60. // (2)
  61. const auth = createTokenAuth(options.auth);
  62. // @ts-ignore ¯\_(ツ)_/¯
  63. hook.wrap("request", auth.hook);
  64. this.auth = auth;
  65. }
  66. }
  67. else {
  68. const { authStrategy, ...otherOptions } = options;
  69. const auth = authStrategy(Object.assign({
  70. request: this.request,
  71. log: this.log,
  72. // we pass the current octokit instance as well as its constructor options
  73. // to allow for authentication strategies that return a new octokit instance
  74. // that shares the same internal state as the current one. The original
  75. // requirement for this was the "event-octokit" authentication strategy
  76. // of https://github.com/probot/octokit-auth-probot.
  77. octokit: this,
  78. octokitOptions: otherOptions,
  79. }, options.auth));
  80. // @ts-ignore ¯\_(ツ)_/¯
  81. hook.wrap("request", auth.hook);
  82. this.auth = auth;
  83. }
  84. // apply plugins
  85. // https://stackoverflow.com/a/16345172
  86. const classConstructor = this.constructor;
  87. classConstructor.plugins.forEach((plugin) => {
  88. Object.assign(this, plugin(this, options));
  89. });
  90. }
  91. static defaults(defaults) {
  92. const OctokitWithDefaults = class extends this {
  93. constructor(...args) {
  94. const options = args[0] || {};
  95. if (typeof defaults === "function") {
  96. super(defaults(options));
  97. return;
  98. }
  99. super(Object.assign({}, defaults, options, options.userAgent && defaults.userAgent
  100. ? {
  101. userAgent: `${options.userAgent} ${defaults.userAgent}`,
  102. }
  103. : null));
  104. }
  105. };
  106. return OctokitWithDefaults;
  107. }
  108. /**
  109. * Attach a plugin (or many) to your Octokit instance.
  110. *
  111. * @example
  112. * const API = Octokit.plugin(plugin1, plugin2, plugin3, ...)
  113. */
  114. static plugin(...newPlugins) {
  115. var _a;
  116. const currentPlugins = this.plugins;
  117. const NewOctokit = (_a = class extends this {
  118. },
  119. _a.plugins = currentPlugins.concat(newPlugins.filter((plugin) => !currentPlugins.includes(plugin))),
  120. _a);
  121. return NewOctokit;
  122. }
  123. }
  124. Octokit.VERSION = VERSION;
  125. Octokit.plugins = [];
  126. export { Octokit };
  127. //# sourceMappingURL=index.js.map