# mula

> like fetch

Latest version **1.1.7** (published 2019-10-27) · 0 weekly downloads

## Install

```sh
npm install mula
pnpm add mula
yarn add mula
bun add mula
```

## Health

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

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.7 |
| Published | 2019-10-27 |
| First published | 2019-09-16 |
| Weekly downloads | 0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 3.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | pillar |
| Maintainers | ymblender |
| Keywords | fetch, axios |

## Links

- npm: https://www.npmjs.com/package/mula
- Repository: https://github.com/ymzuiku/mula
- Homepage: https://github.com/ymzuiku/mula#readme
- Issues: https://github.com/ymzuiku/mula/issues
- npm.io page: https://npm.io/package/mula

## Dependencies (2)

- [xhr2](https://npm.io/package/xhr2.md) ^0.2.0
- [querystring-number](https://npm.io/package/querystring-number.md) ^1.0.6

## 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.1.7 (latest) — 2019-10-27
- 1.1.6 — 2019-10-27
- 1.1.5 — 2019-10-27
- 1.1.4 — 2019-09-30
- 1.1.3 — 2019-09-28
- 1.1.2 — 2019-09-28
- 1.1.1 — 2019-09-28
- 1.1.0 — 2019-09-28
- 1.0.9 — 2019-09-28
- 1.0.8 — 2019-09-28
- 1.0.7 — 2019-09-28
- 1.0.6 — 2019-09-28
- 1.0.5 — 2019-09-28
- 1.0.4 — 2019-09-28
- 1.0.3 — 2019-09-16
- … 8 more at https://npm.io/package/mula/versions

## README

# Tiny XMLHttpRequest, like axios

仅有 1.8k(gzip) 的体积, 为追求前端项目极致体积的开发者提供, 可兼容 Nodejs

## Feature

- 更加简化的返回类型, 焦距最终的返回值, 而不是过程
- 对于每个实例可以预设行为
- Promise 风格
- JSON 自动处理
- GET 请求 params 自动处理

## Example

```js
import Mula from 'mula'; // If in nodejs: 'mula/umd/node';


const mula = Mula({
  // 如果希望覆盖 XMLHttpRequest 对象, mula/umd/node 默认使用 xhr2
  // XMLHttpRequest: require("xhr2")

  // 可选，请求URL前缀
  baseURL: 'http://127.0.0.1:4000',
  // 可选，对 XMLHttpRequire 的返回数据预处理，默认会将http必要信息放入 __http__
  reducer: (e)=>{
    return e;
  }
  ontimeout: e => {
    // 对一些状态统一做处理
    console.log('time-out:', e);
  },
  onerror: e => {
    console.log('have-error:', e);
    // 当遇到错误时，对原始数据做处理，并返回
    e.error = { code: '200' };

    // 如果有返回值，该返回值会取代原有返回值
    return e;
  },
});

const fetchSometing = async () => {
  const data1 = await mula.get('/hello');
  const data2 = await mula.post(
    '/hello',
    { name: 'dog', age: 5 },
    {
      onerror: e => {
        // 对一些状态单独做处理, 例如，弱error就返回null
        return null
      },
    }
  );
  const data3 = await mula.put('/hello', { name: 'dog', age: 5 });
  const data4 = await mula.delete('/hello', { name: 'dog', age: 5 });
  const data4 = await mula.options('/hello', { name: 'dog', age: 5 });
};
```

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