# @ckpack/mock-axios

> Mock axios requests for testing and development

Latest version **0.0.6** (published 2022-03-21) · MIT license · 0 weekly downloads

## Install

```sh
npm install @ckpack/mock-axios
pnpm add @ckpack/mock-axios
yarn add @ckpack/mock-axios
bun add @ckpack/mock-axios
```

## Health

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

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

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.6 |
| Published | 2022-03-21 |
| First published | 2022-03-16 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=16 |
| Dependencies | 0 |
| Unpacked size | 10.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | 陈锴 |
| Maintainers | ckpack |
| Keywords | mock, axios, mock-axios |

## Links

- npm: https://www.npmjs.com/package/@ckpack/mock-axios
- Repository: https://github.com/ckpack/mock-axios
- Issues: https://github.com/ckpack/mock-axios/issues
- npm.io page: https://npm.io/package/@ckpack/mock-axios

## Alternatives

- [pagerjs](https://npm.io/package/pagerjs.md) — 60 weekly downloads
- [whistle.savefor-mock](https://npm.io/package/whistle.savefor-mock.md) — 4 weekly downloads
- [vite-plugin-apitemkin](https://npm.io/package/vite-plugin-apitemkin.md) — 0 weekly downloads
- [facebook-ads-library-api](https://npm.io/package/facebook-ads-library-api.md) — 0 weekly downloads
- [@comp-markdown/wireframe](https://npm.io/package/@comp-markdown/wireframe.md) — 0 weekly downloads

## Recent versions

- 0.0.6 (latest) — 2022-03-21
- 0.0.5 — 2022-03-17
- 0.0.4 — 2022-03-17
- 0.0.3 — 2022-03-17
- 0.0.2 — 2022-03-16
- 0.0.1 — 2022-03-16

## README

# mock-axios

<h4 align="center">
  <a href="/README-ZH.md">中文</a>
  |
  <a href="/README.md">ENGLISH</a>
</h4>

Intercept `Axios` requests and return `Mock` data for testing and development.

## API

### mockAxios

`(mockDatas: MockConfig[], mockOptions?: MockOptions | undefined) => void`: Mock `Axios`

+ `mockDatas`: an array, when `axios` requests, it will check the corresponding `url`, `method`, and return the corresponding `response` data when it matches.
  + `url`: `string | RegExp` type, the url to match, can be a string or a regular expression. When it is a string, it will check whether the `url` requested by `axios` contains the `url`. When it is a regular expression, it will check whether the `url` requested by `axios` matches the `url`.
  + `method`: `GET` | `POST` | `PUT` | `DELETE` | `PATCH`, default is empty to match all types of requests
  + `timeout`: `number` type, default value: `300`, unit: `ms`, the delay time of returning the result
  + `response`: the data object of the returned mock
  + `adapter`: custom adapter function, refer to [adapter](https://github.com/axios/axios/tree/master/lib/adapters), this function accepts two parameters, one is `config`, one is It is the matched `mockData` object, and the return value is the data object of `response`. Through this parameter, you can define the returned data more freely (such as verifying interface permissions).
+ `mockOptions`
  + `isUseDefaultAdapter`: `Boolean`, defaults to `true`, if enabled, requests that are not intercepted will be sent in the default mode of `axios`
  + `isEffect`: `Boolean`, default is `true`, with this parameter you can enable `mockAxios` in test environment and disable `mockAxios` in production environment
  + `isLog`: `Boolean`, the default is `true`, whether to print the request log of `mockAxios`
  + `logger`: `Function`, replace the default log function

```js
import { mockAxios } from '@ckpack/mock-axios';

mockAxios([{
  url: 'https://test.com/v1/user/1',
  response: { 
    data: { 
      id: 1,
      name: 'admin',
    }, 
  },
}], {
  isEffect: process.env.NODE_ENV === 'development',
});
```

### defineConfig

`(mockDatas: MockConfig[]) => MockConfig[]`: Helper function for constructing mockDatas. This function can be used with the IDE to get type hints

+ `mockDatas`: Same as the `mockDatas` parameter of `mockAxios`.

```js
import { defineConfig } from '@ckpack/mock-axios';

const mockDatas = defineConfig([
  {
    url: 'https://test.com/v1/user/1',
    response: {
      data: {
        id: 1,
        name: 'admin',
      },
    },
  },
]);
```

## Example

```ts
// mockAxios.js
import { mockAxios } from '@ckpack/mock-axios';

mockAxios([{
  method: 'GET',
  url: /https:\/\/test.com\/v1\/user\/\d+/,
  response: { 
    data: [{ 
      id: 1,
      name: 'admin',
    }], 
  },
}, {
  method: 'POST',
  url: 'https://test.com/v1/user/create',
  adapter: (axiosConfig) => {
    return {
      data: axiosConfig.data,
    };
  },
}]);
```

in other files

```ts
import axios from 'axios';
import './mockAxios.js';

await axios.get('https://test.com/v1/user/1');
// return { data: [{ id: 1, name: 'admin' }] }

await axios.post('https://test.com/v1/user/create', {
  id: 1,
  name: 'admin',
});
// return { data: { id: 1, name: 'admin' } }
await axios.post('https://test.com/v1/user/create', {
  id: 2,
  name: 'test',
});
// return { data: { id: 2, name: 'test' } }
```

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