Skip to content

Commit fb1d685

Browse files
committed
feat: 新增xmlrpc代理
1 parent ea8bfe8 commit fb1d685

File tree

6 files changed

+237
-31
lines changed

6 files changed

+237
-31
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next
1010

1111
## 最近更新
1212

13+
### 2023-01-11
14+
15+
1、新增xmlrpc代理
16+
1317
### 2022-07-27
1418

1519
1、新增中文分词服务

next.config.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,24 @@ console.log("react path=>", moduleDir("react"))
1717
**/
1818
const nextConfig: NextConfig = {
1919
reactStrictMode: true,
20+
webpack: (config, options) => {
21+
config.module.rules.push({
22+
test: /\.(ts)x?$/, // Just `tsx?` file only
23+
use: [
24+
// options.defaultLoaders.babel, I don't think it's necessary to have this loader too
25+
{
26+
loader: "ts-loader",
27+
options: {
28+
transpileOnly: true,
29+
experimentalWatchApi: true,
30+
onlyCompileBundledFiles: true,
31+
},
32+
},
33+
],
34+
})
35+
36+
return config
37+
},
2038
}
2139

2240
module.exports = nextConfig

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"nodejieba": "^2.6.0",
1818
"react": "18.2.0",
1919
"react-dom": "18.2.0",
20+
"simple-xmlrpc": "^1.1.0",
2021
"typescript": "4.9.4"
2122
},
2223
"devDependencies": {
@@ -27,6 +28,7 @@
2728
"esbuild": "^0.16.16",
2829
"eslint": "8.31.0",
2930
"eslint-config-next": "13.1.1",
30-
"prettier": "^2.8.2"
31+
"prettier": "^2.8.2",
32+
"ts-loader": "^9.4.2"
3133
}
3234
}

pages/api/middleware.ts

Lines changed: 0 additions & 29 deletions
This file was deleted.

pages/api/xmlrpc.ts

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import Cors from "cors"
2+
import initMiddleware from "../../utils/cors/init-middleware"
3+
import { SimpleXmlRpcClient } from "simple-xmlrpc"
4+
import { NextApiRequest, NextApiResponse } from "next"
5+
6+
// Initialize the cors middleware
7+
const cors = initMiddleware(
8+
// You can read more about the available options here: https://github.com/expressjs/cors#configuration-options
9+
Cors({
10+
// Only allow requests with GET, POST and OPTIONS
11+
methods: ["GET", "POST", "OPTIONS"],
12+
})
13+
)
14+
15+
export default async function handler(
16+
req: NextApiRequest,
17+
res: NextApiResponse
18+
) {
19+
// Run cors
20+
await cors(req, res)
21+
22+
let result
23+
24+
const headers = req.headers
25+
console.log(headers)
26+
const body = req.body
27+
28+
// 获取代理参数
29+
console.log("body=>", body)
30+
31+
// =====================================
32+
// =====================================
33+
// 发送真实请求并获取结果
34+
console.log("开始发送真实请求并获取结果")
35+
const xmlrpcApiUrl = body.fetchParams.apiUrl
36+
const xmlrpcCORSParams = body.fetchParams.fetchCORSParams
37+
38+
let err
39+
try {
40+
// console.log("xmlrpcCORSParams.reqMethod=>")
41+
// console.log(xmlrpcCORSParams.reqMethod)
42+
// console.log("xmlrpcCORSParams.reqParams=>")
43+
// console.log(xmlrpcCORSParams.reqParams)
44+
45+
const client = new SimpleXmlRpcClient(xmlrpcApiUrl)
46+
47+
const methodPromise = client.methodCall(
48+
xmlrpcCORSParams.reqMethod,
49+
xmlrpcCORSParams.reqParams
50+
)
51+
methodPromise
52+
.then((resolve) => {
53+
// console.log("methodPromise resolve=>")
54+
// console.log(resolve)
55+
56+
writeData(res, resolve)
57+
console.log("请求处理已成功")
58+
})
59+
.catch((reason) => {
60+
console.log("methodPromise catch=>")
61+
console.error("xmlrpc middleware error", reason)
62+
writeError(res, reason)
63+
console.log("请求处理失败")
64+
})
65+
} catch (e) {
66+
err = e
67+
console.error(e)
68+
writeError(res, err)
69+
console.log("请求处理异常")
70+
}
71+
// ========================================
72+
// ========================================
73+
}
74+
75+
/**
76+
* 输出数据
77+
* @param res
78+
* @param data
79+
*/
80+
function writeData(res: NextApiResponse, data: any) {
81+
writeStatusData(res, data, 200)
82+
}
83+
84+
function writeStatusData(res: NextApiResponse, data: any, status: number) {
85+
// Rest of the API logic
86+
res.status(status)
87+
res.json(data)
88+
}
89+
90+
/**
91+
* 输出错误信息
92+
* @param res
93+
* @param err
94+
*/
95+
function writeError(res: NextApiResponse, err: any) {
96+
writeStatusError(res, err, 500)
97+
}
98+
99+
function writeStatusError(res: NextApiResponse, err: any, status: number) {
100+
const errorJson = JSON.stringify(err)
101+
res.status(status)
102+
res.json(errorJson)
103+
}

0 commit comments

Comments
 (0)