|
| 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