Skip to content

Commit 36d5864

Browse files
committed
feat: 新增fetch代理
1 parent d201b86 commit 36d5864

File tree

3 files changed

+144
-7
lines changed

3 files changed

+144
-7
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next
22

33
## Endpoint
44

5-
[/api/xmlrpc](https://api.terwer.space/api/xmlrpc)
5+
[/api/middleware](https://api.terwer.space/api/middleware)
6+
7+
- [/api/middleware/xmlrpc](https://api.terwer.space/api/middleware/xmlrpc)
8+
9+
- [/api/middleware/fetch](https://api.terwer.space/api/middleware/fetch)
610

711
[/api/translate?q=](https://api.terwer.space/api/translate?q=)
812

@@ -13,6 +17,7 @@ This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next
1317
### 2023-01-11
1418

1519
1、新增xmlrpc代理
20+
2、新增fetch代理
1621

1722
### 2022-07-27
1823

pages/api/middleware/fetch.ts

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
import Cors from "cors"
2+
import initMiddleware from "../../../utils/cors/init-middleware"
3+
import { NextApiRequest, NextApiResponse } from "next"
4+
5+
// Initialize the cors middleware
6+
const cors = initMiddleware(
7+
// You can read more about the available options here: https://github.com/expressjs/cors#configuration-options
8+
Cors({
9+
// Only allow requests with GET, POST and OPTIONS
10+
methods: ["GET", "POST", "OPTIONS"],
11+
})
12+
)
13+
14+
export default async function handler(
15+
req: NextApiRequest,
16+
res: NextApiResponse
17+
) {
18+
// Run cors
19+
await cors(req, res)
20+
21+
// const headers = req.headers;
22+
// logUtil.logInfo(headers)
23+
const body = req.body
24+
// logUtil.logInfo(body)
25+
26+
// 获取代理参数
27+
// console.log("body.fetchParams.apiUrl=>")
28+
// console.log(body.fetchParams.apiUrl)
29+
// console.log("body.fetchParams.fetchOptions=>")
30+
// console.log(body.fetchParams.fetchOptions)
31+
// console.log("body.fetchParams.formJson=>")
32+
// console.log(body.fetchParams.formJson)
33+
34+
// =====================================
35+
// =====================================
36+
// 发送真实请求并获取结果
37+
// console.log("开始发送真实请求并获取结果")
38+
39+
const fetchCORSApiUrl = body.fetchParams.apiUrl
40+
const fetchCORSOptions = body.fetchParams.fetchOptions
41+
const formJson = body.fetchParams.formJson
42+
43+
// 如果是form请求,进行转换
44+
if (formJson) {
45+
// 将formJson转换为formData
46+
const form = new URLSearchParams()
47+
formJson.forEach((item: any) => {
48+
form.append(item.key, item.value)
49+
})
50+
fetchCORSOptions.body = form
51+
}
52+
53+
let err
54+
console.log("fetchCORS.apiUrl=>")
55+
console.log(fetchCORSApiUrl)
56+
console.log("fetchCORS.fetchOptions=>")
57+
console.log(fetchCORSOptions)
58+
59+
fetch(fetchCORSApiUrl, fetchCORSOptions)
60+
.then((response) => {
61+
try {
62+
response.text().then((resText) => {
63+
// console.log("请求完成,准备返回真实结果")
64+
let resJson = {}
65+
try {
66+
resJson = JSON.parse(resText)
67+
} catch (e) {
68+
console.error(e)
69+
}
70+
// console.log(resJson)
71+
72+
const finalRes = {
73+
headers: {
74+
status: response.status,
75+
statusText: response.statusText,
76+
},
77+
body: resJson,
78+
}
79+
console.log(finalRes)
80+
console.log("请求处理已成功")
81+
writeStatusData(res, finalRes, response.status)
82+
})
83+
} catch (e) {
84+
err = e
85+
writeStatusError(res, err, response.status)
86+
console.log("请求处理异常")
87+
console.error(e)
88+
}
89+
})
90+
.catch((reason) => {
91+
// console.log("methodPromise catch=>")
92+
console.log("请求处理失败")
93+
console.error("fetch middleware error=>", reason)
94+
writeError(res, reason)
95+
})
96+
// ========================================
97+
// ========================================
98+
}
99+
100+
/**
101+
* 输出数据
102+
* @param res
103+
* @param data
104+
*/
105+
// function writeData(res: NextApiResponse, data: any) {
106+
// writeStatusData(res, data, 200)
107+
// }
108+
109+
/**
110+
* 输出数据
111+
* @param res
112+
* @param data
113+
* @param status
114+
*/
115+
function writeStatusData(res: NextApiResponse, data: any, status: number) {
116+
// Rest of the API logic
117+
res.status(status)
118+
res.json(data)
119+
}
120+
121+
/**
122+
* 输出错误信息
123+
* @param res
124+
* @param err
125+
*/
126+
function writeError(res: NextApiResponse, err: any) {
127+
writeStatusError(res, err, 500)
128+
}
129+
130+
function writeStatusError(res: NextApiResponse, err: any, status: number) {
131+
const errorJson = JSON.stringify(err)
132+
res.status(status)
133+
res.json(errorJson)
134+
}
Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Cors from "cors"
2-
import initMiddleware from "../../utils/cors/init-middleware"
2+
import initMiddleware from "../../../utils/cors/init-middleware"
33
import { SimpleXmlRpcClient } from "simple-xmlrpc"
44
import { NextApiRequest, NextApiResponse } from "next"
55

@@ -19,14 +19,12 @@ export default async function handler(
1919
// Run cors
2020
await cors(req, res)
2121

22-
let result
23-
24-
const headers = req.headers
25-
console.log(headers)
22+
// const headers = req.headers
23+
// console.log(headers)
2624
const body = req.body
2725

2826
// 获取代理参数
29-
console.log("body=>", body)
27+
// console.log("body=>", body)
3028

3129
// =====================================
3230
// =====================================

0 commit comments

Comments
 (0)