123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- import * as A from "fp-ts/Array"
- import * as E from "fp-ts/Either"
- import * as TE from "fp-ts/TaskEither"
- import { pipe, flow } from "fp-ts/function"
- import {
- HoppCollection,
- HoppRESTRequest,
- makeCollection,
- translateToNewRequest,
- } from "@hoppscotch/data"
- import { TeamCollection } from "../teams/TeamCollection"
- import { TeamRequest } from "../teams/TeamRequest"
- import { GQLError, runGQLQuery } from "./GQLClient"
- import {
- GetCollectionChildrenIDsDocument,
- GetCollectionRequestsDocument,
- GetCollectionTitleDocument,
- } from "./graphql"
- export const BACKEND_PAGE_SIZE = 10
- const getCollectionChildrenIDs = async (collID: string) => {
- const collsList: string[] = []
- while (true) {
- const data = await runGQLQuery({
- query: GetCollectionChildrenIDsDocument,
- variables: {
- collectionID: collID,
- cursor:
- collsList.length > 0 ? collsList[collsList.length - 1] : undefined,
- },
- })
- if (E.isLeft(data)) {
- return E.left(data.left)
- }
- collsList.push(...data.right.collection!.children.map((x) => x.id))
- if (data.right.collection!.children.length !== BACKEND_PAGE_SIZE) break
- }
- return E.right(collsList)
- }
- const getCollectionRequests = async (collID: string) => {
- const reqList: TeamRequest[] = []
- while (true) {
- const data = await runGQLQuery({
- query: GetCollectionRequestsDocument,
- variables: {
- collectionID: collID,
- cursor: reqList.length > 0 ? reqList[reqList.length - 1].id : undefined,
- },
- })
- if (E.isLeft(data)) {
- return E.left(data.left)
- }
- reqList.push(
- ...data.right.requestsInCollection.map(
- (x) =>
- <TeamRequest>{
- id: x.id,
- request: translateToNewRequest(JSON.parse(x.request)),
- collectionID: collID,
- title: x.title,
- }
- )
- )
- if (data.right.requestsInCollection.length !== BACKEND_PAGE_SIZE) break
- }
- return E.right(reqList)
- }
- export const getCompleteCollectionTree = (
- collID: string
- ): TE.TaskEither<GQLError<string>, TeamCollection> =>
- pipe(
- TE.Do,
- TE.bind("title", () =>
- pipe(
- () =>
- runGQLQuery({
- query: GetCollectionTitleDocument,
- variables: {
- collectionID: collID,
- },
- }),
- TE.map((x) => x.collection!.title)
- )
- ),
- TE.bind("children", () =>
- pipe(
- // TaskEither -> () => Promise<Either>
- () => getCollectionChildrenIDs(collID),
- TE.chain(flow(A.map(getCompleteCollectionTree), TE.sequenceArray))
- )
- ),
- TE.bind("requests", () => () => getCollectionRequests(collID)),
- TE.map(
- ({ title, children, requests }) =>
- <TeamCollection>{
- id: collID,
- children,
- requests,
- title,
- }
- )
- )
- export const teamCollToHoppRESTColl = (
- coll: TeamCollection
- ): HoppCollection<HoppRESTRequest> =>
- makeCollection({
- name: coll.title,
- folders: coll.children?.map(teamCollToHoppRESTColl) ?? [],
- requests: coll.requests?.map((x) => x.request) ?? [],
- })
|