mirror of
https://github.com/Radiquum/AniX.git
synced 2025-04-29 01:19:41 +05:00
feat: add collection create\edit api request
This commit is contained in:
parent
5cde53c1d3
commit
9f3ca2e6d9
4 changed files with 140 additions and 48 deletions
|
@ -1,6 +1,7 @@
|
|||
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,
|
||||
|
@ -25,14 +26,24 @@ export async function POST(
|
|||
) {
|
||||
const { endpoint } = params;
|
||||
let API_V2: boolean | string =
|
||||
req.nextUrl.searchParams.get("API_V2") || false;
|
||||
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 body = JSON.stringify( await req.json());
|
||||
let body;
|
||||
const ReqContentTypeHeader = req.headers.get("Content-Type") || "";
|
||||
let ResContentTypeHeader = "";
|
||||
|
||||
const response = await fetchDataViaPost(url, body, API_V2);
|
||||
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);
|
||||
}
|
||||
|
|
|
@ -28,11 +28,16 @@ export const fetchDataViaGet = async (
|
|||
export const fetchDataViaPost = async (
|
||||
url: string,
|
||||
body: string,
|
||||
API_V2: string | boolean = false
|
||||
API_V2: string | boolean = false,
|
||||
contentType: string = ""
|
||||
) => {
|
||||
if (API_V2) {
|
||||
HEADERS["API-Version"] = "v2";
|
||||
}
|
||||
if (contentType != "") {
|
||||
HEADERS["Content-Type"] = contentType;
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch(url, {
|
||||
method: "POST",
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue