123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- <template>
- <AppPaneLayout>
- <template #primary>
- <HttpRequest />
- <HttpRequestOptions />
- </template>
- <template #secondary>
- <HttpResponse />
- </template>
- <template #sidebar>
- <HttpSidebar />
- </template>
- </AppPaneLayout>
- </template>
- <script lang="ts">
- import {
- defineComponent,
- onBeforeMount,
- onBeforeUnmount,
- onMounted,
- Ref,
- ref,
- useContext,
- watch,
- } from "@nuxtjs/composition-api"
- import { Subscription } from "rxjs"
- import {
- HoppRESTRequest,
- HoppRESTAuthOAuth2,
- safelyExtractRESTRequest,
- isEqualHoppRESTRequest,
- } from "@hoppscotch/data"
- import {
- getRESTRequest,
- setRESTRequest,
- setRESTAuth,
- restAuth$,
- getDefaultRESTRequest,
- } from "~/newstore/RESTSession"
- import { translateExtURLParams } from "~/helpers/RESTExtURLParams"
- import {
- pluckRef,
- useI18n,
- useStream,
- useToast,
- } from "~/helpers/utils/composables"
- import { loadRequestFromSync, startRequestSync } from "~/helpers/fb/request"
- import { onLoggedIn } from "~/helpers/fb/auth"
- import { oauthRedirect } from "~/helpers/oauth"
- function bindRequestToURLParams() {
- const { route } = useContext()
- // Get URL parameters and set that as the request
- onMounted(() => {
- const query = route.value.query
- // If query params are empty, or contains code or error param (these are from Oauth Redirect)
- // We skip URL params parsing
- if (Object.keys(query).length === 0 || query.code || query.error) return
- setRESTRequest(
- safelyExtractRESTRequest(
- translateExtURLParams(query),
- getDefaultRESTRequest()
- )
- )
- })
- }
- function oAuthURL() {
- const auth = useStream(
- restAuth$,
- { authType: "none", authActive: true },
- setRESTAuth
- )
- const oauth2Token = pluckRef(auth as Ref<HoppRESTAuthOAuth2>, "token")
- onBeforeMount(async () => {
- const tokenInfo = await oauthRedirect()
- if (Object.prototype.hasOwnProperty.call(tokenInfo, "access_token")) {
- if (typeof tokenInfo === "object") {
- oauth2Token.value = tokenInfo.access_token
- }
- }
- })
- }
- function setupRequestSync(
- confirmSync: Ref<boolean>,
- requestForSync: Ref<HoppRESTRequest | null>
- ) {
- const { route } = useContext()
- // Subscription to request sync
- let sub: Subscription | null = null
- // Load request on login resolve and start sync
- onLoggedIn(async () => {
- if (
- Object.keys(route.value.query).length === 0 &&
- !(route.value.query.code || route.value.query.error)
- ) {
- const request = await loadRequestFromSync()
- if (request) {
- if (!isEqualHoppRESTRequest(request, getRESTRequest())) {
- requestForSync.value = request
- confirmSync.value = true
- }
- }
- }
- sub = startRequestSync()
- })
- // Stop subscription to stop syncing
- onBeforeUnmount(() => {
- sub?.unsubscribe()
- })
- }
- export default defineComponent({
- setup() {
- const requestForSync = ref<HoppRESTRequest | null>(null)
- const confirmSync = ref(false)
- const toast = useToast()
- const t = useI18n()
- watch(confirmSync, (newValue) => {
- if (newValue) {
- toast.show(`${t("confirm.sync")}`, {
- duration: 0,
- action: [
- {
- text: `${t("action.yes")}`,
- onClick: (_, toastObject) => {
- syncRequest()
- toastObject.goAway(0)
- },
- },
- {
- text: `${t("action.no")}`,
- onClick: (_, toastObject) => {
- toastObject.goAway(0)
- },
- },
- ],
- })
- }
- })
- const syncRequest = () => {
- setRESTRequest(
- safelyExtractRESTRequest(requestForSync.value!, getDefaultRESTRequest())
- )
- }
- setupRequestSync(confirmSync, requestForSync)
- bindRequestToURLParams()
- oAuthURL()
- return {
- confirmSync,
- syncRequest,
- oAuthURL,
- requestForSync,
- }
- },
- })
- </script>
|