RequestOptions.vue 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. <template>
  2. <div class="flex flex-col flex-1 h-full">
  3. <SmartTabs
  4. v-model="selectedOptionTab"
  5. styles="sticky bg-primary top-upperPrimaryStickyFold z-10"
  6. render-inactive-tabs
  7. >
  8. <SmartTab
  9. :id="'query'"
  10. :label="`${t('tab.query')}`"
  11. :indicator="gqlQueryString && gqlQueryString.length > 0 ? true : false"
  12. >
  13. <div
  14. class="sticky z-10 flex items-center justify-between pl-4 border-b bg-primary border-dividerLight top-upperSecondaryStickyFold gqlRunQuery"
  15. >
  16. <label class="font-semibold text-secondaryLight">
  17. {{ t("request.query") }}
  18. </label>
  19. <div class="flex">
  20. <ButtonSecondary
  21. v-tippy="{ theme: 'tooltip', delay: [500, 20], allowHTML: true }"
  22. :title="`${t(
  23. 'request.run'
  24. )} <xmp>${getSpecialKey()}</xmp><xmp>G</xmp>`"
  25. :label="`${t('request.run')}`"
  26. svg="play"
  27. class="rounded-none !text-accent !hover:text-accentDark"
  28. @click.native="runQuery()"
  29. />
  30. <ButtonSecondary
  31. ref="saveRequest"
  32. v-tippy="{ theme: 'tooltip', delay: [500, 20], allowHTML: true }"
  33. :title="`${t(
  34. 'request.save'
  35. )} <xmp>${getSpecialKey()}</xmp><xmp>S</xmp>`"
  36. :label="`${t('request.save')}`"
  37. svg="save"
  38. class="rounded-none"
  39. @click.native="saveRequest"
  40. />
  41. <ButtonSecondary
  42. v-tippy="{ theme: 'tooltip' }"
  43. to="https://docs.hoppscotch.io/graphql"
  44. blank
  45. :title="t('app.wiki')"
  46. svg="help-circle"
  47. />
  48. <ButtonSecondary
  49. v-tippy="{ theme: 'tooltip' }"
  50. :title="t('action.clear_all')"
  51. svg="trash-2"
  52. @click.native="clearGQLQuery()"
  53. />
  54. <ButtonSecondary
  55. v-tippy="{ theme: 'tooltip' }"
  56. :title="t('action.prettify')"
  57. :svg="`${prettifyQueryIcon}`"
  58. @click.native="prettifyQuery"
  59. />
  60. <ButtonSecondary
  61. v-tippy="{ theme: 'tooltip' }"
  62. :title="t('action.copy')"
  63. :svg="`${copyQueryIcon}`"
  64. @click.native="copyQuery"
  65. />
  66. </div>
  67. </div>
  68. <div ref="queryEditor" class="flex flex-col flex-1"></div>
  69. </SmartTab>
  70. <SmartTab
  71. :id="'variables'"
  72. :label="`${t('tab.variables')}`"
  73. :indicator="variableString && variableString.length > 0 ? true : false"
  74. >
  75. <div
  76. class="sticky z-10 flex items-center justify-between pl-4 border-b bg-primary border-dividerLight top-upperSecondaryStickyFold"
  77. >
  78. <label class="font-semibold text-secondaryLight">
  79. {{ t("request.variables") }}
  80. </label>
  81. <div class="flex">
  82. <ButtonSecondary
  83. v-tippy="{ theme: 'tooltip' }"
  84. to="https://docs.hoppscotch.io/graphql"
  85. blank
  86. :title="t('app.wiki')"
  87. svg="help-circle"
  88. />
  89. <ButtonSecondary
  90. v-tippy="{ theme: 'tooltip' }"
  91. :title="t('action.clear_all')"
  92. svg="trash-2"
  93. @click.native="clearGQLVariables()"
  94. />
  95. <ButtonSecondary
  96. ref="prettifyRequest"
  97. v-tippy="{ theme: 'tooltip' }"
  98. :title="t('action.prettify')"
  99. :svg="prettifyVariablesIcon"
  100. @click.native="prettifyVariableString"
  101. />
  102. <ButtonSecondary
  103. v-tippy="{ theme: 'tooltip' }"
  104. :title="t('action.copy')"
  105. :svg="`${copyVariablesIcon}`"
  106. @click.native="copyVariables"
  107. />
  108. </div>
  109. </div>
  110. <div ref="variableEditor" class="flex flex-col flex-1"></div>
  111. </SmartTab>
  112. <SmartTab
  113. :id="'headers'"
  114. :label="`${t('tab.headers')}`"
  115. :info="activeGQLHeadersCount === 0 ? null : `${activeGQLHeadersCount}`"
  116. >
  117. <div
  118. class="sticky z-10 flex items-center justify-between pl-4 border-b bg-primary border-dividerLight top-upperSecondaryStickyFold"
  119. >
  120. <label class="font-semibold text-secondaryLight">
  121. {{ t("tab.headers") }}
  122. </label>
  123. <div class="flex">
  124. <ButtonSecondary
  125. v-tippy="{ theme: 'tooltip' }"
  126. to="https://docs.hoppscotch.io/graphql"
  127. blank
  128. :title="t('app.wiki')"
  129. svg="help-circle"
  130. />
  131. <ButtonSecondary
  132. v-tippy="{ theme: 'tooltip' }"
  133. :title="t('action.clear_all')"
  134. svg="trash-2"
  135. @click.native="clearContent()"
  136. />
  137. <ButtonSecondary
  138. v-tippy="{ theme: 'tooltip' }"
  139. :title="t('state.bulk_mode')"
  140. svg="edit"
  141. :class="{ '!text-accent': bulkMode }"
  142. @click.native="bulkMode = !bulkMode"
  143. />
  144. <ButtonSecondary
  145. v-tippy="{ theme: 'tooltip' }"
  146. :title="t('add.new')"
  147. svg="plus"
  148. :disabled="bulkMode"
  149. @click.native="addHeader"
  150. />
  151. </div>
  152. </div>
  153. <div
  154. v-if="bulkMode"
  155. ref="bulkEditor"
  156. class="flex flex-col flex-1"
  157. ></div>
  158. <div v-else>
  159. <draggable
  160. v-model="workingHeaders"
  161. animation="250"
  162. handle=".draggable-handle"
  163. draggable=".draggable-content"
  164. ghost-class="cursor-move"
  165. chosen-class="bg-primaryLight"
  166. drag-class="cursor-grabbing"
  167. >
  168. <div
  169. v-for="(header, index) in workingHeaders"
  170. :key="`header-${header.id}-${index}`"
  171. class="flex border-b divide-x divide-dividerLight border-dividerLight draggable-content group"
  172. >
  173. <span>
  174. <ButtonSecondary
  175. svg="grip-vertical"
  176. class="cursor-auto text-primary hover:text-primary"
  177. :class="{
  178. 'draggable-handle group-hover:text-secondaryLight !cursor-grab':
  179. index !== workingHeaders?.length - 1,
  180. }"
  181. tabindex="-1"
  182. />
  183. </span>
  184. <SmartAutoComplete
  185. :placeholder="`${t('count.header', { count: index + 1 })}`"
  186. :source="commonHeaders"
  187. :spellcheck="false"
  188. :value="header.key"
  189. autofocus
  190. styles="
  191. bg-transparent
  192. flex
  193. flex-1
  194. py-1
  195. px-4
  196. truncate
  197. "
  198. class="flex-1 !flex"
  199. @input="
  200. updateHeader(index, {
  201. id: header.id,
  202. key: $event,
  203. value: header.value,
  204. active: header.active,
  205. })
  206. "
  207. />
  208. <input
  209. class="flex flex-1 px-4 py-2 bg-transparent"
  210. :placeholder="`${t('count.value', { count: index + 1 })}`"
  211. :name="`value ${String(index)}`"
  212. :value="header.value"
  213. autofocus
  214. @change="
  215. updateHeader(index, {
  216. id: header.id,
  217. key: header.key,
  218. value: $event.target.value,
  219. active: header.active,
  220. })
  221. "
  222. />
  223. <span>
  224. <ButtonSecondary
  225. v-tippy="{ theme: 'tooltip' }"
  226. :title="
  227. header.hasOwnProperty('active')
  228. ? header.active
  229. ? t('action.turn_off')
  230. : t('action.turn_on')
  231. : t('action.turn_off')
  232. "
  233. :svg="
  234. header.hasOwnProperty('active')
  235. ? header.active
  236. ? 'check-circle'
  237. : 'circle'
  238. : 'check-circle'
  239. "
  240. color="green"
  241. @click.native="
  242. updateHeader(index, {
  243. id: header.id,
  244. key: header.key,
  245. value: header.value,
  246. active: !header.active,
  247. })
  248. "
  249. />
  250. </span>
  251. <span>
  252. <ButtonSecondary
  253. v-tippy="{ theme: 'tooltip' }"
  254. :title="t('action.remove')"
  255. svg="trash"
  256. color="red"
  257. @click.native="deleteHeader(index)"
  258. />
  259. </span>
  260. </div>
  261. </draggable>
  262. <div
  263. v-if="workingHeaders.length === 0"
  264. class="flex flex-col items-center justify-center p-4 text-secondaryLight"
  265. >
  266. <img
  267. :src="`/images/states/${$colorMode.value}/add_category.svg`"
  268. loading="lazy"
  269. class="inline-flex flex-col object-contain object-center w-16 h-16 my-4"
  270. :alt="`${t('empty.headers')}`"
  271. />
  272. <span class="pb-4 text-center">
  273. {{ t("empty.headers") }}
  274. </span>
  275. <ButtonSecondary
  276. :label="`${t('add.new')}`"
  277. filled
  278. svg="plus"
  279. class="mb-4"
  280. @click.native="addHeader"
  281. />
  282. </div>
  283. </div>
  284. </SmartTab>
  285. <SmartTab :id="'authorization'" :label="`${t('tab.authorization')}`">
  286. <GraphqlAuthorization />
  287. </SmartTab>
  288. </SmartTabs>
  289. <CollectionsSaveRequest
  290. mode="graphql"
  291. :show="showSaveRequestModal"
  292. @hide-modal="hideRequestModal"
  293. />
  294. </div>
  295. </template>
  296. <script setup lang="ts">
  297. import { Ref, computed, reactive, ref, watch } from "@nuxtjs/composition-api"
  298. import clone from "lodash/clone"
  299. import * as gql from "graphql"
  300. import * as E from "fp-ts/Either"
  301. import * as O from "fp-ts/Option"
  302. import * as A from "fp-ts/Array"
  303. import * as RA from "fp-ts/ReadonlyArray"
  304. import { pipe, flow } from "fp-ts/function"
  305. import {
  306. GQLHeader,
  307. makeGQLRequest,
  308. rawKeyValueEntriesToString,
  309. parseRawKeyValueEntriesE,
  310. RawKeyValueEntry,
  311. } from "@hoppscotch/data"
  312. import draggable from "vuedraggable"
  313. import isEqual from "lodash/isEqual"
  314. import cloneDeep from "lodash/cloneDeep"
  315. import { refAutoReset } from "@vueuse/core"
  316. import { copyToClipboard } from "~/helpers/utils/clipboard"
  317. import {
  318. useNuxt,
  319. useReadonlyStream,
  320. useStream,
  321. useI18n,
  322. useToast,
  323. } from "~/helpers/utils/composables"
  324. import {
  325. gqlAuth$,
  326. gqlHeaders$,
  327. gqlQuery$,
  328. gqlResponse$,
  329. gqlURL$,
  330. gqlVariables$,
  331. setGQLAuth,
  332. setGQLHeaders,
  333. setGQLQuery,
  334. setGQLResponse,
  335. setGQLVariables,
  336. } from "~/newstore/GQLSession"
  337. import { commonHeaders } from "~/helpers/headers"
  338. import { GQLConnection } from "~/helpers/GQLConnection"
  339. import { makeGQLHistoryEntry, addGraphqlHistoryEntry } from "~/newstore/history"
  340. import { logHoppRequestRunToAnalytics } from "~/helpers/fb/analytics"
  341. import { getCurrentStrategyID } from "~/helpers/network"
  342. import { useCodemirror } from "~/helpers/editor/codemirror"
  343. import jsonLinter from "~/helpers/editor/linting/json"
  344. import { createGQLQueryLinter } from "~/helpers/editor/linting/gqlQuery"
  345. import queryCompleter from "~/helpers/editor/completion/gqlQuery"
  346. import { defineActionHandler } from "~/helpers/actions"
  347. import { getPlatformSpecialKey as getSpecialKey } from "~/helpers/platformutils"
  348. import { objRemoveKey } from "~/helpers/functional/object"
  349. type OptionTabs = "query" | "headers" | "variables" | "authorization"
  350. const selectedOptionTab = ref<OptionTabs>("query")
  351. const t = useI18n()
  352. const props = defineProps<{
  353. conn: GQLConnection
  354. }>()
  355. const toast = useToast()
  356. const nuxt = useNuxt()
  357. const url = useReadonlyStream(gqlURL$, "")
  358. const gqlQueryString = useStream(gqlQuery$, "", setGQLQuery)
  359. const variableString = useStream(gqlVariables$, "", setGQLVariables)
  360. const idTicker = ref(0)
  361. const bulkMode = ref(false)
  362. const bulkHeaders = ref("")
  363. const bulkEditor = ref<any | null>(null)
  364. const deletionToast = ref<{ goAway: (delay: number) => void } | null>(null)
  365. useCodemirror(bulkEditor, bulkHeaders, {
  366. extendedEditorConfig: {
  367. mode: "text/x-yaml",
  368. placeholder: `${t("state.bulk_mode_placeholder")}`,
  369. },
  370. linter: null,
  371. completer: null,
  372. environmentHighlights: false,
  373. })
  374. // The functional headers list (the headers actually in the system)
  375. const headers = useStream(gqlHeaders$, [], setGQLHeaders) as Ref<GQLHeader[]>
  376. const auth = useStream(
  377. gqlAuth$,
  378. { authType: "none", authActive: true },
  379. setGQLAuth
  380. )
  381. // The UI representation of the headers list (has the empty end header)
  382. const workingHeaders = ref<Array<GQLHeader & { id: number }>>([
  383. {
  384. id: idTicker.value++,
  385. key: "",
  386. value: "",
  387. active: true,
  388. },
  389. ])
  390. // Rule: Working Headers always have one empty header or the last element is always an empty header
  391. watch(workingHeaders, (headersList) => {
  392. if (
  393. headersList.length > 0 &&
  394. headersList[headersList.length - 1].key !== ""
  395. ) {
  396. workingHeaders.value.push({
  397. id: idTicker.value++,
  398. key: "",
  399. value: "",
  400. active: true,
  401. })
  402. }
  403. })
  404. // Sync logic between headers and working headers
  405. watch(
  406. headers,
  407. (newHeadersList) => {
  408. // Sync should overwrite working headers
  409. const filteredWorkingHeaders = pipe(
  410. workingHeaders.value,
  411. A.filterMap(
  412. flow(
  413. O.fromPredicate((e) => e.key !== ""),
  414. O.map(objRemoveKey("id"))
  415. )
  416. )
  417. )
  418. const filteredBulkHeaders = pipe(
  419. parseRawKeyValueEntriesE(bulkHeaders.value),
  420. E.map(
  421. flow(
  422. RA.filter((e) => e.key !== ""),
  423. RA.toArray
  424. )
  425. ),
  426. E.getOrElse(() => [] as RawKeyValueEntry[])
  427. )
  428. if (!isEqual(newHeadersList, filteredWorkingHeaders)) {
  429. workingHeaders.value = pipe(
  430. newHeadersList,
  431. A.map((x) => ({ id: idTicker.value++, ...x }))
  432. )
  433. }
  434. if (!isEqual(newHeadersList, filteredBulkHeaders)) {
  435. bulkHeaders.value = rawKeyValueEntriesToString(newHeadersList)
  436. }
  437. },
  438. { immediate: true }
  439. )
  440. watch(workingHeaders, (newWorkingHeaders) => {
  441. const fixedHeaders = pipe(
  442. newWorkingHeaders,
  443. A.filterMap(
  444. flow(
  445. O.fromPredicate((e) => e.key !== ""),
  446. O.map(objRemoveKey("id"))
  447. )
  448. )
  449. )
  450. if (!isEqual(headers.value, fixedHeaders)) {
  451. headers.value = cloneDeep(fixedHeaders)
  452. }
  453. })
  454. // Bulk Editor Syncing with Working Headers
  455. watch(bulkHeaders, (newBulkHeaders) => {
  456. const filteredBulkHeaders = pipe(
  457. parseRawKeyValueEntriesE(newBulkHeaders),
  458. E.map(
  459. flow(
  460. RA.filter((e) => e.key !== ""),
  461. RA.toArray
  462. )
  463. ),
  464. E.getOrElse(() => [] as RawKeyValueEntry[])
  465. )
  466. if (!isEqual(headers.value, filteredBulkHeaders)) {
  467. headers.value = filteredBulkHeaders
  468. }
  469. })
  470. watch(workingHeaders, (newHeadersList) => {
  471. // If we are in bulk mode, don't apply direct changes
  472. if (bulkMode.value) return
  473. try {
  474. const currentBulkHeaders = bulkHeaders.value.split("\n").map((item) => ({
  475. key: item.substring(0, item.indexOf(":")).trimLeft().replace(/^#/, ""),
  476. value: item.substring(item.indexOf(":") + 1).trimLeft(),
  477. active: !item.trim().startsWith("#"),
  478. }))
  479. const filteredHeaders = newHeadersList.filter((x) => x.key !== "")
  480. if (!isEqual(currentBulkHeaders, filteredHeaders)) {
  481. bulkHeaders.value = rawKeyValueEntriesToString(filteredHeaders)
  482. }
  483. } catch (e) {
  484. toast.error(`${t("error.something_went_wrong")}`)
  485. console.error(e)
  486. }
  487. })
  488. const addHeader = () => {
  489. workingHeaders.value.push({
  490. id: idTicker.value++,
  491. key: "",
  492. value: "",
  493. active: true,
  494. })
  495. }
  496. const updateHeader = (index: number, header: GQLHeader & { id: number }) => {
  497. workingHeaders.value = workingHeaders.value.map((h, i) =>
  498. i === index ? header : h
  499. )
  500. }
  501. const deleteHeader = (index: number) => {
  502. const headersBeforeDeletion = clone(workingHeaders.value)
  503. if (
  504. !(
  505. headersBeforeDeletion.length > 0 &&
  506. index === headersBeforeDeletion.length - 1
  507. )
  508. ) {
  509. if (deletionToast.value) {
  510. deletionToast.value.goAway(0)
  511. deletionToast.value = null
  512. }
  513. deletionToast.value = toast.success(`${t("state.deleted")}`, {
  514. action: [
  515. {
  516. text: `${t("action.undo")}`,
  517. onClick: (_, toastObject) => {
  518. workingHeaders.value = headersBeforeDeletion
  519. toastObject.goAway(0)
  520. deletionToast.value = null
  521. },
  522. },
  523. ],
  524. onComplete: () => {
  525. deletionToast.value = null
  526. },
  527. })
  528. }
  529. workingHeaders.value.splice(index, 1)
  530. }
  531. const clearContent = () => {
  532. // set headers list to the initial state
  533. workingHeaders.value = [
  534. {
  535. id: idTicker.value++,
  536. key: "",
  537. value: "",
  538. active: true,
  539. },
  540. ]
  541. bulkHeaders.value = ""
  542. }
  543. const activeGQLHeadersCount = computed(
  544. () =>
  545. headers.value.filter((x) => x.active && (x.key !== "" || x.value !== ""))
  546. .length
  547. )
  548. const variableEditor = ref<any | null>(null)
  549. useCodemirror(
  550. variableEditor,
  551. variableString,
  552. reactive({
  553. extendedEditorConfig: {
  554. mode: "application/ld+json",
  555. placeholder: `${t("request.variables")}`,
  556. },
  557. linter: computed(() =>
  558. variableString.value.length > 0 ? jsonLinter : null
  559. ),
  560. completer: null,
  561. environmentHighlights: false,
  562. })
  563. )
  564. const queryEditor = ref<any | null>(null)
  565. const schemaString = useReadonlyStream(props.conn.schema$, null)
  566. useCodemirror(queryEditor, gqlQueryString, {
  567. extendedEditorConfig: {
  568. mode: "graphql",
  569. placeholder: `${t("request.query")}`,
  570. },
  571. linter: createGQLQueryLinter(schemaString),
  572. completer: queryCompleter(schemaString),
  573. environmentHighlights: false,
  574. })
  575. const copyQueryIcon = refAutoReset<"copy" | "check">("copy", 1000)
  576. const copyVariablesIcon = refAutoReset<"copy" | "check">("copy", 1000)
  577. const prettifyQueryIcon = refAutoReset<"wand" | "check" | "info">("wand", 1000)
  578. const prettifyVariablesIcon = refAutoReset<"wand" | "check" | "info">(
  579. "wand",
  580. 1000
  581. )
  582. const showSaveRequestModal = ref(false)
  583. const copyQuery = () => {
  584. copyToClipboard(gqlQueryString.value)
  585. copyQueryIcon.value = "check"
  586. toast.success(`${t("state.copied_to_clipboard")}`)
  587. }
  588. const response = useStream(gqlResponse$, "", setGQLResponse)
  589. const runQuery = async () => {
  590. const startTime = Date.now()
  591. nuxt.value.$loading.start()
  592. response.value = "loading"
  593. try {
  594. const runURL = clone(url.value)
  595. const runHeaders = clone(headers.value)
  596. const runQuery = clone(gqlQueryString.value)
  597. const runVariables = clone(variableString.value)
  598. const runAuth = clone(auth.value)
  599. const responseText = await props.conn.runQuery(
  600. runURL,
  601. runHeaders,
  602. runQuery,
  603. runVariables,
  604. runAuth
  605. )
  606. const duration = Date.now() - startTime
  607. nuxt.value.$loading.finish()
  608. response.value = JSON.stringify(JSON.parse(responseText), null, 2)
  609. addGraphqlHistoryEntry(
  610. makeGQLHistoryEntry({
  611. request: makeGQLRequest({
  612. name: "",
  613. url: runURL,
  614. query: runQuery,
  615. headers: runHeaders,
  616. variables: runVariables,
  617. auth: runAuth,
  618. }),
  619. response: response.value,
  620. star: false,
  621. })
  622. )
  623. toast.success(`${t("state.finished_in", { duration })}`)
  624. } catch (e: any) {
  625. response.value = `${e}`
  626. nuxt.value.$loading.finish()
  627. toast.error(
  628. `${t("error.something_went_wrong")}. ${t("error.check_console_details")}`,
  629. {}
  630. )
  631. console.error(e)
  632. }
  633. logHoppRequestRunToAnalytics({
  634. platform: "graphql-query",
  635. strategy: getCurrentStrategyID(),
  636. })
  637. }
  638. const hideRequestModal = () => {
  639. showSaveRequestModal.value = false
  640. }
  641. const prettifyQuery = () => {
  642. try {
  643. gqlQueryString.value = gql.print(gql.parse(gqlQueryString.value))
  644. prettifyQueryIcon.value = "check"
  645. } catch (e) {
  646. toast.error(`${t("error.gql_prettify_invalid_query")}`)
  647. prettifyQueryIcon.value = "info"
  648. }
  649. }
  650. const saveRequest = () => {
  651. showSaveRequestModal.value = true
  652. }
  653. const copyVariables = () => {
  654. copyToClipboard(variableString.value)
  655. copyVariablesIcon.value = "check"
  656. toast.success(`${t("state.copied_to_clipboard")}`)
  657. }
  658. const prettifyVariableString = () => {
  659. try {
  660. const jsonObj = JSON.parse(variableString.value)
  661. variableString.value = JSON.stringify(jsonObj, null, 2)
  662. prettifyVariablesIcon.value = "check"
  663. } catch (e) {
  664. console.error(e)
  665. prettifyVariablesIcon.value = "info"
  666. toast.error(`${t("error.json_prettify_invalid_body")}`)
  667. }
  668. }
  669. const clearGQLQuery = () => {
  670. gqlQueryString.value = ""
  671. }
  672. const clearGQLVariables = () => {
  673. variableString.value = ""
  674. }
  675. defineActionHandler("request.send-cancel", runQuery)
  676. defineActionHandler("request.save", saveRequest)
  677. defineActionHandler("request.reset", clearGQLQuery)
  678. </script>