RESTExtURLParams.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import { FormDataKeyValue, HoppRESTRequest } from "@hoppscotch/data"
  2. import { isJSONContentType } from "./utils/contenttypes"
  3. import { getDefaultRESTRequest } from "~/newstore/RESTSession"
  4. /**
  5. * Handles translations for all the hopp.io REST Shareable URL params
  6. */
  7. export function translateExtURLParams(
  8. urlParams: Record<string, any>
  9. ): HoppRESTRequest {
  10. if (urlParams.v) return parseV1ExtURL(urlParams)
  11. else return parseV0ExtURL(urlParams)
  12. }
  13. function parseV0ExtURL(urlParams: Record<string, any>): HoppRESTRequest {
  14. const resolvedReq = getDefaultRESTRequest()
  15. if (urlParams.method && typeof urlParams.method === "string") {
  16. resolvedReq.method = urlParams.method
  17. }
  18. if (urlParams.url && typeof urlParams.url === "string") {
  19. if (urlParams.path && typeof urlParams.path === "string") {
  20. resolvedReq.endpoint = `${urlParams.url}/${urlParams.path}`
  21. } else {
  22. resolvedReq.endpoint = urlParams.url
  23. }
  24. }
  25. if (urlParams.headers && typeof urlParams.headers === "string") {
  26. resolvedReq.headers = JSON.parse(urlParams.headers)
  27. }
  28. if (urlParams.params && typeof urlParams.params === "string") {
  29. resolvedReq.params = JSON.parse(urlParams.params)
  30. }
  31. if (urlParams.httpUser && typeof urlParams.httpUser === "string") {
  32. resolvedReq.auth = {
  33. authType: "basic",
  34. authActive: true,
  35. username: urlParams.httpUser,
  36. password: urlParams.httpPassword ?? "",
  37. }
  38. }
  39. if (urlParams.bearerToken && typeof urlParams.bearerToken === "string") {
  40. resolvedReq.auth = {
  41. authType: "bearer",
  42. authActive: true,
  43. token: urlParams.bearerToken,
  44. }
  45. }
  46. if (urlParams.contentType) {
  47. if (urlParams.contentType === "multipart/form-data") {
  48. resolvedReq.body = {
  49. contentType: "multipart/form-data",
  50. body: JSON.parse(urlParams.bodyParams || "[]").map(
  51. (x: any) =>
  52. <FormDataKeyValue>{
  53. active: x.active,
  54. key: x.key,
  55. value: x.value,
  56. isFile: false,
  57. }
  58. ),
  59. }
  60. } else if (isJSONContentType(urlParams.contentType)) {
  61. if (urlParams.rawParams) {
  62. resolvedReq.body = {
  63. contentType: urlParams.contentType,
  64. body: urlParams.rawParams,
  65. }
  66. } else {
  67. resolvedReq.body = {
  68. contentType: urlParams.contentType,
  69. body: urlParams.bodyParams,
  70. }
  71. }
  72. } else {
  73. resolvedReq.body = {
  74. contentType: urlParams.contentType,
  75. body: urlParams.rawParams,
  76. }
  77. }
  78. }
  79. return resolvedReq
  80. }
  81. function parseV1ExtURL(urlParams: Record<string, any>): HoppRESTRequest {
  82. const resolvedReq = getDefaultRESTRequest()
  83. if (urlParams.headers && typeof urlParams.headers === "string") {
  84. resolvedReq.headers = JSON.parse(urlParams.headers)
  85. }
  86. if (urlParams.params && typeof urlParams.params === "string") {
  87. resolvedReq.params = JSON.parse(urlParams.params)
  88. }
  89. if (urlParams.method && typeof urlParams.method === "string") {
  90. resolvedReq.method = urlParams.method
  91. }
  92. if (urlParams.endpoint && typeof urlParams.endpoint === "string") {
  93. resolvedReq.endpoint = urlParams.endpoint
  94. }
  95. if (urlParams.body && typeof urlParams.body === "string") {
  96. resolvedReq.body = JSON.parse(urlParams.body)
  97. }
  98. return resolvedReq
  99. }