Open-Source API Development Ecosystem • https://hoppscotch.io • Offline, On-Prem & Cloud • Web, Desktop & CLI • Open-Source Alternative to Postman, Insomnia
TypeScript apiapi-clientapi-restapi-testingdeveloper-toolsgraphqlhttphttp-clientpwarestrest-apispatestingtesting-toolstoolsvuevuejswebsocket
chore: remove top level awaits (#4778)
Nivedin23633a99a14b
diff --git a/packages/hoppscotch-common/src/components/settings/Proxy.vue b/packages/hoppscotch-common/src/components/settings/Proxy.vueindex f1ee95bf4..a0de5a4cf 100644--- a/packages/hoppscotch-common/src/components/settings/Proxy.vue+++ b/packages/hoppscotch-common/src/components/settings/Proxy.vue@@ -56,12 +56,14 @@ const currentUser = useReadonlyStream(platform.auth.getCurrentUser())-watch(currentUser, async () => {- if (!currentUser.value) {- PROXY_URL.value = await getDefaultProxyUrl()+watch(+ () => currentUser.value,+ async () => {+ if (!currentUser.value) {+ PROXY_URL.value = await getDefaultProxyUrl()+ }}-})-+)const proxyEnabled = computed(() =>interceptorService.currentInterceptorID.value ===diff --git a/packages/hoppscotch-common/src/helpers/strategies/AxiosStrategy.ts b/packages/hoppscotch-common/src/helpers/strategies/AxiosStrategy.tsdeleted file mode 100644index 18bf22f3e..000000000--- a/packages/hoppscotch-common/src/helpers/strategies/AxiosStrategy.ts+++ /dev/null@@ -1,166 +0,0 @@-import axios, { AxiosRequestConfig } from "axios"-import { v4 } from "uuid"-import { pipe } from "fp-ts/function"-import * as TE from "fp-ts/TaskEither"-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()--type ProxyHeaders = {- "multipart-part-key"?: string-}--type ProxyPayloadType = FormData | (AxiosRequestConfig & { wantsBinary: true })--export const cancelRunningAxiosRequest = () => {- cancelSource.cancel()-- // Create a new cancel token- cancelSource = axios.CancelToken.source()-}--const getProxyPayload = (- req: AxiosRequestConfig,- multipartKey: string | null-) => {- let payload: ProxyPayloadType = {- ...req,- wantsBinary: true,- accessToken: import.meta.env.VITE_PROXYSCOTCH_ACCESS_TOKEN ?? "",- }-- if (payload.data instanceof FormData) {- const formData = payload.data- payload.data = ""- formData.append(multipartKey!, JSON.stringify(payload))- payload = formData- }-- return payload-}--const defaultProxyURL = await getDefaultProxyUrl()--const preProcessRequest = (req: AxiosRequestConfig): AxiosRequestConfig => {- const reqClone = cloneDeep(req)-- // If the parameters are URLSearchParams, inject them to URL instead- // This prevents issues of marshalling the URLSearchParams to the proxy- if (reqClone.params instanceof URLSearchParams) {- try {- const url = new URL(reqClone.url ?? "")-- for (const [key, value] of reqClone.params.entries()) {- url.searchParams.append(key, value)- }-- reqClone.url = url.toString()- } catch (e) {- // making this a non-empty block, so we can make the linter happy.- // we should probably use, allowEmptyCatch, or take the time to do something with the caught errors :)- }-- reqClone.params = {}- }-- return reqClone-}--const axiosWithProxy: NetworkStrategy = (req) =>- pipe(- TE.Do,-- TE.bind("processedReq", () => TE.of(preProcessRequest(req))),-- // If the request has FormData, the proxy needs a key- TE.bind("multipartKey", ({ processedReq }) =>- TE.of(- processedReq.data instanceof FormData- ? `proxyRequestData-${v4()}`- : null- )- ),-- // Build headers to send- TE.bind("headers", ({ processedReq, multipartKey }) =>- TE.of(- processedReq.data instanceof FormData- ? <ProxyHeaders>{- "multipart-part-key": multipartKey,- }- : <ProxyHeaders>{}- )- ),-- // Create payload- TE.bind("payload", ({ processedReq, multipartKey }) =>- TE.of(getProxyPayload(processedReq, multipartKey))- ),-- // Run the proxy request- TE.chain(({ payload, headers }) =>- TE.tryCatch(- () =>- axios.post(- settingsStore.value.PROXY_URL || defaultProxyURL,- payload,- {- headers,- cancelToken: cancelSource.token,- }- ),- (reason) =>- axios.isCancel(reason)- ? "cancellation" // Convert cancellation errors into cancellation strings- : reason- )- ),-- // Check success predicate- TE.chain(- TE.fromPredicate(- ({ data }) => data.success,- ({ data }) => data.data.message || "Proxy Error"- )- ),-- // Process Base64- TE.chain(({ data }) => {- if (data.isBinary) {- data.data = decodeB64StringToArrayBuffer(data.data)- }-- return TE.of(data)- })- )--const axiosWithoutProxy: NetworkStrategy = (req) =>- pipe(- TE.tryCatch(- () =>- axios({- ...req,- cancelToken: (cancelSource && cancelSource.token) || "",- responseType: "arraybuffer",- }),- (e) => (axios.isCancel(e) ? "cancellation" : (e as any))- ),-- TE.orElse((e) =>- e !== "cancellation" && e.response- ? TE.right(e.response as NetworkResponse)- : TE.left(e)- )- )--const axiosStrategy: NetworkStrategy = (req) =>- pipe(- req,- settingsStore.value.PROXY_ENABLED ? axiosWithProxy : axiosWithoutProxy- )--export default axiosStrategydiff --git a/packages/hoppscotch-common/src/newstore/settings.ts b/packages/hoppscotch-common/src/newstore/settings.tsindex f45c76876..3028af6c8 100644--- a/packages/hoppscotch-common/src/newstore/settings.ts+++ b/packages/hoppscotch-common/src/newstore/settings.ts@@ -3,7 +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"+import { DEFAULT_HOPP_PROXY_URL, getDefaultProxyUrl } from "~/helpers/proxyUrl"export const HoppBgColors = ["system", "light", "dark", "black"] as const@@ -83,59 +83,65 @@ export type SettingsDef = {CUSTOM_NAMING_STYLE: string}-const defaultProxyURL = await getDefaultProxyUrl()+let defaultProxyURL = DEFAULT_HOPP_PROXY_URL-export const getDefaultSettings = (): SettingsDef => ({- syncCollections: true,- syncHistory: true,- syncEnvironments: true,-- WRAP_LINES: {- httpRequestBody: true,- httpResponseBody: true,- httpHeaders: true,- httpParams: true,- httpUrlEncoded: true,- httpPreRequest: true,- httpTest: true,- httpRequestVariables: true,- graphqlQuery: true,- graphqlResponseBody: true,- graphqlHeaders: false,- graphqlVariables: false,- graphqlSchema: true,- importCurl: true,- codeGen: true,- cookie: true,- multipartFormdata: true,- },+getDefaultProxyUrl().then((url) => {+ defaultProxyURL = url+})- // Set empty because interceptor module will set the default value- CURRENT_INTERCEPTOR_ID: "",+export const getDefaultSettings = (): SettingsDef => {+ return {+ syncCollections: true,+ syncHistory: true,+ syncEnvironments: true,++ WRAP_LINES: {+ httpRequestBody: true,+ httpResponseBody: true,+ httpHeaders: true,+ httpParams: true,+ httpUrlEncoded: true,+ httpPreRequest: true,+ httpTest: true,+ httpRequestVariables: true,+ graphqlQuery: true,+ graphqlResponseBody: true,+ graphqlHeaders: false,+ graphqlVariables: false,+ graphqlSchema: true,+ importCurl: true,+ codeGen: true,+ cookie: true,+ multipartFormdata: true,+ },- // TODO: Interceptor related settings should move under the interceptor systems- PROXY_URL: defaultProxyURL,- URL_EXCLUDES: {- auth: true,- httpUser: true,- httpPassword: true,- bearerToken: true,- oauth2Token: true,- },- THEME_COLOR: "indigo",- BG_COLOR: "system",- ENCODE_MODE: "enable",- TELEMETRY_ENABLED: true,- EXPAND_NAVIGATION: false,- SIDEBAR: true,- SIDEBAR_ON_LEFT: false,- COLUMN_LAYOUT: true,-- HAS_OPENED_SPOTLIGHT: false,- ENABLE_AI_EXPERIMENTS: true,- AI_REQUEST_NAMING_STYLE: "DESCRIPTIVE_WITH_SPACES",- CUSTOM_NAMING_STYLE: "",-})+ // Set empty because interceptor module will set the default value+ CURRENT_INTERCEPTOR_ID: "",++ // TODO: Interceptor related settings should move under the interceptor systems+ PROXY_URL: defaultProxyURL,+ URL_EXCLUDES: {+ auth: true,+ httpUser: true,+ httpPassword: true,+ bearerToken: true,+ oauth2Token: true,+ },+ THEME_COLOR: "indigo",+ BG_COLOR: "system",+ ENCODE_MODE: "enable",+ TELEMETRY_ENABLED: true,+ EXPAND_NAVIGATION: false,+ SIDEBAR: true,+ SIDEBAR_ON_LEFT: false,+ COLUMN_LAYOUT: true,++ HAS_OPENED_SPOTLIGHT: false,+ ENABLE_AI_EXPERIMENTS: true,+ AI_REQUEST_NAMING_STYLE: "DESCRIPTIVE_WITH_SPACES",+ CUSTOM_NAMING_STYLE: "",+ }+}type ApplySettingPayload = {[K in keyof SettingsDef]: {diff --git a/packages/hoppscotch-common/src/pages/r/_id.vue b/packages/hoppscotch-common/src/pages/r/_id.vueindex aae2f7953..dae9570d8 100644--- a/packages/hoppscotch-common/src/pages/r/_id.vue+++ b/packages/hoppscotch-common/src/pages/r/_id.vue@@ -139,7 +139,6 @@ const addRequestToTab = () => {type: "request",request: safelyExtractRESTRequest(request, getDefaultRESTRequest()),isDirty: false,- type: "request",})router.push({ path: "/" })diff --git a/packages/hoppscotch-common/src/pages/settings.vue b/packages/hoppscotch-common/src/pages/settings.vueindex 933322e74..5b88e71ef 100644--- a/packages/hoppscotch-common/src/pages/settings.vue+++ b/packages/hoppscotch-common/src/pages/settings.vue@@ -248,8 +248,8 @@</template><script setup lang="ts">-import { ref, computed, watch } from "vue"-import { applySetting, toggleSetting } from "~/newstore/settings"+import { ref, computed } from "vue"+import { toggleSetting } from "~/newstore/settings"import { useSetting } from "@composables/settings"import { useI18n } from "@composables/i18n"import { useColorMode } from "@composables/theming"@@ -285,7 +285,6 @@ const interceptorsWithSettings = computed(() =>)const ACCENT_COLOR = useSetting("THEME_COLOR")-const PROXY_URL = useSetting("PROXY_URL")const TELEMETRY_ENABLED = useSetting("TELEMETRY_ENABLED")const EXPAND_NAVIGATION = useSetting("EXPAND_NAVIGATION")const SIDEBAR_ON_LEFT = useSetting("SIDEBAR_ON_LEFT")@@ -326,21 +325,9 @@ const hasPlatformTelemetry = Boolean(platform.platformFeatureFlags.hasTelemetry)const confirmRemove = ref(false)-const proxySettings = computed(() => ({- url: PROXY_URL.value,-}))-const hasAIExperimentsSupport =!!platform.experiments?.aiExperiments?.enableAIExperiments-watch(- proxySettings,- ({ url }) => {- applySetting("PROXY_URL", url)- },- { deep: true }-)-const showConfirmModal = () => {if (TELEMETRY_ENABLED.value) confirmRemove.value = trueelse toggleSetting("TELEMETRY_ENABLED")diff --git a/packages/hoppscotch-common/src/platform/std/interceptors/proxy.ts b/packages/hoppscotch-common/src/platform/std/interceptors/proxy.tsindex 1ae32a0b9..ed8f52187 100644--- a/packages/hoppscotch-common/src/platform/std/interceptors/proxy.ts+++ b/packages/hoppscotch-common/src/platform/std/interceptors/proxy.ts@@ -37,12 +37,12 @@ const getProxyPayload = (return payload}-const defaultProxyURL = await getDefaultProxyUrl()-async function runRequest(req: AxiosRequestConfig,cancelToken: CancelToken): RequestRunResult["response"] {+ const defaultProxyURL = await getDefaultProxyUrl()+const multipartKey =req.data instanceof FormData ? `proxyRequestData-${v4()}` : null