Add nodejs request codegenerator (#1227)

Co-authored-by: Liyas Thomas <liyascthomas@gmail.com>
This commit is contained in:
Yasio
2020-10-04 14:41:07 +02:00
committed by GitHub
parent 745683f2a8
commit cb944bcfb2
2 changed files with 74 additions and 1 deletions

View File

@@ -3,6 +3,7 @@ import { JSFetchCodegen } from "./generators/js-fetch"
import { CurlCodegen } from "./generators/curl"
import { JSAxiosCodegen } from "./generators/js-axios"
import { GoNativeCodegen } from "./generators/go-native"
import { NodeJsRequestCodegen } from "./generators/nodejs-request"
/* Register code generators here.
* A code generator is defined as an object with the following structure.
@@ -12,7 +13,7 @@ import { GoNativeCodegen } from "./generators/go-native"
* generator: (ctx) => string
*
*/
export const codegens = [JSXHRCodegen, JSFetchCodegen, CurlCodegen, JSAxiosCodegen, GoNativeCodegen]
export const codegens = [JSXHRCodegen, JSFetchCodegen, CurlCodegen, JSAxiosCodegen, GoNativeCodegen, NodeJsRequestCodegen]
export function generateCodeWithGenerator(codegenID, context) {
if (codegenID) {

View File

@@ -0,0 +1,72 @@
import { isJSONContentType } from "~/helpers/utils/contenttypes"
export const NodeJsRequestCodegen = {
id: "nodejs-request",
name: "NodeJs Request",
generator: ({
url,
pathName,
queryString,
auth,
httpUser,
httpPassword,
bearerToken,
method,
rawInput,
rawParams,
rawRequestBody,
contentType,
headers,
}) => {
const requestString = []
let genHeaders = []
requestString.push(`const request = require('request');\n`)
requestString.push(`const options = {\n`)
requestString.push(` method: '${method.toLowerCase()}',\n`)
requestString.push(` url: '${url}${pathName}${queryString}'`)
if (auth === "Basic Auth") {
const basic = `${httpUser}:${httpPassword}`
genHeaders.push(
` "Authorization": "Basic ${window.btoa(unescape(encodeURIComponent(basic)))}",\n`
)
} else if (auth === "Bearer Token" || auth === "OAuth 2.0") {
genHeaders.push(` "Authorization": "Bearer ${bearerToken}",\n`)
}
if (["POST", "PUT", "PATCH", "DELETE"].includes(method)) {
let requestBody = rawInput ? rawParams : rawRequestBody
let reqBodyType = 'formData'
if (isJSONContentType(contentType)) {
requestBody = `JSON.stringify(${requestBody})`
reqBodyType = 'body'
} else if (contentType.includes("x-www-form-urlencoded")) {
if (requestBody.indexOf('=') > -1) {
const rq = requestBody.split('=')
requestBody = `{"${rq[0]}": "${rq[1]}"}`
}
reqBodyType = 'form'
}
if (contentType) {
genHeaders.push(` "Content-Type": "${contentType}; charset=utf-8",\n`)
}
requestString.push(`,\n ${reqBodyType}: ${requestBody}`)
}
if (headers.length > 0) {
headers.forEach(({ key, value }) => {
if (key) genHeaders.push(` "${key}": "${value}",\n`)
})
}
if (genHeaders.length > 0 || headers.length > 0) {
requestString.push(`,\n headers: {\n${genHeaders.join("").slice(0, -2)}\n }`)
}
requestString.push(`\n}`)
requestString.push(`\nrequest(options, (error, response) => {\n`)
requestString.push(` if (error) throw new Error(error);\n`)
requestString.push(` console.log(response.body);\n`)
requestString.push(`});`)
return requestString.join("")
},
}