Headers.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. <template>
  2. <div class="flex flex-col flex-1">
  3. <div
  4. class="sticky z-10 flex items-center justify-between pl-4 border-b bg-primary border-dividerLight top-upperMobileSecondaryStickyFold sm:top-upperSecondaryStickyFold"
  5. >
  6. <label class="font-semibold text-secondaryLight">
  7. {{ t("request.header_list") }}
  8. </label>
  9. <div class="flex">
  10. <ButtonSecondary
  11. v-tippy="{ theme: 'tooltip' }"
  12. to="https://docs.hoppscotch.io/features/headers"
  13. blank
  14. :title="t('app.wiki')"
  15. svg="help-circle"
  16. />
  17. <ButtonSecondary
  18. v-tippy="{ theme: 'tooltip' }"
  19. :title="t('action.clear_all')"
  20. svg="trash-2"
  21. @click.native="clearContent()"
  22. />
  23. <ButtonSecondary
  24. v-tippy="{ theme: 'tooltip' }"
  25. :title="t('state.bulk_mode')"
  26. svg="edit"
  27. :class="{ '!text-accent': bulkMode }"
  28. @click.native="bulkMode = !bulkMode"
  29. />
  30. <ButtonSecondary
  31. v-tippy="{ theme: 'tooltip' }"
  32. :title="t('add.new')"
  33. svg="plus"
  34. :disabled="bulkMode"
  35. @click.native="addHeader"
  36. />
  37. </div>
  38. </div>
  39. <div v-if="bulkMode" ref="bulkEditor" class="flex flex-col flex-1"></div>
  40. <div v-else>
  41. <draggable
  42. v-model="workingHeaders"
  43. animation="250"
  44. handle=".draggable-handle"
  45. draggable=".draggable-content"
  46. ghost-class="cursor-move"
  47. chosen-class="bg-primaryLight"
  48. drag-class="cursor-grabbing"
  49. >
  50. <div
  51. v-for="(header, index) in workingHeaders"
  52. :key="`header-${header.id}-${index}`"
  53. class="flex border-b divide-x divide-dividerLight border-dividerLight draggable-content group"
  54. >
  55. <span>
  56. <ButtonSecondary
  57. svg="grip-vertical"
  58. class="cursor-auto text-primary hover:text-primary"
  59. :class="{
  60. 'draggable-handle group-hover:text-secondaryLight !cursor-grab':
  61. index !== workingHeaders?.length - 1,
  62. }"
  63. tabindex="-1"
  64. />
  65. </span>
  66. <SmartAutoComplete
  67. :placeholder="`${t('count.header', { count: index + 1 })}`"
  68. :source="commonHeaders"
  69. :spellcheck="false"
  70. :value="header.key"
  71. autofocus
  72. styles="
  73. bg-transparent
  74. flex
  75. flex-1
  76. py-1
  77. px-4
  78. truncate
  79. "
  80. class="flex-1 !flex"
  81. @input="
  82. updateHeader(index, {
  83. id: header.id,
  84. key: $event,
  85. value: header.value,
  86. active: header.active,
  87. })
  88. "
  89. />
  90. <SmartEnvInput
  91. v-model="header.value"
  92. :placeholder="`${t('count.value', { count: index + 1 })}`"
  93. @change="
  94. updateHeader(index, {
  95. id: header.id,
  96. key: header.key,
  97. value: $event,
  98. active: header.active,
  99. })
  100. "
  101. />
  102. <span>
  103. <ButtonSecondary
  104. v-tippy="{ theme: 'tooltip' }"
  105. :title="
  106. header.hasOwnProperty('active')
  107. ? header.active
  108. ? t('action.turn_off')
  109. : t('action.turn_on')
  110. : t('action.turn_off')
  111. "
  112. :svg="
  113. header.hasOwnProperty('active')
  114. ? header.active
  115. ? 'check-circle'
  116. : 'circle'
  117. : 'check-circle'
  118. "
  119. color="green"
  120. @click.native="
  121. updateHeader(index, {
  122. id: header.id,
  123. key: header.key,
  124. value: header.value,
  125. active: !header.active,
  126. })
  127. "
  128. />
  129. </span>
  130. <span>
  131. <ButtonSecondary
  132. v-tippy="{ theme: 'tooltip' }"
  133. :title="t('action.remove')"
  134. svg="trash"
  135. color="red"
  136. @click.native="deleteHeader(index)"
  137. />
  138. </span>
  139. </div>
  140. <div
  141. v-for="(header, index) in computedHeaders"
  142. :key="`header-${index}`"
  143. class="flex border-b divide-x divide-dividerLight border-dividerLight draggable-content group"
  144. >
  145. <span>
  146. <ButtonSecondary
  147. svg="lock"
  148. class="opacity-25 cursor-auto text-secondaryLight bg-divider"
  149. tabindex="-1"
  150. />
  151. </span>
  152. <SmartEnvInput
  153. v-model="header.header.key"
  154. :placeholder="`${t('count.value', { count: index + 1 })}`"
  155. readonly
  156. />
  157. <SmartEnvInput
  158. :value="mask(header)"
  159. :placeholder="`${t('count.value', { count: index + 1 })}`"
  160. readonly
  161. />
  162. <span>
  163. <ButtonSecondary
  164. v-if="header.source === 'auth'"
  165. :svg="masking ? 'eye' : 'eye-off'"
  166. @click.native="toggleMask()"
  167. />
  168. <ButtonSecondary
  169. v-else
  170. svg="arrow-up-right"
  171. class="cursor-auto text-primary hover:text-primary"
  172. />
  173. </span>
  174. <span>
  175. <ButtonSecondary
  176. svg="arrow-up-right"
  177. @click.native="changeTab(header.source)"
  178. />
  179. </span>
  180. </div>
  181. </draggable>
  182. <div
  183. v-if="workingHeaders.length === 0"
  184. class="flex flex-col items-center justify-center p-4 text-secondaryLight"
  185. >
  186. <img
  187. :src="`/images/states/${$colorMode.value}/add_category.svg`"
  188. loading="lazy"
  189. class="inline-flex flex-col object-contain object-center w-16 h-16 my-4"
  190. :alt="`${t('empty.headers')}`"
  191. />
  192. <span class="pb-4 text-center">{{ t("empty.headers") }}</span>
  193. <ButtonSecondary
  194. filled
  195. :label="`${t('add.new')}`"
  196. svg="plus"
  197. class="mb-4"
  198. @click.native="addHeader"
  199. />
  200. </div>
  201. </div>
  202. </div>
  203. </template>
  204. <script setup lang="ts">
  205. import { computed, Ref, ref, watch } from "@nuxtjs/composition-api"
  206. import isEqual from "lodash/isEqual"
  207. import {
  208. HoppRESTHeader,
  209. parseRawKeyValueEntriesE,
  210. rawKeyValueEntriesToString,
  211. RawKeyValueEntry,
  212. } from "@hoppscotch/data"
  213. import { flow, pipe } from "fp-ts/function"
  214. import * as RA from "fp-ts/ReadonlyArray"
  215. import * as E from "fp-ts/Either"
  216. import * as O from "fp-ts/Option"
  217. import * as A from "fp-ts/Array"
  218. import cloneDeep from "lodash/cloneDeep"
  219. import draggable from "vuedraggable"
  220. import { RequestOptionTabs } from "./RequestOptions.vue"
  221. import { useCodemirror } from "~/helpers/editor/codemirror"
  222. import {
  223. getRESTRequest,
  224. restHeaders$,
  225. restRequest$,
  226. setRESTHeaders,
  227. } from "~/newstore/RESTSession"
  228. import { commonHeaders } from "~/helpers/headers"
  229. import {
  230. useI18n,
  231. useReadonlyStream,
  232. useStream,
  233. useToast,
  234. } from "~/helpers/utils/composables"
  235. import linter from "~/helpers/editor/linting/rawKeyValue"
  236. import { throwError } from "~/helpers/functional/error"
  237. import { objRemoveKey } from "~/helpers/functional/object"
  238. import {
  239. ComputedHeader,
  240. getComputedHeaders,
  241. } from "~/helpers/utils/EffectiveURL"
  242. import { aggregateEnvs$, getAggregateEnvs } from "~/newstore/environments"
  243. const t = useI18n()
  244. const toast = useToast()
  245. const idTicker = ref(0)
  246. const bulkMode = ref(false)
  247. const bulkHeaders = ref("")
  248. const bulkEditor = ref<any | null>(null)
  249. const deletionToast = ref<{ goAway: (delay: number) => void } | null>(null)
  250. const emit = defineEmits<{
  251. (e: "change-tab", value: RequestOptionTabs): void
  252. }>()
  253. useCodemirror(bulkEditor, bulkHeaders, {
  254. extendedEditorConfig: {
  255. mode: "text/x-yaml",
  256. placeholder: `${t("state.bulk_mode_placeholder")}`,
  257. },
  258. linter,
  259. completer: null,
  260. environmentHighlights: true,
  261. })
  262. // The functional headers list (the headers actually in the system)
  263. const headers = useStream(restHeaders$, [], setRESTHeaders) as Ref<
  264. HoppRESTHeader[]
  265. >
  266. // The UI representation of the headers list (has the empty end headers)
  267. const workingHeaders = ref<Array<HoppRESTHeader & { id: number }>>([
  268. {
  269. id: idTicker.value++,
  270. key: "",
  271. value: "",
  272. active: true,
  273. },
  274. ])
  275. // Rule: Working Headers always have last element is always an empty header
  276. watch(workingHeaders, (headersList) => {
  277. if (
  278. headersList.length > 0 &&
  279. headersList[headersList.length - 1].key !== ""
  280. ) {
  281. workingHeaders.value.push({
  282. id: idTicker.value++,
  283. key: "",
  284. value: "",
  285. active: true,
  286. })
  287. }
  288. })
  289. // Sync logic between headers and working/bulk headers
  290. watch(
  291. headers,
  292. (newHeadersList) => {
  293. // Sync should overwrite working headers
  294. const filteredWorkingHeaders = pipe(
  295. workingHeaders.value,
  296. A.filterMap(
  297. flow(
  298. O.fromPredicate((e) => e.key !== ""),
  299. O.map(objRemoveKey("id"))
  300. )
  301. )
  302. )
  303. const filteredBulkHeaders = pipe(
  304. parseRawKeyValueEntriesE(bulkHeaders.value),
  305. E.map(
  306. flow(
  307. RA.filter((e) => e.key !== ""),
  308. RA.toArray
  309. )
  310. ),
  311. E.getOrElse(() => [] as RawKeyValueEntry[])
  312. )
  313. if (!isEqual(newHeadersList, filteredWorkingHeaders)) {
  314. workingHeaders.value = pipe(
  315. newHeadersList,
  316. A.map((x) => ({ id: idTicker.value++, ...x }))
  317. )
  318. }
  319. if (!isEqual(newHeadersList, filteredBulkHeaders)) {
  320. bulkHeaders.value = rawKeyValueEntriesToString(newHeadersList)
  321. }
  322. },
  323. { immediate: true }
  324. )
  325. watch(workingHeaders, (newWorkingHeaders) => {
  326. const fixedHeaders = pipe(
  327. newWorkingHeaders,
  328. A.filterMap(
  329. flow(
  330. O.fromPredicate((e) => e.key !== ""),
  331. O.map(objRemoveKey("id"))
  332. )
  333. )
  334. )
  335. if (!isEqual(headers.value, fixedHeaders)) {
  336. headers.value = cloneDeep(fixedHeaders)
  337. }
  338. })
  339. watch(bulkHeaders, (newBulkHeaders) => {
  340. const filteredBulkHeaders = pipe(
  341. parseRawKeyValueEntriesE(newBulkHeaders),
  342. E.map(
  343. flow(
  344. RA.filter((e) => e.key !== ""),
  345. RA.toArray
  346. )
  347. ),
  348. E.getOrElse(() => [] as RawKeyValueEntry[])
  349. )
  350. if (!isEqual(headers.value, filteredBulkHeaders)) {
  351. headers.value = filteredBulkHeaders
  352. }
  353. })
  354. const addHeader = () => {
  355. workingHeaders.value.push({
  356. id: idTicker.value++,
  357. key: "",
  358. value: "",
  359. active: true,
  360. })
  361. }
  362. const updateHeader = (
  363. index: number,
  364. header: HoppRESTHeader & { id: number }
  365. ) => {
  366. workingHeaders.value = workingHeaders.value.map((h, i) =>
  367. i === index ? header : h
  368. )
  369. }
  370. const deleteHeader = (index: number) => {
  371. const headersBeforeDeletion = cloneDeep(workingHeaders.value)
  372. if (
  373. !(
  374. headersBeforeDeletion.length > 0 &&
  375. index === headersBeforeDeletion.length - 1
  376. )
  377. ) {
  378. if (deletionToast.value) {
  379. deletionToast.value.goAway(0)
  380. deletionToast.value = null
  381. }
  382. deletionToast.value = toast.success(`${t("state.deleted")}`, {
  383. action: [
  384. {
  385. text: `${t("action.undo")}`,
  386. onClick: (_, toastObject) => {
  387. workingHeaders.value = headersBeforeDeletion
  388. toastObject.goAway(0)
  389. deletionToast.value = null
  390. },
  391. },
  392. ],
  393. onComplete: () => {
  394. deletionToast.value = null
  395. },
  396. })
  397. }
  398. workingHeaders.value = pipe(
  399. workingHeaders.value,
  400. A.deleteAt(index),
  401. O.getOrElseW(() => throwError("Working Headers Deletion Out of Bounds"))
  402. )
  403. }
  404. const clearContent = () => {
  405. // set params list to the initial state
  406. workingHeaders.value = [
  407. {
  408. id: idTicker.value++,
  409. key: "",
  410. value: "",
  411. active: true,
  412. },
  413. ]
  414. bulkHeaders.value = ""
  415. }
  416. const restRequest = useReadonlyStream(restRequest$, getRESTRequest())
  417. const aggregateEnvs = useReadonlyStream(aggregateEnvs$, getAggregateEnvs())
  418. const computedHeaders = computed(() =>
  419. getComputedHeaders(restRequest.value, aggregateEnvs.value)
  420. )
  421. const masking = ref(true)
  422. const toggleMask = () => {
  423. masking.value = !masking.value
  424. }
  425. const mask = (header: ComputedHeader) => {
  426. if (header.source === "auth" && masking.value)
  427. return header.header.value.replace(/\S/gi, "*")
  428. return header.header.value
  429. }
  430. const changeTab = (tab: ComputedHeader["source"]) => {
  431. if (tab === "auth") emit("change-tab", "authorization")
  432. else emit("change-tab", "bodyParams")
  433. }
  434. </script>