0.10.1 • Published 8 months ago

vite-plugin-koa-mock v0.10.1

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

vite-plugin-koa-mock

npm-version license

English | 中文

Serve mock API with Koa.js in Vite projects.

logger

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
}

License

MIT License © 2024-PRESENT mys1024

0.10.0

8 months ago

0.10.1

8 months ago

0.9.13

2 years ago

0.9.12

2 years ago

0.9.11

2 years ago

0.9.10

2 years ago

0.9.9

2 years ago

0.9.8

2 years ago

0.9.7

2 years ago

0.9.6

2 years ago

0.9.5

2 years ago

0.9.4

2 years ago

0.9.1

2 years ago

0.9.0

2 years ago