diff --git a/packages/hoppscotch-common/locales/en.json b/packages/hoppscotch-common/locales/en.json index c2ffd477..077bc0ef 100644 --- a/packages/hoppscotch-common/locales/en.json +++ b/packages/hoppscotch-common/locales/en.json @@ -823,7 +823,7 @@ "proxy_url": "Proxy URL", "proxy_use_toggle": "Use the proxy middleware to send requests", "read_the": "Read the", - "reset_default": "Reset to default", + "reset_default": "Use Default Proxy", "short_codes": "Short codes", "short_codes_description": "Short codes which were created by you.", "sidebar_on_left": "Sidebar on left", diff --git a/packages/hoppscotch-common/src/components/settings/Proxy.vue b/packages/hoppscotch-common/src/components/settings/Proxy.vue index 270706b0..f1ee95bf 100644 --- a/packages/hoppscotch-common/src/components/settings/Proxy.vue +++ b/packages/hoppscotch-common/src/components/settings/Proxy.vue @@ -36,10 +36,13 @@ import { useSetting } from "~/composables/settings" import IconRotateCCW from "~icons/lucide/rotate-ccw" import IconCheck from "~icons/lucide/check" import { useToast } from "~/composables/toast" -import { computed } from "vue" +import { computed, watch } from "vue" import { useService } from "dioc/vue" import { InterceptorService } from "~/services/interceptor.service" import { proxyInterceptor } from "~/platform/std/interceptors/proxy" +import { useReadonlyStream } from "~/composables/stream" +import { platform } from "~/platform" +import { getDefaultProxyUrl } from "~/helpers/proxyUrl" const t = useI18n() const toast = useToast() @@ -48,6 +51,17 @@ const interceptorService = useService(InterceptorService) const PROXY_URL = useSetting("PROXY_URL") +const currentUser = useReadonlyStream( + platform.auth.getCurrentUserStream(), + platform.auth.getCurrentUser() +) + +watch(currentUser, async () => { + if (!currentUser.value) { + PROXY_URL.value = await getDefaultProxyUrl() + } +}) + const proxyEnabled = computed( () => interceptorService.currentInterceptorID.value === @@ -59,8 +73,8 @@ const clearIcon = refAutoReset( 1000 ) -const resetProxy = () => { - PROXY_URL.value = "https://proxy.hoppscotch.io/" +const resetProxy = async () => { + PROXY_URL.value = await getDefaultProxyUrl() clearIcon.value = IconCheck toast.success(`${t("state.cleared")}`) } diff --git a/packages/hoppscotch-common/src/helpers/proxyUrl.ts b/packages/hoppscotch-common/src/helpers/proxyUrl.ts new file mode 100644 index 00000000..ac2a9083 --- /dev/null +++ b/packages/hoppscotch-common/src/helpers/proxyUrl.ts @@ -0,0 +1,22 @@ +import { platform } from "~/platform" +import * as E from "fp-ts/Either" + +// Default proxy URL +export const DEFAULT_HOPP_PROXY_URL = "https://proxy.hoppscotch.io/" + +// Get default proxy URL from platform or return default +export const getDefaultProxyUrl = async () => { + const proxyAppUrl = platform?.infra?.getProxyAppUrl + + if (proxyAppUrl) { + const res = await proxyAppUrl() + + if (E.isRight(res)) { + return res.right.value + } + + return DEFAULT_HOPP_PROXY_URL + } + + return DEFAULT_HOPP_PROXY_URL +} diff --git a/packages/hoppscotch-common/src/helpers/strategies/AxiosStrategy.ts b/packages/hoppscotch-common/src/helpers/strategies/AxiosStrategy.ts index 137bc659..18bf22f3 100644 --- a/packages/hoppscotch-common/src/helpers/strategies/AxiosStrategy.ts +++ b/packages/hoppscotch-common/src/helpers/strategies/AxiosStrategy.ts @@ -6,6 +6,7 @@ import { cloneDeep } from "lodash-es" import { NetworkResponse, NetworkStrategy } from "../network" import { decodeB64StringToArrayBuffer } from "../utils/b64" import { settingsStore } from "~/newstore/settings" +import { getDefaultProxyUrl } from "../proxyUrl" let cancelSource = axios.CancelToken.source() @@ -42,6 +43,8 @@ const getProxyPayload = ( return payload } +const defaultProxyURL = await getDefaultProxyUrl() + const preProcessRequest = (req: AxiosRequestConfig): AxiosRequestConfig => { const reqClone = cloneDeep(req) @@ -103,7 +106,7 @@ const axiosWithProxy: NetworkStrategy = (req) => TE.tryCatch( () => axios.post( - settingsStore.value.PROXY_URL || "https://proxy.hoppscotch.io", + settingsStore.value.PROXY_URL || defaultProxyURL, payload, { headers, diff --git a/packages/hoppscotch-common/src/newstore/settings.ts b/packages/hoppscotch-common/src/newstore/settings.ts index 8581fbf5..f45c7687 100644 --- a/packages/hoppscotch-common/src/newstore/settings.ts +++ b/packages/hoppscotch-common/src/newstore/settings.ts @@ -3,6 +3,7 @@ import { Observable } from "rxjs" import { distinctUntilChanged, pluck } from "rxjs/operators" import type { KeysMatching } from "~/types/ts-utils" import DispatchingStore, { defineDispatchers } from "./DispatchingStore" +import { getDefaultProxyUrl } from "~/helpers/proxyUrl" export const HoppBgColors = ["system", "light", "dark", "black"] as const @@ -82,6 +83,8 @@ export type SettingsDef = { CUSTOM_NAMING_STYLE: string } +const defaultProxyURL = await getDefaultProxyUrl() + export const getDefaultSettings = (): SettingsDef => ({ syncCollections: true, syncHistory: true, @@ -111,7 +114,7 @@ export const getDefaultSettings = (): SettingsDef => ({ CURRENT_INTERCEPTOR_ID: "", // TODO: Interceptor related settings should move under the interceptor systems - PROXY_URL: "https://proxy.hoppscotch.io/", + PROXY_URL: defaultProxyURL, URL_EXCLUDES: { auth: true, httpUser: true, diff --git a/packages/hoppscotch-common/src/platform/infra.ts b/packages/hoppscotch-common/src/platform/infra.ts index b1659ac9..d9591e2f 100644 --- a/packages/hoppscotch-common/src/platform/infra.ts +++ b/packages/hoppscotch-common/src/platform/infra.ts @@ -1,5 +1,11 @@ import * as E from "fp-ts/Either" +type ProxyAppUrl = { + value: string + name: string +} + export type InfraPlatformDef = { getIsSMTPEnabled?: () => Promise> + getProxyAppUrl?: () => Promise> } diff --git a/packages/hoppscotch-common/src/platform/std/interceptors/proxy.ts b/packages/hoppscotch-common/src/platform/std/interceptors/proxy.ts index d9e68072..1ae32a0b 100644 --- a/packages/hoppscotch-common/src/platform/std/interceptors/proxy.ts +++ b/packages/hoppscotch-common/src/platform/std/interceptors/proxy.ts @@ -7,6 +7,7 @@ import axios from "axios" import { settingsStore } from "~/newstore/settings" import { decodeB64StringToArrayBuffer } from "~/helpers/utils/b64" import SettingsProxy from "~/components/settings/Proxy.vue" +import { getDefaultProxyUrl } from "~/helpers/proxyUrl" type ProxyHeaders = { "multipart-part-key"?: string @@ -36,6 +37,8 @@ const getProxyPayload = ( return payload } +const defaultProxyURL = await getDefaultProxyUrl() + async function runRequest( req: AxiosRequestConfig, cancelToken: CancelToken @@ -55,7 +58,7 @@ async function runRequest( try { // TODO: Validation for the proxy result const { data } = await axios.post( - settingsStore.value.PROXY_URL ?? "https://proxy.hoppscotch.io", + settingsStore.value.PROXY_URL ?? defaultProxyURL, payload, { headers,