Gathering detailed insights and metrics for vite-plugin-mock-server
Gathering detailed insights and metrics for vite-plugin-mock-server
npm install vite-plugin-mock-server
Typescript
Module System
Node Version
NPM Version
83.8
Supply Chain
99.5
Quality
76.9
Maintenance
100
Vulnerability
100
License
TypeScript (72.42%)
Vue (17.54%)
JavaScript (4.12%)
HTML (4.09%)
Batchfile (0.96%)
Shell (0.86%)
Verify real, reachable, and deliverable emails with instant MX records, SMTP checks, and disposable email detection.
Total Downloads
617,648
Last Day
1,394
Last Week
6,194
Last Month
23,693
Last Year
316,499
MIT License
66 Stars
59 Commits
13 Forks
1 Branches
6 Contributors
Updated on Feb 18, 2025
Latest Version
1.3.1
Package Id
vite-plugin-mock-server@1.3.1
Unpacked Size
36.62 kB
Size
8.67 kB
File Count
9
NPM Version
10.5.0
Node Version
20.12.2
Published on
Aug 22, 2024
Cumulative downloads
Total Downloads
Last Day
38.6%
1,394
Compared to previous day
Last Week
22.3%
6,194
Compared to previous week
Last Month
17.4%
23,693
Compared to previous month
Last Year
32.8%
316,499
Compared to previous year
2
4
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. Support and compatibility with express.js middlewares.
node version: >=12.0.0
vite version: >=2.0.0
1# if using npm 2npm i vite-plugin-mock-server -D 3# if using yarn 4yarn add vite-plugin-mock-server -D
1cd ./example 2npm install 3npm run dev
1import { defineConfig } from 'vite' 2import vue from '@vitejs/plugin-vue' 3import bodyParser from 'body-parser' 4import cookieParser from 'cookie-parser' 5import mockServer from 'vite-plugin-mock-server' 6 7export default defineConfig({ 8 plugins: [ 9 vue(), 10 mockServer({ 11 logLevel: 'info', 12 middlewares: [ 13 cookieParser(), 14 bodyParser.json(), 15 bodyParser.urlencoded(), 16 bodyParser.text(), 17 bodyParser.raw() 18 ] 19 }) 20 ] 21})
1import { defineConfig } from 'vite' 2import vue from '@vitejs/plugin-vue' 3import mockServer from 'vite-plugin-mock-server' 4 5export default defineConfig({ 6 plugins: [ 7 vue(), 8 mockServer()) 9 ] 10})
mockModules
Ignore manual configuration, it will be filled in automatically.
1export type MockOptions = { 2 logLevel?: 'info' | 'error' | 'off' 3 urlPrefixes?: string[] 4 mockJsSuffix?: string 5 mockTsSuffix?: string 6 mockRootDir?: string 7 mockModules?: string[] 8 noHandlerResponse404?: boolean 9 middlewares?: MockLayer[] 10 printStartupLog?: boolean 11} 12 13// default options 14const options: MockOptions = { 15 logLevel: 'info', 16 urlPrefixes: [ '/api/' ], 17 mockRootDir: './mock', 18 mockJsSuffix: '.mock.js', 19 mockTsSuffix: '.mock.ts', 20 noHandlerResponse404: true, 21 mockModules: [], 22 middlewares: [], 23 printStartupLog: true 24}
1type Request = Connect.IncomingMessage & { 2 body?: any, 3 params?: { [key: string]: string }, 4 query?: { [key: string]: string }, 5 cookies?: { [key: string]: string }, 6 session?: any 7}
1export type MockFunction = { 2 ( 3 req: Request, 4 res: http.ServerResponse, 5 /** @deprecated in 2.0, use req.params **/ 6 urlVars?: { [key: string]: string } 7 ): void 8}
1export type MockHandler = { 2 pattern: string, 3 method?: string, 4 handle: MockFunction 5}
1export type MockLayer = ( 2 req: Request, 3 res: http.ServerResponse, 4 next: Connect.NextFunction 5) => void;
The pattern
is an ant-style path pattern string,
use @howiefh/ant-path-matcher
to match the pattern
and request URL
.
1// example/mock/es.mock.ts 2 3import { MockHandler } from '../../src' 4 5const mocks: MockHandler[] = [ 6 { 7 pattern: '/api/test1/1', 8 handle: (req, res) => { 9 res.end('Hello world!' + req.url) 10 } 11 }, 12 { 13 pattern: '/api/test1/*', 14 handle: (req, res) => { 15 res.end('Hello world!' + req.url) 16 } 17 }, 18 { 19 pattern: '/api/test1/users/{userId}', 20 handle: (req, res) => { 21 const data = { 22 url: req.url, 23 params: req.params, 24 query: req.query, 25 body: req.body 26 } 27 res.setHeader('Content-Type', 'application/json') 28 res.end(JSON.stringify(data)) 29 } 30 }, 31 { 32 pattern: '/api/test1/body/json', 33 method: 'POST', 34 handle: (req, res) => { 35 res.setHeader('Content-Type', 'application/json') 36 37 //req is incomingMessage which extends stream.Readable 38 // --> https://nodejs.org/api/stream.html#readablereadsize 39 // res.end need to be within the function 40 // there is a size limit for the bodyString to get parsed 41 req.on('data', (bodyString: string) => { 42 let body: object = JSON.parse(bodyString) 43 res.end(JSON.stringify(body)) 44 }) 45 } 46 }, 47] 48 49export default mocks 50 51// example/mock/apis/es2.mock.ts 52 53import { MockHandler } from 'vite-plugin-mock-server' 54 55export default (): MockHandler[] => [ 56 { 57 pattern: '/api/test2/1', 58 handle: (req, res) => { 59 res.end('Hello world!' + req.url) 60 } 61 }, 62 { 63 pattern: '/api/test2/2', 64 handle: (req, res) => { 65 res.statusCode = 203 66 res.end('Hello world!' + req.url) 67 } 68 } 69]
1// example/mock/cjs.mock.js 2 3module.exports = [ 4 { 5 pattern: '/api/merchant1', 6 method: 'GET', 7 handle: (req, res) => { 8 res.end('merchant1:' + req.url) 9 } 10 }, 11 { 12 pattern: '/api/merchant2', 13 method: 'GET', 14 handle: (req, res) => { 15 res.end('merchant2:' + req.url) 16 } 17 }, 18 { 19 pattern: '/api/merchant2', 20 method: 'GET', 21 handle: (req, res) => { 22 res.end('merchant3:' + req.url) 23 } 24 } 25] 26 27// example/mock/apis/cjs2.mock.js 28 29module.exports = [ 30 { 31 pattern: '/api/hello1', 32 method: 'GET', 33 handle: (req, res) => { 34 res.end('hello1:' + req.url) 35 } 36 }, 37 { 38 pattern: '/api/hello2', 39 method: 'GET', 40 handle: (req, res) => { 41 res.end('hello2:' + req.url) 42 } 43 }, 44 { 45 pattern: '/api/hello3', 46 method: 'GET', 47 handle: (req, res) => { 48 res.end('hello2:' + req.url) 49 } 50 } 51]
MIT
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
Reason
dependency not pinned by hash detected -- score normalized to 6
Details
Reason
Found 4/11 approved changesets -- score normalized to 3
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
security policy file not detected
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
13 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-03-03
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More