vite-plugin-mock-server
Advanced tools
Comparing version 1.0.4 to 1.1.0
/// <reference types="node" /> | ||
import { Plugin, Connect } from 'vite'; | ||
import * as http from 'http'; | ||
declare type Request = Connect.IncomingMessage & { | ||
body?: any; | ||
params?: { | ||
[key: string]: string; | ||
}; | ||
query?: { | ||
[key: string]: string; | ||
}; | ||
cookies?: { | ||
[key: string]: string; | ||
}; | ||
session?: any; | ||
}; | ||
export declare type MockFunction = { | ||
(req: Connect.IncomingMessage, res: http.ServerResponse, urlVars?: { | ||
(req: Request, res: http.ServerResponse, | ||
/** @deprecated in 2.0, use req.params **/ | ||
urlVars?: { | ||
[key: string]: string; | ||
}): void; | ||
}; | ||
export declare type MockLayer = (req: Request, res: http.ServerResponse, next: Connect.NextFunction) => void; | ||
export declare type MockHandler = { | ||
@@ -22,4 +38,5 @@ pattern: string; | ||
noHandlerResponse404?: boolean; | ||
middlewares?: MockLayer[]; | ||
}; | ||
declare const _default: (options?: MockOptions) => Plugin; | ||
export default _default; |
@@ -37,2 +37,7 @@ "use strict"; | ||
}); | ||
if (options.middlewares) { | ||
for (const [, layer] of options.middlewares.entries()) { | ||
server.middlewares.use(layer); | ||
} | ||
} | ||
server.middlewares.use((req, res, next) => { | ||
@@ -68,8 +73,4 @@ doHandle(options, matcher, req, res, next); | ||
for (const [, handler] of handlers.entries()) { | ||
let path = req.url; | ||
const index = path.indexOf('?'); | ||
if (index > 0) { | ||
path = path.substring(0, index); | ||
} | ||
let pathVars = {}; | ||
const [path, qs] = req.url.split('?'); | ||
const pathVars = {}; | ||
let matched = matcher.doMatch(handler.pattern, path, true, pathVars); | ||
@@ -81,2 +82,4 @@ if (matched && handler.method) { | ||
logInfo('matched and call mock handler', handler, 'pathVars', pathVars); | ||
req.params = pathVars; | ||
req.query = parseQueryString(qs); | ||
handler.handle(req, res, { ...pathVars }); | ||
@@ -210,2 +213,9 @@ return; | ||
}; | ||
const parseQueryString = (qs) => !qs ? {} : decodeURI(qs) | ||
.split('&') | ||
.map(param => param.split('=')) | ||
.reduce((values, [key, value]) => { | ||
values[key] = value; | ||
return values; | ||
}, {}); | ||
const logErr = (...optionalParams) => { | ||
@@ -212,0 +222,0 @@ if (LOG_LEVEL === 'off') |
{ | ||
"name": "vite-plugin-mock-server", | ||
"version": "1.0.4", | ||
"version": "1.1.0", | ||
"description": "Vite mock server plugin", | ||
@@ -20,5 +20,5 @@ "main": "dist/index.js", | ||
"author": { | ||
"name": "牧码山人", | ||
"email": "mumashanren@gmail.com", | ||
"url": "https://github.com/mumashanren" | ||
"name": "Octoape", | ||
"email": "octoape@qq.com", | ||
"url": "https://github.com/octoape" | ||
}, | ||
@@ -25,0 +25,0 @@ "license": "MIT", |
@@ -5,5 +5,8 @@ # vite-plugin-mock-server | ||
Provide local mocks for [Vite](https://vitejs.dev). | ||
Provide local mocks for [Vite]. | ||
A mock server plugin for [Vite](https://vitejs.dev), 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. | ||
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](https://github.com/expressjs/)*** middlewares. | ||
@@ -33,3 +36,3 @@ ## Install | ||
- Config plugin in vite.config.ts | ||
- Config plugin in vite.config.ts, compatible with express.js middlewares. | ||
@@ -39,3 +42,5 @@ ```ts | ||
import vue from '@vitejs/plugin-vue' | ||
import mockServer from 'vite-plugin-mock-server' | ||
import bodyParser from 'body-parser' | ||
import cookieParser from 'cookie-parser' | ||
import mockServer from '../src' | ||
@@ -46,3 +51,10 @@ export default defineConfig({ | ||
mockServer({ | ||
logLevel: 'info' | ||
logLevel: 'info', | ||
middlewares: [ | ||
cookieParser(), | ||
bodyParser.json(), | ||
bodyParser.urlencoded(), | ||
bodyParser.text(), | ||
bodyParser.raw() | ||
] | ||
}) | ||
@@ -68,2 +80,3 @@ ] | ||
noHandlerResponse404?: boolean | ||
middlewares?: MockLayer[] | ||
} | ||
@@ -79,6 +92,19 @@ | ||
noHandlerResponse404: true, | ||
mockModules: [] | ||
mockModules: [], | ||
middlewares: [] | ||
} | ||
``` | ||
- Request | ||
```ts | ||
type Request = Connect.IncomingMessage & { | ||
body?: any, | ||
params?: { [key: string]: string }, | ||
query?: { [key: string]: string }, | ||
cookies?: { [key: string]: string }, | ||
session?: any | ||
} | ||
``` | ||
- MockFunction | ||
@@ -88,3 +114,8 @@ | ||
export type MockFunction = { | ||
(req: Connect.IncomingMessage, res: http.ServerResponse, urlVars?: { [key: string]: string }): void | ||
( | ||
req: Request, | ||
res: http.ServerResponse, | ||
/** @deprecated in 2.0, use req.params **/ | ||
urlVars?: { [key: string]: string } | ||
): void | ||
} | ||
@@ -103,5 +134,17 @@ ``` | ||
- MockLayer | ||
```ts | ||
export type MockLayer = ( | ||
req: Request, | ||
res: http.ServerResponse, | ||
next: Connect.NextFunction | ||
) => void; | ||
``` | ||
## Mock file examples | ||
The `pattern` is an ant-style path pattern string, use ***[@howiefh/ant-path-matcher](https://www.npmjs.com/package/@howiefh/ant-path-matcher)*** to match the `pattern` and `request URL`. | ||
The `pattern` is an ant-style path pattern string, | ||
use ***[@howiefh/ant-path-matcher](https://www.npmjs.com/package/@howiefh/ant-path-matcher)*** | ||
to match the `pattern` and `request URL`. | ||
@@ -111,2 +154,4 @@ ```ts | ||
import { MockHandler } from '../../src' | ||
const mocks: MockHandler[] = [ | ||
@@ -127,6 +172,8 @@ { | ||
pattern: '/api/test1/users/{userId}', | ||
handle: (req, res, pathVars) => { | ||
handle: (req, res) => { | ||
const data = { | ||
url: req.url, | ||
pathVars: pathVars | ||
params: req.params, | ||
query: req.query, | ||
body: req.body | ||
} | ||
@@ -138,12 +185,9 @@ res.setHeader('Content-Type', 'application/json') | ||
{ | ||
pattern: '/api/test1/users/{userId}/{blogId}', | ||
handle: (req, res, pathVars) => { | ||
const data = { | ||
url: req.url, | ||
pathVars: pathVars | ||
} | ||
pattern: '/api/test1/body/json', | ||
method: 'POST', | ||
handle: (req, res) => { | ||
res.setHeader('Content-Type', 'application/json') | ||
res.end(JSON.stringify(data)) | ||
res.end(JSON.stringify(req.body)) | ||
} | ||
} | ||
}, | ||
] | ||
@@ -233,2 +277,2 @@ | ||
[npm-url]: https://npmjs.com/package/vite-plugin-mock-server | ||
[vite-url]: https://vitejs.dev | ||
[Vite]: https://vitejs.dev |
@@ -13,6 +13,25 @@ import { Plugin, ViteDevServer, Connect } from 'vite' | ||
type Request = Connect.IncomingMessage & { | ||
body?: any, | ||
params?: { [key: string]: string }, | ||
query?: { [key: string]: string }, | ||
cookies?: { [key: string]: string }, | ||
session?: any | ||
} | ||
export type MockFunction = { | ||
(req: Connect.IncomingMessage, res: http.ServerResponse, urlVars?: { [key: string]: string }): void | ||
( | ||
req: Request, | ||
res: http.ServerResponse, | ||
/** @deprecated in 2.0, use req.params **/ | ||
urlVars?: { [key: string]: string } | ||
): void | ||
} | ||
export type MockLayer = ( | ||
req: Request, | ||
res: http.ServerResponse, | ||
next: Connect.NextFunction | ||
) => void; | ||
export type MockHandler = { | ||
@@ -32,2 +51,3 @@ pattern: string, | ||
noHandlerResponse404?: boolean | ||
middlewares?: MockLayer[] | ||
} | ||
@@ -59,2 +79,7 @@ | ||
}) | ||
if (options.middlewares) { | ||
for (const [, layer] of options.middlewares.entries()) { | ||
server.middlewares.use(layer); | ||
} | ||
} | ||
server.middlewares.use(( | ||
@@ -74,3 +99,3 @@ req: Connect.IncomingMessage, | ||
matcher: AntPathMatcher, | ||
req: Connect.IncomingMessage, | ||
req: Request, | ||
res: http.ServerResponse, | ||
@@ -99,8 +124,4 @@ next: Connect.NextFunction | ||
for (const [, handler] of handlers.entries()) { | ||
let path = req.url | ||
const index = path.indexOf('?') | ||
if (index > 0) { | ||
path = path.substring(0, index) | ||
} | ||
let pathVars: { [key: string]: string } = {} | ||
const [path, qs] = req.url.split('?') | ||
const pathVars: { [key: string]: string } = {}; | ||
let matched = matcher.doMatch(handler.pattern, path, true, pathVars) | ||
@@ -110,4 +131,7 @@ if (matched && handler.method) { | ||
} | ||
if (matched) { | ||
logInfo('matched and call mock handler', handler, 'pathVars', pathVars) | ||
req.params = pathVars; | ||
req.query = parseQueryString(qs); | ||
handler.handle(req, res, { ...pathVars }) | ||
@@ -236,5 +260,13 @@ return | ||
const parseQueryString = (qs: string): { [key: string]: string } => !qs ? {} : decodeURI(qs) | ||
.split('&') | ||
.map(param => param.split('=')) | ||
.reduce((values: { [key: string]: string }, [key, value]) => { | ||
values[key] = value; | ||
return values; | ||
}, {}) | ||
const logErr = (...optionalParams: any[]) => { | ||
if (LOG_LEVEL === 'off') return | ||
console.error('[vite-plugin-mock-server]', optionalParams) | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
34336
532
266
1