|
| 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 | +} |
0 commit comments