This commit is contained in:
2024-06-01 18:35:10 +02:00
parent c0e02defa8
commit 7c98da1df8
8 changed files with 54 additions and 441 deletions
+33
View File
@@ -0,0 +1,33 @@
import path from "path"
import type { IUser } from "~/netcode/interfaces"
enum HTTPMethods {
GET = "GET",
POST = "POST"
}
const Endpoints = {
LOGIN: { url: "/login", method: HTTPMethods.POST, body: null }
} as const
type ReturnTypes = {
LOGIN: IUser
}
type EnpointsNames = keyof typeof Endpoints
const config = useRuntimeConfig();
export default async function api<T extends EnpointsNames>(target: T, body?: typeof Endpoints[T]["body"]): Promise<ReturnTypes[T]> {
const data = await fetch(path.join(config.public.endpoint, Endpoints[target].url), {
credentials: "same-origin",
mode: "same-origin",
method: Endpoints[target].method,
body,
})
return await data.json() as ReturnTypes[T]
}