queries.ts 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import { pipe, flow } from "fp-ts/function"
  2. import * as O from "fp-ts/Option"
  3. import * as A from "fp-ts/Array"
  4. import * as Sep from "fp-ts/Separated"
  5. import { HoppRESTParam } from "@hoppscotch/data"
  6. const isDangling = ([_, value]: [string, string]) => !value
  7. /**
  8. * Converts queries to HoppRESTParam format and separates dangling ones
  9. * @param params Array of key value pairs of queries
  10. * @returns Object containing separated queries and dangling queries
  11. */
  12. export function getQueries(params: Array<[string, string]>): {
  13. queries: Array<HoppRESTParam>
  14. danglingParams: Array<string>
  15. } {
  16. return pipe(
  17. params,
  18. O.of,
  19. O.map(
  20. flow(
  21. A.partition(isDangling),
  22. Sep.bimap(
  23. A.map(([key, value]) => ({
  24. key,
  25. value,
  26. active: true,
  27. })),
  28. A.map(([key]) => key)
  29. ),
  30. (sep) => ({
  31. queries: sep.left,
  32. danglingParams: sep.right,
  33. })
  34. )
  35. ),
  36. O.getOrElseW(() => ({
  37. queries: [],
  38. danglingParams: [],
  39. }))
  40. )
  41. }