123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467 |
- <template>
- <div class="flex flex-col flex-1">
- <div
- class="sticky z-10 flex items-center justify-between pl-4 border-b bg-primary border-dividerLight top-upperMobileSecondaryStickyFold sm:top-upperSecondaryStickyFold"
- >
- <label class="font-semibold text-secondaryLight">
- {{ t("request.header_list") }}
- </label>
- <div class="flex">
- <ButtonSecondary
- v-tippy="{ theme: 'tooltip' }"
- to="https://docs.hoppscotch.io/features/headers"
- blank
- :title="t('app.wiki')"
- svg="help-circle"
- />
- <ButtonSecondary
- v-tippy="{ theme: 'tooltip' }"
- :title="t('action.clear_all')"
- svg="trash-2"
- @click.native="clearContent()"
- />
- <ButtonSecondary
- v-tippy="{ theme: 'tooltip' }"
- :title="t('state.bulk_mode')"
- svg="edit"
- :class="{ '!text-accent': bulkMode }"
- @click.native="bulkMode = !bulkMode"
- />
- <ButtonSecondary
- v-tippy="{ theme: 'tooltip' }"
- :title="t('add.new')"
- svg="plus"
- :disabled="bulkMode"
- @click.native="addHeader"
- />
- </div>
- </div>
- <div v-if="bulkMode" ref="bulkEditor" class="flex flex-col flex-1"></div>
- <div v-else>
- <draggable
- v-model="workingHeaders"
- animation="250"
- handle=".draggable-handle"
- draggable=".draggable-content"
- ghost-class="cursor-move"
- chosen-class="bg-primaryLight"
- drag-class="cursor-grabbing"
- >
- <div
- v-for="(header, index) in workingHeaders"
- :key="`header-${header.id}-${index}`"
- class="flex border-b divide-x divide-dividerLight border-dividerLight draggable-content group"
- >
- <span>
- <ButtonSecondary
- svg="grip-vertical"
- class="cursor-auto text-primary hover:text-primary"
- :class="{
- 'draggable-handle group-hover:text-secondaryLight !cursor-grab':
- index !== workingHeaders?.length - 1,
- }"
- tabindex="-1"
- />
- </span>
- <SmartAutoComplete
- :placeholder="`${t('count.header', { count: index + 1 })}`"
- :source="commonHeaders"
- :spellcheck="false"
- :value="header.key"
- autofocus
- styles="
- bg-transparent
- flex
- flex-1
- py-1
- px-4
- truncate
- "
- class="flex-1 !flex"
- @input="
- updateHeader(index, {
- id: header.id,
- key: $event,
- value: header.value,
- active: header.active,
- })
- "
- />
- <SmartEnvInput
- v-model="header.value"
- :placeholder="`${t('count.value', { count: index + 1 })}`"
- @change="
- updateHeader(index, {
- id: header.id,
- key: header.key,
- value: $event,
- active: header.active,
- })
- "
- />
- <span>
- <ButtonSecondary
- v-tippy="{ theme: 'tooltip' }"
- :title="
- header.hasOwnProperty('active')
- ? header.active
- ? t('action.turn_off')
- : t('action.turn_on')
- : t('action.turn_off')
- "
- :svg="
- header.hasOwnProperty('active')
- ? header.active
- ? 'check-circle'
- : 'circle'
- : 'check-circle'
- "
- color="green"
- @click.native="
- updateHeader(index, {
- id: header.id,
- key: header.key,
- value: header.value,
- active: !header.active,
- })
- "
- />
- </span>
- <span>
- <ButtonSecondary
- v-tippy="{ theme: 'tooltip' }"
- :title="t('action.remove')"
- svg="trash"
- color="red"
- @click.native="deleteHeader(index)"
- />
- </span>
- </div>
- <div
- v-for="(header, index) in computedHeaders"
- :key="`header-${index}`"
- class="flex border-b divide-x divide-dividerLight border-dividerLight draggable-content group"
- >
- <span>
- <ButtonSecondary
- svg="lock"
- class="opacity-25 cursor-auto text-secondaryLight bg-divider"
- tabindex="-1"
- />
- </span>
- <SmartEnvInput
- v-model="header.header.key"
- :placeholder="`${t('count.value', { count: index + 1 })}`"
- readonly
- />
- <SmartEnvInput
- :value="mask(header)"
- :placeholder="`${t('count.value', { count: index + 1 })}`"
- readonly
- />
- <span>
- <ButtonSecondary
- v-if="header.source === 'auth'"
- :svg="masking ? 'eye' : 'eye-off'"
- @click.native="toggleMask()"
- />
- <ButtonSecondary
- v-else
- svg="arrow-up-right"
- class="cursor-auto text-primary hover:text-primary"
- />
- </span>
- <span>
- <ButtonSecondary
- svg="arrow-up-right"
- @click.native="changeTab(header.source)"
- />
- </span>
- </div>
- </draggable>
- <div
- v-if="workingHeaders.length === 0"
- class="flex flex-col items-center justify-center p-4 text-secondaryLight"
- >
- <img
- :src="`/images/states/${$colorMode.value}/add_category.svg`"
- loading="lazy"
- class="inline-flex flex-col object-contain object-center w-16 h-16 my-4"
- :alt="`${t('empty.headers')}`"
- />
- <span class="pb-4 text-center">{{ t("empty.headers") }}</span>
- <ButtonSecondary
- filled
- :label="`${t('add.new')}`"
- svg="plus"
- class="mb-4"
- @click.native="addHeader"
- />
- </div>
- </div>
- </div>
- </template>
- <script setup lang="ts">
- import { computed, Ref, ref, watch } from "@nuxtjs/composition-api"
- import isEqual from "lodash/isEqual"
- import {
- HoppRESTHeader,
- parseRawKeyValueEntriesE,
- rawKeyValueEntriesToString,
- RawKeyValueEntry,
- } from "@hoppscotch/data"
- import { flow, pipe } from "fp-ts/function"
- import * as RA from "fp-ts/ReadonlyArray"
- import * as E from "fp-ts/Either"
- import * as O from "fp-ts/Option"
- import * as A from "fp-ts/Array"
- import cloneDeep from "lodash/cloneDeep"
- import draggable from "vuedraggable"
- import { RequestOptionTabs } from "./RequestOptions.vue"
- import { useCodemirror } from "~/helpers/editor/codemirror"
- import {
- getRESTRequest,
- restHeaders$,
- restRequest$,
- setRESTHeaders,
- } from "~/newstore/RESTSession"
- import { commonHeaders } from "~/helpers/headers"
- import {
- useI18n,
- useReadonlyStream,
- useStream,
- useToast,
- } from "~/helpers/utils/composables"
- import linter from "~/helpers/editor/linting/rawKeyValue"
- import { throwError } from "~/helpers/functional/error"
- import { objRemoveKey } from "~/helpers/functional/object"
- import {
- ComputedHeader,
- getComputedHeaders,
- } from "~/helpers/utils/EffectiveURL"
- import { aggregateEnvs$, getAggregateEnvs } from "~/newstore/environments"
- const t = useI18n()
- const toast = useToast()
- const idTicker = ref(0)
- const bulkMode = ref(false)
- const bulkHeaders = ref("")
- const bulkEditor = ref<any | null>(null)
- const deletionToast = ref<{ goAway: (delay: number) => void } | null>(null)
- const emit = defineEmits<{
- (e: "change-tab", value: RequestOptionTabs): void
- }>()
- useCodemirror(bulkEditor, bulkHeaders, {
- extendedEditorConfig: {
- mode: "text/x-yaml",
- placeholder: `${t("state.bulk_mode_placeholder")}`,
- },
- linter,
- completer: null,
- environmentHighlights: true,
- })
- // The functional headers list (the headers actually in the system)
- const headers = useStream(restHeaders$, [], setRESTHeaders) as Ref<
- HoppRESTHeader[]
- >
- // The UI representation of the headers list (has the empty end headers)
- const workingHeaders = ref<Array<HoppRESTHeader & { id: number }>>([
- {
- id: idTicker.value++,
- key: "",
- value: "",
- active: true,
- },
- ])
- // Rule: Working Headers always have last element is always an empty header
- watch(workingHeaders, (headersList) => {
- if (
- headersList.length > 0 &&
- headersList[headersList.length - 1].key !== ""
- ) {
- workingHeaders.value.push({
- id: idTicker.value++,
- key: "",
- value: "",
- active: true,
- })
- }
- })
- // Sync logic between headers and working/bulk headers
- watch(
- headers,
- (newHeadersList) => {
- // Sync should overwrite working headers
- const filteredWorkingHeaders = pipe(
- workingHeaders.value,
- A.filterMap(
- flow(
- O.fromPredicate((e) => e.key !== ""),
- O.map(objRemoveKey("id"))
- )
- )
- )
- const filteredBulkHeaders = pipe(
- parseRawKeyValueEntriesE(bulkHeaders.value),
- E.map(
- flow(
- RA.filter((e) => e.key !== ""),
- RA.toArray
- )
- ),
- E.getOrElse(() => [] as RawKeyValueEntry[])
- )
- if (!isEqual(newHeadersList, filteredWorkingHeaders)) {
- workingHeaders.value = pipe(
- newHeadersList,
- A.map((x) => ({ id: idTicker.value++, ...x }))
- )
- }
- if (!isEqual(newHeadersList, filteredBulkHeaders)) {
- bulkHeaders.value = rawKeyValueEntriesToString(newHeadersList)
- }
- },
- { immediate: true }
- )
- watch(workingHeaders, (newWorkingHeaders) => {
- const fixedHeaders = pipe(
- newWorkingHeaders,
- A.filterMap(
- flow(
- O.fromPredicate((e) => e.key !== ""),
- O.map(objRemoveKey("id"))
- )
- )
- )
- if (!isEqual(headers.value, fixedHeaders)) {
- headers.value = cloneDeep(fixedHeaders)
- }
- })
- watch(bulkHeaders, (newBulkHeaders) => {
- const filteredBulkHeaders = pipe(
- parseRawKeyValueEntriesE(newBulkHeaders),
- E.map(
- flow(
- RA.filter((e) => e.key !== ""),
- RA.toArray
- )
- ),
- E.getOrElse(() => [] as RawKeyValueEntry[])
- )
- if (!isEqual(headers.value, filteredBulkHeaders)) {
- headers.value = filteredBulkHeaders
- }
- })
- const addHeader = () => {
- workingHeaders.value.push({
- id: idTicker.value++,
- key: "",
- value: "",
- active: true,
- })
- }
- const updateHeader = (
- index: number,
- header: HoppRESTHeader & { id: number }
- ) => {
- workingHeaders.value = workingHeaders.value.map((h, i) =>
- i === index ? header : h
- )
- }
- const deleteHeader = (index: number) => {
- const headersBeforeDeletion = cloneDeep(workingHeaders.value)
- if (
- !(
- headersBeforeDeletion.length > 0 &&
- index === headersBeforeDeletion.length - 1
- )
- ) {
- if (deletionToast.value) {
- deletionToast.value.goAway(0)
- deletionToast.value = null
- }
- deletionToast.value = toast.success(`${t("state.deleted")}`, {
- action: [
- {
- text: `${t("action.undo")}`,
- onClick: (_, toastObject) => {
- workingHeaders.value = headersBeforeDeletion
- toastObject.goAway(0)
- deletionToast.value = null
- },
- },
- ],
- onComplete: () => {
- deletionToast.value = null
- },
- })
- }
- workingHeaders.value = pipe(
- workingHeaders.value,
- A.deleteAt(index),
- O.getOrElseW(() => throwError("Working Headers Deletion Out of Bounds"))
- )
- }
- const clearContent = () => {
- // set params list to the initial state
- workingHeaders.value = [
- {
- id: idTicker.value++,
- key: "",
- value: "",
- active: true,
- },
- ]
- bulkHeaders.value = ""
- }
- const restRequest = useReadonlyStream(restRequest$, getRESTRequest())
- const aggregateEnvs = useReadonlyStream(aggregateEnvs$, getAggregateEnvs())
- const computedHeaders = computed(() =>
- getComputedHeaders(restRequest.value, aggregateEnvs.value)
- )
- const masking = ref(true)
- const toggleMask = () => {
- masking.value = !masking.value
- }
- const mask = (header: ComputedHeader) => {
- if (header.source === "auth" && masking.value)
- return header.header.value.replace(/\S/gi, "*")
- return header.header.value
- }
- const changeTab = (tab: ComputedHeader["source"]) => {
- if (tab === "auth") emit("change-tab", "authorization")
- else emit("change-tab", "bodyParams")
- }
- </script>
|