@cogears/http-client
v1.1.7
Published
http client module
Downloads
20
Readme
@cogears/http-client
http客户端模块,同时兼容浏览器和node环境。
安装
npm install @cogears/http-client
接口列表
- HttpClient
- HttpApi
具体实现可以查看types定义
用例
- 直接使用HttpClient发起请求,适用于少量http请求的场景:
import HttpClient from '@cogears/http-client'
const http = new HttpClient()
let {status, body, headers} = await http.get(url)
await http.post(url, http.json({name:'tom'}))
- 使用HttpApi包装器,适用于有大量业务接口需要统一管理的场景:
import {HttpApi} from '@cogears/http-client'
class MyApi extends HttpApi {
constructor() {
super(domain)
}
preRequest({query, body, headers}) {
return {query, body, headers}
}
async postRequest(response, url) {
response = await super.postRequest(response, url)
return response
}
doSomeRequest(){
return this.get(apiUrl)
}
...
}