@cogears/http-client
TypeScript icon, indicating that this package has built-in type declarations

1.1.7 • Public • Published

@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)
    }

    ...
}

Readme

Keywords

Package Sidebar

Install

npm i @cogears/http-client

Weekly Downloads

13

Version

1.1.7

License

none

Unpacked Size

20.1 kB

Total Files

11

Last publish

Collaborators

  • stone.grandia