feat: migrate to vue 3 + vite (#2553)

Co-authored-by: amk-dev <akash.k.mohan98@gmail.com>
Co-authored-by: liyasthomas <liyascthomas@gmail.com>
This commit is contained in:
Andrew Bastin
2022-09-29 10:55:21 +05:30
committed by GitHub
parent 77a561b581
commit 8b300fab5d
685 changed files with 17102 additions and 25942 deletions

View File

@@ -0,0 +1,164 @@
<template>
<div class="flex flex-col flex-1">
<div
class="sticky z-10 flex items-center justify-between pl-4 border-b bg-primary border-dividerLight top-upperMobileStickyFold sm:top-upperMobileTertiaryStickyFold"
>
<label class="font-semibold text-secondaryLight">
{{ t("request.raw_body") }}
</label>
<div class="flex">
<ButtonSecondary
v-tippy="{ theme: 'tooltip' }"
to="https://docs.hoppscotch.io/features/body"
blank
:title="t('app.wiki')"
:icon="IconHelpCircle"
/>
<ButtonSecondary
v-tippy="{ theme: 'tooltip' }"
:title="t('state.linewrap')"
:class="{ '!text-accent': linewrapEnabled }"
:icon="IconWrapText"
@click.prevent="linewrapEnabled = !linewrapEnabled"
/>
<ButtonSecondary
v-tippy="{ theme: 'tooltip' }"
:title="t('action.clear')"
:icon="IconTrash2"
@click="clearContent"
/>
<ButtonSecondary
v-if="contentType && contentType.endsWith('json')"
v-tippy="{ theme: 'tooltip' }"
:title="t('action.prettify')"
:icon="prettifyIcon"
@click="prettifyRequestBody"
/>
<label for="payload">
<ButtonSecondary
v-tippy="{ theme: 'tooltip' }"
:title="t('import.title')"
:icon="IconFilePlus"
@click="$refs.payload.click()"
/>
</label>
<input
ref="payload"
class="input"
name="payload"
type="file"
@change="uploadPayload"
/>
</div>
</div>
<div ref="rawBodyParameters" class="flex flex-col flex-1"></div>
</div>
</template>
<script setup lang="ts">
import IconHelpCircle from "~icons/lucide/help-circle"
import IconWrapText from "~icons/lucide/wrap-text"
import IconTrash2 from "~icons/lucide/trash-2"
import IconFilePlus from "~icons/lucide/file-plus"
import IconWand from "~icons/lucide/wand"
import IconCheck from "~icons/lucide/check"
import IconInfo from "~icons/lucide/info"
import { computed, reactive, Ref, ref } from "vue"
import * as TO from "fp-ts/TaskOption"
import { pipe } from "fp-ts/function"
import { HoppRESTReqBody, ValidContentTypes } from "@hoppscotch/data"
import { refAutoReset } from "@vueuse/core"
import { useCodemirror } from "@composables/codemirror"
import { getEditorLangForMimeType } from "@helpers/editorutils"
import { pluckRef } from "@composables/ref"
import { useI18n } from "@composables/i18n"
import { useToast } from "@composables/toast"
import { isJSONContentType } from "~/helpers/utils/contenttypes"
import { useRESTRequestBody } from "~/newstore/RESTSession"
import jsonLinter from "~/helpers/editor/linting/json"
import { readFileAsText } from "~/helpers/functional/files"
type PossibleContentTypes = Exclude<
ValidContentTypes,
"multipart/form-data" | "application/x-www-form-urlencoded"
>
const t = useI18n()
const props = defineProps<{
contentType: PossibleContentTypes
}>()
const toast = useToast()
const rawParamsBody = pluckRef(
useRESTRequestBody() as Ref<
HoppRESTReqBody & {
contentType: PossibleContentTypes
}
>,
"body"
)
const prettifyIcon = refAutoReset<
typeof IconWand | typeof IconCheck | typeof IconInfo
>(IconWand, 1000)
const rawInputEditorLang = computed(() =>
getEditorLangForMimeType(props.contentType)
)
const langLinter = computed(() =>
isJSONContentType(props.contentType) ? jsonLinter : null
)
const linewrapEnabled = ref(true)
const rawBodyParameters = ref<any | null>(null)
useCodemirror(
rawBodyParameters,
rawParamsBody,
reactive({
extendedEditorConfig: {
lineWrapping: linewrapEnabled,
mode: rawInputEditorLang,
placeholder: t("request.raw_body").toString(),
},
linter: langLinter,
completer: null,
environmentHighlights: true,
})
)
const clearContent = () => {
rawParamsBody.value = ""
}
const uploadPayload = async (e: InputEvent) => {
await pipe(
(e.target as HTMLInputElement).files?.[0],
TO.of,
TO.chain(TO.fromPredicate((f): f is File => f !== undefined)),
TO.chain(readFileAsText),
TO.matchW(
() => toast.error(`${t("action.choose_file")}`),
(result) => {
rawParamsBody.value = result
toast.success(`${t("state.file_imported")}`)
}
)
)()
}
const prettifyRequestBody = () => {
try {
const jsonObj = JSON.parse(rawParamsBody.value)
rawParamsBody.value = JSON.stringify(jsonObj, null, 2)
prettifyIcon.value = IconCheck
} catch (e) {
console.error(e)
prettifyIcon.value = IconInfo
toast.error(`${t("error.json_prettify_invalid_body")}`)
}
}
</script>