https://github.com/thundernet8/vite-plugin-api-mocker
A local api mocker plugin for vite
https://github.com/thundernet8/vite-plugin-api-mocker
Last synced: over 1 year ago
JSON representation
A local api mocker plugin for vite
- Host: GitHub
- URL: https://github.com/thundernet8/vite-plugin-api-mocker
- Owner: thundernet8
- License: mit
- Created: 2021-03-01T07:02:23.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2021-03-01T07:26:02.000Z (over 5 years ago)
- Last Synced: 2024-04-29T15:22:46.499Z (about 2 years ago)
- Language: TypeScript
- Size: 10.7 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# vite-plugin-api-mocker
A local api mocker plugin for vite,with delay response and auto-added api path prefix following file system supports
## install
```bash
yarn add vite-plugin-api-mocker -D
```
## usage
```ts
// vite.config.ts
import { defineConfig } from 'vite';
import mockerPlugin from 'vite-plugin-api-mocker';
export default defineConfig({
// ... other config
plugins: [
mockerPlugin({
dir: Path.join(process.cwd(), 'mock'), // mock files root folder
delay: 1000, // delay 1000ms before response
}),
],
});
```
## mock file
```js
// /mockroot/index.js -> GET /hello
exports['GET /hello'] = {
msg: 'hello world',
};
// /mockroot/api/example.js -> GET /api/example/:id
exports['GET /:id(\\d+)'] = {
foo: 'bar',
};
// /mockroot/api/example.js -> POST /api/example/users
exports['POST /users'] = (req, res) => {
res.writeHead(200, {
'content-type': 'application/json'
});
res.end({
code: 0,
msg: '',
data: [{
// ...
}]
})
};
```