refactor: move login from function to middleware

This commit is contained in:
Kentai Radiquum 2025-03-22 01:15:48 +05:00
parent 19dbd69fd5
commit 0bf00b11e5
Signed by: Radiquum
GPG key ID: 858E8EE696525EED
5 changed files with 96 additions and 92 deletions

View file

@ -49,26 +49,33 @@ export default async function middleware(
const path = url.pathname.match(/\/api\/proxy\/(.*)/)?.[1] + url.search;
const ReqContentTypeHeader = request.headers.get("Content-Type") || "";
const ReqSignHeader = request.headers.get("Sign") || null;
let ResContentTypeHeader = "";
let body = null;
if (ReqContentTypeHeader.split(";")[0] == "multipart/form-data") {
ResContentTypeHeader = ReqContentTypeHeader;
body = await request.arrayBuffer();
} else if (ReqContentTypeHeader == "application/x-www-form-urlencoded") {
ResContentTypeHeader = ReqContentTypeHeader;
} else {
ResContentTypeHeader = "application/json; charset=UTF-8";
body = JSON.stringify(await request.json());
}
const data = await fetchDataViaPost(
let resHeaders = {};
resHeaders["Content-Type"] = ResContentTypeHeader;
ReqSignHeader && (resHeaders["Sign"] = ReqSignHeader);
const { data, error } = await fetchDataViaPost(
`${API_URL}/${path}`,
body,
isApiV2,
ResContentTypeHeader
resHeaders
);
if (!data) {
return new Response(JSON.stringify({ message: "Error Fetching Data" }), {
if (error) {
return new Response(JSON.stringify(error), {
status: 500,
headers: {
"Content-Type": "application/json",