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
This commit is contained in:
@@ -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();`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user