feat: import cURL on paste (#2037)

* feat: import cURL on paste

* feat: import cURL on paste

* feat: pasting cURL command on url field does not paste it in

Co-authored-by: Andrew Bastin <andrewbastin.k@gmail.com>
This commit is contained in:
Anwarul Islam
2021-12-23 01:57:06 +06:00
committed by GitHub
parent 10586e5288
commit 1eb9eb911e
4 changed files with 46 additions and 2 deletions

View File

@@ -11,6 +11,7 @@
<template #footer> <template #footer>
<span class="flex"> <span class="flex">
<ButtonPrimary <ButtonPrimary
ref="importButton"
:label="`${t('import.title')}`" :label="`${t('import.title')}`"
@click.native="handleImport" @click.native="handleImport"
/> />
@@ -24,7 +25,7 @@
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { ref } from "@nuxtjs/composition-api" import { ref, watch } from "@nuxtjs/composition-api"
import { import {
HoppRESTHeader, HoppRESTHeader,
HoppRESTParam, HoppRESTParam,
@@ -43,6 +44,8 @@ const curl = ref("")
const curlEditor = ref<any | null>(null) const curlEditor = ref<any | null>(null)
const props = defineProps<{ show: boolean; text: string }>()
useCodemirror(curlEditor, curl, { useCodemirror(curlEditor, curl, {
extendedEditorConfig: { extendedEditorConfig: {
mode: "application/x-sh", mode: "application/x-sh",
@@ -53,7 +56,15 @@ useCodemirror(curlEditor, curl, {
environmentHighlights: false, environmentHighlights: false,
}) })
defineProps<{ show: boolean }>() watch(
() => props.show,
() => {
if (props.show) {
curl.value = props.text.toString()
}
},
{ immediate: false }
)
const emit = defineEmits<{ const emit = defineEmits<{
(e: "hide-modal"): void (e: "hide-modal"): void

View File

@@ -52,6 +52,7 @@
focus-visible:bg-transparent focus-visible:bg-transparent
" "
@enter="newSendRequest()" @enter="newSendRequest()"
@paste="onPasteUrl($event)"
/> />
</div> </div>
</div> </div>
@@ -196,6 +197,7 @@
</span> </span>
</div> </div>
<HttpImportCurl <HttpImportCurl
:text="curlText"
:show="showCurlImportModal" :show="showCurlImportModal"
@hide-modal="showCurlImportModal = false" @hide-modal="showCurlImportModal = false"
/> />
@@ -267,6 +269,7 @@ const nuxt = useNuxt()
const { subscribeToStream } = useStreamSubscriber() const { subscribeToStream } = useStreamSubscriber()
const newEndpoint = useStream(restEndpoint$, "", setRESTEndpoint) const newEndpoint = useStream(restEndpoint$, "", setRESTEndpoint)
const curlText = ref("")
const newMethod = useStream(restMethod$, "", updateRESTMethod) const newMethod = useStream(restMethod$, "", updateRESTMethod)
const loading = ref(false) const loading = ref(false)
@@ -342,6 +345,27 @@ const newSendRequest = async () => {
} }
} }
const onPasteUrl = (e: { event: ClipboardEvent; previousValue: string }) => {
if (!e) return
const clipboardData = e.event.clipboardData
const pastedData = clipboardData?.getData("Text")
if (!pastedData) return
if (isCURL(pastedData)) {
e.event.preventDefault()
showCurlImportModal.value = true
curlText.value = pastedData
newEndpoint.value = e.previousValue
}
}
function isCURL(curl: string) {
return curl.includes("curl ")
}
const cancelRequest = () => { const cancelRequest = () => {
loading.value = false loading.value = false
updateRESTResponse(null) updateRESTResponse(null)

View File

@@ -16,6 +16,7 @@
@keyup="$emit('keyup', $event)" @keyup="$emit('keyup', $event)"
@click="$emit('click', $event)" @click="$emit('click', $event)"
@keydown="$emit('keydown', $event)" @keydown="$emit('keydown', $event)"
@paste="handlePaste"
></div> ></div>
</div> </div>
</template> </template>
@@ -113,6 +114,9 @@ export default defineComponent({
}, },
methods: { methods: {
handlePaste(ev) {
this.$emit("paste", { event: ev, previousValue: this.internalValue })
},
handleChange() { handleChange() {
this.debouncedHandler = debounce(function () { this.debouncedHandler = debounce(function () {
if (this.internalValue !== this.$refs.editor.textContent) { if (this.internalValue !== this.$refs.editor.textContent) {

View File

@@ -69,6 +69,11 @@ const parseCurlCommand = (curlCommand: string) => {
curlCommand = curlCommand.replace(/\\/gi, "") curlCommand = curlCommand.replace(/\\/gi, "")
curlCommand = curlCommand.replace(/\n/g, "") curlCommand = curlCommand.replace(/\n/g, "")
} }
// replace string for insomnia
curlCommand = curlCommand.replace(/--request /, "-X ")
curlCommand = curlCommand.replace(/--header /, "-H ")
curlCommand = curlCommand.replace(/--url /, " ")
// yargs parses -XPOST as separate arguments. just prescreen for it. // yargs parses -XPOST as separate arguments. just prescreen for it.
curlCommand = curlCommand.replace(/ -XPOST/, " -X POST") curlCommand = curlCommand.replace(/ -XPOST/, " -X POST")
curlCommand = curlCommand.replace(/ -XGET/, " -X GET") curlCommand = curlCommand.replace(/ -XGET/, " -X GET")