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

@@ -21,6 +21,9 @@ export const JavascriptXhrCodegen = {
}) => {
const requestString = []
requestString.push("const xhr = new XMLHttpRequest()")
requestString.push(`xhr.addEventListener("readystatechange", function() {`)
requestString.push(` if(this.readyState === 4) {`)
requestString.push(` console.log(this.responseText)\n }\n})`)
const user = auth === "Basic Auth" ? `'${httpUser}'` : null
const password = auth === "Basic Auth" ? `'${httpPassword}'` : null
@@ -40,14 +43,14 @@ export const JavascriptXhrCodegen = {
}
if (["POST", "PUT", "PATCH", "DELETE"].includes(method)) {
let requestBody = rawInput ? rawParams : rawRequestBody
if (isJSONContentType(contentType)) {
requestBody = `JSON.stringify(${requestBody})`
} else if (contentType.includes("x-www-form-urlencoded")) {
requestBody = `"${requestBody}"`
if (contentType && requestBody) {
if (isJSONContentType(contentType)) {
requestBody = `JSON.stringify(${requestBody})`
} else if (contentType.includes("x-www-form-urlencoded")) {
requestBody = `"${requestBody}"`
}
}
requestString.push(
`xhr.setRequestHeader('Content-Type', '${contentType}; charset=utf-8')`
)
requestBody = requestBody || ""
requestString.push(`xhr.send(${requestBody})`)
} else {
requestString.push("xhr.send()")