123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- <template>
- <SmartModal v-if="show" :title="`${t('import.curl')}`" @close="hideModal">
- <template #body>
- <div class="flex flex-col px-2">
- <div ref="curlEditor" class="border border-dividerLight rounded"></div>
- </div>
- </template>
- <template #footer>
- <span class="flex">
- <ButtonPrimary
- :label="`${t('import.title')}`"
- @click.native="handleImport"
- />
- <ButtonSecondary
- :label="`${t('action.cancel')}`"
- @click.native="hideModal"
- />
- </span>
- </template>
- </SmartModal>
- </template>
- <script setup lang="ts">
- import { ref } from "@nuxtjs/composition-api"
- import {
- HoppRESTHeader,
- HoppRESTParam,
- makeRESTRequest,
- } from "@hoppscotch/data"
- import parseCurlCommand from "~/helpers/curlparser"
- import { useCodemirror } from "~/helpers/editor/codemirror"
- import { setRESTRequest } from "~/newstore/RESTSession"
- import { useI18n, useToast } from "~/helpers/utils/composables"
- const t = useI18n()
- const toast = useToast()
- const curl = ref("")
- const curlEditor = ref<any | null>(null)
- useCodemirror(curlEditor, curl, {
- extendedEditorConfig: {
- mode: "application/x-sh",
- placeholder: `${t("request.enter_curl")}`,
- },
- linter: null,
- completer: null,
- })
- defineProps<{ show: boolean }>()
- const emit = defineEmits<{
- (e: "hide-modal"): void
- }>()
- const hideModal = () => {
- emit("hide-modal")
- }
- const handleImport = () => {
- const text = curl.value
- try {
- const parsedCurl = parseCurlCommand(text)
- const { origin, pathname } = new URL(
- parsedCurl.url.replace(/"/g, "").replace(/'/g, "")
- )
- const endpoint = origin + pathname
- const headers: HoppRESTHeader[] = []
- const params: HoppRESTParam[] = []
- if (parsedCurl.query) {
- for (const key of Object.keys(parsedCurl.query)) {
- const val = parsedCurl.query[key]!
- if (Array.isArray(val)) {
- val.forEach((value) => {
- params.push({
- key,
- value,
- active: true,
- })
- })
- } else {
- params.push({
- key,
- value: val!,
- active: true,
- })
- }
- }
- }
- if (parsedCurl.headers) {
- for (const key of Object.keys(parsedCurl.headers)) {
- headers.push({
- key,
- value: parsedCurl.headers[key],
- active: true,
- })
- }
- }
- const method = parsedCurl.method.toUpperCase()
- setRESTRequest(
- makeRESTRequest({
- name: "Untitled request",
- endpoint,
- method,
- params,
- headers,
- preRequestScript: "",
- testScript: "",
- auth: {
- authType: "none",
- authActive: true,
- },
- body: {
- contentType: "application/json",
- body: "",
- },
- })
- )
- } catch (e) {
- console.error(e)
- toast.error(`${t("error.curl_invalid_format")}`)
- }
- hideModal()
- }
- </script>
|