Skip to content

Commit 16346c2

Browse files
committed
✨ feat: node 转发请求,决绝跨域
1 parent 71c9deb commit 16346c2

10 files changed

Lines changed: 58 additions & 17 deletions

File tree

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
{
9191
"id": 137,
9292
"code": "mechanisms",
93-
"name": "居有屋_外部机构",
93+
"name": "外部机构",
9494
"internalType": 0,
9595
"needArea": 0,
9696
"assetTypes": [],
@@ -223,7 +223,7 @@ routes 目录下创建新的 mock 文件
223223

224224
配置表单
225225

226-
<p align="center"><img style="height:500px" src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/5b24b7d804ac4db1a3673fd2aa4aa4cd~tplv-k3u1fbpfcp-zoom-1.image"/></p>
226+
<p align="center"><a href="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/5b24b7d804ac4db1a3673fd2aa4aa4cd~tplv-k3u1fbpfcp-zoom-1.image"><img style="height:500px;max-height:500px" src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/5b24b7d804ac4db1a3673fd2aa4aa4cd~tplv-k3u1fbpfcp-zoom-1.image"/></a></p>
227227

228228
生成代码
229229

@@ -335,12 +335,15 @@ routes 目录下创建新的 mock 文件
335335
### 表单
336336

337337
选择表单区块,配置 Schema 表单
338+
338339
![](https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/5a0c793a67924583a4cb83569483687c~tplv-k3u1fbpfcp-zoom-1.image)
339340

340341
## 自定义区块
341342

342343
创建新的区块
344+
343345
![](https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/48f22790459d49f7b708a7684f9d2321~tplv-k3u1fbpfcp-zoom-1.image)
346+
344347
![](https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/ca76e8e939334df48d7bd3512dadf40a~tplv-k3u1fbpfcp-zoom-1.image)
345348

346349
配置 Schema 表单:复制旧区块的 schema.json 内容覆盖到当前区块的 schema.json 文件内。将 schema 字段内容导入网页 https://xrender.fun/~demos/docs-generator-demo-0 中重新配置。

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"displayName": "lowcode",
44
"description": "低代码工具",
55
"author": "wjkang <ruoxieme@gmail.com>",
6-
"version": "1.4.0",
6+
"version": "1.4.1",
77
"icon": "asset/icon.png",
88
"publisher": "wjkang",
99
"repository": "https://github.com/lowcoding/lowcode-vscode",

src/utils/request.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import axios from 'axios';
1+
import axios, { AxiosRequestConfig } from 'axios';
2+
import common from 'mocha/lib/interfaces/common';
23

34
const https = require('https');
45

src/webview/controllers/request.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import axios, { AxiosRequestConfig } from 'axios';
2+
import { IMessage } from '../type';
3+
4+
export const axiosRequest = async (
5+
message: IMessage<{
6+
config: AxiosRequestConfig;
7+
}>,
8+
) => {
9+
const res = await axios.request(message.data.config);
10+
return res.data;
11+
};

src/webview/index.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,20 @@ export const showWebView = (options: {
114114
setWebviewHtml(panel);
115115
const disposables: vscode.Disposable[] = [];
116116
panel.webview.onDidReceiveMessage(
117-
async (message: { cmd: string; cbid: string; data: any }) => {
117+
async (message: {
118+
cmd: string;
119+
cbid: string;
120+
data: any;
121+
skipError?: boolean;
122+
}) => {
118123
if (routes[message.cmd]) {
119124
try {
120125
const res = await routes[message.cmd](message);
121126
invokeCallback(panel, message.cbid, res);
122127
} catch (ex: any) {
123-
window.showErrorMessage(ex.toString());
128+
if (!message.skipError) {
129+
window.showErrorMessage(ex.toString());
130+
}
124131
invokeErrorCallback(panel, message.cbid, ex);
125132
}
126133
} else {

src/webview/routes/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import * as block from '../controllers/block';
1010
import * as json from '../controllers/json';
1111
import * as config from '../controllers/config';
1212
import * as intelliSense from '../controllers/intelliSense';
13+
import * as reqeust from '../controllers/request';
1314

1415
export const routes: Record<string, any> = {
1516
alert: alert.alert,
@@ -46,4 +47,5 @@ export const routes: Record<string, any> = {
4647
savePluginConfig: config.savePluginConfig,
4748

4849
refreshIntelliSense: intelliSense.refreshIntelliSense,
50+
request: reqeust.axiosRequest,
4951
};

webview-react/src/hooks/useCheckVankeInternal.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useMount } from 'ahooks';
2-
import { request } from 'umi';
2+
import { nodeRequest } from '@/webview/service';
33
import { useState } from './useImmer';
44

55
const useCheckVankeInternal = () => {
@@ -8,7 +8,7 @@ const useCheckVankeInternal = () => {
88
);
99

1010
useMount(() => {
11-
request('https://yapi.onewo.com', { skipErrorHandler: true })
11+
nodeRequest({ url: 'https://yapi.onewo.com' })
1212
.then(() => {
1313
setIsVankeInternal(true);
1414
})

webview-react/src/pages/scaffold/List/useController.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ const useController = () => {
2828
s.fetch = true;
2929
});
3030
const promises: ReturnType<typeof getScaffolds>[] = [
31-
getScaffolds('https://gitee.com/lowcoding/scaffold/raw/master/index.json'),
31+
getScaffolds(
32+
'https://gitee.com/lowcoding/scaffold/raw/master/index.json',
33+
),
3234
];
3335
if (isVankeInternal) {
3436
promises.push(

webview-react/src/webview/index.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ if (process.env.NODE_ENV !== 'production') {
1313
message: 'call vscode',
1414
description: `cmd: ${message.cmd}`,
1515
});
16-
(callbacks[message.cbid] || function() {})(
16+
(callbacks[message.cbid] || function () {})(
1717
require(`./mock/${message.cmd}`).default,
1818
);
1919
}, 1000);
2020
},
2121
};
2222
}
2323
export function callVscode(
24-
data: { cmd: string; data?: any },
24+
data: { cmd: string; data?: any; skipError?: boolean },
2525
cb?: (data: any) => void,
2626
errorCb?: (data: any) => void,
2727
) {
@@ -40,10 +40,10 @@ export function callVscode(
4040
}
4141
}
4242

43-
export function callVscodePromise(cmd: string, data: any) {
43+
export function callVscodePromise(cmd: string, data: any, skipError?: boolean) {
4444
return new Promise((resolve, reject) => {
4545
callVscode(
46-
{ cmd, data },
46+
{ cmd, data, skipError },
4747
(res) => {
4848
resolve(res);
4949
},
@@ -54,10 +54,14 @@ export function callVscodePromise(cmd: string, data: any) {
5454
});
5555
}
5656

57-
export function request<T = unknown>(params: { cmd: string; data?: any }) {
57+
export function request<T = unknown>(params: {
58+
cmd: string;
59+
data?: any;
60+
skipError?: boolean;
61+
}) {
5862
return new Promise<T>((resolve, reject) => {
5963
callVscode(
60-
{ cmd: params.cmd, data: params.data },
64+
{ cmd: params.cmd, data: params.data, skipError: params.skipError },
6165
(res) => {
6266
resolve(res);
6367
},
@@ -74,9 +78,9 @@ window.addEventListener('message', (event) => {
7478
// 来自vscode的回调
7579
case 'vscodeCallback':
7680
if (message.code === 200) {
77-
(callbacks[message.cbid] || function() {})(message.data);
81+
(callbacks[message.cbid] || function () {})(message.data);
7882
} else {
79-
(errorCallbacks[message.cbid] || function() {})(message.data);
83+
(errorCallbacks[message.cbid] || function () {})(message.data);
8084
}
8185
delete callbacks[message.cbid]; // 执行完回调删除
8286
delete errorCallbacks[message.cbid]; // 执行完回调删除

webview-react/src/webview/service.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { AxiosRequestConfig } from 'axios';
12
import { request } from './index';
23

34
export interface IGetLocalMaterialsResult {
@@ -461,3 +462,13 @@ export function executeVscodeCommand(data: { command: string }) {
461462
data,
462463
});
463464
}
465+
466+
export function nodeRequest<IResult = unknown>(config: AxiosRequestConfig) {
467+
return request<IResult>({
468+
cmd: 'request',
469+
skipError: true,
470+
data: {
471+
config,
472+
},
473+
});
474+
}

0 commit comments

Comments
 (0)