feat: import hoppscotch collections from file
This commit is contained in:
committed by
Andrew Bastin
parent
fac82200d1
commit
d6ec3158bc
@@ -34,11 +34,13 @@
|
|||||||
class="flex space-y-4 flex-col"
|
class="flex space-y-4 flex-col"
|
||||||
>
|
>
|
||||||
<p>{{ t("import.json_description") }}</p>
|
<p>{{ t("import.json_description") }}</p>
|
||||||
<label for="inputChooseFileToImportFrom" class="bg-pink-200">
|
<label
|
||||||
bbb
|
for="inputChooseFileToImportFrom"
|
||||||
|
class="cursor-pointer text-secondary hover:text-secondaryDark focus-visible:text-secondaryDark border border-divider hover:border-dividerDark focus-visible:border-dividerDark rounded px-4 font-semibold py-2 transition inline-flex items-center justify-center whitespace-nowrap focus:outline-none"
|
||||||
|
>
|
||||||
|
<SmartIcon class="svg-icons mr-2" name="upload" />
|
||||||
|
{{ fileName || t("action.choose_file") }}
|
||||||
</label>
|
</label>
|
||||||
<SmartIcon name="upload" />
|
|
||||||
{{ t("action.choose_file") }}
|
|
||||||
<input
|
<input
|
||||||
id="inputChooseFileToImportFrom"
|
id="inputChooseFileToImportFrom"
|
||||||
ref="inputChooseFileToImportFrom"
|
ref="inputChooseFileToImportFrom"
|
||||||
@@ -48,7 +50,10 @@
|
|||||||
:accept="step.metadata.acceptedFileTypes"
|
:accept="step.metadata.acceptedFileTypes"
|
||||||
@change="importFromJSON"
|
@change="importFromJSON"
|
||||||
/>
|
/>
|
||||||
<ButtonPrimary :label="t('import.title')" />
|
<ButtonPrimary
|
||||||
|
:label="t('import.title')"
|
||||||
|
@click.native="finishImport"
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -186,9 +191,9 @@ import { currentUser$ } from "~/helpers/fb/auth"
|
|||||||
import * as teamUtils from "~/helpers/teams/utils"
|
import * as teamUtils from "~/helpers/teams/utils"
|
||||||
import { parseInsomniaCollection } from "~/helpers/utils/parseInsomniaCollection"
|
import { parseInsomniaCollection } from "~/helpers/utils/parseInsomniaCollection"
|
||||||
import {
|
import {
|
||||||
|
appendRESTCollections,
|
||||||
restCollections$,
|
restCollections$,
|
||||||
setRESTCollections,
|
setRESTCollections,
|
||||||
appendRESTCollections,
|
|
||||||
Collection,
|
Collection,
|
||||||
makeCollection,
|
makeCollection,
|
||||||
} from "~/newstore/collections"
|
} from "~/newstore/collections"
|
||||||
@@ -217,12 +222,12 @@ const t = useI18n()
|
|||||||
const myCollections = useReadonlyStream(restCollections$, [])
|
const myCollections = useReadonlyStream(restCollections$, [])
|
||||||
const currentUser = useReadonlyStream(currentUser$, null)
|
const currentUser = useReadonlyStream(currentUser$, null)
|
||||||
|
|
||||||
|
// Template refs
|
||||||
const mode = ref("import_export")
|
const mode = ref("import_export")
|
||||||
const mySelectedCollectionID = ref(undefined)
|
const mySelectedCollectionID = ref(undefined)
|
||||||
const collectionJson = ref("")
|
const collectionJson = ref("")
|
||||||
|
const inputChooseFileToImportFrom = ref<HTMLInputElement | any>()
|
||||||
// Template refs
|
const fileName = ref<string>("")
|
||||||
const inputChooseFileToImportFrom = ref<HTMLInputElement>()
|
|
||||||
|
|
||||||
const getJSONCollection = async () => {
|
const getJSONCollection = async () => {
|
||||||
if (props.collectionsType.type === "my-collections") {
|
if (props.collectionsType.type === "my-collections") {
|
||||||
@@ -315,6 +320,8 @@ const hideModal = () => {
|
|||||||
emit("hide-modal")
|
emit("hide-modal")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const stepsValue = ref<string[]>([])
|
||||||
|
|
||||||
const hasFolder = (item: { item?: any }) => {
|
const hasFolder = (item: { item?: any }) => {
|
||||||
return Object.prototype.hasOwnProperty.call(item, "item")
|
return Object.prototype.hasOwnProperty.call(item, "item")
|
||||||
}
|
}
|
||||||
@@ -492,19 +499,22 @@ const isInsomniaCollection = (collection: any) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const importFromJSON = () => {
|
const importFromJSON = () => {
|
||||||
if (!inputChooseFileToImportFrom.value) return
|
if (!inputChooseFileToImportFrom.value[0]) return
|
||||||
|
|
||||||
if (
|
if (
|
||||||
!inputChooseFileToImportFrom.value.files ||
|
!inputChooseFileToImportFrom.value[0].files ||
|
||||||
inputChooseFileToImportFrom.value.files.length === 0
|
inputChooseFileToImportFrom.value[0].files.length === 0
|
||||||
) {
|
) {
|
||||||
|
inputChooseFileToImportFrom.value[0].value = ""
|
||||||
toast.show(t("action.choose_file").toString())
|
toast.show(t("action.choose_file").toString())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fileName.value = inputChooseFileToImportFrom.value[0].files[0].name
|
||||||
|
|
||||||
const reader = new FileReader()
|
const reader = new FileReader()
|
||||||
|
|
||||||
reader.onload = async ({ target }) => {
|
reader.onload = ({ target }) => {
|
||||||
let content = target!.result as string | null
|
let content = target!.result as string | null
|
||||||
|
|
||||||
if (!content) {
|
if (!content) {
|
||||||
@@ -513,7 +523,7 @@ const importFromJSON = () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let collections = JSON.parse(content)
|
let collections = JSON.parse(content)
|
||||||
await importerAction(content)
|
stepsValue.value.push(content)
|
||||||
|
|
||||||
if (isInsomniaCollection(collections)) {
|
if (isInsomniaCollection(collections)) {
|
||||||
collections = parseInsomniaCollection(content)
|
collections = parseInsomniaCollection(content)
|
||||||
@@ -552,12 +562,11 @@ const importFromJSON = () => {
|
|||||||
failedImport()
|
failedImport()
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
appendRESTCollections(collections)
|
|
||||||
fileImported()
|
fileImported()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
reader.readAsText(inputChooseFileToImportFrom.value.files[0])
|
reader.readAsText(inputChooseFileToImportFrom.value[0].files[0])
|
||||||
inputChooseFileToImportFrom.value.value = ""
|
inputChooseFileToImportFrom.value[0].value = ""
|
||||||
}
|
}
|
||||||
|
|
||||||
const importFromMyCollections = () => {
|
const importFromMyCollections = () => {
|
||||||
@@ -607,12 +616,25 @@ const importerType = ref(0)
|
|||||||
const importerModule = RESTCollectionImporters[importerType.value]
|
const importerModule = RESTCollectionImporters[importerType.value]
|
||||||
const importerSteps = importerModule.steps
|
const importerSteps = importerModule.steps
|
||||||
|
|
||||||
|
const finishImport = async () => {
|
||||||
|
await importerAction(stepsValue.value[0])
|
||||||
|
.then(() => {
|
||||||
|
fileImported()
|
||||||
|
})
|
||||||
|
.catch(() => {
|
||||||
|
failedImport()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
const importerAction = async (content: string) => {
|
const importerAction = async (content: string) => {
|
||||||
const result = await importerModule.importer([content])()
|
const result = await importerModule.importer([content])()
|
||||||
if (E.isLeft(result)) {
|
if (E.isLeft(result)) {
|
||||||
console.log("error", result.left)
|
console.log("error", result.left)
|
||||||
|
toast.error(t("error.something_went_wrong").toString())
|
||||||
} else if (E.isRight(result)) {
|
} else if (E.isRight(result)) {
|
||||||
|
appendRESTCollections(result.right)
|
||||||
console.log("success", result)
|
console.log("success", result)
|
||||||
|
toast.success(t("state.file_imported").toString())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
Reference in New Issue
Block a user