// TS 中的typeof,是在静态环境运行的
// return (...[endpoint, config]: Parameters<typeof http>) =>
export const useHttp = () => {
const { user } = useAuth();
// utility type 的用法:用泛型给它传入一个其他类型,然后utility type对这个类型进行某种操作
return useCallback(
(...[endpoint, config]: Parameters<typeof http>) =>
http(endpoint, { ...config, token: user?.token }),
[user?.token]
);
};
const bootstrapUser = async () => {
let user = null;
const token = auth.getToken();
if (token) {
const data = await http("me", { token });
user = data.user;
}
return user;
};