接口前缀
应用于整个实例的请求体,后续请求都会带上
特性:
// 直接发请求
const res = await fetcher().get<Blog>("https://nickyzj.run:3030/blogs/hello-world", {
params: {
page: 2,
pageSize: 10,
}
});
// 创建实例,发请求
const api = fetcher("https://nickyzj.run:3030", {
headers: {
Authorization: "Bearer token"
}
});
const res = await api.get<Blog>("/blogs/hello-world");
// 安全返回请求结果,不抛异常
const [error, data] = await to(api.get<Blog>("/blogs/hello-world"));
if (error) {
// ...
}
// ...
// 缓存请求结果
const getBlogs = withCache(api.get);
await getBlogs("/blogs");
await getBlogs("/blogs"); // 不发请求,使用缓存
基于 Fetch API 的请求实例