nodejs-axios.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. export const NodejsAxiosCodegen = {
  2. id: "nodejs-axios",
  3. name: "NodeJs Axios",
  4. language: "javascript",
  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. contentType,
  18. headers,
  19. }) => {
  20. const requestString = []
  21. const genHeaders = []
  22. const requestBody = rawInput ? rawParams : rawRequestBody
  23. requestString.push(
  24. `axios.${method.toLowerCase()}('${url}${pathName}?${queryString}'`
  25. )
  26. if (requestBody.length !== 0) {
  27. requestString.push(", ")
  28. }
  29. if (headers) {
  30. headers.forEach(({ key, value }) => {
  31. if (key) genHeaders.push(` "${key}": "${value}",\n`)
  32. })
  33. }
  34. if (contentType) {
  35. genHeaders.push(`"Content-Type": "${contentType}; charset=utf-8",\n`)
  36. }
  37. if (auth === "Basic Auth") {
  38. const basic = `${httpUser}:${httpPassword}`
  39. genHeaders.push(
  40. ` "Authorization": "Basic ${window.btoa(
  41. unescape(encodeURIComponent(basic))
  42. )}",\n`
  43. )
  44. } else if (auth === "Bearer Token" || auth === "OAuth 2.0") {
  45. genHeaders.push(` "Authorization": "Bearer ${bearerToken}",\n`)
  46. }
  47. requestString.push(
  48. `${requestBody},{ \n headers : {${genHeaders.join("").slice(0, -2)}}\n})`
  49. )
  50. requestString.push(".then(response => {\n")
  51. requestString.push(" console.log(response);\n")
  52. requestString.push("})")
  53. requestString.push(".catch(e => {\n")
  54. requestString.push(" console.error(e);\n")
  55. requestString.push("})\n")
  56. return requestString.join("")
  57. },
  58. }