import { API_HOST, isServer } from "libs/config";

export function http<T = any>(uri: string, config?: RequestInit) {
  return fetch(isServer ? `${API_HOST}${uri}` : uri, config).then((res) =>
    res.json()
  ) as Promise<ResData<T>>;
}

export function get<T = any>(uri: string) {
  return http<T>(uri);
}

export function post<T = any>(uri: string, body?: unknown) {
  return http<T>(uri, {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify(body),
  });
}
