ImportCurl.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. <textarea-autosize
  6. id="import-curl"
  7. v-model="curl"
  8. class="font-mono textarea floating-input"
  9. autofocus
  10. rows="8"
  11. placeholder=" "
  12. />
  13. <label for="import-curl">
  14. {{ $t("request.enter_curl") }}
  15. </label>
  16. </div>
  17. </template>
  18. <template #footer>
  19. <span>
  20. <ButtonPrimary
  21. :label="$t('import.title')"
  22. @click.native="handleImport"
  23. />
  24. <ButtonSecondary
  25. :label="$t('action.cancel')"
  26. @click.native="hideModal"
  27. />
  28. </span>
  29. </template>
  30. </SmartModal>
  31. </template>
  32. <script lang="ts">
  33. import { defineComponent } from "@nuxtjs/composition-api"
  34. import parseCurlCommand from "~/helpers/curlparser"
  35. import {
  36. HoppRESTHeader,
  37. HoppRESTParam,
  38. makeRESTRequest,
  39. } from "~/helpers/types/HoppRESTRequest"
  40. import { setRESTRequest } from "~/newstore/RESTSession"
  41. export default defineComponent({
  42. props: {
  43. show: Boolean,
  44. },
  45. emits: ["hide-modal"],
  46. data() {
  47. return {
  48. curl: "",
  49. }
  50. },
  51. methods: {
  52. hideModal() {
  53. this.$emit("hide-modal")
  54. },
  55. handleImport() {
  56. const text = this.curl
  57. try {
  58. const parsedCurl = parseCurlCommand(text)
  59. const { origin, pathname } = new URL(
  60. parsedCurl.url.replace(/"/g, "").replace(/'/g, "")
  61. )
  62. const endpoint = origin + pathname
  63. const headers: HoppRESTHeader[] = []
  64. const params: HoppRESTParam[] = []
  65. if (parsedCurl.query) {
  66. for (const key of Object.keys(parsedCurl.query)) {
  67. const val = parsedCurl.query[key]!
  68. if (Array.isArray(val)) {
  69. val.forEach((value) => {
  70. params.push({
  71. key,
  72. value,
  73. active: true,
  74. })
  75. })
  76. } else {
  77. params.push({
  78. key,
  79. value: val!,
  80. active: true,
  81. })
  82. }
  83. }
  84. }
  85. if (parsedCurl.headers) {
  86. for (const key of Object.keys(parsedCurl.headers)) {
  87. headers.push({
  88. key,
  89. value: parsedCurl.headers[key],
  90. active: true,
  91. })
  92. }
  93. }
  94. const method = parsedCurl.method.toUpperCase()
  95. // let rawInput = false
  96. // let rawParams: any | null = null
  97. // if (parsedCurl.data) {
  98. // rawInput = true
  99. // rawParams = parsedCurl.data
  100. // }
  101. this.showCurlImportModal = false
  102. setRESTRequest(
  103. makeRESTRequest({
  104. name: "Untitled request",
  105. endpoint,
  106. method,
  107. params,
  108. headers,
  109. preRequestScript: "",
  110. testScript: "",
  111. auth: {
  112. authType: "none",
  113. authActive: true,
  114. },
  115. body: {
  116. contentType: "application/json",
  117. body: "",
  118. },
  119. })
  120. )
  121. } catch (e) {
  122. console.error(e)
  123. this.$toast.error(this.$t("error.curl_invalid_format").toString(), {
  124. icon: "error_outline",
  125. })
  126. }
  127. this.hideModal()
  128. },
  129. },
  130. })
  131. </script>