index.vue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <template>
  2. <Splitpanes
  3. class="smart-splitter"
  4. :rtl="SIDEBAR_ON_LEFT && windowInnerWidth.x.value >= 768"
  5. :class="{
  6. '!flex-row-reverse': SIDEBAR_ON_LEFT && windowInnerWidth.x.value >= 768,
  7. }"
  8. :horizontal="!(windowInnerWidth.x.value >= 768)"
  9. >
  10. <Pane size="75" min-size="65" class="hide-scrollbar !overflow-auto">
  11. <Splitpanes class="smart-splitter" :horizontal="COLUMN_LAYOUT">
  12. <Pane
  13. :size="COLUMN_LAYOUT ? 45 : 50"
  14. class="hide-scrollbar !overflow-auto"
  15. >
  16. <HttpRequest />
  17. <SmartTabs styles="sticky bg-primary top-upperPrimaryStickyFold z-10">
  18. <SmartTab
  19. :id="'params'"
  20. :label="`${$t('tab.parameters')}`"
  21. :selected="true"
  22. :info="`${newActiveParamsCount$}`"
  23. >
  24. <HttpParameters />
  25. </SmartTab>
  26. <SmartTab :id="'bodyParams'" :label="`${$t('tab.body')}`">
  27. <HttpBody />
  28. </SmartTab>
  29. <SmartTab
  30. :id="'headers'"
  31. :label="`${$t('tab.headers')}`"
  32. :info="`${newActiveHeadersCount$}`"
  33. >
  34. <HttpHeaders />
  35. </SmartTab>
  36. <SmartTab
  37. :id="'authorization'"
  38. :label="`${$t('tab.authorization')}`"
  39. >
  40. <HttpAuthorization />
  41. </SmartTab>
  42. <SmartTab
  43. :id="'preRequestScript'"
  44. :label="`${$t('tab.pre_request_script')}`"
  45. :indicator="preRequestScript.length > 0"
  46. >
  47. <HttpPreRequestScript />
  48. </SmartTab>
  49. <SmartTab
  50. :id="'tests'"
  51. :label="`${$t('tab.tests')}`"
  52. :indicator="testScript.length > 0"
  53. >
  54. <HttpTests />
  55. </SmartTab>
  56. </SmartTabs>
  57. </Pane>
  58. <Pane
  59. :size="COLUMN_LAYOUT ? 65 : 50"
  60. class="flex flex-col hide-scrollbar !overflow-auto"
  61. >
  62. <HttpResponse ref="response" />
  63. </Pane>
  64. </Splitpanes>
  65. </Pane>
  66. <Pane
  67. v-if="SIDEBAR"
  68. size="25"
  69. min-size="20"
  70. class="hide-scrollbar !overflow-auto"
  71. >
  72. <SmartTabs styles="sticky bg-primary z-10 top-0" vertical>
  73. <SmartTab
  74. :id="'history'"
  75. icon="clock"
  76. :label="`${$t('tab.history')}`"
  77. :selected="true"
  78. >
  79. <History ref="historyComponent" :page="'rest'" />
  80. </SmartTab>
  81. <SmartTab
  82. :id="'collections'"
  83. icon="folder"
  84. :label="`${$t('tab.collections')}`"
  85. >
  86. <Collections />
  87. </SmartTab>
  88. <SmartTab
  89. :id="'env'"
  90. icon="layers"
  91. :label="`${$t('environment.title')}`"
  92. >
  93. <Environments />
  94. </SmartTab>
  95. </SmartTabs>
  96. </Pane>
  97. <SmartConfirmModal
  98. :show="confirmSync"
  99. :title="`${$t('confirm.sync')}`"
  100. @hide-modal="confirmSync = false"
  101. @resolve="syncRequest"
  102. />
  103. </Splitpanes>
  104. </template>
  105. <script lang="ts">
  106. import {
  107. defineComponent,
  108. onBeforeMount,
  109. onBeforeUnmount,
  110. onMounted,
  111. Ref,
  112. ref,
  113. useContext,
  114. } from "@nuxtjs/composition-api"
  115. import { Splitpanes, Pane } from "splitpanes"
  116. import "splitpanes/dist/splitpanes.css"
  117. import { map } from "rxjs/operators"
  118. import { Subscription } from "rxjs"
  119. import isEqual from "lodash/isEqual"
  120. import { HoppRESTRequest, HoppRESTAuthOAuth2 } from "@hoppscotch/data"
  121. import { useSetting } from "~/newstore/settings"
  122. import {
  123. restActiveParamsCount$,
  124. restActiveHeadersCount$,
  125. getRESTRequest,
  126. setRESTRequest,
  127. setRESTAuth,
  128. restAuth$,
  129. useTestScript,
  130. usePreRequestScript,
  131. } from "~/newstore/RESTSession"
  132. import { translateExtURLParams } from "~/helpers/RESTExtURLParams"
  133. import {
  134. pluckRef,
  135. useReadonlyStream,
  136. useStream,
  137. } from "~/helpers/utils/composables"
  138. import { loadRequestFromSync, startRequestSync } from "~/helpers/fb/request"
  139. import { onLoggedIn } from "~/helpers/fb/auth"
  140. import { oauthRedirect } from "~/helpers/oauth"
  141. import useWindowSize from "~/helpers/utils/useWindowSize"
  142. function bindRequestToURLParams() {
  143. const { route } = useContext()
  144. // Get URL parameters and set that as the request
  145. onMounted(() => {
  146. const query = route.value.query
  147. // If query params are empty, or contains code or error param (these are from Oauth Redirect)
  148. // We skip URL params parsing
  149. if (Object.keys(query).length === 0 || query.code || query.error) return
  150. setRESTRequest(translateExtURLParams(query))
  151. })
  152. }
  153. function oAuthURL() {
  154. const auth = useStream(
  155. restAuth$,
  156. { authType: "none", authActive: true },
  157. setRESTAuth
  158. )
  159. const oauth2Token = pluckRef(auth as Ref<HoppRESTAuthOAuth2>, "token")
  160. onBeforeMount(async () => {
  161. const tokenInfo = await oauthRedirect()
  162. if (Object.prototype.hasOwnProperty.call(tokenInfo, "access_token")) {
  163. if (typeof tokenInfo === "object") {
  164. oauth2Token.value = tokenInfo.access_token
  165. }
  166. }
  167. })
  168. }
  169. function setupRequestSync(
  170. confirmSync: Ref<boolean>,
  171. requestForSync: Ref<HoppRESTRequest | null>
  172. ) {
  173. const { route } = useContext()
  174. // Subscription to request sync
  175. let sub: Subscription | null = null
  176. // Load request on login resolve and start sync
  177. onLoggedIn(async () => {
  178. if (
  179. Object.keys(route.value.query).length === 0 &&
  180. !(route.value.query.code || route.value.query.error)
  181. ) {
  182. const request = await loadRequestFromSync()
  183. if (request) {
  184. // setRESTRequest(request)
  185. if (!isEqual(request, getRESTRequest())) {
  186. requestForSync.value = request
  187. confirmSync.value = true
  188. }
  189. }
  190. }
  191. sub = startRequestSync()
  192. })
  193. // Stop subscripton to stop syncing
  194. onBeforeUnmount(() => {
  195. sub?.unsubscribe()
  196. })
  197. }
  198. export default defineComponent({
  199. components: { Splitpanes, Pane },
  200. setup() {
  201. const requestForSync = ref<HoppRESTRequest | null>(null)
  202. const testScript = useTestScript()
  203. const preRequestScript = usePreRequestScript()
  204. const confirmSync = ref(false)
  205. const syncRequest = () => {
  206. setRESTRequest(requestForSync.value!)
  207. }
  208. setupRequestSync(confirmSync, requestForSync)
  209. bindRequestToURLParams()
  210. return {
  211. windowInnerWidth: useWindowSize(),
  212. newActiveParamsCount$: useReadonlyStream(
  213. restActiveParamsCount$.pipe(
  214. map((e) => {
  215. if (e === 0) return null
  216. return `${e}`
  217. })
  218. ),
  219. null
  220. ),
  221. newActiveHeadersCount$: useReadonlyStream(
  222. restActiveHeadersCount$.pipe(
  223. map((e) => {
  224. if (e === 0) return null
  225. return `${e}`
  226. })
  227. ),
  228. null
  229. ),
  230. SIDEBAR: useSetting("SIDEBAR"),
  231. COLUMN_LAYOUT: useSetting("COLUMN_LAYOUT"),
  232. SIDEBAR_ON_LEFT: useSetting("SIDEBAR_ON_LEFT"),
  233. confirmSync,
  234. syncRequest,
  235. oAuthURL,
  236. requestForSync,
  237. testScript,
  238. preRequestScript,
  239. }
  240. },
  241. })
  242. </script>