ImportCurl.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <template>
  2. <SmartModal v-if="show" :title="`${$t('import.curl')}`" @close="hideModal">
  3. <template #body>
  4. <div class="flex flex-col px-2">
  5. <div ref="curlEditor" class="border border-dividerLight rounded"></div>
  6. </div>
  7. </template>
  8. <template #footer>
  9. <span class="flex">
  10. <ButtonPrimary
  11. :label="`${$t('import.title')}`"
  12. @click.native="handleImport"
  13. />
  14. <ButtonSecondary
  15. :label="`${$t('action.cancel')}`"
  16. @click.native="hideModal"
  17. />
  18. </span>
  19. </template>
  20. </SmartModal>
  21. </template>
  22. <script setup lang="ts">
  23. import { ref, useContext } from "@nuxtjs/composition-api"
  24. import parseCurlCommand from "~/helpers/curlparser"
  25. import { useCodemirror } from "~/helpers/editor/codemirror"
  26. import {
  27. HoppRESTHeader,
  28. HoppRESTParam,
  29. makeRESTRequest,
  30. } from "~/helpers/types/HoppRESTRequest"
  31. import { setRESTRequest } from "~/newstore/RESTSession"
  32. const {
  33. $toast,
  34. app: { i18n },
  35. } = useContext()
  36. const t = i18n.t.bind(i18n)
  37. const curl = ref("")
  38. const curlEditor = ref<any | null>(null)
  39. useCodemirror(curlEditor, curl, {
  40. extendedEditorConfig: {
  41. mode: "application/x-sh",
  42. placeholder: `${t("request.enter_curl")}`,
  43. },
  44. linter: null,
  45. completer: null,
  46. })
  47. defineProps<{ show: boolean }>()
  48. const emit = defineEmits<{
  49. (e: "hide-modal"): void
  50. }>()
  51. const hideModal = () => {
  52. emit("hide-modal")
  53. }
  54. const handleImport = () => {
  55. const text = curl.value
  56. try {
  57. const parsedCurl = parseCurlCommand(text)
  58. const { origin, pathname } = new URL(
  59. parsedCurl.url.replace(/"/g, "").replace(/'/g, "")
  60. )
  61. const endpoint = origin + pathname
  62. const headers: HoppRESTHeader[] = []
  63. const params: HoppRESTParam[] = []
  64. if (parsedCurl.query) {
  65. for (const key of Object.keys(parsedCurl.query)) {
  66. const val = parsedCurl.query[key]!
  67. if (Array.isArray(val)) {
  68. val.forEach((value) => {
  69. params.push({
  70. key,
  71. value,
  72. active: true,
  73. })
  74. })
  75. } else {
  76. params.push({
  77. key,
  78. value: val!,
  79. active: true,
  80. })
  81. }
  82. }
  83. }
  84. if (parsedCurl.headers) {
  85. for (const key of Object.keys(parsedCurl.headers)) {
  86. headers.push({
  87. key,
  88. value: parsedCurl.headers[key],
  89. active: true,
  90. })
  91. }
  92. }
  93. const method = parsedCurl.method.toUpperCase()
  94. setRESTRequest(
  95. makeRESTRequest({
  96. name: "Untitled request",
  97. endpoint,
  98. method,
  99. params,
  100. headers,
  101. preRequestScript: "",
  102. testScript: "",
  103. auth: {
  104. authType: "none",
  105. authActive: true,
  106. },
  107. body: {
  108. contentType: "application/json",
  109. body: "",
  110. },
  111. })
  112. )
  113. } catch (e) {
  114. console.error(e)
  115. $toast.error(`${t("error.curl_invalid_format")}`, {
  116. icon: "error_outline",
  117. })
  118. }
  119. hideModal()
  120. }
  121. </script>