api-client/components/http/RawBody.vue

130 lines
3.2 KiB
Vue
Raw Normal View History

<template>
<div>
<div
class="
bg-primary
2021-07-17 17:40:28 +00:00
border-b border-dividerLight
flex flex-1
top-upperSecondaryStickyFold
2021-08-08 15:17:50 +00:00
pl-4
2021-07-17 17:40:28 +00:00
z-10
sticky
items-center
justify-between
"
>
<label for="rawBody" class="font-semibold">
2021-07-11 22:50:00 +00:00
{{ $t("raw_request_body") }}
</label>
2021-07-24 16:46:48 +00:00
<div class="flex">
2021-07-11 22:50:00 +00:00
<ButtonSecondary
2021-07-22 18:37:39 +00:00
v-if="contentType.endsWith('json')"
2021-07-11 22:50:00 +00:00
ref="prettifyRequest"
v-tippy="{ theme: 'tooltip' }"
:title="$t('prettify_body')"
:icon="prettifyIcon"
@click.native="prettifyRequestBody"
/>
<label for="payload">
<ButtonSecondary
v-tippy="{ theme: 'tooltip' }"
2021-08-02 15:27:18 +00:00
:title="$t('import.json')"
2021-07-11 22:50:00 +00:00
icon="post_add"
@click.native="$refs.payload.click()"
/>
2021-07-11 22:50:00 +00:00
</label>
<input
ref="payload"
class="input"
name="payload"
type="file"
@change="uploadPayload"
/>
<ButtonSecondary
v-tippy="{ theme: 'tooltip' }"
:title="$t('clear')"
icon="clear_all"
@click.native="clearContent('rawParams', $event)"
/>
</div>
</div>
<div class="relative">
<SmartAceEditor
v-model="rawParamsBody"
:lang="rawInputEditorLang"
:options="{
2021-08-08 15:17:50 +00:00
maxLines: Infinity,
minLines: 16,
fontSize: '12px',
2021-07-11 22:50:00 +00:00
autoScrollEditorIntoView: true,
showPrintMargin: false,
useWorker: false,
}"
/>
</div>
</div>
</template>
<script>
2021-07-22 18:37:39 +00:00
import { defineComponent } from "@nuxtjs/composition-api"
import { getEditorLangForMimeType } from "~/helpers/editorutils"
2021-07-22 18:37:39 +00:00
import { pluckRef } from "~/helpers/utils/composables"
import { useRESTRequestBody } from "~/newstore/RESTSession"
2021-07-22 18:37:39 +00:00
export default defineComponent({
props: {
2021-07-22 18:37:39 +00:00
contentType: {
type: String,
required: true,
},
},
2021-07-22 18:37:39 +00:00
setup() {
return {
2021-07-22 18:37:39 +00:00
rawParamsBody: pluckRef(useRESTRequestBody(), "body"),
2021-06-29 11:34:02 +00:00
prettifyIcon: "photo_filter",
}
},
computed: {
rawInputEditorLang() {
return getEditorLangForMimeType(this.contentType)
},
},
methods: {
2021-07-22 18:37:39 +00:00
clearContent() {
this.rawParamsBody = ""
},
uploadPayload() {
const file = this.$refs.payload.files[0]
if (file !== undefined && file !== null) {
const reader = new FileReader()
reader.onload = ({ target }) => {
2021-07-22 18:37:39 +00:00
this.rawParamsBody = target.result
}
reader.readAsText(file)
this.$toast.info(this.$t("file_imported"), {
icon: "attach_file",
})
} else {
this.$toast.error(this.$t("choose_file"), {
icon: "attach_file",
})
}
this.$refs.payload.value = ""
},
prettifyRequestBody() {
try {
const jsonObj = JSON.parse(this.rawParamsBody)
this.rawParamsBody = JSON.stringify(jsonObj, null, 2)
2021-06-29 11:34:02 +00:00
this.prettifyIcon = "done"
setTimeout(() => (this.prettifyIcon = "photo_filter"), 1000)
} catch (e) {
console.error(e)
this.$toast.error(`${this.$t("json_prettify_invalid_body")}`, {
icon: "error",
})
}
},
},
2021-07-22 18:37:39 +00:00
})
</script>