github.com/jfrog/frogbot@v1.1.1-0.20231221090046-821a26f50338/action/node_modules/@octokit/graphql/dist-node/index.js (about)

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