fix: collection parser for hopp importer

This commit is contained in:
liyasthomas
2021-12-23 13:04:47 +05:30
committed by Andrew Bastin
parent 681a957611
commit a9292eed9e
4 changed files with 24 additions and 5 deletions

View File

@@ -156,6 +156,7 @@
<script setup lang="ts"> <script setup lang="ts">
import { ref } from "@nuxtjs/composition-api" import { ref } from "@nuxtjs/composition-api"
import { HoppRESTRequest, translateToNewRequest } from "@hoppscotch/data" import { HoppRESTRequest, translateToNewRequest } from "@hoppscotch/data"
import * as E from "fp-ts/Either"
import { apolloClient } from "~/helpers/apollo" import { apolloClient } from "~/helpers/apollo"
import { import {
useAxios, useAxios,
@@ -173,6 +174,7 @@ import {
Collection, Collection,
makeCollection, makeCollection,
} from "~/newstore/collections" } from "~/newstore/collections"
import { RESTCollectionImporters } from "~/helpers/import-export/import"
const props = defineProps<{ const props = defineProps<{
show: boolean show: boolean
@@ -557,7 +559,7 @@ const importFromJSON = () => {
const reader = new FileReader() const reader = new FileReader()
reader.onload = ({ target }) => { reader.onload = async ({ target }) => {
let content = target!.result as string | null let content = target!.result as string | null
if (!content) { if (!content) {
@@ -566,6 +568,8 @@ const importFromJSON = () => {
} }
let collections = JSON.parse(content) let collections = JSON.parse(content)
await hoppImporterOutput(content)
if (isInsomniaCollection(collections)) { if (isInsomniaCollection(collections)) {
collections = parseInsomniaCollection(content) collections = parseInsomniaCollection(content)
content = JSON.stringify(collections) content = JSON.stringify(collections)
@@ -653,4 +657,16 @@ const exportJSON = () => {
URL.revokeObjectURL(url) URL.revokeObjectURL(url)
}, 1000) }, 1000)
} }
const hoppImporter = RESTCollectionImporters[0]
const hoppImporterOutput = async (content: string) => {
const importedFunc = await hoppImporter.importer()
const result = await importedFunc(content)()
if (E.isLeft(result)) {
console.log("error", result.left)
} else if (E.isRight(result)) {
console.log("success", result)
}
}
</script> </script>

View File

@@ -8,10 +8,13 @@ import {
translateToNewRESTCollection, translateToNewRESTCollection,
} from "~/newstore/collections" } from "~/newstore/collections"
const importer: HoppImporter<Collection<HoppRESTRequest>> = (content) => const importer: HoppImporter<Collection<HoppRESTRequest>[]> = (content) =>
pipe( pipe(
E.tryCatch( E.tryCatch(
() => translateToNewRESTCollection(content), () =>
JSON.parse(content).map((coll: any) =>
translateToNewRESTCollection(coll)
),
() => IMPORTER_INVALID_FILE_FORMAT () => IMPORTER_INVALID_FILE_FORMAT
), ),
TE.fromEither TE.fromEither

View File

@@ -30,7 +30,7 @@ export type HoppImporterDefintion<T> = {
} }
export const RESTCollectionImporters: HoppImporterDefintion< export const RESTCollectionImporters: HoppImporterDefintion<
Collection<HoppRESTRequest> Collection<HoppRESTRequest>[]
>[] = [ >[] = [
{ {
name: "Hoppscotch REST Collection", name: "Hoppscotch REST Collection",

View File

@@ -236,7 +236,7 @@
"from_postman": "Import from Postman", "from_postman": "Import from Postman",
"from_url": "Import from URL", "from_url": "Import from URL",
"gist_url": "Enter Gist URL", "gist_url": "Enter Gist URL",
"json": "Import from JSON", "json": "Import from file",
"title": "Import" "title": "Import"
}, },
"layout": { "layout": {