helpers.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import * as A from "fp-ts/Array"
  2. import * as E from "fp-ts/Either"
  3. import * as TE from "fp-ts/TaskEither"
  4. import { pipe, flow } from "fp-ts/function"
  5. import {
  6. HoppCollection,
  7. HoppRESTRequest,
  8. makeCollection,
  9. translateToNewRequest,
  10. } from "@hoppscotch/data"
  11. import { TeamCollection } from "../teams/TeamCollection"
  12. import { TeamRequest } from "../teams/TeamRequest"
  13. import { GQLError, runGQLQuery } from "./GQLClient"
  14. import {
  15. GetCollectionChildrenIDsDocument,
  16. GetCollectionRequestsDocument,
  17. GetCollectionTitleDocument,
  18. } from "./graphql"
  19. export const BACKEND_PAGE_SIZE = 10
  20. const getCollectionChildrenIDs = async (collID: string) => {
  21. const collsList: string[] = []
  22. while (true) {
  23. const data = await runGQLQuery({
  24. query: GetCollectionChildrenIDsDocument,
  25. variables: {
  26. collectionID: collID,
  27. cursor:
  28. collsList.length > 0 ? collsList[collsList.length - 1] : undefined,
  29. },
  30. })
  31. if (E.isLeft(data)) {
  32. return E.left(data.left)
  33. }
  34. collsList.push(...data.right.collection!.children.map((x) => x.id))
  35. if (data.right.collection!.children.length !== BACKEND_PAGE_SIZE) break
  36. }
  37. return E.right(collsList)
  38. }
  39. const getCollectionRequests = async (collID: string) => {
  40. const reqList: TeamRequest[] = []
  41. while (true) {
  42. const data = await runGQLQuery({
  43. query: GetCollectionRequestsDocument,
  44. variables: {
  45. collectionID: collID,
  46. cursor: reqList.length > 0 ? reqList[reqList.length - 1].id : undefined,
  47. },
  48. })
  49. if (E.isLeft(data)) {
  50. return E.left(data.left)
  51. }
  52. reqList.push(
  53. ...data.right.requestsInCollection.map(
  54. (x) =>
  55. <TeamRequest>{
  56. id: x.id,
  57. request: translateToNewRequest(JSON.parse(x.request)),
  58. collectionID: collID,
  59. title: x.title,
  60. }
  61. )
  62. )
  63. if (data.right.requestsInCollection.length !== BACKEND_PAGE_SIZE) break
  64. }
  65. return E.right(reqList)
  66. }
  67. export const getCompleteCollectionTree = (
  68. collID: string
  69. ): TE.TaskEither<GQLError<string>, TeamCollection> =>
  70. pipe(
  71. TE.Do,
  72. TE.bind("title", () =>
  73. pipe(
  74. () =>
  75. runGQLQuery({
  76. query: GetCollectionTitleDocument,
  77. variables: {
  78. collectionID: collID,
  79. },
  80. }),
  81. TE.map((x) => x.collection!.title)
  82. )
  83. ),
  84. TE.bind("children", () =>
  85. pipe(
  86. // TaskEither -> () => Promise<Either>
  87. () => getCollectionChildrenIDs(collID),
  88. TE.chain(flow(A.map(getCompleteCollectionTree), TE.sequenceArray))
  89. )
  90. ),
  91. TE.bind("requests", () => () => getCollectionRequests(collID)),
  92. TE.map(
  93. ({ title, children, requests }) =>
  94. <TeamCollection>{
  95. id: collID,
  96. children,
  97. requests,
  98. title,
  99. }
  100. )
  101. )
  102. export const teamCollToHoppRESTColl = (
  103. coll: TeamCollection
  104. ): HoppCollection<HoppRESTRequest> =>
  105. makeCollection({
  106. name: coll.title,
  107. folders: coll.children?.map(teamCollToHoppRESTColl) ?? [],
  108. requests: coll.requests?.map((x) => x.request) ?? [],
  109. })