33 lines
755 B
TypeScript
33 lines
755 B
TypeScript
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]
|
|
} |