diff --git a/packages/arex-request/package.json b/packages/arex-request/package.json index be17d18e..4889aef3 100644 --- a/packages/arex-request/package.json +++ b/packages/arex-request/package.json @@ -1,6 +1,6 @@ { "name": "@arextest/arex-request", - "version": "0.3.10", + "version": "0.3.14", "type": "module", "main": "dist/arex-request.js", "module": "dist/arex-request.js", diff --git a/packages/arex-request/src/components/Request/index.tsx b/packages/arex-request/src/components/Request/index.tsx index bf3cf020..14172ffc 100644 --- a/packages/arex-request/src/components/Request/index.tsx +++ b/packages/arex-request/src/components/Request/index.tsx @@ -4,7 +4,6 @@ import { Button, Checkbox, Select } from 'antd'; import React, { FC, useState } from 'react'; import { useTranslation } from 'react-i18next'; -import { isClient } from '../../constant'; import { sendRequest } from '../../helpers'; import { useArexRequestProps, useArexRequestStore } from '../../hooks'; import { ArexEnvironment, ArexRESTRequest, ArexRESTResponse } from '../../types'; @@ -38,7 +37,7 @@ export type RequestProps = { const Request: FC = () => { const { onBeforeRequest = (request: ArexRESTRequest) => request, onRequest } = useArexRequestProps(); - const { store, dispatch } = useArexRequestStore(); + const { store, dispatch, request } = useArexRequestStore(); const { t } = useTranslation(); const [timeoutId, setTimeoutId] = useState(); @@ -57,35 +56,7 @@ const Request: FC = () => { return; } - const ready = isClient || window.__AREX_EXTENSION_INSTALLED__; - dispatch((state) => { - state.response = { - type: ready ? 'loading' : 'EXTENSION_NOT_INSTALLED', - headers: undefined, - }; - }); - - if (!ready) return; - - sendRequest(onBeforeRequest(store.request), store.environment) - .then((res) => { - onRequest?.(null, { request: store.request, environment: store.environment }, res); - dispatch((state) => { - state.response = res.response; - state.consoles = res.consoles; - state.visualizer = res.visualizer; - state.testResult = res.testResult; - }); - }) - .catch((err) => { - onRequest?.(err, { request: store.request, environment: store.environment }, null); - dispatch((state) => { - state.response = { - type: err.code, - error: err, - }; - }); - }); + request(); }; return ( diff --git a/packages/arex-request/src/constant.ts b/packages/arex-request/src/constant.ts index 2b1a56db..d1ff1824 100644 --- a/packages/arex-request/src/constant.ts +++ b/packages/arex-request/src/constant.ts @@ -4,6 +4,6 @@ export const AREX_EXTENSION_CHROME_STORE_UEL = 'https://chromewebstore.google.com/detail/arex-chrome-extension/jmmficadjneeekafmnheppeoehlgjdjj'; // electron client -export const isClient = import.meta.env.MODE === 'electron'; -export const isClientDev = isClient && import.meta.env.DEV; -export const isClientProd = isClient && import.meta.env.PROD; +export const isClient = () => !!window.electron; +export const isClientDev = isClient() && import.meta.env.DEV; +export const isClientProd = isClient() && import.meta.env.PROD; diff --git a/packages/arex-request/src/providers/RequestStoreProvider.tsx b/packages/arex-request/src/providers/RequestStoreProvider.tsx index a45adc7e..d33236c6 100644 --- a/packages/arex-request/src/providers/RequestStoreProvider.tsx +++ b/packages/arex-request/src/providers/RequestStoreProvider.tsx @@ -90,13 +90,14 @@ const RequestStoreProvider: FC = (props) => { }, [environmentProps]); const request = async (): Promise => { + const ready = isClient() || window.__AREX_EXTENSION_INSTALLED__; + dispatch((state) => { state.response = { - type: window.__AREX_EXTENSION_INSTALLED__ ? 'loading' : 'EXTENSION_NOT_INSTALLED', + type: ready ? 'loading' : 'EXTENSION_NOT_INSTALLED', headers: undefined, }; }); - const ready = isClient || window.__AREX_EXTENSION_INSTALLED__; if (!ready) return; diff --git a/packages/arex-request/src/vite-env.d.ts b/packages/arex-request/src/vite-env.d.ts index fc3dea67..2f6a9526 100644 --- a/packages/arex-request/src/vite-env.d.ts +++ b/packages/arex-request/src/vite-env.d.ts @@ -6,5 +6,6 @@ declare global { message: MessageInstance; __AREX_EXTENSION_INSTALLED__: boolean; // 是否安装了arex-chrome-extension __AREX_EXTENSION_VERSION__: string; // arex-chrome-extension 最新版本号 + electron?: any; } }