RESTExtURLParams.ts 3.2 KB

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