api-client/components/collections/ImportExport.vue

554 lines
16 KiB
Vue
Raw Normal View History

2019-10-22 05:00:13 +00:00
<template>
<SmartModal
v-if="show"
:title="`${$t('modal.import_export')} ${$t('collections')}`"
@close="hideModal"
>
<template #actions>
<ButtonSecondary
v-if="mode == 'import_from_my_collections'"
v-tippy="{ theme: 'tooltip' }"
title="Back"
icon="arrow_back"
@click.native="
mode = 'import_export'
mySelectedCollectionID = undefined
"
/>
<span>
2021-07-02 16:30:08 +00:00
<tippy
v-if="
mode == 'import_export' && collectionsType.type == 'my-collections'
"
2021-07-05 16:52:15 +00:00
ref="options"
2021-07-08 07:30:41 +00:00
interactive
2021-07-02 16:30:08 +00:00
trigger="click"
theme="popover"
arrow
>
2021-07-02 16:30:08 +00:00
<template #trigger>
<ButtonSecondary
2021-07-02 16:30:08 +00:00
v-tippy="{ theme: 'tooltip' }"
:title="$t('more')"
2021-07-03 13:14:58 +00:00
icon="more_vert"
/>
2021-07-02 16:30:08 +00:00
</template>
2021-07-05 16:52:15 +00:00
<SmartItem
icon="assignment_returned"
2021-08-02 15:27:18 +00:00
:label="$t('import.from_gist')"
2021-07-05 16:52:15 +00:00
@click.native="
readCollectionGist
$refs.options.tippy().hide()
"
/>
<SmartItem
2021-07-02 16:30:08 +00:00
v-tippy="{ theme: 'tooltip' }"
2021-07-05 16:52:15 +00:00
:title="
!currentUser
2021-08-02 15:27:18 +00:00
? $t('export.require_github')
2021-07-05 16:52:15 +00:00
: currentUser.provider !== 'github.com'
2021-08-02 15:27:18 +00:00
? $t('export.require_github')
2021-07-05 16:52:15 +00:00
: null
"
:disabled="
!currentUser
? true
: currentUser.provider !== 'github.com'
? true
: false
"
icon="assignment_turned_in"
2021-08-02 15:27:18 +00:00
:label="$t('export.create_secret_gist')"
2021-07-05 16:52:15 +00:00
@click.native="
createCollectionGist
$refs.options.tippy().hide()
"
/>
2021-07-02 16:30:08 +00:00
</tippy>
</span>
</template>
<template #body>
2021-07-09 17:19:45 +00:00
<div v-if="mode == 'import_export'" class="flex flex-col space-y-2">
<SmartItem
2021-07-02 16:30:08 +00:00
v-tippy="{ theme: 'tooltip' }"
:title="$t('replace_current')"
2021-07-03 13:14:58 +00:00
icon="folder_special"
:label="$t('replace_json')"
@click.native="openDialogChooseFileToReplaceWith"
/>
<input
ref="inputChooseFileToReplaceWith"
class="input"
type="file"
style="display: none"
accept="application/json"
@change="replaceWithJSON"
/>
2021-07-09 17:19:45 +00:00
<SmartItem
2021-07-02 16:30:08 +00:00
v-tippy="{ theme: 'tooltip' }"
:title="$t('preserve_current')"
2021-07-03 13:14:58 +00:00
icon="create_new_folder"
2021-08-02 15:27:18 +00:00
:label="$t('import.json')"
2021-07-03 13:14:58 +00:00
@click.native="openDialogChooseFileToImportFrom"
/>
<input
ref="inputChooseFileToImportFrom"
class="input"
type="file"
style="display: none"
accept="application/json"
@change="importFromJSON"
/>
2021-07-09 17:19:45 +00:00
<SmartItem
2021-05-18 16:09:55 +00:00
v-if="collectionsType.type == 'team-collections'"
2021-07-02 16:30:08 +00:00
v-tippy="{ theme: 'tooltip' }"
:title="$t('preserve_current')"
2021-07-03 13:14:58 +00:00
icon="folder_shared"
2021-08-02 15:27:18 +00:00
:label="$t('import.from_my_collections')"
2021-07-03 13:14:58 +00:00
@click.native="mode = 'import_from_my_collections'"
/>
2021-07-09 17:19:45 +00:00
<SmartItem
2021-07-02 16:30:08 +00:00
v-tippy="{ theme: 'tooltip' }"
:title="$t('download_file')"
2021-07-03 13:14:58 +00:00
icon="drive_file_move"
2021-08-02 15:27:18 +00:00
:label="$t('export.as_json')"
2021-07-03 13:14:58 +00:00
@click.native="exportJSON"
/>
2020-12-11 10:29:03 +00:00
</div>
2021-07-09 17:19:45 +00:00
<div
v-if="mode == 'import_from_my_collections'"
2021-07-17 17:40:28 +00:00
class="flex flex-col px-2"
2021-07-09 17:19:45 +00:00
>
<div class="select-wrapper">
<select
type="text"
class="select"
autofocus
@change="
($event) => {
mySelectedCollectionID = $event.target.value
}
"
>
2021-05-18 16:09:55 +00:00
<option
:key="undefined"
:value="undefined"
hidden
disabled
selected
>
Select Collection
</option>
2021-05-18 16:09:55 +00:00
<option
v-for="(collection, index) in myCollections"
2021-07-13 23:49:08 +00:00
:key="`collection-${index}`"
2021-05-18 16:09:55 +00:00
:value="index"
>
{{ collection.name }}
</option>
</select>
2021-07-09 17:19:45 +00:00
</div>
2019-12-17 19:13:15 +00:00
</div>
</template>
<template #footer>
<div v-if="mode == 'import_from_my_collections'">
<span>
2021-07-09 17:19:45 +00:00
<ButtonPrimary
:disabled="mySelectedCollectionID == undefined"
2021-07-03 13:14:58 +00:00
icon="create_new_folder"
2021-08-02 15:27:18 +00:00
:label="$t('import.title')"
2021-07-03 13:14:58 +00:00
@click.native="importFromMyCollections"
/>
</span>
</div>
</template>
</SmartModal>
2019-10-22 05:00:13 +00:00
</template>
<script>
2021-08-12 08:14:10 +00:00
import { defineComponent } from "@nuxtjs/composition-api"
2021-06-14 04:07:30 +00:00
import { currentUser$ } from "~/helpers/fb/auth"
2021-05-18 16:09:55 +00:00
import * as teamUtils from "~/helpers/teams/utils"
2021-08-12 08:14:10 +00:00
import { useReadonlyStream } from "~/helpers/utils/composables"
import {
restCollections$,
setRESTCollections,
appendRESTCollections,
} from "~/newstore/collections"
2020-01-25 06:51:47 +00:00
2021-08-12 08:14:10 +00:00
export default defineComponent({
2021-05-18 16:09:55 +00:00
props: {
show: Boolean,
collectionsType: { type: Object, default: () => {} },
},
2021-08-12 08:14:10 +00:00
setup() {
return {
myCollections: useReadonlyStream(restCollections$, []),
currentUser: useReadonlyStream(currentUser$, null),
}
},
2020-01-25 06:51:47 +00:00
data() {
return {
2020-12-11 10:29:03 +00:00
showJsonCode: false,
mode: "import_export",
mySelectedCollectionID: undefined,
collectionJson: "",
2020-02-24 18:44:50 +00:00
}
2020-01-25 06:51:47 +00:00
},
2019-11-02 05:32:21 +00:00
methods: {
2020-12-07 08:44:02 +00:00
async createCollectionGist() {
this.getJSONCollection()
2020-12-07 08:44:02 +00:00
await this.$axios
.$post(
"https://api.github.com/gists",
{
files: {
"hoppscotch-collections.json": {
content: this.collectionJson,
2020-12-07 08:44:02 +00:00
},
},
},
{
headers: {
2021-06-14 04:07:30 +00:00
Authorization: `token ${this.currentUser.accessToken}`,
2020-12-07 08:44:02 +00:00
Accept: "application/vnd.github.v3+json",
},
}
)
2021-05-18 16:09:55 +00:00
.then((res) => {
2021-08-02 15:27:18 +00:00
this.$toast.success(this.$t("export.gist_created"), {
2020-12-07 08:44:02 +00:00
icon: "done",
})
2021-05-18 16:09:55 +00:00
window.open(res.html_url)
2020-12-07 08:44:02 +00:00
})
.catch((e) => {
this.$toast.error(this.$t("error.something_went_wrong"), {
2020-12-07 08:44:02 +00:00
icon: "error",
})
console.error(e)
2020-12-07 08:44:02 +00:00
})
},
2020-12-08 06:35:10 +00:00
async readCollectionGist() {
2021-08-02 15:27:18 +00:00
const gist = prompt(this.$t("import.gist_url"))
2020-12-08 06:35:10 +00:00
if (!gist) return
await this.$axios
.$get(`https://api.github.com/gists/${gist.split("/").pop()}`, {
headers: {
Accept: "application/vnd.github.v3+json",
},
})
.then(({ files }) => {
2021-05-18 16:09:55 +00:00
const collections = JSON.parse(Object.values(files)[0].content)
setRESTCollections(collections)
2020-12-08 06:35:10 +00:00
this.fileImported()
})
.catch((e) => {
2020-12-08 06:35:10 +00:00
this.failedImport()
console.error(e)
2020-12-08 06:35:10 +00:00
})
},
2019-12-17 19:13:15 +00:00
hideModal() {
this.mode = "import_export"
this.mySelectedCollectionID = undefined
this.$emit("hide-modal")
2019-11-02 05:32:21 +00:00
},
openDialogChooseFileToReplaceWith() {
2020-02-24 18:44:50 +00:00
this.$refs.inputChooseFileToReplaceWith.click()
2019-11-02 05:32:21 +00:00
},
openDialogChooseFileToImportFrom() {
2020-02-24 18:44:50 +00:00
this.$refs.inputChooseFileToImportFrom.click()
2019-10-22 05:00:13 +00:00
},
2019-11-02 05:32:21 +00:00
replaceWithJSON() {
2021-05-18 16:09:55 +00:00
const reader = new FileReader()
2020-10-21 06:50:32 +00:00
reader.onload = ({ target }) => {
2021-05-18 16:09:55 +00:00
const content = target.result
2020-02-24 18:44:50 +00:00
let collections = JSON.parse(content)
2020-02-15 22:36:38 +00:00
if (collections[0]) {
2021-05-18 16:09:55 +00:00
const [name, folders, requests] = Object.keys(collections[0])
if (
name === "name" &&
folders === "folders" &&
requests === "requests"
) {
2020-02-15 22:36:38 +00:00
// Do nothing
}
2021-05-18 16:09:55 +00:00
} else if (
collections.info &&
collections.info.schema.includes("v2.1.0")
) {
collections = [this.parsePostmanCollection(collections)]
2020-02-15 22:36:38 +00:00
} else {
2021-03-18 14:55:12 +00:00
this.failedImport()
2020-02-15 22:36:38 +00:00
}
2021-05-18 16:09:55 +00:00
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()
}
2020-02-24 18:44:50 +00:00
}
reader.readAsText(this.$refs.inputChooseFileToReplaceWith.files[0])
this.$refs.inputChooseFileToReplaceWith.value = ""
2019-10-22 05:00:13 +00:00
},
2019-11-02 05:32:21 +00:00
importFromJSON() {
2021-05-18 16:09:55 +00:00
const reader = new FileReader()
2020-10-21 06:50:32 +00:00
reader.onload = ({ target }) => {
2021-05-18 16:09:55 +00:00
const content = target.result
2020-02-24 18:44:50 +00:00
let collections = JSON.parse(content)
2020-02-15 22:36:38 +00:00
if (collections[0]) {
2021-05-18 16:09:55 +00:00
const [name, folders, requests] = Object.keys(collections[0])
if (
name === "name" &&
folders === "folders" &&
requests === "requests"
) {
2020-02-15 22:36:38 +00:00
// Do nothing
}
2021-05-18 16:09:55 +00:00
} 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 = [this.parsePostmanCollection(collections)]
2020-02-15 22:36:38 +00:00
} else {
2021-03-18 14:55:12 +00:00
this.failedImport()
return
2020-02-15 22:36:38 +00:00
}
2021-05-18 16:09:55 +00:00
if (this.collectionsType.type === "team-collections") {
teamUtils
.importFromJSON(
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()
}
2020-02-24 18:44:50 +00:00
}
reader.readAsText(this.$refs.inputChooseFileToImportFrom.files[0])
this.$refs.inputChooseFileToImportFrom.value = ""
2019-10-22 05:00:13 +00:00
},
importFromMyCollections() {
2021-05-18 16:09:55 +00:00
teamUtils
.importFromMyCollections(
this.$apollo,
this.mySelectedCollectionID,
this.collectionsType.selectedTeam.id
)
.then((success) => {
if (success) {
this.fileImported()
this.$emit("update-team-collections")
} else {
this.failedImport()
}
})
.catch((e) => {
console.error(e)
this.failedImport()
})
},
async getJSONCollection() {
2021-05-18 16:09:55 +00:00
if (this.collectionsType.type === "my-collections") {
2021-05-28 04:23:11 +00:00
this.collectionJson = JSON.stringify(this.myCollections, null, 2)
} else {
2021-05-18 16:09:55 +00:00
this.collectionJson = await teamUtils.exportAsJSON(
this.$apollo,
this.collectionsType.selectedTeam.id
)
}
return this.collectionJson
},
2019-11-02 05:32:21 +00:00
exportJSON() {
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]}`
document.body.appendChild(a)
a.click()
this.$toast.success(this.$t("download_started"), {
icon: "done",
2020-02-24 18:44:50 +00:00
})
setTimeout(() => {
document.body.removeChild(a)
URL.revokeObjectURL(url)
}, 1000)
2020-01-25 06:51:47 +00:00
},
fileImported() {
this.$toast.success(this.$t("file_imported"), {
icon: "folder_shared",
2020-02-24 18:44:50 +00:00
})
2020-02-15 22:36:38 +00:00
},
failedImport() {
2021-08-02 15:27:18 +00:00
this.$toast.error(this.$t("import.failed"), {
icon: "error",
2020-02-24 18:44:50 +00:00
})
2020-02-15 22:36:38 +00:00
},
2020-10-21 06:50:32 +00:00
parsePostmanCollection({ info, name, item }) {
const hoppscotchCollection = {
2020-10-21 06:50:32 +00:00
name: "",
folders: [],
requests: [],
}
hoppscotchCollection.name = info ? info.name : name
2020-10-21 06:50:32 +00:00
if (item && item.length > 0) {
2021-05-18 16:09:55 +00:00
for (const collectionItem of item) {
if (collectionItem.request) {
2021-05-18 16:09:55 +00:00
if (
Object.prototype.hasOwnProperty.call(
hoppscotchCollection,
2021-05-18 16:09:55 +00:00
"folders"
)
) {
hoppscotchCollection.name = info ? info.name : name
hoppscotchCollection.requests.push(
2021-05-18 16:09:55 +00:00
this.parsePostmanRequest(collectionItem)
)
} else {
hoppscotchCollection.name = name || ""
hoppscotchCollection.requests.push(
2021-05-18 16:09:55 +00:00
this.parsePostmanRequest(collectionItem)
)
}
} else if (this.hasFolder(collectionItem)) {
hoppscotchCollection.folders.push(
2021-05-18 16:09:55 +00:00
this.parsePostmanCollection(collectionItem)
)
2020-02-15 22:36:38 +00:00
} else {
hoppscotchCollection.requests.push(
2021-05-18 16:09:55 +00:00
this.parsePostmanRequest(collectionItem)
)
2020-02-15 22:36:38 +00:00
}
}
}
return hoppscotchCollection
2020-02-15 22:36:38 +00:00
},
2020-06-11 14:25:40 +00:00
parsePostmanRequest({ name, request }) {
2021-05-18 16:09:55 +00:00
const pwRequest = {
url: "",
path: "",
method: "",
auth: "",
httpUser: "",
httpPassword: "",
passwordFieldType: "password",
bearerToken: "",
2020-02-23 19:00:22 +00:00
headers: [],
params: [],
bodyParams: [],
rawParams: "",
2020-02-23 19:00:22 +00:00
rawInput: false,
contentType: "",
requestType: "",
name: "",
2020-02-24 18:44:50 +00:00
}
2020-02-15 22:36:38 +00:00
2020-06-11 14:25:40 +00:00
pwRequest.name = name
if (request.url) {
2021-05-18 16:09:55 +00:00
const requestObjectUrl = request.url.raw.match(
/^(.+:\/\/[^/]+|{[^/]+})(\/[^?]+|).*$/
)
if (requestObjectUrl) {
pwRequest.url = requestObjectUrl[1]
pwRequest.path = requestObjectUrl[2] ? requestObjectUrl[2] : ""
}
2020-04-20 04:44:10 +00:00
}
2020-06-11 14:25:40 +00:00
pwRequest.method = request.method
2021-05-18 16:09:55 +00:00
const itemAuth = request.auth ? request.auth : ""
const authType = itemAuth ? itemAuth.type : ""
if (authType === "basic") {
pwRequest.auth = "Basic Auth"
2020-02-23 19:00:22 +00:00
pwRequest.httpUser =
2021-05-18 16:09:55 +00:00
itemAuth.basic[0].key === "username"
? itemAuth.basic[0].value
: itemAuth.basic[1].value
2020-02-23 19:00:22 +00:00
pwRequest.httpPassword =
2021-05-18 16:09:55 +00:00
itemAuth.basic[0].key === "password"
? itemAuth.basic[0].value
: itemAuth.basic[1].value
} else if (authType === "oauth2") {
pwRequest.auth = "OAuth 2.0"
2020-02-23 19:00:22 +00:00
pwRequest.bearerToken =
itemAuth.oauth2[0].key === "accessToken"
2020-02-23 19:00:22 +00:00
? itemAuth.oauth2[0].value
2020-02-24 18:44:50 +00:00
: itemAuth.oauth2[1].value
} else if (authType === "bearer") {
pwRequest.auth = "Bearer Token"
2020-02-24 18:44:50 +00:00
pwRequest.bearerToken = itemAuth.bearer[0].value
2020-02-15 22:36:38 +00:00
}
2021-05-18 16:09:55 +00:00
const requestObjectHeaders = request.header
2020-02-15 22:36:38 +00:00
if (requestObjectHeaders) {
2020-02-24 18:44:50 +00:00
pwRequest.headers = requestObjectHeaders
2021-05-18 16:09:55 +00:00
for (const header of pwRequest.headers) {
2020-02-24 18:44:50 +00:00
delete header.name
delete header.type
2020-02-15 22:36:38 +00:00
}
}
if (request.url) {
2021-05-18 16:09:55 +00:00
const requestObjectParams = request.url.query
if (requestObjectParams) {
pwRequest.params = requestObjectParams
2021-05-18 16:09:55 +00:00
for (const param of pwRequest.params) {
delete param.disabled
}
2020-02-15 22:36:38 +00:00
}
}
2020-06-11 14:25:40 +00:00
if (request.body) {
if (request.body.mode === "urlencoded") {
2021-05-18 16:09:55 +00:00
const params = request.body.urlencoded
pwRequest.bodyParams = params || []
for (const param of pwRequest.bodyParams) {
2020-02-24 18:44:50 +00:00
delete param.type
2020-02-15 22:36:38 +00:00
}
2020-06-11 14:25:40 +00:00
} else if (request.body.mode === "raw") {
2020-02-24 18:44:50 +00:00
pwRequest.rawInput = true
2020-06-11 14:25:40 +00:00
pwRequest.rawParams = request.body.raw
2020-02-15 22:36:38 +00:00
}
}
2020-02-24 18:44:50 +00:00
return pwRequest
},
hasFolder(item) {
2021-05-18 16:09:55 +00:00
return Object.prototype.hasOwnProperty.call(item, "item")
},
2020-02-24 18:44:50 +00:00
},
2021-08-12 08:14:10 +00:00
})
2019-10-22 15:57:48 +00:00
</script>