fix: code generators (#1985)

Co-authored-by: liyasthomas <liyascthomas@gmail.com>
This commit is contained in:
Deepanshu Dhruw
2021-11-30 07:46:45 +05:30
committed by GitHub
parent 2a59557851
commit 520ac8ede5
21 changed files with 285 additions and 263 deletions

View File

@@ -13,8 +13,8 @@ export const JavascriptJqueryCodegen = {
method,
rawInput,
rawParams,
rawRequestBody,
contentType,
rawRequestBody,
headers,
}) => {
const requestString = []
@@ -24,10 +24,15 @@ export const JavascriptJqueryCodegen = {
`jQuery.ajax({\n url: "${url}${pathName}?${queryString}"`
)
requestString.push(`,\n method: "${method.toUpperCase()}"`)
const requestBody = rawInput ? rawParams : rawRequestBody
let requestBody = rawInput ? rawParams : rawRequestBody
if (requestBody.length !== 0) {
requestString.push(`,\n body: ${requestBody}`)
if (["POST", "PUT", "PATCH", "DELETE"].includes(method)) {
if (contentType && contentType.includes("x-www-form-urlencoded")) {
requestBody = `"${requestBody}"`
} else {
requestBody = requestBody.replaceAll("}", " }")
}
requestString.push(`,\n data: ${requestBody}`)
}
if (headers) {
headers.forEach(({ key, value }) => {
@@ -35,11 +40,6 @@ export const JavascriptJqueryCodegen = {
})
}
if (contentType) {
genHeaders.push(` "Content-Type": "${contentType}; charset=utf-8",\n`)
requestString.push(`,\n contentType: "${contentType}; charset=utf-8"`)
}
if (auth === "Basic Auth") {
const basic = `${httpUser}:${httpPassword}`
genHeaders.push(
@@ -50,10 +50,12 @@ export const JavascriptJqueryCodegen = {
} else if (auth === "Bearer Token" || auth === "OAuth 2.0") {
genHeaders.push(` "Authorization": "Bearer ${bearerToken}",\n`)
}
requestString.push(
`,\n headers: {\n${genHeaders.join("").slice(0, -2)}\n }\n})`
)
requestString.push(".then(response => {\n")
if (genHeaders.length > 0) {
requestString.push(
`,\n headers: {\n${genHeaders.join("").slice(0, -2)}\n }`
)
}
requestString.push("\n}).then(response => {\n")
requestString.push(" console.log(response);\n")
requestString.push("})")
requestString.push(".catch(e => {\n")