mirror of
https://github.com/Radiquum/AniX.git
synced 2025-04-29 01:19:41 +05:00
refactor: change API proxy from Serverless Functions to Serverless Middlewares to save Function Invocations on vercel
This commit is contained in:
parent
11343eb7f8
commit
ad1c56f593
9 changed files with 81 additions and 60 deletions
|
@ -1,49 +0,0 @@
|
|||
import { NextResponse, NextRequest } from "next/server";
|
||||
import { fetchDataViaGet, fetchDataViaPost } from "../utils";
|
||||
import { API_URL } from "../config";
|
||||
import { buffer } from "stream/consumers";
|
||||
|
||||
export async function GET(
|
||||
req: NextRequest,
|
||||
{ params }: { params: { endpoint: Array<string> } }
|
||||
) {
|
||||
const { endpoint } = params;
|
||||
let API_V2: boolean | string =
|
||||
req.nextUrl.searchParams.get("API_V2") || false;
|
||||
if (API_V2 === "true") {
|
||||
req.nextUrl.searchParams.delete("API_V2");
|
||||
}
|
||||
const query = req.nextUrl.searchParams.toString();
|
||||
const url = `${API_URL}/${endpoint.join("/")}${query ? `?${query}` : ""}`;
|
||||
|
||||
const response = await fetchDataViaGet(url, API_V2);
|
||||
return NextResponse.json(response);
|
||||
}
|
||||
|
||||
export async function POST(
|
||||
req: NextRequest,
|
||||
{ params }: { params: { endpoint: Array<string> } }
|
||||
) {
|
||||
const { endpoint } = params;
|
||||
let API_V2: boolean | string =
|
||||
req.nextUrl.searchParams.get("API_V2") || false;
|
||||
if (API_V2 === "true") {
|
||||
req.nextUrl.searchParams.delete("API_V2");
|
||||
}
|
||||
const query = req.nextUrl.searchParams.toString();
|
||||
const url = `${API_URL}/${endpoint.join("/")}${query ? `?${query}` : ""}`;
|
||||
let body;
|
||||
const ReqContentTypeHeader = req.headers.get("Content-Type") || "";
|
||||
let ResContentTypeHeader = "";
|
||||
|
||||
if (ReqContentTypeHeader.split(";")[0] == "multipart/form-data") {
|
||||
ResContentTypeHeader = ReqContentTypeHeader;
|
||||
body = await req.arrayBuffer();
|
||||
} else {
|
||||
ResContentTypeHeader = "application/json; charset=UTF-8";
|
||||
body = JSON.stringify(await req.json());
|
||||
}
|
||||
|
||||
const response = await fetchDataViaPost(url, body, API_V2, ResContentTypeHeader);
|
||||
return NextResponse.json(response);
|
||||
}
|
|
@ -1,7 +1,7 @@
|
|||
export const CURRENT_APP_VERSION = "3.1.1";
|
||||
|
||||
export const API_URL = "https://api.anixart.tv";
|
||||
export const API_PREFIX = "/api";
|
||||
export const API_PREFIX = "/api/proxy";
|
||||
export const USER_AGENT =
|
||||
"AnixartApp/8.2.1-23121216 (Android 9; SDK 28; arm64-v8a; samsung SM-G975N; en)";
|
||||
|
||||
|
@ -25,7 +25,6 @@ export const ENDPOINTS = {
|
|||
},
|
||||
collection: {
|
||||
base: `${API_PREFIX}/collection`,
|
||||
list: `${API_PREFIX}/collection/list`,
|
||||
addRelease: `${API_PREFIX}/collectionMy/release/add`,
|
||||
create: `${API_PREFIX}/collectionMy/create`,
|
||||
delete: `${API_PREFIX}/collectionMy/delete`,
|
||||
|
|
|
@ -16,7 +16,7 @@ export const fetchDataViaGet = async (
|
|||
headers: HEADERS,
|
||||
});
|
||||
if (response.status !== 200) {
|
||||
throw new Error("Error fetching data");
|
||||
return null;
|
||||
}
|
||||
const data = await response.json();
|
||||
return data;
|
||||
|
@ -45,7 +45,7 @@ export const fetchDataViaPost = async (
|
|||
body: body,
|
||||
});
|
||||
if (response.status !== 200) {
|
||||
throw new Error("Error fetching data");
|
||||
return null;
|
||||
}
|
||||
const data = await response.json();
|
||||
return data;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue