Giter Site home page Giter Site logo

fegg / vite-plugin-mock-server Goto Github PK

View Code? Open in Web Editor NEW

This project forked from enjoycoding/vite-plugin-mock-server

0.0 0.0 0.0 53 KB

A mock server plugin for Vite.

License: MIT License

Shell 1.45% JavaScript 6.94% TypeScript 76.45% HTML 2.97% Batchfile 1.63% Vue 10.57%

vite-plugin-mock-server's Introduction

vite-plugin-mock-server

npm

Provide local mocks for Vite.

A mock server plugin for Vite, developed based on TypeScript. And support using TypeScript and JavaScript to write Mock API. When the Mock API file is modified, it will be hot updated automatically.

Install

node version: >=12.0.0

vite version: >=2.0.0

# if using npm
npm i vite-plugin-mock-server -D
# if using yarn
yarn add vite-plugin-mock-server -D

Run example

cd ./example
npm install
npm run dev

Usage

  • Config plugin in vite.config.ts
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import mockServer from 'vite-plugin-mock-server'

export default defineConfig({
  plugins: [
    vue(),
    mockServer({
      logLevel: 'info'
    })
  ]
})

Module exports

  • MockOptions

mockModules Ignore manual configuration, it will be filled in automatically.

export type MockOptions = {
  logLevel?: 'info' | 'error' | 'off'
  urlPrefixes?: string[]
  mockJsSuffix?: string
  mockTsSuffix?: string
  mockRootDir?: string
  mockModules?: string[]
  noHandlerResponse404?: boolean
}

// default options
const options: MockOptions = {
  logLevel: 'info',
  urlPrefixes: [ '/api/' ],
  mockRootDir: './mock',
  mockJsSuffix: '.mock.js',
  mockTsSuffix: '.mock.ts',
  noHandlerResponse404: true,
  mockModules: [] 
}
  • MockFunction
export type MockFunction = {
  (req: Connect.IncomingMessage, res: http.ServerResponse, urlVars?: { [key: string]: string }): void
}
  • MockHandler
export type MockHandler = {
  pattern: string,
  method?: string,
  handle: MockFunction
}

Mock file examples

The pattern is an ant-style path pattern string, use @howiefh/ant-path-matcher to match the pattern and request URL.

// example/mock/es.mock.ts

const mocks: MockHandler[] = [
  {
    pattern: '/api/test1/1',
    handle: (req, res) => {
      res.end('Hello world!' + req.url)
    }
  },
  {
    pattern: '/api/test1/*',
    handle: (req, res) => {
      res.end('Hello world!' + req.url)
    }
  },
  {
    pattern: '/api/test1/users/{userId}',
    handle: (req, res, pathVars) => {
      const data = {
        url: req.url,
        pathVars: pathVars
      }
      res.setHeader('Content-Type', 'application/json')
      res.end(JSON.stringify(data))
    }
  },
  {
    pattern: '/api/test1/users/{userId}/{blogId}',
    handle: (req, res, pathVars) => {
      const data = {
        url: req.url,
        pathVars: pathVars
      }
      res.setHeader('Content-Type', 'application/json')
      res.end(JSON.stringify(data))
    }
  }
]

export default mocks

// example/mock/apis/es2.mock.ts

import { MockHandler } from 'vite-plugin-mock-server'

export default (): MockHandler[] => [
  {
    pattern: '/api/test2/1',
    handle: (req, res) => {
      res.end('Hello world!' + req.url)
    }
  },
  {
    pattern: '/api/test2/2',
    handle: (req, res) => {
      res.end('Hello world!' + req.url)
    }
  }
]
// example/mock/cjs.mock.js

module.exports = [
  {
    pattern: '/api/merchant1',
    method: 'GET',
    handle: (req, res) => {
      res.end('merchant1:' + req.url)
    }
  },
  {
    pattern: '/api/merchant2',
    method: 'GET',
    handle: (req, res) => {
      res.end('merchant2:' + req.url)
    }
  },
  {
    pattern: '/api/merchant2',
    method: 'GET',
    handle: (req, res) => {
      res.end('merchant3:' + req.url)
    }
  }
]

// example/mock/apis/cjs2.mock.js

module.exports = [
  {
    pattern: '/api/hello1',
    method: 'GET',
    handle: (req, res) => {
      res.end('hello1:' + req.url)
    }
  },
  {
    pattern: '/api/hello2',
    method: 'GET',
    handle: (req, res) => {
      res.end('hello2:' + req.url)
    }
  },
  {
    pattern: '/api/hello3',
    method: 'GET',
    handle: (req, res) => {
      res.end('hello2:' + req.url)
    }
  }
]

License

MIT

vite-plugin-mock-server's People

Contributors

octoape avatar steveworkman avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.