123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- <template>
- <SmartModal v-if="show" :title="$t('import.curl')" @close="hideModal">
- <template #body>
- <div class="flex flex-col px-2">
- <textarea-autosize
- id="import-curl"
- v-model="curl"
- class="font-mono textarea floating-input"
- autofocus
- rows="8"
- placeholder=" "
- />
- <label for="import-curl">
- {{ $t("request.enter_curl") }}
- </label>
- </div>
- </template>
- <template #footer>
- <span>
- <ButtonPrimary
- :label="$t('import.title')"
- @click.native="handleImport"
- />
- <ButtonSecondary
- :label="$t('action.cancel')"
- @click.native="hideModal"
- />
- </span>
- </template>
- </SmartModal>
- </template>
- <script lang="ts">
- import { defineComponent } from "@nuxtjs/composition-api"
- import parseCurlCommand from "~/helpers/curlparser"
- import {
- HoppRESTHeader,
- HoppRESTParam,
- makeRESTRequest,
- } from "~/helpers/types/HoppRESTRequest"
- import { setRESTRequest } from "~/newstore/RESTSession"
- export default defineComponent({
- props: {
- show: Boolean,
- },
- emits: ["hide-modal"],
- data() {
- return {
- curl: "",
- }
- },
- methods: {
- hideModal() {
- this.$emit("hide-modal")
- },
- handleImport() {
- const text = this.curl
- 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()
- // let rawInput = false
- // let rawParams: any | null = null
- // if (parsedCurl.data) {
- // rawInput = true
- // rawParams = parsedCurl.data
- // }
- this.showCurlImportModal = false
- 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)
- this.$toast.error(this.$t("error.curl_invalid_format").toString(), {
- icon: "error_outline",
- })
- }
- this.hideModal()
- },
- },
- })
- </script>
|