UNPKG

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