Added separated code generator code for cURL, JS Fetch and JS XHR
This commit is contained in:
61
helpers/codegen/generators/js-fetch.js
Normal file
61
helpers/codegen/generators/js-fetch.js
Normal file
@@ -0,0 +1,61 @@
|
||||
export const JSFetchCodegen = {
|
||||
id: "js-fetch",
|
||||
name: "JavaScript Fetch",
|
||||
generator: ({
|
||||
url,
|
||||
pathName,
|
||||
queryString,
|
||||
auth,
|
||||
httpUser,
|
||||
httpPassword,
|
||||
bearerToken,
|
||||
method,
|
||||
rawInput,
|
||||
rawParams,
|
||||
rawRequestBody,
|
||||
contentType,
|
||||
headers,
|
||||
}) => {
|
||||
const requestString = []
|
||||
let genHeaders = []
|
||||
requestString.push(`fetch("${url}${pathName}${queryString}", {\n`)
|
||||
requestString.push(` method: "${method}",\n`)
|
||||
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
|
||||
if (isJSONContentType(contentType)) {
|
||||
requestBody = `JSON.stringify(${requestBody})`
|
||||
} else if (contentType.includes("x-www-form-urlencoded")) {
|
||||
requestBody = `"${requestBody}"`
|
||||
}
|
||||
|
||||
requestString.push(` body: ${requestBody},\n`)
|
||||
genHeaders.push(` "Content-Type": "${contentType}; charset=utf-8",\n`)
|
||||
}
|
||||
if (headers) {
|
||||
headers.forEach(({ key, value }) => {
|
||||
if (key) genHeaders.push(` "${key}": "${value}",\n`)
|
||||
})
|
||||
}
|
||||
genHeaders = genHeaders.join("").slice(0, -2)
|
||||
requestString.push(` headers: {\n${genHeaders}\n },\n`)
|
||||
requestString.push(' credentials: "same-origin"\n')
|
||||
requestString.push("}).then(function(response) {\n")
|
||||
requestString.push(" response.status\n")
|
||||
requestString.push(" response.statusText\n")
|
||||
requestString.push(" response.headers\n")
|
||||
requestString.push(" response.url\n\n")
|
||||
requestString.push(" return response.text()\n")
|
||||
requestString.push("}).catch(function(error) {\n")
|
||||
requestString.push(" error.message\n")
|
||||
requestString.push("})")
|
||||
return requestString.join("")
|
||||
},
|
||||
}
|
||||
Reference in New Issue
Block a user