shc-pc
TypeScript icon, indicating that this package has built-in type declarations

1.1.0 • Public • Published

shc-pc

NPM version Build Status Coverage Status Known Vulnerabilities changelog license

npm-image

符合接口规范的 HTTP 客户端 - PC

功能

扩展 StandardHttpClient

  • 发送请求前开启 loading(通过 nprogress 实现)
  • 请求结束后关闭 loading
  • 请求出错时给予用户错误提示(通过 element-uiMessage 实现)

http-api-error-tip

示例

import HttpClient from 'shc-pc';

var httpClient = new HttpClient();

httpClient.send({
    url: 'https://httpbin.org/json'
}).then(function([data, response]) {
    console.log('data', data);
});

更多使用方法详见 StandardHttpClient

扩展

  • 如果需要自定义错误提示, 可以继承之后重写 handleError 方法

    import SHC from 'shc-pc';
    
    class HttpClient extends SHC {
        constructor() {
            super({ // 可以在这里传入默认参数
                withCredentials: true,
                timeout: 10 * 1000
            });
        }
    
        handleError(error) {
            super.handleError(error);
    
            if (error._errorCode === 'B1') {
                alert('特殊处理业务错误 B1');
            } else if (error._errorCode === 'B2') {
                alert('特殊处理业务错误 B2');
            }
        }
    }
    
    export default new HttpClient();

Readme

Keywords

Package Sidebar

Install

npm i shc-pc

Weekly Downloads

16

Version

1.1.0

License

MIT

Unpacked Size

16.3 kB

Total Files

13

Last publish

Collaborators

  • ufologist