Просмотр исходного кода

fix: playground request doesn't work for local deployment (#810)

Signed-off-by: shanghaikid <jiangruiyi@gmail.com>
ryjiang 4 месяцев назад
Родитель
Сommit
c24453f2bb

+ 9 - 1
client/src/pages/play/Play.tsx

@@ -50,8 +50,16 @@ const Play: FC = () => {
 
   const extensions = useMemo(() => {
     const { address, token, username, password } = authReq;
+
     const getBaseUrl = () => {
-      return address.startsWith('http') ? address : `http://${address}`;
+      if (!address.startsWith('http')) {
+        if (!/:(\d+)/.test(address)) {
+          return `http://${address}:19530`;
+        }
+        return `http://${address}`;
+      }
+
+      return address;
     };
     return [
       placeholder('Write your code here'),

+ 2 - 4
client/src/pages/play/language/extensions/codelens.ts

@@ -5,9 +5,7 @@ import { EditorView, Decoration, DecorationSet } from '@codemirror/view';
 import { AxiosError } from 'axios';
 import { MILVUS_RESTFUL_DOC_URL, CLOUD_RESTFUL_DOC_URL } from '@/consts';
 import { CustomEventNameEnum, PlaygroundExtensionParams } from '../../Types';
-import { createPlaygroundRequest, DocumentEventManager } from '../../utils';
-
-const apiPlaygroundRequest = createPlaygroundRequest('backend');
+import { playgroundRequest, DocumentEventManager } from '../../utils';
 
 class CodeLensWidget extends WidgetType {
   constructor(
@@ -139,7 +137,7 @@ export const codeLensDecoration = (options: PlaygroundExtensionParams) =>
                     CustomEventNameEnum.PlaygroundResponseDetail,
                     { loading: true, response: 'running' }
                   );
-                  const res = await apiPlaygroundRequest(params);
+                  const res = await playgroundRequest(params);
                   DocumentEventManager.dispatch(
                     CustomEventNameEnum.PlaygroundResponseDetail,
                     { response: res.data, loading: false }

+ 18 - 34
client/src/pages/play/utils/request.ts

@@ -9,38 +9,22 @@ type PlaygroundRequestOptions = {
   body?: Record<string, any>;
 };
 
-function isLocalhost(url: string): boolean {
-  const regex =
-    /^(http:\/\/|https:\/\/)?(localhost|127\.0\.0\.1)(:\d+)?(\/.*)?$/;
-  return regex.test(url);
-}
+export const playgroundRequest = (options: PlaygroundRequestOptions) => {
+  const {
+    url,
+    method = 'POST',
+    host = '',
+    headers = {},
+    body = {},
+    params = {},
+  } = options;
 
-export const createPlaygroundRequest =
-  (type: 'frontend' | 'backend') => (options: PlaygroundRequestOptions) => {
-    const {
-      url,
-      method = 'POST',
-      host = '',
-      headers = {},
-      body = {},
-      params = {},
-    } = options;
-    if (isLocalhost(host) || type === 'frontend') {
-      return axios.request({
-        url,
-        method,
-        headers,
-        baseURL: host,
-        data: body,
-        params,
-      });
-    }
-    return axios.post('/api/v1/playground', {
-      host,
-      url,
-      headers,
-      method,
-      body,
-      params,
-    });
-  };
+  return axios.post('/api/v1/playground', {
+    host,
+    url,
+    headers,
+    method,
+    body,
+    params,
+  });
+};