api-client/components/environments/ImportExport.vue

256 lines
7.4 KiB
Vue
Raw Normal View History

2020-02-23 16:38:15 +00:00
<template>
<SmartModal v-if="show" @close="hideModal">
<template #header>
<h3 class="heading">
{{ $t("import_export") }} {{ $t("environments") }}
</h3>
<div>
<v-popover>
<button
v-tooltip.left="$t('more')"
class="tooltip-target icon button"
>
<i class="material-icons">more_vert</i>
</button>
<template #popover>
<div>
<button
v-close-popover
class="icon button"
@click="readEnvironmentGist"
>
<i class="material-icons">assignment_returned</i>
<span>{{ $t("import_from_gist") }}</span>
</button>
</div>
<div
v-tooltip.bottom="{
content: !currentUser
? $t('login_with_github_to') + $t('create_secret_gist')
: currentUser.provider !== 'github.com'
? $t('login_with_github_to') + $t('create_secret_gist')
: null,
}"
>
<button
v-close-popover
:disabled="
!currentUser
? true
2021-06-14 04:07:30 +00:00
: currentUser.provider !== 'github.com'
? true
: false
"
class="icon button"
@click="createEnvironmentGist"
2020-12-11 10:29:03 +00:00
>
<i class="material-icons">assignment_turned_in</i>
<span>{{ $t("create_secret_gist") }}</span>
</button>
</div>
</template>
</v-popover>
<button class="icon button" @click="hideModal">
<i class="material-icons">close</i>
</button>
2020-12-11 10:29:03 +00:00
</div>
</template>
<template #body>
2020-12-11 16:54:34 +00:00
<div class="flex flex-col items-start p-2">
2020-12-11 10:29:03 +00:00
<button
2021-05-18 06:26:59 +00:00
v-tooltip="$t('replace_current')"
class="icon button"
2020-12-11 10:29:03 +00:00
@click="openDialogChooseFileToReplaceWith"
>
<i class="material-icons">folder_special</i>
2020-12-11 10:29:03 +00:00
<span>{{ $t("replace_json") }}</span>
<input
2021-05-18 06:26:59 +00:00
ref="inputChooseFileToReplaceWith"
class="input"
2020-12-11 10:29:03 +00:00
type="file"
style="display: none"
accept="application/json"
2021-05-18 06:26:59 +00:00
@change="replaceWithJSON"
2020-12-11 10:29:03 +00:00
/>
</button>
<button
2021-05-18 06:26:59 +00:00
v-tooltip="$t('preserve_current')"
class="icon button"
2020-12-11 10:29:03 +00:00
@click="openDialogChooseFileToImportFrom"
>
<i class="material-icons">create_new_folder</i>
2020-12-11 10:29:03 +00:00
<span>{{ $t("import_json") }}</span>
<input
2021-05-18 06:26:59 +00:00
ref="inputChooseFileToImportFrom"
class="input"
2020-12-11 10:29:03 +00:00
type="file"
style="display: none"
accept="application/json"
2021-05-18 06:26:59 +00:00
@change="importFromJSON"
2020-12-11 10:29:03 +00:00
/>
</button>
<button
v-tooltip="$t('download_file')"
class="icon button"
@click="exportJSON"
>
<i class="material-icons">drive_file_move</i>
<span>
{{ $t("export_as_json") }}
</span>
</button>
2020-02-23 16:38:15 +00:00
</div>
</template>
</SmartModal>
2020-02-23 16:38:15 +00:00
</template>
<script>
2021-06-14 04:07:30 +00:00
import { currentUser$ } from "~/helpers/fb/auth"
import {
environments$,
replaceEnvironments,
appendEnvironments,
} from "~/newstore/environments"
2020-02-23 16:38:15 +00:00
export default {
2021-05-18 06:26:59 +00:00
props: {
show: Boolean,
},
subscriptions() {
return {
environments: environments$,
2021-06-14 04:07:30 +00:00
currentUser: currentUser$,
}
},
2020-02-23 16:38:15 +00:00
computed: {
environmentJson() {
2021-06-07 04:13:28 +00:00
return JSON.stringify(this.environments, null, 2)
2020-02-24 18:44:50 +00:00
},
2020-02-23 16:38:15 +00:00
},
methods: {
async createEnvironmentGist() {
await this.$axios
.$post(
"https://api.github.com/gists",
{
files: {
"hoppscotch-environments.json": {
content: this.environmentJson,
},
},
},
{
headers: {
2021-06-14 04:07:30 +00:00
Authorization: `token ${this.currentUser.accessToken}`,
Accept: "application/vnd.github.v3+json",
},
}
)
2021-05-18 06:26:59 +00:00
.then((res) => {
this.$toast.success(this.$t("gist_created"), {
icon: "done",
})
2021-05-18 06:26:59 +00:00
window.open(res.html_url)
})
.catch((error) => {
this.$toast.error(this.$t("something_went_wrong"), {
icon: "error",
})
console.log(error)
})
},
async readEnvironmentGist() {
2021-05-18 06:26:59 +00:00
const 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 }) => {
2021-05-18 06:26:59 +00:00
const environments = JSON.parse(Object.values(files)[0].content)
2021-06-07 04:13:28 +00:00
replaceEnvironments(environments)
this.fileImported()
})
.catch((error) => {
this.failedImport()
console.log(error)
})
},
2020-02-23 16:38:15 +00:00
hideModal() {
this.$emit("hide-modal")
2020-02-23 16:38:15 +00:00
},
openDialogChooseFileToReplaceWith() {
2020-02-24 18:44:50 +00:00
this.$refs.inputChooseFileToReplaceWith.click()
2020-02-23 16:38:15 +00:00
},
openDialogChooseFileToImportFrom() {
2020-02-24 18:44:50 +00:00
this.$refs.inputChooseFileToImportFrom.click()
2020-02-23 16:38:15 +00:00
},
replaceWithJSON() {
2021-05-18 06:26:59 +00:00
const reader = new FileReader()
2020-10-21 06:50:32 +00:00
reader.onload = ({ target }) => {
2021-05-18 06:26:59 +00:00
const content = target.result
const environments = JSON.parse(content)
2021-06-07 04:13:28 +00:00
replaceEnvironments(environments)
2020-02-24 18:44:50 +00:00
}
reader.readAsText(this.$refs.inputChooseFileToReplaceWith.files[0])
this.fileImported()
this.$refs.inputChooseFileToReplaceWith.value = ""
2020-02-23 16:38:15 +00:00
},
importFromJSON() {
2021-05-18 06:26:59 +00:00
const reader = new FileReader()
2020-10-21 06:50:32 +00:00
reader.onload = ({ target }) => {
2021-05-18 06:26:59 +00:00
const content = target.result
const importFileObj = JSON.parse(content)
if (
2021-05-18 06:26:59 +00:00
importFileObj._postman_variable_scope === "environment" ||
importFileObj._postman_variable_scope === "globals"
) {
this.importFromPostman(importFileObj)
} else {
this.importFromPostwoman(importFileObj)
}
2020-02-24 18:44:50 +00:00
}
reader.readAsText(this.$refs.inputChooseFileToImportFrom.files[0])
this.$refs.inputChooseFileToImportFrom.value = ""
2020-02-23 16:38:15 +00:00
},
importFromPostwoman(environments) {
2021-06-07 04:13:28 +00:00
appendEnvironments(environments)
this.fileImported()
},
2020-10-21 06:50:32 +00:00
importFromPostman({ name, values }) {
2021-05-18 06:26:59 +00:00
const environment = { name, variables: [] }
values.forEach(({ key, value }) =>
environment.variables.push({ key, value })
)
const environments = [environment]
this.importFromPostwoman(environments)
},
2020-02-23 16:38:15 +00:00
exportJSON() {
2020-02-24 18:44:50 +00:00
let text = this.environmentJson
text = text.replace(/\n/g, "\r\n")
2021-05-18 06:26:59 +00:00
const blob = new Blob([text], {
type: "text/json",
2020-02-24 18:44:50 +00:00
})
2021-05-18 06:26:59 +00:00
const anchor = document.createElement("a")
anchor.download = "hoppscotch-environment.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-02-23 16:38:15 +00:00
},
fileImported() {
this.$toast.info(this.$t("file_imported"), {
icon: "folder_shared",
2020-02-24 18:44:50 +00:00
})
},
},
}
2020-02-23 16:38:15 +00:00
</script>