mirror of
https://github.com/zuiidea/antd-admin
synced 2026-10-16 20:23:56 +00:00
1.6 KiB
1.6 KiB
HTTP requests
This project uses a small fetch-based HTTP helper implemented at src/utils/request.ts (export default api). The helper centralizes:
- Prefixing paths with
config.apiPrefix. - Handling
params(query string) andbody(JSON or FormData). - Injecting an
Authorizationheader from local storage when available. - Unified response parsing and error handling (expects backend responses in the form
{ success, message, data }).
Key behaviors
api(path, options)mergesconfig.apiPrefix+pathto build the URL.- If
options.paramsis provided it's serialized and appended as query string. options.bodyhandling:- FormData is sent as-is (do not set Content-Type manually).
- Strings and Blobs are sent directly.
- Plain objects are JSON.stringified and Content-Type defaults to
application/json.
- If a token exists in local storage (key
app-auth-token), the helper injects anAuthorization: Bearer <token>header unless one is already provided. - Responses are expected to be JSON with
{ success: boolean, message?: string, data: T }. Non-success responses throw an Error and show a UI message (usesantd'smessage).
Usage (TypeScript-friendly):
import api from '@/utils/request';
// GET with query params
const page = await api('/users', { method: 'GET', params: { page: 1, pageSize: 10 } });
// POST with JSON body
const created = await api('/user', { method: 'POST', body: { name: 'Alice' } });
The helper returns the data field from successful responses. Check src/utils/request.ts for exact typings and behavior.