mocker-api
Advanced tools
Comparing version
@@ -1,4 +0,20 @@ | ||
import { Mocker } from './'; | ||
export default function name(proxy: Mocker, timer?: number): { | ||
[key: string]: any; | ||
}; | ||
import { MockerProxyRoute, MockerResult } from './'; | ||
/** | ||
* You can use functional tool to enhance mock. [#17](https://github.com/jaywcjlove/webpack-api-mocker/issues/17) | ||
* | ||
* ```js | ||
* const delay = require('mocker-api/lib/delay'); | ||
* const noProxy = process.env.NO_PROXY === 'true'; | ||
* | ||
* const proxy = { | ||
* 'GET /api/user': { | ||
* id: 1, | ||
* username: 'kenny', | ||
* sex: 6 | ||
* }, | ||
* // ... | ||
* } | ||
* module.exports = (noProxy ? {} : delay(proxy, 1000)); | ||
* ``` | ||
*/ | ||
export default function delay(proxy: MockerProxyRoute, timer?: number): MockerResult; |
@@ -6,5 +6,23 @@ "use strict"; | ||
}); | ||
exports["default"] = name; | ||
exports["default"] = delay; | ||
function name(proxy) { | ||
/** | ||
* You can use functional tool to enhance mock. [#17](https://github.com/jaywcjlove/webpack-api-mocker/issues/17) | ||
* | ||
* ```js | ||
* const delay = require('mocker-api/lib/delay'); | ||
* const noProxy = process.env.NO_PROXY === 'true'; | ||
* | ||
* const proxy = { | ||
* 'GET /api/user': { | ||
* id: 1, | ||
* username: 'kenny', | ||
* sex: 6 | ||
* }, | ||
* // ... | ||
* } | ||
* module.exports = (noProxy ? {} : delay(proxy, 1000)); | ||
* ``` | ||
*/ | ||
function delay(proxy) { | ||
var timer = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0; | ||
@@ -11,0 +29,0 @@ var mockApi = {}; |
@@ -9,26 +9,69 @@ /// <reference types="node" /> | ||
import chokidar from 'chokidar'; | ||
declare type ProxyTargetUrl = string | Partial<URL.Url>; | ||
export declare type ProxyTargetUrl = string | Partial<URL.Url>; | ||
export declare type MockerResultFunction = ((req: Request, res: Response, next?: NextFunction) => void); | ||
export declare type MockerResult = string | { | ||
[key: string]: any; | ||
} | MockerResultFunction; | ||
export interface Mocker { | ||
export declare type MockerResult = string | number | Array<any> | Record<string, any> | MockerResultFunction; | ||
/** | ||
* Setting a proxy router. | ||
* @example | ||
* | ||
* ```json | ||
* { | ||
* '/api/user': { | ||
* id: 1, | ||
* username: 'kenny', | ||
* sex: 6 | ||
* }, | ||
* 'DELETE /api/user/:id': (req, res) => { | ||
* res.send({ status: 'ok', message: '删除成功!' }); | ||
* } | ||
* } | ||
* ``` | ||
*/ | ||
export interface MockerProxyRoute extends Record<string, MockerResult> { | ||
/** | ||
* This is the option parameter setting for apiMocker | ||
* Priority processing. | ||
* apiMocker(app, path, option) | ||
* {@link MockerOption} | ||
*/ | ||
_proxy?: MockerOption; | ||
[key: string]: MockerResult; | ||
} | ||
export declare type HttpProxyListeners = { | ||
start: (req: http.IncomingMessage, res: http.ServerResponse, target: ProxyTargetUrl) => void; | ||
proxyReq: (proxyReq: http.ClientRequest, req: http.IncomingMessage, res: http.ServerResponse, options: httpProxy.ServerOptions) => void; | ||
proxyRes: (proxyRes: http.IncomingMessage, req: http.IncomingMessage, res: http.ServerResponse) => void; | ||
proxyReqWs: (proxyReq: http.ClientRequest, req: http.IncomingMessage, socket: net.Socket, options: httpProxy.ServerOptions, head: any) => void; | ||
econnreset: (err: Error, req: http.IncomingMessage, res: http.ServerResponse, target: ProxyTargetUrl) => void; | ||
end: (req: http.IncomingMessage, res: http.ServerResponse, proxyRes: http.IncomingMessage) => void; | ||
close: (proxyRes: http.IncomingMessage, proxySocket: net.Socket, proxyHead: any) => void; | ||
} & { | ||
[key: string]: () => void; | ||
}; | ||
/** | ||
* Listening for proxy events. | ||
* This options contains listeners for [node-http-proxy](https://github.com/http-party/node-http-proxy#listening-for-proxy-events). | ||
* {typeof httpProxy.on} | ||
* {@link httpProxy} | ||
*/ | ||
export interface HttpProxyListeners extends Record<string, any> { | ||
start?: (req: http.IncomingMessage, res: http.ServerResponse, target: ProxyTargetUrl) => void; | ||
proxyReq?: (proxyReq: http.ClientRequest, req: http.IncomingMessage, res: http.ServerResponse, options: httpProxy.ServerOptions) => void; | ||
proxyRes?: (proxyRes: http.IncomingMessage, req: http.IncomingMessage, res: http.ServerResponse) => void; | ||
proxyReqWs?: (proxyReq: http.ClientRequest, req: http.IncomingMessage, socket: net.Socket, options: httpProxy.ServerOptions, head: any) => void; | ||
econnreset?: (err: Error, req: http.IncomingMessage, res: http.ServerResponse, target: ProxyTargetUrl) => void; | ||
end?: (req: http.IncomingMessage, res: http.ServerResponse, proxyRes: http.IncomingMessage) => void; | ||
/** | ||
* This event is emitted once the proxy websocket was closed. | ||
*/ | ||
close?: (proxyRes: http.IncomingMessage, proxySocket: net.Socket, proxyHead: any) => void; | ||
} | ||
export interface MockerOption { | ||
/** | ||
* `Boolean` Setting req headers host. | ||
*/ | ||
changeHost?: boolean; | ||
/** | ||
* rewrite target's url path. | ||
* Object-keys will be used as RegExp to match paths. [#62](https://github.com/jaywcjlove/mocker-api/issues/62) | ||
* @default `{}` | ||
*/ | ||
pathRewrite?: Record<string, string>; | ||
/** | ||
* Proxy settings, Turn a path string such as `/user/:name` into a regular expression. [path-to-regexp](https://www.npmjs.com/package/path-to-regexp) | ||
* @default `{}` | ||
*/ | ||
proxy?: Record<string, string>; | ||
/** | ||
* Set the [listen event](https://github.com/nodejitsu/node-http-proxy#listening-for-proxy-events) and [configuration](https://github.com/nodejitsu/node-http-proxy#options) of [http-proxy](https://github.com/nodejitsu/node-http-proxy) | ||
* @default `{}` | ||
*/ | ||
httpProxy?: { | ||
@@ -38,15 +81,56 @@ options?: httpProxy.ServerOptions; | ||
}; | ||
/** | ||
* bodyParser settings. | ||
* @example | ||
* | ||
* ```js | ||
* bodyParser = {"text/plain": "text","text/html": "text"} | ||
* ``` | ||
* | ||
* will parsed `Content-Type='text/plain' and Content-Type='text/html'` with `bodyParser.text` | ||
* | ||
* @default `{}` | ||
*/ | ||
bodyParserConf?: { | ||
[key: string]: 'raw' | 'text' | 'urlencoded' | 'json'; | ||
}; | ||
/** | ||
* [`bodyParserJSON`](https://github.com/expressjs/body-parser/tree/56a2b73c26b2238bc3050ad90af9ab9c62f4eb97#bodyparserjsonoptions) JSON body parser | ||
* @default `{}` | ||
*/ | ||
bodyParserJSON?: bodyParser.OptionsJson; | ||
/** | ||
* [`bodyParserText`](https://github.com/expressjs/body-parser/tree/56a2b73c26b2238bc3050ad90af9ab9c62f4eb97#bodyparsertextoptions) Text body parser | ||
* @default `{}` | ||
*/ | ||
bodyParserText?: bodyParser.OptionsText; | ||
/** | ||
* [`bodyParserRaw`](https://github.com/expressjs/body-parser/tree/56a2b73c26b2238bc3050ad90af9ab9c62f4eb97#bodyparserrawoptions) Raw body parser | ||
* @default `{}` | ||
*/ | ||
bodyParserRaw?: bodyParser.Options; | ||
/** | ||
* [`bodyParserUrlencoded`](https://github.com/expressjs/body-parser/tree/56a2b73c26b2238bc3050ad90af9ab9c62f4eb97#bodyparserurlencodedoptions) URL-encoded form body parser | ||
* @default `{}` | ||
*/ | ||
bodyParserUrlencoded?: bodyParser.OptionsUrlencoded; | ||
/** | ||
* Options object as defined [chokidar api options](https://github.com/paulmillr/chokidar#api) | ||
* @default `{}` | ||
*/ | ||
watchOptions?: chokidar.WatchOptions; | ||
header?: { | ||
[key: string]: string | number | string[]; | ||
}; | ||
/** | ||
* Access Control Allow options. | ||
* @default `{}` | ||
* @example | ||
* ```js | ||
* { | ||
* header: { | ||
* 'Access-Control-Allow-Methods': 'POST, GET, OPTIONS, PUT, DELETE', | ||
* } | ||
* } | ||
* ``` | ||
*/ | ||
header?: Record<string, string | number | string[]>; | ||
} | ||
export default function (app: Application, watchFile: string | string[] | Mocker, conf?: MockerOption): (req: Request, res: Response, next: NextFunction) => void; | ||
export {}; | ||
export default function (app: Application, watchFile: string | string[] | MockerProxyRoute, conf?: MockerOption): (req: Request, res: Response, next: NextFunction) => void; |
{ | ||
"name": "mocker-api", | ||
"version": "2.2.0", | ||
"version": "2.3.0", | ||
"description": "This is dev support mock RESTful API.", | ||
@@ -16,2 +16,3 @@ "bin": { | ||
"build:types": "tsbb types", | ||
"doc": "typedoc", | ||
"test": "tsbb test", | ||
@@ -52,3 +53,3 @@ "coverage": "tsbb test --coverage" | ||
"body-parser": "1.19.0", | ||
"chokidar": "3.4.1", | ||
"chokidar": "3.4.2", | ||
"clear-module": "4.1.1", | ||
@@ -65,4 +66,5 @@ "colors-cli": "1.0.27", | ||
"@types/jest": "25.2.1", | ||
"tsbb": "1.7.6" | ||
"tsbb": "1.7.6", | ||
"typedoc": "0.17.8" | ||
} | ||
} |
@@ -1,6 +0,24 @@ | ||
import { Request, Response, NextFunction } from 'express'; | ||
import { Mocker, MockerResultFunction } from './'; | ||
import { Request, Response } from 'express'; | ||
import { MockerProxyRoute, MockerResult, MockerResultFunction } from './'; | ||
export default function name(proxy: Mocker, timer: number = 0) { | ||
const mockApi: { [key: string]: any } | ((req: Request, res: Response, next: NextFunction) => void) = {}; | ||
/** | ||
* You can use functional tool to enhance mock. [#17](https://github.com/jaywcjlove/webpack-api-mocker/issues/17) | ||
* | ||
* ```js | ||
* const delay = require('mocker-api/lib/delay'); | ||
* const noProxy = process.env.NO_PROXY === 'true'; | ||
* | ||
* const proxy = { | ||
* 'GET /api/user': { | ||
* id: 1, | ||
* username: 'kenny', | ||
* sex: 6 | ||
* }, | ||
* // ... | ||
* } | ||
* module.exports = (noProxy ? {} : delay(proxy, 1000)); | ||
* ``` | ||
*/ | ||
export default function delay(proxy: MockerProxyRoute, timer: number = 0): MockerResult { | ||
const mockApi: MockerResult = {}; | ||
Object.keys(proxy).forEach((key) => { | ||
@@ -7,0 +25,0 @@ const result = proxy[key]; |
128
src/index.ts
@@ -14,14 +14,41 @@ import URL from 'url'; | ||
type ProxyTargetUrl = string | Partial<URL.Url>; | ||
export type ProxyTargetUrl = string | Partial<URL.Url>; | ||
export type MockerResultFunction = ((req: Request, res: Response, next?: NextFunction) => void); | ||
export type MockerResult = string | { [key: string]: any } | MockerResultFunction; | ||
export type MockerResult = string | number| Array<any> | Record<string, any> | MockerResultFunction; | ||
export interface Mocker { | ||
/** | ||
* Setting a proxy router. | ||
* @example | ||
* | ||
* ```json | ||
* { | ||
* '/api/user': { | ||
* id: 1, | ||
* username: 'kenny', | ||
* sex: 6 | ||
* }, | ||
* 'DELETE /api/user/:id': (req, res) => { | ||
* res.send({ status: 'ok', message: '删除成功!' }); | ||
* } | ||
* } | ||
* ``` | ||
*/ | ||
export interface MockerProxyRoute extends Record<string, MockerResult> { | ||
/** | ||
* This is the option parameter setting for apiMocker | ||
* Priority processing. | ||
* apiMocker(app, path, option) | ||
* {@link MockerOption} | ||
*/ | ||
_proxy?: MockerOption; | ||
[key: string]: MockerResult; | ||
} | ||
export type HttpProxyListeners = { | ||
start: ( | ||
/** | ||
* Listening for proxy events. | ||
* This options contains listeners for [node-http-proxy](https://github.com/http-party/node-http-proxy#listening-for-proxy-events). | ||
* {typeof httpProxy.on} | ||
* {@link httpProxy} | ||
*/ | ||
export interface HttpProxyListeners extends Record<string, any> { | ||
start?: ( | ||
req: http.IncomingMessage, | ||
@@ -31,3 +58,3 @@ res: http.ServerResponse, | ||
) => void; | ||
proxyReq: ( | ||
proxyReq?: ( | ||
proxyReq: http.ClientRequest, | ||
@@ -38,3 +65,3 @@ req: http.IncomingMessage, | ||
) => void | ||
proxyRes: ( | ||
proxyRes?: ( | ||
proxyRes: http.IncomingMessage, | ||
@@ -44,3 +71,3 @@ req: http.IncomingMessage, | ||
) => void | ||
proxyReqWs: ( | ||
proxyReqWs?: ( | ||
proxyReq: http.ClientRequest, | ||
@@ -52,3 +79,3 @@ req: http.IncomingMessage, | ||
) => void | ||
econnreset: ( | ||
econnreset?: ( | ||
err: Error, | ||
@@ -59,8 +86,11 @@ req: http.IncomingMessage, | ||
) => void | ||
end: ( | ||
end?: ( | ||
req: http.IncomingMessage, | ||
res: http.ServerResponse, | ||
proxyRes: http.IncomingMessage | ||
) => void | ||
close: ( | ||
) => void; | ||
/** | ||
* This event is emitted once the proxy websocket was closed. | ||
*/ | ||
close?: ( | ||
proxyRes: http.IncomingMessage, | ||
@@ -70,10 +100,24 @@ proxySocket: net.Socket, | ||
) => void | ||
} & { | ||
[key: string]: () => void; | ||
} | ||
export interface MockerOption { | ||
/** | ||
* `Boolean` Setting req headers host. | ||
*/ | ||
changeHost?: boolean; | ||
/** | ||
* rewrite target's url path. | ||
* Object-keys will be used as RegExp to match paths. [#62](https://github.com/jaywcjlove/mocker-api/issues/62) | ||
* @default `{}` | ||
*/ | ||
pathRewrite?: Record<string, string>, | ||
/** | ||
* Proxy settings, Turn a path string such as `/user/:name` into a regular expression. [path-to-regexp](https://www.npmjs.com/package/path-to-regexp) | ||
* @default `{}` | ||
*/ | ||
proxy?: Record<string, string>, | ||
/** | ||
* Set the [listen event](https://github.com/nodejitsu/node-http-proxy#listening-for-proxy-events) and [configuration](https://github.com/nodejitsu/node-http-proxy#options) of [http-proxy](https://github.com/nodejitsu/node-http-proxy) | ||
* @default `{}` | ||
*/ | ||
httpProxy?: { | ||
@@ -83,17 +127,59 @@ options?: httpProxy.ServerOptions; | ||
}; | ||
/** | ||
* bodyParser settings. | ||
* @example | ||
* | ||
* ```js | ||
* bodyParser = {"text/plain": "text","text/html": "text"} | ||
* ``` | ||
* | ||
* will parsed `Content-Type='text/plain' and Content-Type='text/html'` with `bodyParser.text` | ||
* | ||
* @default `{}` | ||
*/ | ||
bodyParserConf?: { | ||
[key: string]: 'raw' | 'text' | 'urlencoded' | 'json'; | ||
}; | ||
/** | ||
* [`bodyParserJSON`](https://github.com/expressjs/body-parser/tree/56a2b73c26b2238bc3050ad90af9ab9c62f4eb97#bodyparserjsonoptions) JSON body parser | ||
* @default `{}` | ||
*/ | ||
bodyParserJSON?: bodyParser.OptionsJson; | ||
/** | ||
* [`bodyParserText`](https://github.com/expressjs/body-parser/tree/56a2b73c26b2238bc3050ad90af9ab9c62f4eb97#bodyparsertextoptions) Text body parser | ||
* @default `{}` | ||
*/ | ||
bodyParserText?: bodyParser.OptionsText; | ||
/** | ||
* [`bodyParserRaw`](https://github.com/expressjs/body-parser/tree/56a2b73c26b2238bc3050ad90af9ab9c62f4eb97#bodyparserrawoptions) Raw body parser | ||
* @default `{}` | ||
*/ | ||
bodyParserRaw?: bodyParser.Options; | ||
/** | ||
* [`bodyParserUrlencoded`](https://github.com/expressjs/body-parser/tree/56a2b73c26b2238bc3050ad90af9ab9c62f4eb97#bodyparserurlencodedoptions) URL-encoded form body parser | ||
* @default `{}` | ||
*/ | ||
bodyParserUrlencoded?: bodyParser.OptionsUrlencoded; | ||
/** | ||
* Options object as defined [chokidar api options](https://github.com/paulmillr/chokidar#api) | ||
* @default `{}` | ||
*/ | ||
watchOptions?: chokidar.WatchOptions; | ||
header?: { | ||
[key: string]: string | number | string[]; | ||
} | ||
/** | ||
* Access Control Allow options. | ||
* @default `{}` | ||
* @example | ||
* ```js | ||
* { | ||
* header: { | ||
* 'Access-Control-Allow-Methods': 'POST, GET, OPTIONS, PUT, DELETE', | ||
* } | ||
* } | ||
* ``` | ||
*/ | ||
header?: Record<string,string | number | string[]> | ||
} | ||
const pathToRegexp = toRegexp.pathToRegexp; | ||
let mocker: Mocker = {}; | ||
let mocker: MockerProxyRoute = {}; | ||
@@ -122,3 +208,3 @@ function pathMatch(options: TokensToRegexpOptions & ParseOptions) { | ||
export default function (app: Application, watchFile: string | string[] | Mocker, conf: MockerOption = {}) { | ||
export default function (app: Application, watchFile: string | string[] | MockerProxyRoute, conf: MockerOption = {}) { | ||
const watchFiles = Array.isArray(watchFile) ? watchFile : typeof watchFile === 'string' ? [watchFile] : []; | ||
@@ -125,0 +211,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
87088
13.53%1047
27.06%4
33.33%+ Added
- Removed
Updated