fix: json linting not working on raw body + small ts corrections

This commit is contained in:
Andrew Bastin
2022-01-22 22:16:27 +05:30
parent 238e41ccda
commit 3bb65f2115
2 changed files with 70 additions and 17 deletions

View File

@@ -57,26 +57,57 @@
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { computed, reactive, ref } from "@nuxtjs/composition-api" import {
computed,
reactive,
Ref,
ref,
watchEffect,
} from "@nuxtjs/composition-api"
import * as TO from "fp-ts/TaskOption"
import { pipe } from "fp-ts/function"
import { HoppRESTReqBody, ValidContentTypes } from "~/../hoppscotch-data/dist"
import { useCodemirror } from "~/helpers/editor/codemirror" import { useCodemirror } from "~/helpers/editor/codemirror"
import { getEditorLangForMimeType } from "~/helpers/editorutils" import { getEditorLangForMimeType } from "~/helpers/editorutils"
import { pluckRef, useI18n, useToast } from "~/helpers/utils/composables" import { pluckRef, useI18n, useToast } from "~/helpers/utils/composables"
import { isJSONContentType } from "~/helpers/utils/contenttypes"
import { useRESTRequestBody } from "~/newstore/RESTSession" 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 t = useI18n()
const props = defineProps<{ const props = defineProps<{
contentType: string contentType: PossibleContentTypes
}>() }>()
const toast = useToast() const toast = useToast()
const rawParamsBody = pluckRef(useRESTRequestBody(), "body") const rawParamsBody = pluckRef(
useRESTRequestBody() as Ref<
HoppRESTReqBody & {
contentType: PossibleContentTypes
}
>,
"body"
)
const prettifyIcon = ref("wand") const prettifyIcon = ref("wand")
const rawInputEditorLang = computed(() => const rawInputEditorLang = computed(() =>
getEditorLangForMimeType(props.contentType) getEditorLangForMimeType(props.contentType)
) )
const langLinter = computed(() =>
isJSONContentType(props.contentType) ? jsonLinter : null
)
watchEffect(() => console.log(rawInputEditorLang.value))
const linewrapEnabled = ref(true) const linewrapEnabled = ref(true)
const rawBodyParameters = ref<any | null>(null) const rawBodyParameters = ref<any | null>(null)
@@ -87,9 +118,9 @@ useCodemirror(
extendedEditorConfig: { extendedEditorConfig: {
lineWrapping: linewrapEnabled, lineWrapping: linewrapEnabled,
mode: rawInputEditorLang, mode: rawInputEditorLang,
placeholder: t("request.raw_body"), placeholder: t("request.raw_body").toString(),
}, },
linter: null, linter: langLinter,
completer: null, completer: null,
environmentHighlights: true, environmentHighlights: true,
}) })
@@ -99,18 +130,21 @@ const clearContent = () => {
rawParamsBody.value = "" rawParamsBody.value = ""
} }
const uploadPayload = (e: InputEvent) => { const uploadPayload = async (e: InputEvent) => {
const file = e.target.files[0] await pipe(
if (file !== undefined && file !== null) { (e.target as HTMLInputElement).files?.[0],
const reader = new FileReader() TO.of,
reader.onload = ({ target }) => { TO.chain(TO.fromPredicate((f): f is File => f !== undefined)),
rawParamsBody.value = target?.result TO.chain(readFileAsText),
}
reader.readAsText(file) TO.matchW(
toast.success(`${t("state.file_imported")}`) () => toast.error(`${t("action.choose_file")}`),
} else { (result) => {
toast.error(`${t("action.choose_file")}`) rawParamsBody.value = result
} toast.success(`${t("state.file_imported")}`)
}
)
)()
} }
const prettifyRequestBody = () => { const prettifyRequestBody = () => {
try { try {

View File

@@ -0,0 +1,19 @@
import * as TO from "fp-ts/TaskOption"
export const readFileAsText = (file: File) =>
TO.tryCatch(
() =>
new Promise<string>((resolve, reject) => {
const reader = new FileReader()
reader.onload = () => {
resolve(reader.result as string)
}
reader.onerror = () => {
reject(new Error("File err"))
}
reader.readAsText(file)
})
)