# art-apis

> - 接口统一管理 - 支持多 host - 支持公共配置 - 支持 restful 接口 - 支持重新请求

Latest version **1.0.0** (published 2021-05-01) · MIT license · 0 weekly downloads

## Install

```sh
npm install art-apis
pnpm add art-apis
yarn add art-apis
bun add art-apis
```

## 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.0 |
| Published | 2021-05-01 |
| First published | 2021-05-01 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=6.0.0 |
| Dependencies | 1 |
| Unpacked size | 1.1 MB |
| Known vulnerabilities | 0 (+25 in 1 direct dependencies) |
| Install scripts | no |
| Author | 1518190293@qq.com |
| Maintainers | vnues |
| Keywords | apis, axios, request, ajax |

## Links

- npm: https://www.npmjs.com/package/art-apis
- Repository: https://github.com/art-design-ui/apis
- Homepage: https://github.com/art-design-ui/apis#readme
- Issues: https://github.com/art-design-ui/apis/issues
- npm.io page: https://npm.io/package/art-apis

## Dependencies (1)

- [axios](https://npm.io/package/axios.md) ^0.19.2

## Alternatives

- [launchdarkly-js-client-sdk](https://npm.io/package/launchdarkly-js-client-sdk.md) — 2.5M weekly downloads
- [@elastic/elasticsearch](https://npm.io/package/@elastic/elasticsearch.md) — 2.1M weekly downloads
- [@c8y/client](https://npm.io/package/@c8y/client.md) — 15.3K weekly downloads
- [@signaldb/maverickjs](https://npm.io/package/@signaldb/maverickjs.md) — 1.7K weekly downloads
- [@bbc/http-transport-cache](https://npm.io/package/@bbc/http-transport-cache.md) — 1.2K weekly downloads

## Recent versions

- 1.0.0 (latest) — 2021-05-01

## README

# 基于 axios 封装的接口管理方案

- 接口统一管理
- 支持多 host
- 支持公共配置
- 支持 restful 接口
- 支持重新请求

## example

```ts
import createInstance from '../../src/apis'
import { ApisMap } from '../../src/types'

const serverMap = {
  baseServer: {
    baseMap: {
      localprod: '',
      prod: 'https://wwww.baidu.com',
      stage: 'https://wwww.baidu.com',
      test: 'https://wwww.baidu.com',
      dev: 'https:/wwww.baidu.com',
      local: 'http://127.0.0.1:4320',
      baseURL: 'https://localhost:8080'
    },
    default: true
  },
  'api-test': {
    baseMap: {
      localprod: '',
      prod: 'https://www.baidu.com',
      stage: 'https://www.baidu.com',
      test: 'https://www.baidu.com',
      dev: 'https:/www.baidu.com',
      local: `http://127.0.0.1:4320`,
      baseURL: 'https://localhost:8080'
    }
  }
}

const apiMap: ApisMap = {
  getBaseInfo: {
    method: 'get',
    url: '/base/get'
  },
  getBaseRestInfo: {
    method: 'get',
    url: '/base/get/:id/kill/:test'
  }
}

/**
 * 测试拦截器
 */
createInstance.useReq(
  function(config) {
    console.log('请求中间拦截器1')
    config.headers.Authorization = 'Bearer'
    return config
  },
  function(error) {
    return Promise.reject(error)
  }
)
createInstance.useReq(
  function(config) {
    console.log('请求中间拦截器2')
    config.headers.Authorization = 'Bearer'
    return config
  },
  function(error) {
    return Promise.reject(error)
  }
)

createInstance.useRes(
  function(result) {
    console.log('响应中间拦截器')
    return result
  },
  function(error) {
    return Promise.reject(error)
  }
)

let apis = createInstance(serverMap, apiMap)

apis.getBaseInfo({ params: { name: 'vnues' } }).then(res => {
  console.log(res)
})

/**
 * 测试restful
 */

apis.getBaseRestInfo({ rest: { id: 9527, test: 250 } }).then(res => {
  console.log(res)
})
```

### 重新请求

> 当网断了或者请求超时，我们想要 axios 重新发起请求，如何做到？

```ts
/**
 * config配置{ retry: 5, retryDelay: 1000 }
 * 尝试retry次数
 * 隔多久进行retry
 */
axios
  .get('/some/endpoint', { retry: 5, retryDelay: 1000 })
  .then(function(res) {
    console.log('success', res.data)
  })
  .catch(function(err) {
    console.log('failed', err)
  })
```

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