api-client/components/collections/ImportExport.vue

400 lines
13 KiB
Vue
Raw Normal View History

2019-10-22 05:00:13 +00:00
<template>
<SmartModal v-if="show" @close="hideModal">
2019-10-25 08:14:34 +00:00
<div slot="header">
2020-12-11 10:29:03 +00:00
<div class="row-wrapper">
<h3 class="title">{{ $t("import_export") }} {{ $t("collections") }}</h3>
<div>
<v-popover>
<button class="tooltip-target icon" v-tooltip.left="$t('more')">
<i class="material-icons">more_vert</i>
2020-01-25 06:51:47 +00:00
</button>
2020-12-11 10:29:03 +00:00
<template slot="popover">
<div>
<button class="icon" @click="readCollectionGist" v-close-popover>
<i class="material-icons">assignment_returned</i>
<span>{{ $t("import_from_gist") }}</span>
</button>
</div>
<div
v-tooltip.bottom="{
content: !fb.currentUser
? $t('login_with_github_to') + $t('create_secret_gist')
: fb.currentUser.provider !== 'github.com'
? $t('login_with_github_to') + $t('create_secret_gist')
: null,
}"
>
<button
:disabled="
!fb.currentUser ? true : fb.currentUser.provider !== 'github.com' ? true : false
"
class="icon"
@click="createCollectionGist"
v-close-popover
>
<i class="material-icons">assignment_turned_in</i>
<span>{{ $t("create_secret_gist") }}</span>
</button>
</div>
</template>
</v-popover>
<button class="icon" @click="hideModal">
<i class="material-icons">close</i>
</button>
</div>
</div>
2019-10-25 08:14:34 +00:00
</div>
2020-12-11 16:54:34 +00:00
<div slot="body" class="flex flex-col">
<div class="flex flex-col items-start p-2">
2020-12-11 10:29:03 +00:00
<span
v-tooltip="{
content: !fb.currentUser ? $t('login_first') : $t('replace_current'),
}"
>
<button :disabled="!fb.currentUser" class="icon" @click="syncCollections">
<i class="material-icons">folder_shared</i>
<span>{{ $t("import_from_sync") }}</span>
</button>
</span>
<button
class="icon"
@click="openDialogChooseFileToReplaceWith"
v-tooltip="$t('replace_current')"
>
<i class="material-icons">create_new_folder</i>
<span>{{ $t("replace_json") }}</span>
<input
type="file"
@change="replaceWithJSON"
style="display: none"
ref="inputChooseFileToReplaceWith"
accept="application/json"
/>
</button>
<button
class="icon"
@click="openDialogChooseFileToImportFrom"
v-tooltip="$t('preserve_current')"
>
<i class="material-icons">folder_special</i>
<span>{{ $t("import_json") }}</span>
<input
type="file"
@change="importFromJSON"
style="display: none"
ref="inputChooseFileToImportFrom"
accept="application/json"
/>
</button>
</div>
<div v-if="showJsonCode" class="row-wrapper">
<textarea v-model="collectionJson" rows="8" readonly></textarea>
</div>
2019-10-22 05:00:13 +00:00
</div>
2019-10-25 08:14:34 +00:00
<div slot="footer">
2020-09-22 17:06:37 +00:00
<div class="row-wrapper">
2020-12-11 10:29:03 +00:00
<span>
<SmartToggle :on="showJsonCode" @change="showJsonCode = $event">
2020-12-11 10:29:03 +00:00
{{ $t("show_code") }}
</SmartToggle>
2020-12-11 10:29:03 +00:00
</span>
2019-12-17 19:13:15 +00:00
<span>
<button class="icon" @click="hideModal">
{{ $t("cancel") }}
2019-10-25 08:14:34 +00:00
</button>
2020-02-24 18:44:50 +00:00
<button class="icon primary" @click="exportJSON" v-tooltip="$t('download_file')">
{{ $t("export") }}
2019-12-17 19:13:15 +00:00
</button>
</span>
</div>
2019-10-25 08:14:34 +00:00
</div>
</SmartModal>
2019-10-22 05:00:13 +00:00
</template>
<script>
import { fb } from "~/helpers/fb"
import { getSettingSubject } from "~/newstore/settings"
2020-01-25 06:51:47 +00:00
2019-11-02 05:32:21 +00:00
export default {
2020-01-25 06:51:47 +00:00
data() {
return {
2020-02-24 18:44:50 +00:00
fb,
2020-12-11 10:29:03 +00:00
showJsonCode: false,
2020-02-24 18:44:50 +00:00
}
2020-01-25 06:51:47 +00:00
},
subscriptions() {
SYNC_COLLECTIONS: getSettingSubject("syncCollections")
},
2019-11-02 05:32:21 +00:00
props: {
2020-02-24 18:44:50 +00:00
show: Boolean,
2019-11-02 05:32:21 +00:00
},
computed: {
collectionJson() {
2020-02-24 18:44:50 +00:00
return JSON.stringify(this.$store.state.postwoman.collections, null, 2)
},
2019-11-02 05:32:21 +00:00
},
methods: {
2020-12-07 08:44:02 +00:00
async createCollectionGist() {
await this.$axios
.$post(
"https://api.github.com/gists",
{
files: {
"hoppscotch-collections.json": {
content: this.collectionJson,
},
},
},
{
headers: {
2020-12-08 12:20:32 +00:00
Authorization: `token ${fb.currentUser.accessToken}`,
2020-12-07 08:44:02 +00:00
Accept: "application/vnd.github.v3+json",
},
}
)
.then(({ html_url }) => {
2020-12-07 08:44:02 +00:00
this.$toast.success(this.$t("gist_created"), {
icon: "done",
})
window.open(html_url)
2020-12-07 08:44:02 +00:00
})
.catch((error) => {
this.$toast.error(this.$t("something_went_wrong"), {
icon: "error",
})
console.log(error)
})
},
2020-12-08 06:35:10 +00:00
async readCollectionGist() {
let gist = prompt(this.$t("enter_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 }) => {
let collections = JSON.parse(Object.values(files)[0].content)
2021-03-18 14:55:12 +00:00
this.$store.commit("postwoman/replaceCollections", { data: collections, flag: "rest" })
2020-12-08 06:35:10 +00:00
this.fileImported()
this.syncToFBCollections()
})
.catch((error) => {
this.failedImport()
console.log(error)
})
},
2019-12-17 19:13:15 +00:00
hideModal() {
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() {
2020-02-24 18:44:50 +00:00
let reader = new FileReader()
2020-10-21 06:50:32 +00:00
reader.onload = ({ target }) => {
let 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]) {
2020-02-24 18:44:50 +00:00
let [name, folders, requests] = Object.keys(collections[0])
if (name === "name" && folders === "folders" && requests === "requests") {
2020-02-15 22:36:38 +00:00
// Do nothing
}
} 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()
return
2020-02-15 22:36:38 +00:00
}
2021-03-18 14:55:12 +00:00
this.$store.commit("postwoman/replaceCollections", { data: collections, flag: "rest" })
2020-02-24 18:44:50 +00:00
this.fileImported()
this.syncToFBCollections()
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() {
2020-02-24 18:44:50 +00:00
let reader = new FileReader()
2020-10-21 06:50:32 +00:00
reader.onload = ({ target }) => {
let 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]) {
2020-02-24 18:44:50 +00:00
let [name, folders, requests] = Object.keys(collections[0])
if (name === "name" && folders === "folders" && requests === "requests") {
2020-02-15 22:36:38 +00:00
// Do nothing
}
} else if (collections.info && collections.info.schema.includes("v2.1.0")) {
//replace the variables, postman uses {{var}}, Hoppscotch uses <<var>>
2020-09-22 17:06:37 +00:00
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-03-18 14:55:12 +00:00
this.$store.commit("postwoman/importCollections", { data: collections, flag: "rest" })
2020-02-24 18:44:50 +00:00
this.fileImported()
this.syncToFBCollections()
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
},
2019-11-02 05:32:21 +00:00
exportJSON() {
2020-02-24 18:44:50 +00:00
let text = this.collectionJson
text = text.replace(/\n/g, "\r\n")
2019-11-02 05:32:21 +00:00
let blob = new Blob([text], {
type: "text/json",
2020-02-24 18:44:50 +00:00
})
let anchor = document.createElement("a")
anchor.download = "hoppscotch-collection.json"
2020-02-24 18:44:50 +00:00
anchor.href = window.URL.createObjectURL(blob)
anchor.target = "_blank"
anchor.style.display = "none"
2020-02-24 18:44:50 +00:00
document.body.appendChild(anchor)
anchor.click()
document.body.removeChild(anchor)
this.$toast.success(this.$t("download_started"), {
icon: "done",
2020-02-24 18:44:50 +00:00
})
2020-01-25 06:51:47 +00:00
},
syncCollections() {
2021-03-18 14:55:12 +00:00
this.$store.commit("postwoman/replaceCollections", {
data: fb.currentCollections,
flag: "rest",
})
2020-02-24 18:44:50 +00:00
this.fileImported()
2020-01-25 06:51:47 +00:00
},
syncToFBCollections() {
if (fb.currentUser !== null && this.SYNC_COLLECTIONS) {
fb.writeCollections(
JSON.parse(JSON.stringify(this.$store.state.postwoman.collections)),
"collections"
)
}
},
2020-01-25 06:51:47 +00:00
fileImported() {
this.$toast.info(this.$t("file_imported"), {
icon: "folder_shared",
2020-02-24 18:44:50 +00:00
})
2020-02-15 22:36:38 +00:00
},
failedImport() {
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 }) {
let postwomanCollection = {
name: "",
folders: [],
requests: [],
}
2020-10-21 06:50:32 +00:00
postwomanCollection.name = info ? info.name : name
2020-10-21 06:50:32 +00:00
if (item && item.length > 0) {
for (let collectionItem of item) {
if (collectionItem.request) {
2020-10-21 06:50:32 +00:00
if (postwomanCollection.hasOwnProperty("folders")) {
postwomanCollection.name = info ? info.name : name
postwomanCollection.requests.push(this.parsePostmanRequest(collectionItem))
} else {
2020-10-21 06:50:32 +00:00
postwomanCollection.name = name ? name : ""
postwomanCollection.requests.push(this.parsePostmanRequest(collectionItem))
}
} else if (this.hasFolder(collectionItem)) {
postwomanCollection.folders.push(this.parsePostmanCollection(collectionItem))
2020-02-15 22:36:38 +00:00
} else {
2020-02-24 18:44:50 +00:00
postwomanCollection.requests.push(this.parsePostmanRequest(collectionItem))
2020-02-15 22:36:38 +00:00
}
}
}
2020-02-24 18:44:50 +00:00
return postwomanCollection
2020-02-15 22:36:38 +00:00
},
2020-06-11 14:25:40 +00:00
parsePostmanRequest({ name, request }) {
2020-02-15 22:36:38 +00:00
let 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) {
let 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
let itemAuth = request.auth ? request.auth : ""
let authType = itemAuth ? itemAuth.type : ""
if (authType === "basic") {
pwRequest.auth = "Basic Auth"
2020-02-23 19:00:22 +00:00
pwRequest.httpUser =
itemAuth.basic[0].key === "username" ? itemAuth.basic[0].value : itemAuth.basic[1].value
2020-02-23 19:00:22 +00:00
pwRequest.httpPassword =
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
}
2020-06-11 14:25:40 +00:00
let requestObjectHeaders = request.header
2020-02-15 22:36:38 +00:00
if (requestObjectHeaders) {
2020-02-24 18:44:50 +00:00
pwRequest.headers = requestObjectHeaders
2020-02-15 22:36:38 +00:00
for (let 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) {
let requestObjectParams = request.url.query
if (requestObjectParams) {
pwRequest.params = requestObjectParams
for (let 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") {
let params = request.body.urlencoded
2020-02-24 18:44:50 +00:00
pwRequest.bodyParams = params ? params : []
2020-02-23 19:00:22 +00:00
for (let 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) {
2020-10-21 06:50:32 +00:00
return item.hasOwnProperty("item")
},
2020-02-24 18:44:50 +00:00
},
}
2019-10-22 15:57:48 +00:00
</script>