UNPKG

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