1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, '__esModule', { value: true });
|
4 |
|
5 | var request = require('@octokit/request');
|
6 | var universalUserAgent = require('universal-user-agent');
|
7 |
|
8 | const VERSION = "5.0.5";
|
9 |
|
10 | function _buildMessageForResponseErrors(data) {
|
11 | return `Request failed due to following response errors:\n` + data.errors.map(e => ` - ${e.message}`).join("\n");
|
12 | }
|
13 | class 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 |
|
21 | this.errors = response.errors;
|
22 | this.data = response.data;
|
23 |
|
24 |
|
25 | if (Error.captureStackTrace) {
|
26 | Error.captureStackTrace(this, this.constructor);
|
27 | }
|
28 | }
|
29 | }
|
30 |
|
31 | const NON_VARIABLE_OPTIONS = ["method", "baseUrl", "url", "headers", "request", "query", "mediaType"];
|
32 | const FORBIDDEN_VARIABLE_OPTIONS = ["query", "method", "url"];
|
33 | const GHES_V3_SUFFIX_REGEX = /\/api\/v3\/?$/;
|
34 | function 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 |
|
59 |
|
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 |
|
76 | function 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 |
|
87 | const graphql$1 = withDefaults(request.request, {
|
88 | headers: {
|
89 | "user-agent": `octokit-graphql.js/${VERSION} ${universalUserAgent.getUserAgent()}`
|
90 | },
|
91 | method: "POST",
|
92 | url: "/graphql"
|
93 | });
|
94 | function withCustomRequest(customRequest) {
|
95 | return withDefaults(customRequest, {
|
96 | method: "POST",
|
97 | url: "/graphql"
|
98 | });
|
99 | }
|
100 |
|
101 | exports.GraphqlResponseError = GraphqlResponseError;
|
102 | exports.graphql = graphql$1;
|
103 | exports.withCustomRequest = withCustomRequest;
|
104 |
|