vite-plugin-koa-mock
v0.10.1
Published
Serve mock API with Koa.js in Vite projects.
Downloads
63
Readme
vite-plugin-koa-mock
English | 中文
Serve mock API with Koa.js in Vite projects.
Install
npm install -D vite-plugin-koa-mock
Usage
Config vite.config.js
:
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:
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
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
}