index.ts 560 B

1234567891011121314151617181920
  1. import { apiHost, isServer } from "../../libs/config";
  2. import type { ResData } from "../../types/http";
  3. export function http<T = any>(uri: string, config?: RequestInit) {
  4. return fetch(isServer ? `${apiHost}${uri}` : uri, config).then((res) =>
  5. res.json()
  6. ) as Promise<ResData<T>>;
  7. }
  8. export function get<T = any>(uri: string) {
  9. return http<T>(uri);
  10. }
  11. export function post<T = any>(uri: string, body?: unknown) {
  12. return http<T>(uri, {
  13. method: "POST",
  14. headers: { "Content-Type": "application/json" },
  15. body: JSON.stringify(body),
  16. });
  17. }