refactor: collections import export

This commit is contained in:
Andrew Bastin 2021-12-20 15:58:04 +05:30
parent ba3d3430c0
commit ba3df75a23
4 changed files with 465 additions and 368 deletions

View file

@ -42,7 +42,7 @@
@click.native=" @click.native="
() => { () => {
readCollectionGist() readCollectionGist()
$refs.options.tippy().hide() options.tippy().hide()
} }
" "
/> />
@ -50,10 +50,10 @@
v-tippy="{ theme: 'tooltip' }" v-tippy="{ theme: 'tooltip' }"
:title=" :title="
!currentUser !currentUser
? $t('export.require_github') ? `${t('export.require_github')}`
: currentUser.provider !== 'github.com' : currentUser.provider !== 'github.com'
? $t('export.require_github') ? `${t('export.require_github')}`
: null : undefined
" "
> >
<SmartItem <SmartItem
@ -69,7 +69,7 @@
@click.native=" @click.native="
() => { () => {
createCollectionGist() createCollectionGist()
$refs.options.tippy().hide() options.tippy().hide()
} }
" "
/> />
@ -177,394 +177,484 @@
</SmartModal> </SmartModal>
</template> </template>
<script> <script setup lang="ts">
import { defineComponent } from "@nuxtjs/composition-api" import { ref } from "@nuxtjs/composition-api"
import { translateToNewRequest } from "@hoppscotch/data" import { HoppRESTRequest, translateToNewRequest } from "@hoppscotch/data"
import { apolloClient } from "~/helpers/apollo"
import {
useAxios,
useI18n,
useReadonlyStream,
useToast,
} from "~/helpers/utils/composables"
import { currentUser$ } from "~/helpers/fb/auth" import { currentUser$ } from "~/helpers/fb/auth"
import * as teamUtils from "~/helpers/teams/utils" import * as teamUtils from "~/helpers/teams/utils"
import { useReadonlyStream } from "~/helpers/utils/composables"
import { parseInsomniaCollection } from "~/helpers/utils/parseInsomniaCollection" import { parseInsomniaCollection } from "~/helpers/utils/parseInsomniaCollection"
import { import {
restCollections$, restCollections$,
setRESTCollections, setRESTCollections,
appendRESTCollections, appendRESTCollections,
Collection,
makeCollection,
} from "~/newstore/collections" } from "~/newstore/collections"
export default defineComponent({ const props = defineProps<{
props: { show: boolean
show: Boolean, collectionsType:
collectionsType: { type: Object, default: () => {} }, | {
}, type: "team-collections"
setup() { selectedTeam: {
return { id: string
myCollections: useReadonlyStream(restCollections$, []),
currentUser: useReadonlyStream(currentUser$, null),
}
},
data() {
return {
showJsonCode: false,
mode: "import_export",
mySelectedCollectionID: undefined,
collectionJson: "",
}
},
methods: {
async createCollectionGist() {
this.getJSONCollection()
await this.$axios
.$post(
"https://api.github.com/gists",
{
files: {
"hoppscotch-collections.json": {
content: this.collectionJson,
},
},
},
{
headers: {
Authorization: `token ${this.currentUser.accessToken}`,
Accept: "application/vnd.github.v3+json",
},
}
)
.then((res) => {
this.$toast.success(this.$t("export.gist_created"))
window.open(res.html_url)
})
.catch((e) => {
this.$toast.error(this.$t("error.something_went_wrong"))
console.error(e)
})
},
async readCollectionGist() {
const gist = prompt(this.$t("import.gist_url"))
if (!gist) return
await this.$axios
.$get(`https://api.github.com/gists/${gist.split("/").pop()}`, {
headers: {
Accept: "application/vnd.github.v3+json",
},
})
.then(({ files }) => {
const collections = JSON.parse(Object.values(files)[0].content)
setRESTCollections(collections)
this.fileImported()
})
.catch((e) => {
this.failedImport()
console.error(e)
})
},
hideModal() {
this.mode = "import_export"
this.mySelectedCollectionID = undefined
this.$emit("hide-modal")
},
openDialogChooseFileToReplaceWith() {
this.$refs.inputChooseFileToReplaceWith.click()
},
openDialogChooseFileToImportFrom() {
this.$refs.inputChooseFileToImportFrom.click()
},
replaceWithJSON() {
const reader = new FileReader()
reader.onload = ({ target }) => {
const content = target.result
let collections = JSON.parse(content)
if (collections[0]) {
const [name, folders, requests] = Object.keys(collections[0])
if (
name === "name" &&
folders === "folders" &&
requests === "requests"
) {
// Do nothing
}
} else if (
collections.info &&
collections.info.schema.includes("v2.1.0")
) {
collections = [this.parsePostmanCollection(collections)]
} else {
this.failedImport()
}
if (this.collectionsType.type === "team-collections") {
teamUtils
.replaceWithJSON(
this.$apollo,
collections,
this.collectionsType.selectedTeam.id
)
.then((status) => {
if (status) {
this.fileImported()
} else {
this.failedImport()
}
})
.catch((e) => {
console.error(e)
this.failedImport()
})
} else {
setRESTCollections(collections)
this.fileImported()
} }
} }
reader.readAsText(this.$refs.inputChooseFileToReplaceWith.files[0]) | { type: "my-collections" }
this.$refs.inputChooseFileToReplaceWith.value = "" }>()
},
importFromJSON() { const emit = defineEmits<{
const reader = new FileReader() (e: "hide-modal"): void
reader.onload = ({ target }) => { (e: "update-team-collections"): void
let content = target.result }>()
let collections = JSON.parse(content)
if (this.isInsomniaCollection(collections)) { const axios = useAxios()
collections = parseInsomniaCollection(content) const toast = useToast()
content = JSON.stringify(collections) const t = useI18n()
const myCollections = useReadonlyStream(restCollections$, [])
const currentUser = useReadonlyStream(currentUser$, null)
const mode = ref("import_export")
const mySelectedCollectionID = ref(undefined)
const collectionJson = ref("")
// Template refs
const options = ref<any>()
const inputChooseFileToReplaceWith = ref<HTMLInputElement>()
const inputChooseFileToImportFrom = ref<HTMLInputElement>()
const getJSONCollection = async () => {
if (props.collectionsType.type === "my-collections") {
collectionJson.value = JSON.stringify(myCollections.value, null, 2)
} else {
collectionJson.value = await teamUtils.exportAsJSON(
apolloClient,
props.collectionsType.selectedTeam.id
)
}
return collectionJson.value
}
const createCollectionGist = async () => {
if (!currentUser.value) {
toast.error(t("profile.no_permission").toString())
return
}
getJSONCollection()
try {
const res = await axios.$post(
"https://api.github.com/gists",
{
files: {
"hoppscotch-collections.json": {
content: collectionJson.value,
},
},
},
{
headers: {
Authorization: `token ${currentUser.value.accessToken}`,
Accept: "application/vnd.github.v3+json",
},
}
)
toast.success(t("export.gist_created").toString())
window.open(res.html_url)
} catch (e) {
toast.error(t("error.something_went_wrong").toString())
console.error(e)
}
}
const fileImported = () => {
toast.success(t("state.file_imported").toString())
}
const failedImport = () => {
toast.error(t("import.failed").toString())
}
const readCollectionGist = async () => {
const gist = prompt(t("import.gist_url").toString())
if (!gist) return
try {
const { files } = (await axios.$get(
`https://api.github.com/gists/${gist.split("/").pop()}`,
{
headers: {
Accept: "application/vnd.github.v3+json",
},
}
)) as {
files: {
[fileName: string]: {
content: any
} }
if (collections[0]) { }
const [name, folders, requests] = Object.keys(collections[0]) }
if (
name === "name" && const collections = JSON.parse(Object.values(files)[0].content)
folders === "folders" && setRESTCollections(collections)
requests === "requests" fileImported()
) { } catch (e) {
// Do nothing failedImport()
} console.error(e)
} else if ( }
collections.info && }
collections.info.schema.includes("v2.1.0")
const hideModal = () => {
mode.value = "import_export"
mySelectedCollectionID.value = undefined
emit("hide-modal")
}
const openDialogChooseFileToReplaceWith = () => {
if (inputChooseFileToReplaceWith.value)
inputChooseFileToReplaceWith.value.click()
}
const openDialogChooseFileToImportFrom = () => {
if (inputChooseFileToImportFrom.value)
inputChooseFileToImportFrom.value.click()
}
const hasFolder = (item: { item?: any }) => {
return Object.prototype.hasOwnProperty.call(item, "item")
}
// TODO: I don't even know what is going on here :/
type PostmanCollection = {
info?: {
name: string
}
name: string
item: {
name: string
request: any
item?: any
}[]
folders?: any
}
const parsePostmanCollection = ({ info, name, item }: PostmanCollection) => {
const hoppscotchCollection: Collection<HoppRESTRequest> = makeCollection({
name: "",
folders: [],
requests: [],
})
hoppscotchCollection.name = info ? info.name : name
if (item && item.length > 0) {
for (const collectionItem of item) {
if (collectionItem.request) {
if (
Object.prototype.hasOwnProperty.call(hoppscotchCollection, "folders")
) { ) {
// replace the variables, postman uses {{var}}, Hoppscotch uses <<var>> hoppscotchCollection.name = info ? info.name : name
collections = JSON.parse( hoppscotchCollection.requests.push(
content.replaceAll(/{{([a-zA-Z_$][a-zA-Z_$0-9]*)}}/gi, "<<$1>>") parsePostmanRequest(collectionItem)
) )
collections = [this.parsePostmanCollection(collections)]
} else { } else {
this.failedImport() hoppscotchCollection.name = name || ""
return hoppscotchCollection.requests.push(
parsePostmanRequest(collectionItem)
)
} }
if (this.collectionsType.type === "team-collections") { } else if (hasFolder(collectionItem)) {
teamUtils hoppscotchCollection.folders.push(
.importFromJSON( parsePostmanCollection(collectionItem as any)
this.$apollo,
collections,
this.collectionsType.selectedTeam.id
)
.then((status) => {
if (status) {
this.$emit("update-team-collections")
this.fileImported()
} else {
this.failedImport()
}
})
.catch((e) => {
console.error(e)
this.failedImport()
})
} else {
appendRESTCollections(collections)
this.fileImported()
}
}
reader.readAsText(this.$refs.inputChooseFileToImportFrom.files[0])
this.$refs.inputChooseFileToImportFrom.value = ""
},
importFromMyCollections() {
teamUtils
.importFromMyCollections(
this.$apollo,
this.mySelectedCollectionID,
this.collectionsType.selectedTeam.id
) )
.then((success) => { } else {
if (success) { hoppscotchCollection.requests.push(parsePostmanRequest(collectionItem))
this.fileImported() }
this.$emit("update-team-collections") }
}
return hoppscotchCollection
}
// TODO: Rewrite
const parsePostmanRequest = ({
name,
request,
}: {
name: string
request: any
}) => {
const pwRequest = {
url: "",
path: "",
method: "",
auth: "",
httpUser: "",
httpPassword: "",
passwordFieldType: "password",
bearerToken: "",
headers: [] as { name?: string; type?: string }[],
params: [] as { disabled?: boolean }[],
bodyParams: [] as { type?: string }[],
rawParams: "",
rawInput: false,
contentType: "",
requestType: "",
name: "",
}
pwRequest.name = name
if (request.url) {
const requestObjectUrl = request.url.raw.match(
/^(.+:\/\/[^/]+|{[^/]+})(\/[^?]+|).*$/
)
if (requestObjectUrl) {
pwRequest.url = requestObjectUrl[1]
pwRequest.path = requestObjectUrl[2] ? requestObjectUrl[2] : ""
}
}
pwRequest.method = request.method
const itemAuth = request.auth ? request.auth : ""
const authType = itemAuth ? itemAuth.type : ""
if (authType === "basic") {
pwRequest.auth = "Basic Auth"
pwRequest.httpUser =
itemAuth.basic[0].key === "username"
? itemAuth.basic[0].value
: itemAuth.basic[1].value
pwRequest.httpPassword =
itemAuth.basic[0].key === "password"
? itemAuth.basic[0].value
: itemAuth.basic[1].value
} else if (authType === "oauth2") {
pwRequest.auth = "OAuth 2.0"
pwRequest.bearerToken =
itemAuth.oauth2[0].key === "accessToken"
? itemAuth.oauth2[0].value
: itemAuth.oauth2[1].value
} else if (authType === "bearer") {
pwRequest.auth = "Bearer Token"
pwRequest.bearerToken = itemAuth.bearer[0].value
}
const requestObjectHeaders = request.header
if (requestObjectHeaders) {
pwRequest.headers = requestObjectHeaders
for (const header of pwRequest.headers) {
delete header.name
delete header.type
}
}
if (request.url) {
const requestObjectParams = request.url.query
if (requestObjectParams) {
pwRequest.params = requestObjectParams
for (const param of pwRequest.params) {
delete param.disabled
}
}
}
if (request.body) {
if (request.body.mode === "urlencoded") {
const params = request.body.urlencoded
pwRequest.bodyParams = params || []
for (const param of pwRequest.bodyParams) {
delete param.type
}
} else if (request.body.mode === "raw") {
pwRequest.rawInput = true
pwRequest.rawParams = request.body.raw
}
}
return translateToNewRequest(pwRequest)
}
const replaceWithJSON = () => {
if (!inputChooseFileToReplaceWith.value) return
if (
!inputChooseFileToReplaceWith.value.files ||
inputChooseFileToReplaceWith.value.files.length === 0
) {
toast.show(t("action.choose_file").toString())
return
}
const reader = new FileReader()
reader.onload = ({ target }) => {
const content = target!.result as string | null
if (!content) {
toast.show(t("action.choose_file").toString())
return
}
let collections = JSON.parse(content)
// TODO: File validation
if (collections[0]) {
const [name, folders, requests] = Object.keys(collections[0])
if (name === "name" && folders === "folders" && requests === "requests") {
// Do nothing
}
} else if (collections.info && collections.info.schema.includes("v2.1.0")) {
collections = [parsePostmanCollection(collections)]
} else {
failedImport()
}
if (props.collectionsType.type === "team-collections") {
teamUtils
.replaceWithJSON(
apolloClient,
collections,
props.collectionsType.selectedTeam.id
)
.then((status) => {
if (status) {
fileImported()
} else { } else {
this.failedImport() failedImport()
} }
}) })
.catch((e) => { .catch((e) => {
console.error(e) console.error(e)
this.failedImport() failedImport()
}) })
}, } else {
async getJSONCollection() { setRESTCollections(collections)
if (this.collectionsType.type === "my-collections") { fileImported()
this.collectionJson = JSON.stringify(this.myCollections, null, 2) }
} else { }
this.collectionJson = await teamUtils.exportAsJSON(
this.$apollo, reader.readAsText(inputChooseFileToReplaceWith.value.files[0])
this.collectionsType.selectedTeam.id inputChooseFileToReplaceWith.value.value = ""
}
const isInsomniaCollection = (collection: any) => {
if (typeof collection === "object") {
return (
Object.prototype.hasOwnProperty.call(collection, "__export_source") &&
collection.__export_source.includes("insomnia")
)
}
return false
}
const importFromJSON = () => {
if (!inputChooseFileToImportFrom.value) return
if (
!inputChooseFileToImportFrom.value.files ||
inputChooseFileToImportFrom.value.files.length === 0
) {
toast.show(t("action.choose_file").toString())
return
}
const reader = new FileReader()
reader.onload = ({ target }) => {
let content = target!.result as string | null
if (!content) {
toast.show(t("action.choose_file").toString())
return
}
let collections = JSON.parse(content)
if (isInsomniaCollection(collections)) {
collections = parseInsomniaCollection(content)
content = JSON.stringify(collections)
}
if (collections[0]) {
const [name, folders, requests] = Object.keys(collections[0])
if (name === "name" && folders === "folders" && requests === "requests") {
// Do nothing
}
} else if (collections.info && collections.info.schema.includes("v2.1.0")) {
// replace the variables, postman uses {{var}}, Hoppscotch uses <<var>>
collections = JSON.parse(content.replaceAll(/{{([a-z]+)}}/gi, "<<$1>>"))
collections = [parsePostmanCollection(collections)]
} else {
failedImport()
return
}
if (props.collectionsType.type === "team-collections") {
teamUtils
.importFromJSON(
apolloClient,
collections,
props.collectionsType.selectedTeam.id
) )
} .then((status) => {
return this.collectionJson if (status) {
}, emit("update-team-collections")
exportJSON() { fileImported()
this.getJSONCollection()
const dataToWrite = this.collectionJson
const file = new Blob([dataToWrite], { type: "application/json" })
const a = document.createElement("a")
const url = URL.createObjectURL(file)
a.href = url
// TODO get uri from meta
a.download = `${url.split("/").pop().split("#")[0].split("?")[0]}.json`
document.body.appendChild(a)
a.click()
this.$toast.success(this.$t("state.download_started"))
setTimeout(() => {
document.body.removeChild(a)
URL.revokeObjectURL(url)
}, 1000)
},
fileImported() {
this.$toast.success(this.$t("state.file_imported"))
},
failedImport() {
this.$toast.error(this.$t("import.failed"))
},
parsePostmanCollection({ info, name, item }) {
const hoppscotchCollection = {
name: "",
folders: [],
requests: [],
}
hoppscotchCollection.name = info ? info.name : name
if (item && item.length > 0) {
for (const collectionItem of item) {
if (collectionItem.request) {
if (
Object.prototype.hasOwnProperty.call(
hoppscotchCollection,
"folders"
)
) {
hoppscotchCollection.name = info ? info.name : name
hoppscotchCollection.requests.push(
this.parsePostmanRequest(collectionItem)
)
} else {
hoppscotchCollection.name = name || ""
hoppscotchCollection.requests.push(
this.parsePostmanRequest(collectionItem)
)
}
} else if (this.hasFolder(collectionItem)) {
hoppscotchCollection.folders.push(
this.parsePostmanCollection(collectionItem)
)
} else { } else {
hoppscotchCollection.requests.push( failedImport()
this.parsePostmanRequest(collectionItem)
)
} }
} })
} .catch((e) => {
return hoppscotchCollection console.error(e)
}, failedImport()
parsePostmanRequest({ name, request }) { })
const pwRequest = { } else {
url: "", appendRESTCollections(collections)
path: "", fileImported()
method: "", }
auth: "", }
httpUser: "", reader.readAsText(inputChooseFileToImportFrom.value.files[0])
httpPassword: "", inputChooseFileToImportFrom.value.value = ""
passwordFieldType: "password", }
bearerToken: "",
headers: [],
params: [],
bodyParams: [],
rawParams: "",
rawInput: false,
contentType: "",
requestType: "",
name: "",
}
pwRequest.name = name const importFromMyCollections = () => {
if (request.url) { if (props.collectionsType.type !== "team-collections") return
const requestObjectUrl = request.url.raw.match(
/^(.+:\/\/[^/]+|{[^/]+})(\/[^?]+|).*$/ teamUtils
) .importFromMyCollections(
if (requestObjectUrl) { apolloClient,
pwRequest.url = requestObjectUrl[1] mySelectedCollectionID.value,
pwRequest.path = requestObjectUrl[2] ? requestObjectUrl[2] : "" props.collectionsType.selectedTeam.id
} )
.then((success) => {
if (success) {
fileImported()
emit("update-team-collections")
} else {
failedImport()
} }
pwRequest.method = request.method })
const itemAuth = request.auth ? request.auth : "" .catch((e) => {
const authType = itemAuth ? itemAuth.type : "" console.error(e)
if (authType === "basic") { failedImport()
pwRequest.auth = "Basic Auth" })
pwRequest.httpUser = }
itemAuth.basic[0].key === "username"
? itemAuth.basic[0].value const exportJSON = () => {
: itemAuth.basic[1].value getJSONCollection()
pwRequest.httpPassword =
itemAuth.basic[0].key === "password" const dataToWrite = collectionJson.value
? itemAuth.basic[0].value const file = new Blob([dataToWrite], { type: "application/json" })
: itemAuth.basic[1].value const a = document.createElement("a")
} else if (authType === "oauth2") { const url = URL.createObjectURL(file)
pwRequest.auth = "OAuth 2.0" a.href = url
pwRequest.bearerToken =
itemAuth.oauth2[0].key === "accessToken" // TODO: get uri from meta
? itemAuth.oauth2[0].value a.download = `${url.split("/").pop()!.split("#")[0].split("?")[0]}.json`
: itemAuth.oauth2[1].value document.body.appendChild(a)
} else if (authType === "bearer") { a.click()
pwRequest.auth = "Bearer Token" toast.success(t("state.download_started").toString())
pwRequest.bearerToken = itemAuth.bearer[0].value setTimeout(() => {
} document.body.removeChild(a)
const requestObjectHeaders = request.header URL.revokeObjectURL(url)
if (requestObjectHeaders) { }, 1000)
pwRequest.headers = requestObjectHeaders }
for (const header of pwRequest.headers) {
delete header.name
delete header.type
}
}
if (request.url) {
const requestObjectParams = request.url.query
if (requestObjectParams) {
pwRequest.params = requestObjectParams
for (const param of pwRequest.params) {
delete param.disabled
}
}
}
if (request.body) {
if (request.body.mode === "urlencoded") {
const params = request.body.urlencoded
pwRequest.bodyParams = params || []
for (const param of pwRequest.bodyParams) {
delete param.type
}
} else if (request.body.mode === "raw") {
pwRequest.rawInput = true
pwRequest.rawParams = request.body.raw
}
}
return translateToNewRequest(pwRequest)
},
hasFolder(item) {
return Object.prototype.hasOwnProperty.call(item, "item")
},
isInsomniaCollection(collection) {
if (typeof collection === "object") {
return (
Object.prototype.hasOwnProperty.call(collection, "__export_source") &&
collection.__export_source.includes("insomnia")
)
}
return false
},
},
})
</script> </script>

View file

@ -162,6 +162,11 @@ export function useColorMode() {
return $colorMode return $colorMode
} }
export function useAxios() {
const { $axios } = useContext()
return $axios
}
export function usePolled<T>( export function usePolled<T>(
pollDurationMS: number, pollDurationMS: number,
pollFunc: (stopPolling: () => void) => T pollFunc: (stopPolling: () => void) => T

View file

@ -151,6 +151,7 @@ function defineJumpActions() {
export default defineComponent({ export default defineComponent({
components: { Splitpanes, Pane }, components: { Splitpanes, Pane },
setup() { setup() {
console.log(useContext())
appLayout() appLayout()
hookKeybindingsListener() hookKeybindingsListener()

View file

@ -21,7 +21,8 @@
"@nuxtjs/i18n", "@nuxtjs/i18n",
"@nuxtjs/toast", "@nuxtjs/toast",
"@nuxtjs/sentry", "@nuxtjs/sentry",
"@nuxtjs/color-mode" "@nuxtjs/color-mode",
"@nuxtjs/axios"
] ]
}, },
"exclude": ["node_modules", ".nuxt", "dist"], "exclude": ["node_modules", ".nuxt", "dist"],