api-client/pages/documentation.vue

365 lines
10 KiB
Vue
Raw Normal View History

2019-12-20 09:10:16 +00:00
<template>
2021-07-30 15:36:25 +00:00
<Splitpanes :dbl-click-splitter="false" vertical>
<Pane class="hide-scrollbar !overflow-auto">
<Splitpanes :dbl-click-splitter="false" horizontal>
<Pane class="hide-scrollbar !overflow-auto">
<AppSection label="import">
<div class="flex p-4 items-start justify-between">
<label class="font-semibold">
{{ $t("generate_docs_message") }}
</label>
<span
class="
bg-accentLight
rounded
font-semibold
text-primary
py-1
px-2
inline-flex
"
>
BETA
</span>
</div>
<div
class="
bg-primary
border-b border-dividerLight
flex
top-0
z-10
items-start
justify-between
sticky
"
>
<label for="collectionUpload" class="font-semibold">
2021-07-03 13:14:58 +00:00
<ButtonSecondary
2021-07-30 15:36:25 +00:00
v-tippy="{ theme: 'tooltip' }"
title="JSON"
icon="folder"
:label="$t('import_collections')"
@click.native="$refs.collectionUpload.click()"
2021-07-03 13:14:58 +00:00
/>
2021-07-30 15:36:25 +00:00
</label>
<input
ref="collectionUpload"
class="input"
name="collectionUpload"
type="file"
@change="uploadCollection"
/>
<ButtonSecondary
v-tippy="{ theme: 'tooltip' }"
:title="$t('clear')"
icon="clear_all"
@click.native="collectionJSON = '[]'"
/>
</div>
<SmartAceEditor
v-model="collectionJSON"
:lang="'json'"
:lint="false"
:options="{
maxLines: Infinity,
minLines: 16,
fontSize: '12px',
autoScrollEditorIntoView: true,
showPrintMargin: false,
useWorker: false,
}"
/>
<div
class="
bg-primary
flex
p-4
bottom-0
z-10
justify-between
items-start
sticky
"
>
<ButtonPrimary
:label="$t('generate_docs')"
@click.native="getDoc"
/>
</div>
</AppSection>
</Pane>
<Pane class="hide-scrollbar !overflow-auto">
<AppSection label="documentation">
<div class="flex flex-col">
<div
v-if="items.length === 0"
class="
flex flex-col
text-secondaryLight
p-4
items-center
justify-center
"
>
<i class="opacity-75 pb-2 material-icons">topic</i>
<span class="text-center">
2021-07-06 17:31:18 +00:00
{{ $t("generate_docs_first") }}
2021-07-30 15:36:25 +00:00
</span>
</div>
<div
v-else
class="
bg-primary
border-b border-dividerLight
flex flex-1
p-4
top-0
z-10
sticky
"
>
<div
v-tippy="{ theme: 'tooltip' }"
:title="
!currentUser
? `${$t('login_with_github_to')} ${$t(
'create_secret_gist'
).toLowerCase()}`
: currentUser.provider !== 'github.com'
? `${$t('login_with_github_to')} ${$t(
'create_secret_gist'
).toLowerCase()}`
: 'Beta'
"
>
<ButtonPrimary
:disabled="
2021-07-06 17:31:18 +00:00
!currentUser
2021-07-30 15:36:25 +00:00
? true
2021-07-06 17:31:18 +00:00
: currentUser.provider !== 'github.com'
2021-07-30 15:36:25 +00:00
? true
: false
2021-07-06 17:31:18 +00:00
"
2021-07-30 15:36:25 +00:00
:label="$t('create_secret_gist')"
@click.native="createDocsGist"
/>
2021-07-06 17:31:18 +00:00
</div>
2020-12-11 16:54:34 +00:00
</div>
2021-07-30 15:36:25 +00:00
<div
v-for="(collection, index) in items"
:key="`collection-${index}`"
>
<DocsCollection :collection="collection" />
</div>
</div>
</AppSection>
</Pane>
</Splitpanes>
</Pane>
<Pane
v-if="RIGHT_SIDEBAR"
max-size="35"
size="25"
min-size="20"
class="hide-scrollbar !overflow-auto"
>
<aside>
<Collections
:selected="selected"
:doc="true"
@use-collection="useSelectedCollection($event)"
@remove-collection="removeSelectedCollection($event)"
/>
</aside>
</Pane>
</Splitpanes>
2019-12-20 09:10:16 +00:00
</template>
<script>
import { defineComponent } from "@nuxtjs/composition-api"
2021-07-06 17:31:18 +00:00
import { Splitpanes, Pane } from "splitpanes"
import Mustache from "mustache"
2021-06-14 04:07:30 +00:00
import { currentUser$ } from "~/helpers/fb/auth"
import DocsTemplate from "~/assets/md/docs.md"
import folderContents from "~/assets/md/folderContents.md"
import folderBody from "~/assets/md/folderBody.md"
import { useSetting } from "~/newstore/settings"
export default defineComponent({
2021-07-06 17:31:18 +00:00
components: { Splitpanes, Pane },
setup() {
return {
RIGHT_SIDEBAR: useSetting("RIGHT_SIDEBAR"),
}
},
2019-12-20 09:10:16 +00:00
data() {
return {
collectionJSON: "[]",
2020-02-24 18:44:50 +00:00
items: [],
docsMarkdown: "",
selected: [],
2020-02-24 18:44:50 +00:00
}
2019-12-20 09:10:16 +00:00
},
2021-06-14 04:07:30 +00:00
subscriptions() {
return {
currentUser: currentUser$,
}
},
2021-05-18 16:09:55 +00:00
head() {
return {
title: `Documentation • Hoppscotch`,
}
},
2019-12-20 09:10:16 +00:00
methods: {
async createDocsGist() {
await this.$axios
.$post(
"https://api.github.com/gists",
{
files: {
"api-docs.md": {
content: this.docsMarkdown,
},
},
},
{
headers: {
2021-06-14 04:07:30 +00:00
Authorization: `token ${this.currentUser.accessToken}`,
Accept: "application/vnd.github.v3+json",
},
}
)
2021-05-18 16:09:55 +00:00
.then((res) => {
this.$toast.success(this.$t("gist_created"), {
icon: "done",
})
2021-05-18 16:09:55 +00:00
window.open(res.html_url)
})
.catch((error) => {
this.$toast.error(this.$t("something_went_wrong"), {
icon: "error",
})
console.log(error)
})
},
2019-12-20 09:10:16 +00:00
uploadCollection() {
2021-05-18 16:09:55 +00:00
const file = this.$refs.collectionUpload.files[0]
2019-12-20 09:10:16 +00:00
if (file !== undefined && file !== null) {
2021-05-18 16:09:55 +00:00
const reader = new FileReader()
2020-01-30 18:48:20 +00:00
reader.onload = ({ target }) => {
2020-02-24 18:44:50 +00:00
this.collectionJSON = target.result
}
reader.readAsText(file)
this.$toast.info(this.$t("file_imported"), {
icon: "attach_file",
2020-02-24 18:44:50 +00:00
})
2019-12-20 09:10:16 +00:00
} else {
this.$toast.error(this.$t("choose_file"), {
icon: "attach_file",
2020-02-24 18:44:50 +00:00
})
2019-12-20 09:10:16 +00:00
}
this.$refs.collectionUpload.value = ""
2019-12-20 09:10:16 +00:00
},
2021-05-18 16:09:55 +00:00
assignIDs(items, pref, nestingLevel) {
for (let i = 0; i < items.length; ++i) {
items[i].id = `&emsp;${pref}${i + 1}.`
items[i].ref = `${items[i].name.split(" ").join("-")}`
2021-05-18 16:09:55 +00:00
items[i].nestingLevel = nestingLevel
items[i].folders = this.assignIDs(
items[i].folders,
items[i].id,
nestingLevel + "#"
)
for (let j = 0; j < items[i].requests.length; ++j) {
items[i].requests[j].id = `&emsp;${items[i].id}${i + 1}`
2021-05-18 16:09:55 +00:00
items[i].requests[j].ref = `${items[i].requests[j].name
.split(" ")
.join("-")}`
items[i].requests[j].nestingLevel = nestingLevel + "#"
}
}
return items
},
2019-12-20 09:10:16 +00:00
getDoc() {
2019-12-22 10:19:26 +00:00
try {
2020-02-24 18:44:50 +00:00
this.items = JSON.parse(this.collectionJSON)
this.assignIDs(this.items, "", "#")
2020-04-30 02:31:56 +00:00
this.$toast.clear()
this.$toast.info(this.$t("docs_generated"), {
icon: "book",
2020-02-24 18:44:50 +00:00
})
const docsMarkdown = Mustache.render(
DocsTemplate,
{
collections: this.items,
isHeaders() {
return this.headers.length
},
isParams() {
return this.params.length
},
isAuth() {
return this.auth !== "None"
},
isAuthBasic() {
return this.httpUser && this.httpPassword
},
isRawParams() {
return this.rawParams && this.rawParams !== "{}"
},
isPreRequestScript() {
return (
this.preRequestScript &&
2021-05-18 16:09:55 +00:00
this.preRequestScript !== `// pw.env.set('variable', 'value');`
)
},
isTestScript() {
2021-05-18 16:09:55 +00:00
return (
this.testScript &&
this.testScript !== `// pw.expect('variable').toBe('value');`
)
},
},
{
2021-05-18 16:09:55 +00:00
folderContents,
folderBody,
}
)
this.docsMarkdown = docsMarkdown.replace(/^\s*[\r\n]/gm, "\n\n")
2019-12-22 10:19:26 +00:00
} catch (e) {
this.$toast.error(e, {
icon: "code",
2020-02-24 18:44:50 +00:00
})
2019-12-22 10:19:26 +00:00
}
2020-02-24 18:44:50 +00:00
},
2020-07-18 01:09:45 +00:00
useSelectedCollection(collection) {
2021-05-18 16:09:55 +00:00
if (this.selected.find((coll) => coll === collection)) {
return
}
this.selected.push(collection)
2021-05-18 16:09:55 +00:00
const importCollection = JSON.stringify(this.selected, null, 2)
this.collectionJSON = JSON.stringify(
JSON.parse(importCollection),
null,
2
)
},
removeSelectedCollection(collection) {
2021-05-18 16:09:55 +00:00
this.selected = this.selected.filter((coll) => coll !== collection)
const importCollection = JSON.stringify(this.selected, null, 2)
this.collectionJSON = JSON.stringify(
JSON.parse(importCollection),
null,
2
)
2020-07-18 01:09:45 +00:00
},
2020-02-24 18:44:50 +00:00
},
})
2019-12-20 09:10:16 +00:00
</script>