ImportCurl.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 } from "@nuxtjs/composition-api"
  24. import {
  25. HoppRESTHeader,
  26. HoppRESTParam,
  27. makeRESTRequest,
  28. } from "@hoppscotch/data"
  29. import parseCurlCommand from "~/helpers/curlparser"
  30. import { useCodemirror } from "~/helpers/editor/codemirror"
  31. import { setRESTRequest } from "~/newstore/RESTSession"
  32. import { useI18n, useToast } from "~/helpers/utils/composables"
  33. const t = useI18n()
  34. const toast = useToast()
  35. const curl = ref("")
  36. const curlEditor = ref<any | null>(null)
  37. useCodemirror(curlEditor, curl, {
  38. extendedEditorConfig: {
  39. mode: "application/x-sh",
  40. placeholder: `${t("request.enter_curl")}`,
  41. },
  42. linter: null,
  43. completer: null,
  44. })
  45. defineProps<{ show: boolean }>()
  46. const emit = defineEmits<{
  47. (e: "hide-modal"): void
  48. }>()
  49. const hideModal = () => {
  50. emit("hide-modal")
  51. }
  52. const handleImport = () => {
  53. const text = curl.value
  54. try {
  55. const parsedCurl = parseCurlCommand(text)
  56. const { origin, pathname } = new URL(
  57. parsedCurl.url.replace(/"/g, "").replace(/'/g, "")
  58. )
  59. const endpoint = origin + pathname
  60. const headers: HoppRESTHeader[] = []
  61. const params: HoppRESTParam[] = []
  62. if (parsedCurl.query) {
  63. for (const key of Object.keys(parsedCurl.query)) {
  64. const val = parsedCurl.query[key]!
  65. if (Array.isArray(val)) {
  66. val.forEach((value) => {
  67. params.push({
  68. key,
  69. value,
  70. active: true,
  71. })
  72. })
  73. } else {
  74. params.push({
  75. key,
  76. value: val!,
  77. active: true,
  78. })
  79. }
  80. }
  81. }
  82. if (parsedCurl.headers) {
  83. for (const key of Object.keys(parsedCurl.headers)) {
  84. headers.push({
  85. key,
  86. value: parsedCurl.headers[key],
  87. active: true,
  88. })
  89. }
  90. }
  91. const method = parsedCurl.method.toUpperCase()
  92. setRESTRequest(
  93. makeRESTRequest({
  94. name: "Untitled request",
  95. endpoint,
  96. method,
  97. params,
  98. headers,
  99. preRequestScript: "",
  100. testScript: "",
  101. auth: {
  102. authType: "none",
  103. authActive: true,
  104. },
  105. body: {
  106. contentType: "application/json",
  107. body: "",
  108. },
  109. })
  110. )
  111. } catch (e) {
  112. console.error(e)
  113. toast.error(`${t("error.curl_invalid_format")}`)
  114. }
  115. hideModal()
  116. }
  117. </script>