ImportCurl.vue 3.0 KB

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