Files
tonePage/tone-page-web/lib/api/fetcher.ts
2025-05-12 10:27:50 +08:00

39 lines
988 B
TypeScript

export interface StanderResponse<T> {
statusCode: number;
message: string;
data?: T;
}
export class ApiError extends Error {
constructor(
public statusCode: number,
message: string,
public data?: unknown,
) {
super(message);
this.name = 'ApiError';
}
}
const fetcher = async<T>(url: string, options?: RequestInit): Promise<T> => {
const res = await fetch(url, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
// 自动带上 token
...(typeof window !== 'undefined' && localStorage.getItem('token')
? { Authorization: `Bearer ${localStorage.getItem('token')}` }
: {}),
},
...options,
});
const result = await res.json();
if (result.statusCode !== 200) {
throw new ApiError(result.statusCode, result.message, result.data);
}
return result.data as T;
}
export default fetcher