forked from hoppscotch/hoppscotch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added separated code generator code for cURL, JS Fetch and JS XHR
- Loading branch information
1 parent
66cd75d
commit 70fc078
Showing
3 changed files
with
151 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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") | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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("") | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
export const JSXHRCodegen = { | ||
id: "js-xhr", | ||
name: "JavaScript XHR", | ||
generator: ({ | ||
auth, | ||
httpUser, | ||
httpPassword, | ||
method, | ||
url, | ||
pathName, | ||
queryString, | ||
bearerToken, | ||
headers, | ||
rawInput, | ||
rawParams, | ||
rawRequestBody, | ||
contentType, | ||
}) => { | ||
const requestString = [] | ||
requestString.push("const xhr = new XMLHttpRequest()") | ||
|
||
const user = auth === "Basic Auth" ? `'${httpUser}'` : null | ||
const password = auth === "Basic Auth" ? `'${httpPassword}'` : null | ||
requestString.push( | ||
`xhr.open('${method}', '${url}${pathName}${queryString}', true, ${user}, ${password})` | ||
) | ||
if (auth === "Bearer Token" || auth === "OAuth 2.0") { | ||
requestString.push(`xhr.setRequestHeader('Authorization', 'Bearer ${bearerToken}')`) | ||
} | ||
if (headers) { | ||
headers.forEach(({ key, value }) => { | ||
if (key) requestString.push(`xhr.setRequestHeader('${key}', '${value}')`) | ||
}) | ||
} | ||
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(`xhr.setRequestHeader('Content-Type', '${contentType}; charset=utf-8')`) | ||
requestString.push(`xhr.send(${requestBody})`) | ||
} else { | ||
requestString.push("xhr.send()") | ||
} | ||
return requestString.join("\n") | ||
}, | ||
} |