//复制时倒入qs // @ts-ignore import qs from "qs"; const API_SERVER = process.env.NEXT_PUBLIC_WEB_SITE export interface Config extends RequestInit { token?: string data?: T method?: 'POST' | 'GET' } interface IError { status: number details: [], message: string metadata: any reason: string } const http = async (endpoint: string, {data, token, headers, ...customConfig}: Config) => { const config = { method: "GET", headers: { Authorization: token ? `Bearer ${token}` : '', 'Content-type': data ? 'application/json' : '', }, ...customConfig } if (config.method.toUpperCase() === "GET") { endpoint += `?${qs.stringify(data)}` } else { config.body = JSON.stringify(data || {}) } console.log(API_SERVER + endpoint, token) return fetch(API_SERVER + endpoint, config).then(async response => { if (response.status == 401) { if (typeof window != 'undefined') { // window.location.reload(); } return Promise.reject({message: "请重新登录", status: 401}) } const data = await response.json(); if (response.ok) { return data as V; } else { return Promise.reject(data as IError); } }) } export { http }