index.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <template>
  2. <AppPaneLayout>
  3. <template #primary>
  4. <HttpRequest />
  5. <HttpRequestOptions />
  6. </template>
  7. <template #secondary>
  8. <HttpResponse />
  9. </template>
  10. <template #sidebar>
  11. <HttpSidebar />
  12. </template>
  13. </AppPaneLayout>
  14. </template>
  15. <script lang="ts">
  16. import {
  17. defineComponent,
  18. onBeforeMount,
  19. onBeforeUnmount,
  20. onMounted,
  21. Ref,
  22. ref,
  23. useContext,
  24. watch,
  25. } from "@nuxtjs/composition-api"
  26. import { Subscription } from "rxjs"
  27. import {
  28. HoppRESTRequest,
  29. HoppRESTAuthOAuth2,
  30. safelyExtractRESTRequest,
  31. isEqualHoppRESTRequest,
  32. } from "@hoppscotch/data"
  33. import {
  34. getRESTRequest,
  35. setRESTRequest,
  36. setRESTAuth,
  37. restAuth$,
  38. getDefaultRESTRequest,
  39. } from "~/newstore/RESTSession"
  40. import { translateExtURLParams } from "~/helpers/RESTExtURLParams"
  41. import {
  42. pluckRef,
  43. useI18n,
  44. useStream,
  45. useToast,
  46. } from "~/helpers/utils/composables"
  47. import { loadRequestFromSync, startRequestSync } from "~/helpers/fb/request"
  48. import { onLoggedIn } from "~/helpers/fb/auth"
  49. import { oauthRedirect } from "~/helpers/oauth"
  50. function bindRequestToURLParams() {
  51. const { route } = useContext()
  52. // Get URL parameters and set that as the request
  53. onMounted(() => {
  54. const query = route.value.query
  55. // If query params are empty, or contains code or error param (these are from Oauth Redirect)
  56. // We skip URL params parsing
  57. if (Object.keys(query).length === 0 || query.code || query.error) return
  58. setRESTRequest(
  59. safelyExtractRESTRequest(
  60. translateExtURLParams(query),
  61. getDefaultRESTRequest()
  62. )
  63. )
  64. })
  65. }
  66. function oAuthURL() {
  67. const auth = useStream(
  68. restAuth$,
  69. { authType: "none", authActive: true },
  70. setRESTAuth
  71. )
  72. const oauth2Token = pluckRef(auth as Ref<HoppRESTAuthOAuth2>, "token")
  73. onBeforeMount(async () => {
  74. const tokenInfo = await oauthRedirect()
  75. if (Object.prototype.hasOwnProperty.call(tokenInfo, "access_token")) {
  76. if (typeof tokenInfo === "object") {
  77. oauth2Token.value = tokenInfo.access_token
  78. }
  79. }
  80. })
  81. }
  82. function setupRequestSync(
  83. confirmSync: Ref<boolean>,
  84. requestForSync: Ref<HoppRESTRequest | null>
  85. ) {
  86. const { route } = useContext()
  87. // Subscription to request sync
  88. let sub: Subscription | null = null
  89. // Load request on login resolve and start sync
  90. onLoggedIn(async () => {
  91. if (
  92. Object.keys(route.value.query).length === 0 &&
  93. !(route.value.query.code || route.value.query.error)
  94. ) {
  95. const request = await loadRequestFromSync()
  96. if (request) {
  97. if (!isEqualHoppRESTRequest(request, getRESTRequest())) {
  98. requestForSync.value = request
  99. confirmSync.value = true
  100. }
  101. }
  102. }
  103. sub = startRequestSync()
  104. })
  105. // Stop subscription to stop syncing
  106. onBeforeUnmount(() => {
  107. sub?.unsubscribe()
  108. })
  109. }
  110. export default defineComponent({
  111. setup() {
  112. const requestForSync = ref<HoppRESTRequest | null>(null)
  113. const confirmSync = ref(false)
  114. const toast = useToast()
  115. const t = useI18n()
  116. watch(confirmSync, (newValue) => {
  117. if (newValue) {
  118. toast.show(`${t("confirm.sync")}`, {
  119. duration: 0,
  120. action: [
  121. {
  122. text: `${t("action.yes")}`,
  123. onClick: (_, toastObject) => {
  124. syncRequest()
  125. toastObject.goAway(0)
  126. },
  127. },
  128. {
  129. text: `${t("action.no")}`,
  130. onClick: (_, toastObject) => {
  131. toastObject.goAway(0)
  132. },
  133. },
  134. ],
  135. })
  136. }
  137. })
  138. const syncRequest = () => {
  139. setRESTRequest(
  140. safelyExtractRESTRequest(requestForSync.value!, getDefaultRESTRequest())
  141. )
  142. }
  143. setupRequestSync(confirmSync, requestForSync)
  144. bindRequestToURLParams()
  145. oAuthURL()
  146. return {
  147. confirmSync,
  148. syncRequest,
  149. oAuthURL,
  150. requestForSync,
  151. }
  152. },
  153. })
  154. </script>