关于 Http 的新思路
ts
type IMethod = "GET" | "POST" | "PUT" | "DELETE";
class IRequestConfig<Res, Req> {
method: IMethod;
url: string;
headers?: Record<string, string>;
constructor(
method: IMethod,
url: string,
headers: Record<string, string> = {}
) {
this.method = method;
this.url = url;
this.headers = headers;
}
static get<S, Q = void>(url: string, headers?: Record<string, string>) {
return new IRequestConfig<S, Q>("GET", url, headers);
}
static post<S, Q = void>(url: string, headers?: Record<string, string>) {
return new IRequestConfig<S, Q>("POST", url, headers);
}
request(data: Req): Promise<Res> {
return {} as any;
}
}
interface ITestRequest {
id: string;
}
interface ITestResponse {
code: number;
msg: string;
}
export const Login = {
发送验证码: IRequestConfig.get<ITestResponse, ITestRequest>("/api/xxx"),
密码登陆: IRequestConfig.get<void>("/api/xxx"),
};