shell-httpie.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. export const ShellHttpieCodegen = {
  2. id: "shell-httpie",
  3. name: "Shell HTTPie",
  4. language: "sh",
  5. generator: ({
  6. url,
  7. pathName,
  8. queryString,
  9. auth,
  10. httpUser,
  11. httpPassword,
  12. bearerToken,
  13. method,
  14. rawInput,
  15. rawParams,
  16. rawRequestBody,
  17. headers,
  18. }) => {
  19. const methodsWithBody = ["POST", "PUT", "PATCH", "DELETE"]
  20. const includeBody = methodsWithBody.includes(method)
  21. const requestString = []
  22. let requestBody = rawInput ? rawParams : rawRequestBody
  23. if (requestBody && includeBody) {
  24. requestBody = requestBody.replace(/'/g, "\\'")
  25. // Send request body via redirected input
  26. requestString.push(`echo -n $'${requestBody}' | `)
  27. }
  28. // Executable itself
  29. requestString.push(`http`)
  30. // basic authentication
  31. if (auth === "Basic Auth") {
  32. requestString.push(` -a ${httpUser}:${httpPassword}`)
  33. }
  34. // URL
  35. let escapedUrl = `${url}${pathName}?${queryString}`
  36. escapedUrl = escapedUrl.replace(/'/g, "\\'")
  37. requestString.push(` ${method} $'${escapedUrl}'`)
  38. if (headers) {
  39. headers.forEach(({ key, value }) => {
  40. requestString.push(
  41. ` $'${key.replace(/'/g, "\\'")}:${value.replace(/'/g, "\\'")}'`
  42. )
  43. })
  44. }
  45. if (auth === "Bearer Token" || auth === "OAuth 2.0") {
  46. requestString.push(` 'Authorization:Bearer ${bearerToken}'`)
  47. }
  48. return requestString.join("")
  49. },
  50. }