api-client/components/collections/importExportCollections.vue

296 lines
9.3 KiB
Vue
Raw Normal View History

2019-10-22 05:00:13 +00:00
<template>
2019-12-17 19:13:15 +00:00
<modal v-if="show" @close="hideModal">
2019-10-25 08:14:34 +00:00
<div slot="header">
<ul>
<li>
<div class="flex-wrap">
2020-03-08 16:22:04 +00:00
<h3 class="title">{{ $t("import_export") }} {{ $t("collections") }}</h3>
2019-10-25 08:14:34 +00:00
<div>
2019-12-17 19:13:15 +00:00
<button class="icon" @click="hideModal">
2019-10-25 08:14:34 +00:00
<i class="material-icons">close</i>
</button>
2019-10-22 05:00:13 +00:00
</div>
2019-10-25 08:14:34 +00:00
</div>
2020-01-25 06:51:47 +00:00
<div class="flex-wrap">
<span
v-tooltip="{
2020-02-24 18:44:50 +00:00
content: !fb.currentUser ? $t('login_first') : $t('replace_current'),
2020-01-25 06:51:47 +00:00
}"
>
2020-02-24 18:44:50 +00:00
<button :disabled="!fb.currentUser" class="icon" @click="syncCollections">
2020-01-25 06:51:47 +00:00
<i class="material-icons">folder_shared</i>
<span>{{ $t("import_from_sync") }}</span>
2020-01-25 06:51:47 +00:00
</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>
2020-01-25 06:51:47 +00:00
<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>
2020-01-25 06:51:47 +00:00
<input
type="file"
@change="importFromJSON"
style="display: none;"
ref="inputChooseFileToImportFrom"
accept="application/json"
/>
</button>
</div>
2019-10-25 08:14:34 +00:00
</li>
</ul>
</div>
<div slot="body">
<textarea v-model="collectionJson" rows="8"></textarea>
2019-10-22 05:00:13 +00:00
</div>
2019-10-25 08:14:34 +00:00
<div slot="footer">
2019-12-17 19:13:15 +00:00
<div class="flex-wrap">
<span></span>
<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>
</modal>
2019-10-22 05:00:13 +00:00
</template>
<script>
import { fb } from "~/helpers/fb"
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-01-25 06:51:47 +00:00
},
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
},
components: {
modal: () => import("~/components/ui/modal"),
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: {
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-06-11 14:25:40 +00:00
reader.onload = (event) => {
2020-02-24 18:44:50 +00:00
let content = event.target.result
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")) {
2020-02-24 18:44:50 +00:00
collections = this.parsePostmanCollection(collections)
2020-02-15 22:36:38 +00:00
} else {
2020-02-24 18:44:50 +00:00
return this.failedImport()
2020-02-15 22:36:38 +00:00
}
this.$store.commit("postwoman/importCollections", collections)
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])
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-06-11 14:25:40 +00:00
reader.onload = (event) => {
2020-02-24 18:44:50 +00:00
let content = event.target.result
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")) {
2020-02-24 18:44:50 +00:00
collections = this.parsePostmanCollection(collections)
2020-02-15 22:36:38 +00:00
} else {
2020-02-24 18:44:50 +00:00
return this.failedImport()
2020-02-15 22:36:38 +00:00
}
this.$store.commit("postwoman/importCollections", collections)
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])
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 = "postwoman-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() {
this.$store.commit("postwoman/replaceCollections", fb.currentCollections)
2020-02-24 18:44:50 +00:00
this.fileImported()
2020-01-25 06:51:47 +00:00
},
syncToFBCollections() {
if (fb.currentUser !== null) {
if (fb.currentSettings[0].value) {
fb.writeCollections(JSON.parse(JSON.stringify(this.$store.state.postwoman.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-06-11 15:17:49 +00:00
parsePostmanCollection(collection, folders = true) {
2020-02-23 19:00:22 +00:00
let postwomanCollection = folders
? [
{
name: "",
2020-02-23 19:00:22 +00:00
folders: [],
2020-02-24 18:44:50 +00:00
requests: [],
},
2020-02-23 19:00:22 +00:00
]
2020-02-15 22:36:38 +00:00
: {
name: "",
2020-02-24 18:44:50 +00:00
requests: [],
}
2020-06-11 15:17:49 +00:00
for (let collectionItem of collection.item) {
2020-02-15 22:36:38 +00:00
if (collectionItem.request) {
if (postwomanCollection[0]) {
2020-06-11 15:17:49 +00:00
postwomanCollection[0].name = collection.info ? collection.info.name : ""
2020-02-24 18:44:50 +00:00
postwomanCollection[0].requests.push(this.parsePostmanRequest(collectionItem))
2020-02-15 22:36:38 +00:00
} else {
2020-06-11 15:17:49 +00:00
postwomanCollection.name = collection.name ? collection.name : ""
2020-02-24 18:44:50 +00:00
postwomanCollection.requests.push(this.parsePostmanRequest(collectionItem))
2020-02-15 22:36:38 +00:00
}
} else if (collectionItem.item) {
if (collectionItem.item[0]) {
2020-02-24 18:44:50 +00:00
postwomanCollection[0].folders.push(this.parsePostmanCollection(collectionItem, false))
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
let requestObjectUrl = request.url.raw.match(/^(.+:\/\/[^\/]+|{[^\/]+})(\/[^\?]+|).*$/)
2020-04-20 04:44:10 +00:00
if (requestObjectUrl) {
pwRequest.url = requestObjectUrl[1]
pwRequest.path = requestObjectUrl[2] ? requestObjectUrl[2] : ""
}
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
}
}
2020-06-11 14:25:40 +00:00
let requestObjectParams = request.url.query
2020-02-15 22:36:38 +00:00
if (requestObjectParams) {
2020-02-24 18:44:50 +00:00
pwRequest.params = requestObjectParams
2020-02-15 22:36:38 +00:00
for (let param of pwRequest.params) {
2020-02-24 18:44:50 +00:00
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
},
},
}
2019-10-22 15:57:48 +00:00
</script>