refactor: revamp the importers & exporters systems to be reused (#3425)

Co-authored-by: jamesgeorge007 <jamesgeorge998001@gmail.com>
This commit is contained in:
Akash K
2023-12-06 21:24:29 +05:30
committed by GitHub
parent d9c75ed79e
commit ab7c29d228
90 changed files with 2399 additions and 1892 deletions

View File

@@ -16,12 +16,18 @@ import { HoppRESTRequest, HoppCollection } from "@hoppscotch/data"
import { appendRESTCollections } from "~/newstore/collections"
import { useI18n } from "@composables/i18n"
import { useToast } from "@composables/toast"
import { URLImporters } from "~/helpers/import-export/import/importers"
import { IMPORTER_INVALID_FILE_FORMAT } from "~/helpers/import-export/import"
import { OPENAPI_DEREF_ERROR } from "~/helpers/import-export/import/openapi"
import { isOfType } from "~/helpers/functional/primtive"
import { TELeftType } from "~/helpers/functional/taskEither"
import {
hoppRESTImporter,
hoppPostmanImporter,
hoppInsomniaImporter,
hoppOpenAPIImporter,
} from "~/helpers/import-export/import/importers"
const route = useRoute()
const router = useRouter()
const toast = useToast()
@@ -30,16 +36,33 @@ const t = useI18n()
const IMPORTER_INVALID_TYPE = "importer_invalid_type" as const
const IMPORTER_INVALID_FETCH = "importer_invalid_fetch" as const
// TODO: move this to importers after moving the importer metadatas from respective components to imports/*.ts file
const URLImporters = [
{
id: "hoppscotch",
importer: hoppRESTImporter,
},
{
id: "postman",
importer: hoppPostmanImporter,
},
{
id: "insomnia",
importer: hoppInsomniaImporter,
},
{
id: "openapi",
importer: hoppOpenAPIImporter,
},
]
const importCollections = (url: unknown, type: unknown) =>
pipe(
TE.Do,
TE.bind("importer", () =>
pipe(
URLImporters,
RA.findFirst(
(importer) =>
importer.applicableTo.includes("url-import") && importer.id === type
),
RA.findFirst((importer) => importer.id === type),
TE.fromOption(() => IMPORTER_INVALID_TYPE)
)
),
@@ -56,7 +79,7 @@ const importCollections = (url: unknown, type: unknown) =>
content.data,
TO.fromPredicate(isOfType("string")),
TE.fromTaskOption(() => IMPORTER_INVALID_FILE_FORMAT),
TE.chain((data) => importer.importer([data]))
TE.chain((data) => importer.importer(data))
)
)
)