From 2373a83fe2a43ddd250f0d4153ddb52c70bffa13 Mon Sep 17 00:00:00 2001 From: Tim Lai Date: Wed, 14 Apr 2021 10:55:31 -0700 Subject: [PATCH] feat: remove node_native option from request snippets plugin (#7181) * snippet generator support intended for different shell options only * will not maintain snippet generator for various languages --- docs/usage/configuration.md | 2 +- src/core/index.js | 4 -- src/core/plugins/request-snippets/fn.js | 62 ------------------------- 3 files changed, 1 insertion(+), 67 deletions(-) diff --git a/docs/usage/configuration.md b/docs/usage/configuration.md index 16c4383b499..39ee5cd283f 100644 --- a/docs/usage/configuration.md +++ b/docs/usage/configuration.md @@ -65,7 +65,7 @@ Parameter name | Docker variable | Description `syntaxHighlight.activate` | _Unavailable_ | `Boolean=true`. Whether syntax highlighting should be activated or not. `syntaxHighlight.theme` | _Unavailable_ | `String=["agate"*, "arta", "monokai", "nord", "obsidian", "tomorrow-night"]`. [Highlight.js](https://highlightjs.org/static/demo/) syntax coloring theme to use. (Only these 6 styles are available.) `tryItOutEnabled` | `TRY_IT_OUT_ENABLED` | `Boolean=false`. Controls whether the "Try it out" section should be enabled by default. -`requestSnippets` | _Unavailable_ | `Object`. This is the default configuration section for the the requestSnippets plugin.
requestSnippets: {
  generators: {
    "curl_bash": {
      title: "cURL (bash)",
      syntax: "bash"
    },
    "curl_powershell": {
      title: "cURL (PowerShell)",
      syntax: "powershell"
    },
    "curl_cmd": {
      title: "cURL (CMD)",
      syntax: "bash"
    },
    "node_native": {
      title: "Node.js (Native)",
      syntax: "javascript"
    },
  },
  defaultExpanded: true,
  languagesMask: null, // e.g. only show curl bash = \["curl_bash"\]
}, +`requestSnippets` | _Unavailable_ | `Object`. This is the default configuration section for the the requestSnippets plugin.
requestSnippets: {
  generators: {
    "curl_bash": {
      title: "cURL (bash)",
      syntax: "bash"
    },
    "curl_powershell": {
      title: "cURL (PowerShell)",
      syntax: "powershell"
    },
    "curl_cmd": {
      title: "cURL (CMD)",
      syntax: "bash"
    },
  },
  defaultExpanded: true,
  languagesMask: null, // e.g. only show curl bash = \["curl_bash"\]
}, ##### Network diff --git a/src/core/index.js b/src/core/index.js index 930305ea0be..000b1d3c8d0 100644 --- a/src/core/index.js +++ b/src/core/index.js @@ -68,10 +68,6 @@ export default function SwaggerUI(opts) { title: "cURL (CMD)", syntax: "bash" }, - "node_native": { - title: "Node.js (Native)", - syntax: "javascript" - }, }, defaultExpanded: true, languagesMask: null, // e.g. only show curl bash = ["curl_bash"] diff --git a/src/core/plugins/request-snippets/fn.js b/src/core/plugins/request-snippets/fn.js index f692d2d37b3..d456426cd98 100644 --- a/src/core/plugins/request-snippets/fn.js +++ b/src/core/plugins/request-snippets/fn.js @@ -1,6 +1,5 @@ import win from "../../window" import { Map } from "immutable" -import Url from "url-parse" /** * if duplicate key name existed from FormData entries, @@ -154,64 +153,3 @@ export const requestSnippetGenerator_curl_bash = (request) => { export const requestSnippetGenerator_curl_cmd = (request) => { return curlify(request, escapeCMD, "^\n") } - -// eslint-disable-next-line camelcase -export const requestSnippetGenerator_node_native = (request) => { - const url = new Url(request.get("url")) - let isMultipartFormDataRequest = false - const headers = request.get("headers") - if(headers && headers.size) { - request.get("headers").map((val, key) => { - isMultipartFormDataRequest = isMultipartFormDataRequest || /^content-type$/i.test(key) && /^multipart\/form-data$/i.test(val) - }) - } - const packageStr = url.protocol === "https:" ? "https" : "http" - let reqBody = request.get("body") - if (request.get("body")) { - if (isMultipartFormDataRequest && ["POST", "PUT", "PATCH"].includes(request.get("method"))) { - return "throw new Error(\"Currently unsupported content-type: /^multipart\\/form-data$/i\");" - } else { - if (!Map.isMap(reqBody)) { - if (typeof reqBody !== "string") { - reqBody = JSON.stringify(reqBody) - } - } else { - reqBody = getStringBodyOfMap(request) - } - } - } else if (!request.get("body") && request.get("method") === "POST") { - reqBody = "" - } - - const stringBody = "`" + (reqBody || "") - .replace(/\\n/g, "\n") - .replace(/`/g, "\\`") - + "`" - - return `const http = require("${packageStr}"); - -const options = { - "method": "${request.get("method")}", - "hostname": "${url.host}", - "port": ${url.port || "null"}, - "path": "${url.pathname}"${headers && headers.size ? `, - "headers": { - ${request.get("headers").map((val, key) => `"${key}": "${val}"`).valueSeq().join(",\n ")} - }` : ""} -}; - -const req = http.request(options, function (res) { - const chunks = []; - - res.on("data", function (chunk) { - chunks.push(chunk); - }); - - res.on("end", function () { - const body = Buffer.concat(chunks); - console.log(body.toString()); - }); -}); -${reqBody ? `\nreq.write(${stringBody});` : ""} -req.end();` -}