# lbl-request

> ```shell npm install lbl-request ```

Latest version **1.0.10** (published 2021-10-29) · ISC license · 0 weekly downloads

## Install

```sh
npm install lbl-request
pnpm add lbl-request
yarn add lbl-request
bun add lbl-request
```

## Health

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

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.10 |
| Published | 2021-10-29 |
| First published | 2021-03-01 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 3 |
| Unpacked size | 358.9 KB |
| Known vulnerabilities | 0 (+23 in 1 direct dependencies) |
| Install scripts | no |
| Author | libeilong |
| Maintainers | libeilong |

## Links

- npm: https://www.npmjs.com/package/lbl-request
- npm.io page: https://npm.io/package/lbl-request

## Dependencies (3)

- [axios](https://npm.io/package/axios.md) ^0.21.1
- [json-to-ts](https://npm.io/package/json-to-ts.md) ^1.7.0
- [regenerator-runtime](https://npm.io/package/regenerator-runtime.md) ^0.13.9

## Recent versions

- 1.0.10 (latest) — 2021-10-29
- 1.0.9 — 2021-10-29
- 1.0.8 — 2021-09-08
- 1.0.7 — 2021-08-10
- 1.0.6 — 2021-06-28
- 1.0.5 — 2021-06-28
- 1.0.4 — 2021-06-16
- 1.0.3 — 2021-04-06
- 1.0.2 — 2021-03-11
- 1.0.1 — 2021-03-09
- 1.0.0 — 2021-03-01

## README

#### 安装

```shell
npm install lbl-request
```



#### 如何使用

```typescript
// request.ts

import { Request, Result } from 'lbl-request'
import { AxiosRequestConfig, AxiosResponse } from 'axios'

export const request = new Request({
  // 业务域名
  baseURL: 'https://lbaxios.com/api',
  // 返回 json 的大小写转换：'pascal' | 'camel' | 'none'
  convertCase: 'camel',

  // 成功条件
  successCondition: (result: Result) => {
    return result.code === 0
  },

  // 发生异常时处理
  onError: (result: Result, toastError: boolean) => {
    console.log(`----- ${result.url} 接口报错 -----`)
    console.log(result)

    if (result.status === 401) {
      Util.toast.error('登录状态失效，请重新登录')
      setTimeout(() => {
        StoreUser.logout()
      }, 1000)
    } else if (toastError) {
      Util.toast.error(result.message)
    }
  },

  // 请求头混入
  mixinHeader: (config: AxiosRequestConfig, isBusinessDomain: boolean) => {
    if (!isBusinessDomain) return config

    const token = Util.token.getToken()
    if (token) {
      config.headers.common['Authorization'] = token
    }

    config.headers.common['Accept-Language'] = Util.i18n.getLang()
    return config
  },

  // 响应内容转换为 Result
  responseFormat: (result: Result, response: AxiosResponse) => {
    const data = response.data
    result.code = data.code
    result.message = data.message
    result.data = data.data
    return result
  }
})



// api/user.ts
import { request } from './request'

export async function login() {
  const res = await request.post('/user/login', {
    username: '123456',
    password: 123456
  })

  const { token } = res.hasSuccess()
  Util.token.setToken(token)
}
```

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