index.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import { request } from '@octokit/request';
  2. import { getUserAgent } from 'universal-user-agent';
  3. const VERSION = "4.8.0";
  4. function _buildMessageForResponseErrors(data) {
  5. return (`Request failed due to following response errors:\n` +
  6. data.errors.map((e) => ` - ${e.message}`).join("\n"));
  7. }
  8. class GraphqlResponseError extends Error {
  9. constructor(request, headers, response) {
  10. super(_buildMessageForResponseErrors(response));
  11. this.request = request;
  12. this.headers = headers;
  13. this.response = response;
  14. this.name = "GraphqlResponseError";
  15. // Expose the errors and response data in their shorthand properties.
  16. this.errors = response.errors;
  17. this.data = response.data;
  18. // Maintains proper stack trace (only available on V8)
  19. /* istanbul ignore next */
  20. if (Error.captureStackTrace) {
  21. Error.captureStackTrace(this, this.constructor);
  22. }
  23. }
  24. }
  25. const NON_VARIABLE_OPTIONS = [
  26. "method",
  27. "baseUrl",
  28. "url",
  29. "headers",
  30. "request",
  31. "query",
  32. "mediaType",
  33. ];
  34. const FORBIDDEN_VARIABLE_OPTIONS = ["query", "method", "url"];
  35. const GHES_V3_SUFFIX_REGEX = /\/api\/v3\/?$/;
  36. function graphql(request, query, options) {
  37. if (options) {
  38. if (typeof query === "string" && "query" in options) {
  39. return Promise.reject(new Error(`[@octokit/graphql] "query" cannot be used as variable name`));
  40. }
  41. for (const key in options) {
  42. if (!FORBIDDEN_VARIABLE_OPTIONS.includes(key))
  43. continue;
  44. return Promise.reject(new Error(`[@octokit/graphql] "${key}" cannot be used as variable name`));
  45. }
  46. }
  47. const parsedOptions = typeof query === "string" ? Object.assign({ query }, options) : query;
  48. const requestOptions = Object.keys(parsedOptions).reduce((result, key) => {
  49. if (NON_VARIABLE_OPTIONS.includes(key)) {
  50. result[key] = parsedOptions[key];
  51. return result;
  52. }
  53. if (!result.variables) {
  54. result.variables = {};
  55. }
  56. result.variables[key] = parsedOptions[key];
  57. return result;
  58. }, {});
  59. // workaround for GitHub Enterprise baseUrl set with /api/v3 suffix
  60. // https://github.com/octokit/auth-app.js/issues/111#issuecomment-657610451
  61. const baseUrl = parsedOptions.baseUrl || request.endpoint.DEFAULTS.baseUrl;
  62. if (GHES_V3_SUFFIX_REGEX.test(baseUrl)) {
  63. requestOptions.url = baseUrl.replace(GHES_V3_SUFFIX_REGEX, "/api/graphql");
  64. }
  65. return request(requestOptions).then((response) => {
  66. if (response.data.errors) {
  67. const headers = {};
  68. for (const key of Object.keys(response.headers)) {
  69. headers[key] = response.headers[key];
  70. }
  71. throw new GraphqlResponseError(requestOptions, headers, response.data);
  72. }
  73. return response.data.data;
  74. });
  75. }
  76. function withDefaults(request$1, newDefaults) {
  77. const newRequest = request$1.defaults(newDefaults);
  78. const newApi = (query, options) => {
  79. return graphql(newRequest, query, options);
  80. };
  81. return Object.assign(newApi, {
  82. defaults: withDefaults.bind(null, newRequest),
  83. endpoint: request.endpoint,
  84. });
  85. }
  86. const graphql$1 = withDefaults(request, {
  87. headers: {
  88. "user-agent": `octokit-graphql.js/${VERSION} ${getUserAgent()}`,
  89. },
  90. method: "POST",
  91. url: "/graphql",
  92. });
  93. function withCustomRequest(customRequest) {
  94. return withDefaults(customRequest, {
  95. method: "POST",
  96. url: "/graphql",
  97. });
  98. }
  99. export { GraphqlResponseError, graphql$1 as graphql, withCustomRequest };
  100. //# sourceMappingURL=index.js.map