https://github.com/mys1024/vite-plugin-koa-mock
Serve mock API with Koa.js in Vite projects.
https://github.com/mys1024/vite-plugin-koa-mock
Last synced: 5 months ago
JSON representation
Serve mock API with Koa.js in Vite projects.
- Host: GitHub
- URL: https://github.com/mys1024/vite-plugin-koa-mock
- Owner: mys1024
- License: mit
- Created: 2023-02-08T15:47:16.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-10-18T07:09:03.000Z (over 1 year ago)
- Last Synced: 2025-10-19T03:37:06.490Z (8 months ago)
- Language: TypeScript
- Homepage:
- Size: 366 KB
- Stars: 1
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# vite-plugin-koa-mock
[](https://www.npmjs.com/package/vite-plugin-koa-mock)
[](./LICENSE)
English | [中文](./README_zh.md)
Serve mock API with **Koa.js** in **Vite** projects.

## Install
```shell
npm install -D vite-plugin-koa-mock
```
## Usage
Config `vite.config.js`:
```javascript
import { defineConfig } from 'vite'
import KoaMock from 'vite-plugin-koa-mock'
export default defineConfig({
plugins: [
KoaMock({
mockDir: './mock',
proxyKeys: ['/api'],
}),
],
})
```
Create `mock/index.js` or `mock/index.ts` with your mock APIs:
```javascript
import { Router } from 'vite-plugin-koa-mock'
export const router = new Router()
router.get('/api/foo', (ctx) => {
ctx.body = 'bar'
})
router.get('/api/bar', (ctx) => {
ctx.body = 'foo'
})
```
## Options
```typescript
import type { Options as CorsOptions } from '@koa/cors'
export interface KoaMockOptions {
/**
* The dir for mock APIs.
* @default './mock'
*/
mockDir?: string
/**
* The port for mock server.
* @default 9719
*/
port?: number
/**
* Keys for Vite's configuration `server.proxy`.
* @see https://vitejs.dev/config/server-options.html#server-proxy
* @default ['/api']
*/
proxyKeys?: string[]
/**
* Whether to enable builtin logger middleware.
* @default true
*/
logger?: boolean
/**
* Whether to enable builtin CORS middleware.
* You can configure the CORS middleware by setting an options object.
* @see https://github.com/koajs/cors#corsoptions
* @default true
*/
cors?: boolean | CorsOptions
/**
* Whether to enable builtin body parser middleware.
* @see https://github.com/koajs/bodyparser
* @default true
*/
bodyParser?: boolean
}
```
## License
[MIT](./LICENSE) License © 2024-PRESENT
[mys1024](https://github.com/mys1024)