class Fetcher
extends SyncEventDispatcher<{ request: Parameters<RequestInterceptor>; response: Parameters<ResponseInterceptor>; }>
发送请求核心class对象
▶Example 1
Example 1
const fetcher = createFetcher({ baseURL:"https://examp.com", requestInterceptor(req) { req.headers={ "Authorization": "Bearer bar" } } responseInterceptor(resp, requconfig) { const content = resp.text() console.log(requconfig.url, requconfig.method, content); return content } }) fetcher.options('/api/login',{param:{q:"123"},body:{password:"123"}})
这样,fetcher会使用baseURL+url拼接 成为 pathname,同时自动将param自动拼接成为search参数 请求体会自动将body自动转换为json字符串
new
Fetcher(baseURL: string,config: FetherConfig,)
private
buildRequest(config: InterceptorConfig): Promise<Request>
构建请求参数
private
buildResponse<T = Response>(resp: Response,config: InterceptorConfig,): Promise<T>
构建响应体
get<T = Response>(url: string,options?: Omit<FetherConfig, "body">,): Promise<T>
发起 http methods=get的请求
private
getFormatedURL(pathname: string)
head<T = Response>(url: string,options?: Omit<FetherConfig, "body">,): Promise<T>
method为 head的请求
options<T = Response>(url: string,options?: FetherConfig,): Promise<T>
http method=options的请求
post<T = Response>(url: string,options?: FetherConfig,): Promise<T>
发起http method=post的请求
request<T = Response>(url: string,options?: FetherConfig,): Promise<T>
请求核心方法 其他所有具名方法均调用此方法