|
1 | 1 | import win from "../../window" |
2 | 2 | import { Map } from "immutable" |
3 | | -import Url from "url-parse" |
4 | 3 |
|
5 | 4 | /** |
6 | 5 | * if duplicate key name existed from FormData entries, |
@@ -154,64 +153,3 @@ export const requestSnippetGenerator_curl_bash = (request) => { |
154 | 153 | export const requestSnippetGenerator_curl_cmd = (request) => { |
155 | 154 | return curlify(request, escapeCMD, "^\n") |
156 | 155 | } |
157 | | - |
158 | | -// eslint-disable-next-line camelcase |
159 | | -export const requestSnippetGenerator_node_native = (request) => { |
160 | | - const url = new Url(request.get("url")) |
161 | | - let isMultipartFormDataRequest = false |
162 | | - const headers = request.get("headers") |
163 | | - if(headers && headers.size) { |
164 | | - request.get("headers").map((val, key) => { |
165 | | - isMultipartFormDataRequest = isMultipartFormDataRequest || /^content-type$/i.test(key) && /^multipart\/form-data$/i.test(val) |
166 | | - }) |
167 | | - } |
168 | | - const packageStr = url.protocol === "https:" ? "https" : "http" |
169 | | - let reqBody = request.get("body") |
170 | | - if (request.get("body")) { |
171 | | - if (isMultipartFormDataRequest && ["POST", "PUT", "PATCH"].includes(request.get("method"))) { |
172 | | - return "throw new Error(\"Currently unsupported content-type: /^multipart\\/form-data$/i\");" |
173 | | - } else { |
174 | | - if (!Map.isMap(reqBody)) { |
175 | | - if (typeof reqBody !== "string") { |
176 | | - reqBody = JSON.stringify(reqBody) |
177 | | - } |
178 | | - } else { |
179 | | - reqBody = getStringBodyOfMap(request) |
180 | | - } |
181 | | - } |
182 | | - } else if (!request.get("body") && request.get("method") === "POST") { |
183 | | - reqBody = "" |
184 | | - } |
185 | | - |
186 | | - const stringBody = "`" + (reqBody || "") |
187 | | - .replace(/\\n/g, "\n") |
188 | | - .replace(/`/g, "\\`") |
189 | | - + "`" |
190 | | - |
191 | | - return `const http = require("${packageStr}"); |
192 | | -
|
193 | | -const options = { |
194 | | - "method": "${request.get("method")}", |
195 | | - "hostname": "${url.host}", |
196 | | - "port": ${url.port || "null"}, |
197 | | - "path": "${url.pathname}"${headers && headers.size ? `, |
198 | | - "headers": { |
199 | | - ${request.get("headers").map((val, key) => `"${key}": "${val}"`).valueSeq().join(",\n ")} |
200 | | - }` : ""} |
201 | | -}; |
202 | | -
|
203 | | -const req = http.request(options, function (res) { |
204 | | - const chunks = []; |
205 | | -
|
206 | | - res.on("data", function (chunk) { |
207 | | - chunks.push(chunk); |
208 | | - }); |
209 | | -
|
210 | | - res.on("end", function () { |
211 | | - const body = Buffer.concat(chunks); |
212 | | - console.log(body.toString()); |
213 | | - }); |
214 | | -}); |
215 | | -${reqBody ? `\nreq.write(${stringBody});` : ""} |
216 | | -req.end();` |
217 | | -} |
0 commit comments