refactor: api routes

This commit is contained in:
Kentai Radiquum 2025-05-05 20:46:49 +05:00
parent c74170a14d
commit 802b755d29
Signed by: Radiquum
GPG key ID: 858E8EE696525EED
6 changed files with 95 additions and 55 deletions

View file

@ -1,22 +1,45 @@
const API = process.env.NEXT_PUBLIC_API_URL || "/api";
export const ENDPOINTS = {
createPack: `${API}/pack/new`,
getAllPacks: `${API}/pack/all`,
};
type PACK_IMG_ENDPOINTS = {
type _PACK_ENDPOINT = {
getPack: string;
getPackImage: string;
editPackImage: string;
};
export const PACK_IMG_ENDPOINTS = function (
endpoint: keyof PACK_IMG_ENDPOINTS,
id: string
) {
type _PACKS_ENDPOINT = {
getPacks: string;
createPack: string;
deletePack: string;
};
export const PACK_ENDPOINT = (endpoint: keyof _PACK_ENDPOINT, id: string) => {
if (!id) {
console.error(`ENDPOINT "${endpoint}" REQUIRES A PACK ID`);
return "";
}
const _endpoints = {
getPack: `${API}/pack/${id}`,
getPackImage: `${API}/pack/${id}/image`,
editPackImage: `${API}/pack/${id}/image/edit`,
};
return _endpoints[endpoint];
};
export const PACKS_ENDPOINT = (
endpoint: keyof _PACKS_ENDPOINT,
id?: string | null
) => {
const requireID: string[] = ["deletePack"];
if (requireID.includes(endpoint) && !id) {
console.error(`ENDPOINT "${endpoint}" REQUIRES A PACK ID`);
return "";
}
const _endpoints = {
getPacks: `${API}/packs/all`,
createPack: `${API}/packs/new`,
deletePack: `${API}/packs/${id}/delete`,
};
return _endpoints[endpoint];
};

View file

@ -8,7 +8,7 @@ import { Button } from "flowbite-react";
import { useRouter } from "next/navigation";
import mc from "../../../api/mc_version.json";
import { ENDPOINTS, PACK_IMG_ENDPOINTS } from "@/api/ENDPOINTS";
import { PACKS_ENDPOINT, PACK_ENDPOINT } from "@/api/ENDPOINTS";
import { toast } from "react-toastify";
const mcr = mc.reverse();
@ -52,7 +52,7 @@ export default function PackNew() {
async function _submit() {
const tid = toast.loading(`Creating Pack "${packInfo.title}"`)
const res = await fetch(`${ENDPOINTS.createPack}`, {
const res = await fetch(PACKS_ENDPOINT("createPack"), {
method: "POST",
body: JSON.stringify(packInfo),
headers: {
@ -68,7 +68,7 @@ export default function PackNew() {
}
if (image) {
await fetch(`${PACK_IMG_ENDPOINTS("editPackImage", data.id)}`, {
await fetch(`${PACK_ENDPOINT("editPackImage", data.id)}`, {
method: "POST",
body: JSON.stringify({
image: image,