# @hnutils/utils

> ``` npm install @hnutils/utils ``` #### http使用 http.js ``` import { httpClientInstall } from '@hnutils/utils'; Vue.use(httpClientInstall, { loading(config, msg) { // loading console.log(config); }, error(response) { // error-inspect

Latest version **0.0.9** (published 2022-01-11) · ISC license · 0 weekly downloads

## Install

```sh
npm install @hnutils/utils
pnpm add @hnutils/utils
yarn add @hnutils/utils
bun add @hnutils/utils
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.9 |
| Published | 2022-01-11 |
| First published | 2021-12-10 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 150.1 KB |
| Known vulnerabilities | 0 (+25 in 1 direct dependencies) |
| Install scripts | no |
| Maintainers | tingting_myt, sweet_korol |

## Links

- npm: https://www.npmjs.com/package/@hnutils/utils
- npm.io page: https://npm.io/package/@hnutils/utils

## Dependencies (4)

- [axios](https://npm.io/package/axios.md) ^0.19.2
- [core-js](https://npm.io/package/core-js.md) ^3.6.5
- [node-rsa](https://npm.io/package/node-rsa.md) ^1.1.1
- [crypto-js](https://npm.io/package/crypto-js.md) ^4.1.1

## Recent versions

- 0.0.9 (latest) — 2022-01-11
- 0.0.8 — 2022-01-07
- 0.0.7 — 2022-01-07
- 0.0.5 — 2021-12-28
- 0.0.4 — 2021-12-24
- 0.0.3 — 2021-12-23
- 0.0.2 — 2021-12-10
- 0.0.1 — 2021-12-10

## README

# @hn-utils/utils
#### http包下载
```
npm install @hnutils/utils
```
#### http使用
http.js
```
import { httpClientInstall } from '@hnutils/utils';
Vue.use(httpClientInstall, {
  loading(config, msg) {
    // loading
    console.log(config);
  },
  error(response) {
    // error-inspect
    console.log('response', response);
    if (response.status === 404) {
      message.error('网络异常');
    }
    if (response.status !== 401 && response.status !== 404) {
      if (response.data) {
        message.error(response.data.message || response.data.error);
      } else if (response.message) {
        message.error(response.message);
      } else if (response.error) {
        message.error(response.error);
      } else {
        message.error(response)
      }
    }
  },
  streamTypes: ['application/vnd.ms-excel;charset=UTF-8'],  // 不需要对返回对象处理的content-type
  clear() {
  },
  responseCb(response) {
    console.log('response-inspect', responseCb);
  },
  errHeader: {
    'x-test': '我是x-test请求头报错返回'   // 用于自定义处理请求头特定报错
  },
  requestCb(config) {
    console.log('request-inspect', config);
    <!-- const _sign = window.location.search.split('tgt=')[1];
    if (_sign) {
      setToken(_sign);
    }
    const meta = config.meta || {};
    const isToken = meta.isToken === false;
    if (getToken() && !isToken) {
      config.headers[website.Authorization] = website.Authorization + getToken(); // 让每个请求携带token--['Authorization']为自定义key 请根据实际情况自行修改
    }
    config.headers['csrftoken'] = createCSRFToken(config.url); -->
  },
  encrypt: process.env.NODE_ENV === 'local' ? false : website.VUE_APP_ENCRYPT === 'encrypt', // 加解密
  gateway: process.env.NODE_ENV !== 'local' ? website.VUE_APP_GATEWAY + website.VUE_APP_BASE_API : '',  // 网关
  baseApi: process.env.NODE_ENV !== 'local' ? website.VUE_APP_BASE_API : '',  // baseApi
  mode: process.env.NODE_ENV === 'local' ? 'debugRemote' : '' // local: 本地不加网关; debugRemote：本地加网关;
});
export default Vue.prototype.$http;
```

api.js
```
import request from './http.js';
const xxx = {
  getTableData(data) {
    return request({
      url: 'xxx',
      method: 'post',
      data: data,
      closeEncrypt: true   // 单个关闭加密
      responseType: 'blob' // 文件类型上传，对返回实体不做任何处理
    });
  },
};
export default xxx;

```
#### http代理
```
// 加密接口，本地调试默认加上/proxy代理
getBaseUrl() {
    if (this.options.mode === 'local') {
      return '/proxy${this.options.gateway}';
    }
    if (this.options.mode === 'debugRemote') {
      return `/proxy${this.options.gateway}`;
    }
    return '';
  }
// 其他接口，默认加/proxy代理
getBaseUrl() {
    if (this.options.mode === 'local') {
      return '/proxy';
    }
    if (this.options.mode === 'debugRemote') {
      return `/proxy${this.options.gateway}${this.options.baseApi}`;
    }
    return `${this.options.gateway}${this.options.baseApi}`;
  }
// vue.config.js文件代理相关
devServer: {
    proxy: {
      '/proxy': {
        target: 'http://172.16.120.88:9999', //代理地址，这里设置的地址会代替axios中设置的baseURL
        changeOrigin: true, // 如果接口跨域，需要进行这个参数配置
        pathRewrite: { '/proxy': '' }
      }
    },
  },
```

---
_Source: https://npm.io/package/@hnutils/utils · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
