JSONLensRenderer.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. <template>
  2. <div
  3. v-if="response.type === 'success' || response.type === 'fail'"
  4. class="flex flex-col flex-1"
  5. >
  6. <div
  7. class="sticky z-10 flex items-center justify-between pl-4 border-b bg-primary border-dividerLight top-lowerSecondaryStickyFold"
  8. >
  9. <label class="font-semibold text-secondaryLight">
  10. {{ t("response.body") }}
  11. </label>
  12. <div class="flex items-center">
  13. <ButtonSecondary
  14. v-if="response.body"
  15. v-tippy="{ theme: 'tooltip' }"
  16. :title="t('state.linewrap')"
  17. :class="{ '!text-accent': linewrapEnabled }"
  18. svg="wrap-text"
  19. @click.native.prevent="linewrapEnabled = !linewrapEnabled"
  20. />
  21. <ButtonSecondary
  22. v-if="response.body"
  23. v-tippy="{ theme: 'tooltip' }"
  24. :title="t('action.filter_response')"
  25. svg="filter"
  26. :class="{ '!text-accent': toggleFilter }"
  27. @click.native.prevent="toggleFilterState"
  28. />
  29. <ButtonSecondary
  30. v-if="response.body"
  31. ref="downloadResponse"
  32. v-tippy="{ theme: 'tooltip' }"
  33. :title="t('action.download_file')"
  34. :svg="downloadIcon"
  35. @click.native="downloadResponse"
  36. />
  37. <ButtonSecondary
  38. v-if="response.body"
  39. ref="copyResponse"
  40. v-tippy="{ theme: 'tooltip' }"
  41. :title="t('action.copy')"
  42. :svg="copyIcon"
  43. @click.native="copyResponse"
  44. />
  45. </div>
  46. </div>
  47. <div
  48. v-if="toggleFilter"
  49. class="bg-primary flex sticky top-lowerTertiaryStickyFold z-10 border-b border-dividerLight"
  50. >
  51. <div
  52. class="bg-primaryLight border-divider text-secondaryDark inline-flex flex-1 items-center"
  53. >
  54. <span class="inline-flex flex-1 items-center px-4">
  55. <SmartIcon name="search" class="h-4 w-4 text-secondaryLight" />
  56. <input
  57. v-model="filterQueryText"
  58. v-focus
  59. class="input !border-0 !px-2"
  60. :placeholder="`${t('response.filter_response_body')}`"
  61. type="text"
  62. />
  63. </span>
  64. <div
  65. v-if="filterResponseError"
  66. class="px-2 py-1 text-tiny flex items-center justify-center text-accentContrast rounded"
  67. :class="{
  68. 'bg-red-500':
  69. filterResponseError.type === 'JSON_PARSE_FAILED' ||
  70. filterResponseError.type === 'JSON_PATH_QUERY_ERROR',
  71. 'bg-amber-500': filterResponseError.type === 'RESPONSE_EMPTY',
  72. }"
  73. >
  74. <SmartIcon name="info" class="svg-icons mr-1.5" />
  75. <span>{{ filterResponseError.error }}</span>
  76. </div>
  77. <ButtonSecondary
  78. v-if="response.body"
  79. v-tippy="{ theme: 'tooltip' }"
  80. :title="t('app.wiki')"
  81. svg="help-circle"
  82. to="https://github.com/JSONPath-Plus/JSONPath"
  83. blank
  84. />
  85. </div>
  86. </div>
  87. <div ref="jsonResponse" class="flex flex-col flex-1 h-auto h-full"></div>
  88. <div
  89. v-if="outlinePath"
  90. class="sticky bottom-0 z-10 flex px-2 overflow-auto border-t bg-primaryLight border-dividerLight flex-nowrap hide-scrollbar"
  91. >
  92. <div
  93. v-for="(item, index) in outlinePath"
  94. :key="`item-${index}`"
  95. class="flex items-center"
  96. >
  97. <tippy
  98. ref="outlineOptions"
  99. interactive
  100. trigger="click"
  101. theme="popover"
  102. arrow
  103. >
  104. <template #trigger>
  105. <div v-if="item.kind === 'RootObject'" class="outline-item">{}</div>
  106. <div v-if="item.kind === 'RootArray'" class="outline-item">[]</div>
  107. <div v-if="item.kind === 'ArrayMember'" class="outline-item">
  108. {{ item.index }}
  109. </div>
  110. <div v-if="item.kind === 'ObjectMember'" class="outline-item">
  111. {{ item.name }}
  112. </div>
  113. </template>
  114. <div
  115. v-if="item.kind === 'ArrayMember' || item.kind === 'ObjectMember'"
  116. >
  117. <div
  118. v-if="item.kind === 'ArrayMember'"
  119. class="flex flex-col"
  120. role="menu"
  121. >
  122. <SmartItem
  123. v-for="(arrayMember, astIndex) in item.astParent.values"
  124. :key="`ast-${astIndex}`"
  125. :label="`${astIndex}`"
  126. @click.native="
  127. () => {
  128. jumpCursor(arrayMember)
  129. outlineOptions[index].tippy().hide()
  130. }
  131. "
  132. />
  133. </div>
  134. <div
  135. v-if="item.kind === 'ObjectMember'"
  136. class="flex flex-col"
  137. role="menu"
  138. >
  139. <SmartItem
  140. v-for="(objectMember, astIndex) in item.astParent.members"
  141. :key="`ast-${astIndex}`"
  142. :label="objectMember.key.value"
  143. @click.native="
  144. () => {
  145. jumpCursor(objectMember)
  146. outlineOptions[index].tippy().hide()
  147. }
  148. "
  149. />
  150. </div>
  151. </div>
  152. <div
  153. v-if="item.kind === 'RootObject'"
  154. class="flex flex-col"
  155. role="menu"
  156. >
  157. <SmartItem
  158. label="{}"
  159. @click.native="
  160. () => {
  161. jumpCursor(item.astValue)
  162. outlineOptions[index].tippy().hide()
  163. }
  164. "
  165. />
  166. </div>
  167. <div
  168. v-if="item.kind === 'RootArray'"
  169. class="flex flex-col"
  170. role="menu"
  171. >
  172. <SmartItem
  173. label="[]"
  174. @click.native="
  175. () => {
  176. jumpCursor(item.astValue)
  177. outlineOptions[index].tippy().hide()
  178. }
  179. "
  180. />
  181. </div>
  182. </tippy>
  183. <i
  184. v-if="index + 1 !== outlinePath.length"
  185. class="opacity-50 text-secondaryLight material-icons"
  186. >chevron_right</i
  187. >
  188. </div>
  189. </div>
  190. </div>
  191. </template>
  192. <script setup lang="ts">
  193. import * as LJSON from "lossless-json"
  194. import * as O from "fp-ts/Option"
  195. import * as E from "fp-ts/Either"
  196. import { pipe } from "fp-ts/function"
  197. import { computed, ref, reactive } from "@nuxtjs/composition-api"
  198. import { JSONPath } from "jsonpath-plus"
  199. import { useCodemirror } from "~/helpers/editor/codemirror"
  200. import { HoppRESTResponse } from "~/helpers/types/HoppRESTResponse"
  201. import jsonParse, { JSONObjectMember, JSONValue } from "~/helpers/jsonParse"
  202. import { getJSONOutlineAtPos } from "~/helpers/newOutline"
  203. import {
  204. convertIndexToLineCh,
  205. convertLineChToIndex,
  206. } from "~/helpers/editor/utils"
  207. import { useI18n } from "~/helpers/utils/composables"
  208. import useCopyResponse from "~/helpers/lenses/composables/useCopyResponse"
  209. import useResponseBody from "~/helpers/lenses/composables/useResponseBody"
  210. import useDownloadResponse from "~/helpers/lenses/composables/useDownloadResponse"
  211. const t = useI18n()
  212. const props = defineProps<{
  213. response: HoppRESTResponse
  214. }>()
  215. const { responseBodyText } = useResponseBody(props.response)
  216. const toggleFilter = ref(false)
  217. const filterQueryText = ref("")
  218. type BodyParseError =
  219. | { type: "JSON_PARSE_FAILED" }
  220. | { type: "JSON_PATH_QUERY_FAILED"; error: Error }
  221. const responseJsonObject = computed(() =>
  222. pipe(
  223. responseBodyText.value,
  224. E.tryCatchK(
  225. LJSON.parse,
  226. (): BodyParseError => ({ type: "JSON_PARSE_FAILED" })
  227. )
  228. )
  229. )
  230. const jsonResponseBodyText = computed(() => {
  231. if (filterQueryText.value.length > 0) {
  232. return pipe(
  233. responseJsonObject.value,
  234. E.chain((parsedJSON) =>
  235. E.tryCatch(
  236. () =>
  237. JSONPath({
  238. path: filterQueryText.value,
  239. json: parsedJSON,
  240. }) as undefined,
  241. (err): BodyParseError => ({
  242. type: "JSON_PATH_QUERY_FAILED",
  243. error: err as Error,
  244. })
  245. )
  246. ),
  247. E.map(JSON.stringify)
  248. )
  249. } else {
  250. return E.right(responseBodyText.value)
  251. }
  252. })
  253. const jsonBodyText = computed(() =>
  254. pipe(
  255. jsonResponseBodyText.value,
  256. E.getOrElse(() => responseBodyText.value),
  257. O.tryCatchK(LJSON.parse),
  258. O.map((val) => LJSON.stringify(val, undefined, 2)),
  259. O.getOrElse(() => responseBodyText.value)
  260. )
  261. )
  262. const ast = computed(() =>
  263. pipe(
  264. jsonBodyText.value,
  265. O.tryCatchK(jsonParse),
  266. O.getOrElseW(() => null)
  267. )
  268. )
  269. const filterResponseError = computed(() =>
  270. pipe(
  271. jsonResponseBodyText.value,
  272. E.match(
  273. (e) => {
  274. switch (e.type) {
  275. case "JSON_PATH_QUERY_FAILED":
  276. return { type: "JSON_PATH_QUERY_ERROR", error: e.error.message }
  277. case "JSON_PARSE_FAILED":
  278. return {
  279. type: "JSON_PARSE_FAILED",
  280. error: t("error.json_parsing_failed").toString(),
  281. }
  282. }
  283. },
  284. (result) =>
  285. result === "[]"
  286. ? {
  287. type: "RESPONSE_EMPTY",
  288. error: t("error.no_results_found").toString(),
  289. }
  290. : undefined
  291. )
  292. )
  293. )
  294. const { copyIcon, copyResponse } = useCopyResponse(jsonBodyText)
  295. const { downloadIcon, downloadResponse } = useDownloadResponse(
  296. "application/json",
  297. jsonBodyText
  298. )
  299. const outlineOptions = ref<any | null>(null)
  300. const jsonResponse = ref<any | null>(null)
  301. const linewrapEnabled = ref(true)
  302. const { cursor } = useCodemirror(
  303. jsonResponse,
  304. jsonBodyText,
  305. reactive({
  306. extendedEditorConfig: {
  307. mode: "application/ld+json",
  308. readOnly: true,
  309. lineWrapping: linewrapEnabled,
  310. },
  311. linter: null,
  312. completer: null,
  313. environmentHighlights: true,
  314. })
  315. )
  316. const jumpCursor = (ast: JSONValue | JSONObjectMember) => {
  317. const pos = convertIndexToLineCh(jsonBodyText.value, ast.start)
  318. pos.line--
  319. cursor.value = pos
  320. }
  321. const outlinePath = computed(() =>
  322. pipe(
  323. ast.value,
  324. O.fromNullable,
  325. O.map((ast) =>
  326. getJSONOutlineAtPos(
  327. ast,
  328. convertLineChToIndex(jsonBodyText.value, cursor.value)
  329. )
  330. ),
  331. O.getOrElseW(() => null)
  332. )
  333. )
  334. const toggleFilterState = () => {
  335. filterQueryText.value = ""
  336. toggleFilter.value = !toggleFilter.value
  337. }
  338. </script>
  339. <style lang="scss" scoped>
  340. .outline-item {
  341. @apply cursor-pointer;
  342. @apply flex-grow-0 flex-shrink-0;
  343. @apply text-secondaryLight;
  344. @apply inline-flex;
  345. @apply items-center;
  346. @apply px-2;
  347. @apply py-1;
  348. @apply transition;
  349. @apply hover:text-secondary;
  350. }
  351. </style>