Added separated code generator code for cURL, JS Fetch and JS XHR
This commit is contained in:
41
helpers/codegen/generators/curl.js
Normal file
41
helpers/codegen/generators/curl.js
Normal file
@@ -0,0 +1,41 @@
|
||||
export const CurlCodegen = {
|
||||
id: "curl",
|
||||
name: "cURL",
|
||||
generator: ({
|
||||
method,
|
||||
url,
|
||||
pathName,
|
||||
queryString,
|
||||
httpUser,
|
||||
httpPassword,
|
||||
bearerToken,
|
||||
headers,
|
||||
rawInput,
|
||||
rawParams,
|
||||
rawRequestBody,
|
||||
contentType,
|
||||
}) => {
|
||||
const requestString = []
|
||||
requestString.push(`curl -X ${method}`)
|
||||
requestString.push(` '${url}${pathName}${queryString}'`)
|
||||
if (auth === "Basic Auth") {
|
||||
const basic = `${httpUser}:${httpPassword}`
|
||||
requestString.push(
|
||||
` -H 'Authorization: Basic ${window.btoa(unescape(encodeURIComponent(basic)))}'`
|
||||
)
|
||||
} else if (auth === "Bearer Token" || auth === "OAuth 2.0") {
|
||||
requestString.push(` -H 'Authorization: Bearer ${bearerToken}'`)
|
||||
}
|
||||
if (headers) {
|
||||
headers.forEach(({ key, value }) => {
|
||||
if (key) requestString.push(` -H '${key}: ${value}'`)
|
||||
})
|
||||
}
|
||||
if (["POST", "PUT", "PATCH", "DELETE"].includes(method)) {
|
||||
const requestBody = rawInput ? rawParams : rawRequestBody
|
||||
requestString.push(` -H 'Content-Type: ${contentType}; charset=utf-8'`)
|
||||
requestString.push(` -d '${requestBody}'`)
|
||||
}
|
||||
return requestString.join(" \\\n")
|
||||
},
|
||||
}
|
||||
Reference in New Issue
Block a user