autoresponse
Advanced tools
Comparing version 0.3.0 to 0.4.0
@@ -31,7 +31,7 @@ /** | ||
* lodash api: <https://lodash.com/docs> | ||
* moment api: <http://momentjs.com/docs/> | ||
* dayjs api: <https://github.com/iamkun/dayjs> | ||
* | ||
* @type {Object} | ||
*/ | ||
helper: {_: 'lodash', m: 'moment'}, | ||
helper: {_: 'lodash', m: 'dayjs'}, | ||
@@ -38,0 +38,0 @@ |
@@ -0,1 +1,8 @@ | ||
0.3.0 / 2020-01-21 | ||
=================== | ||
* [^] 更新依赖,要求 `Node >= 8` | ||
* [^] mock 默认辅助的工具方法,使用更轻量级的日期库 [dayjs](https://github.com/iamkun/dayjs) 替代 [moment](http://momentjs.com/docs/) 日期库 | ||
0.3.0 / 2018-11-30 | ||
@@ -2,0 +9,0 @@ =================== |
@@ -29,7 +29,7 @@ /** | ||
* lodash api: <https://lodash.com/docs> | ||
* moment api: <http://momentjs.com/docs/> | ||
* dayjs api: <https://github.com/iamkun/dayjs> | ||
* | ||
* @type {Object} | ||
*/ | ||
helper: {_: 'lodash', m: 'moment'}, | ||
helper: {_: 'lodash', m: 'dayjs'}, | ||
/* eslint-enable fecs-camelcase */ | ||
@@ -36,0 +36,0 @@ |
@@ -9,3 +9,3 @@ /** | ||
var _ = require('lodash'); | ||
var mime = require('mime'); | ||
var mime = require('mime-types'); | ||
var formidable = require('formidable'); | ||
@@ -52,3 +52,3 @@ var webProxy = require('./autoresponse-proxy'); | ||
var contentType = resData.type; | ||
var charset = mime.charsets.lookup(contentType); | ||
var charset = mime.charset(contentType); | ||
if (charset) { | ||
@@ -275,3 +275,3 @@ contentType += (';charset=' + charset); | ||
_.assign(result, { | ||
type: mime.lookup(resType ? ('.' + resType) : '.txt'), | ||
type: mime.lookup(resType ? ('.' + resType) : '.txt') || 'text/plain', | ||
data: String(data) | ||
@@ -278,0 +278,0 @@ }); |
@@ -85,3 +85,3 @@ /** | ||
* @param {string} format 信息的模板,使用 mustache 模板定义,模板变量参考 | ||
* <http://momentjs.com/docs/>,e.g., '{{name.firstName}}-{{name.lastName}}' | ||
* <https://github.com/marak/Faker.js/>,e.g., '{{name.firstName}}-{{name.lastName}}' | ||
* @param {string} locale 语言类型 | ||
@@ -88,0 +88,0 @@ * @return {string} |
@@ -8,3 +8,3 @@ /** | ||
var path2regexp = require('path-to-regexp'); | ||
var { pathToRegexp, parse } = require('path-to-regexp'); | ||
var _ = require('lodash'); | ||
@@ -38,4 +38,4 @@ var util = require('./autoresponse-util'); | ||
var args = []; | ||
var regexp = path2regexp(reqPath, args); | ||
var tokens = args.length ? path2regexp.parse(reqPath) : null; | ||
var regexp = pathToRegexp(reqPath, args); | ||
var tokens = args.length ? parse(reqPath) : null; | ||
return { | ||
@@ -42,0 +42,0 @@ path: reqPath, |
{ | ||
"name": "autoresponse", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"description": "Autoresponse middleare using local data or proxy", | ||
@@ -25,12 +25,12 @@ "main": "index.js", | ||
"dependencies": { | ||
"chalk": "^2.4.1", | ||
"chokidar": "^2.0.4", | ||
"chalk": "^3.0.0", | ||
"chokidar": "^3.3.1", | ||
"dayjs": "^1.8.19", | ||
"etpl": "^3.2.0", | ||
"faker": "^4.1.0", | ||
"formidable": "^1.0.17", | ||
"http-proxy": "^1.16.2", | ||
"lodash": "^4.17.11", | ||
"mime": "^1.3.4", | ||
"moment": "^2.17.1", | ||
"path-to-regexp": "^1.7.0", | ||
"formidable": "^1.2.1", | ||
"http-proxy": "^1.18.0", | ||
"lodash": "^4.17.15", | ||
"mime-types": "^2.1.26", | ||
"path-to-regexp": "^6.1.0", | ||
"qs": "^6.3.0" | ||
@@ -37,0 +37,0 @@ }, |
185
README.md
@@ -0,5 +1,3 @@ | ||
# autoresponse [![NPM Version](https://img.shields.io/npm/v/autoresponse.svg?style=flat)](https://npmjs.org/package/autoresponse) | ||
autoresponse [![NPM Version](https://img.shields.io/npm/v/autoresponse.svg?style=flat)](https://npmjs.org/package/autoresponse) | ||
======== | ||
> A connect middleware for mocking the http request, can used in `edp-webserver` or `webpack-dev-server` mocking | ||
@@ -9,2 +7,4 @@ | ||
**Require Node.js 8+** | ||
```shell | ||
@@ -19,8 +19,9 @@ npm install autoresponse | ||
```javascript | ||
var autoresponse = require('autoresponse')({ | ||
logLevel: 'info', // the level to print log info | ||
post: true, // mock all post request | ||
patch: true, // mock all patch request | ||
var autoresponse = require("autoresponse")({ | ||
logLevel: "info", // the level to print log info | ||
post: true, // mock all post request | ||
patch: true, // mock all patch request | ||
get: { | ||
match: function (reqPathName) { // mock all `/xx/xx` path | ||
match: function(reqPathName) { | ||
// mock all `/xx/xx` path | ||
return !/\.\w+(\?.*)?$/.test(reqPathName); | ||
@@ -36,13 +37,13 @@ } | ||
// by default mock all methods, if method option is not set | ||
match: '/users/:id', // by default use `users.js` mock file | ||
method: ['get', 'patch'] | ||
match: "/users/:id", // by default use `users.js` mock file | ||
method: ["get", "patch"] | ||
}, | ||
{ | ||
match: function (reqPathName, reqMethod) { | ||
match: function(reqPathName, reqMethod) { | ||
return true; | ||
}, | ||
mock: function (reqPathName, reqMethod) { | ||
return 'custom/myMockFile.js'; | ||
mock: function(reqPathName, reqMethod) { | ||
return "custom/myMockFile.js"; | ||
}, | ||
method: 'post' | ||
method: "post" | ||
} | ||
@@ -56,12 +57,13 @@ ] | ||
The mock file like this: | ||
```javascript | ||
module.exports = function (path, queryParam, postParam, context) { | ||
module.exports = function(path, queryParam, postParam, context) { | ||
return { | ||
'timeout': 50, // response timeout, unit is millisecond, default is 0 | ||
'_timeout': 50, // The same as timeout | ||
'_status': 200, // The response http status code, by default 200 | ||
'_header': {}, // The response header | ||
'_data': {}, // The response mock data | ||
'_jsonp': false, // response jsonp | ||
'_callback': 'callback' // The jsonp callback param name, default: callback | ||
timeout: 50, // response timeout, unit is millisecond, default is 0 | ||
_timeout: 50, // The same as timeout | ||
_status: 200, // The response http status code, by default 200 | ||
_header: {}, // The response header | ||
_data: {}, // The response mock data | ||
_jsonp: false, // response jsonp | ||
_callback: "callback" // The jsonp callback param name, default: callback | ||
}; | ||
@@ -83,3 +85,3 @@ | ||
// mock the post request | ||
post: function (path, queryParam, postParam, context) { | ||
post: function(path, queryParam, postParam, context) { | ||
var params = context.params; // the restful path param | ||
@@ -94,3 +96,3 @@ return { | ||
status: 0, | ||
statusInfo: 'patch ok' | ||
statusInfo: "patch ok" | ||
} | ||
@@ -111,4 +113,4 @@ }; | ||
```javascript | ||
var autoresponse = require('autoresponse')({ | ||
logLevel: 'info', | ||
var autoresponse = require("autoresponse")({ | ||
logLevel: "info", | ||
post: true | ||
@@ -118,4 +120,4 @@ }); | ||
var serveStatic = require('serve-static'); | ||
app.use(serveStatic('./webroot')); | ||
var serveStatic = require("serve-static"); | ||
app.use(serveStatic("./webroot")); | ||
``` | ||
@@ -127,21 +129,23 @@ | ||
```javascript | ||
var compiler = Webpack(webpackConfig); | ||
var server = new WebpackDevServer(compiler, { | ||
// install middlewares | ||
setup: function (app) { | ||
var autoresponse = require('autoresponse'); | ||
app.use(autoresponse({ | ||
logLevel: 'debug', | ||
root: projectRootPath, // you can specify the project root path | ||
post: true, // mock all post request | ||
patch: true // mock all patch request | ||
})); | ||
} | ||
}); | ||
```javascript | ||
var compiler = Webpack(webpackConfig); | ||
var server = new WebpackDevServer(compiler, { | ||
// install middlewares | ||
setup: function(app) { | ||
var autoresponse = require("autoresponse"); | ||
app.use( | ||
autoresponse({ | ||
logLevel: "debug", | ||
root: projectRootPath, // you can specify the project root path | ||
post: true, // mock all post request | ||
patch: true // mock all patch request | ||
}) | ||
); | ||
} | ||
}); | ||
server.listen(8888, function() { | ||
console.log('Starting server on port 8888...'); | ||
}); | ||
``` | ||
server.listen(8888, function() { | ||
console.log("Starting server on port 8888..."); | ||
}); | ||
``` | ||
@@ -153,22 +157,17 @@ ## Using in edp-webserver | ||
```javascript | ||
exports.getLocations = function () { | ||
exports.getLocations = function() { | ||
return [ | ||
{ | ||
location: '/', | ||
handler: home( 'index.html' ) | ||
location: "/", | ||
handler: home("index.html") | ||
}, | ||
{ | ||
location: /\.html\b.*$/, | ||
handler: [ | ||
file() | ||
] | ||
handler: [file()] | ||
}, | ||
// add autoresposne mock handler | ||
require('autoresponse')('edp', { watch: true, logLevel: 'info' }), | ||
require("autoresponse")("edp", { watch: true, logLevel: "info" }), | ||
{ | ||
location: /^.*$/, | ||
handler: [ | ||
file(), | ||
proxyNoneExists() | ||
] | ||
handler: [file(), proxyNoneExists()] | ||
} | ||
@@ -184,3 +183,3 @@ ]; | ||
```javascript | ||
var autoresponse = require('autoresponse')({ | ||
var autoresponse = require("autoresponse")({ | ||
// specify whether need auto reload config file when config file change | ||
@@ -196,3 +195,3 @@ watch: true | ||
// The response directory to mock, by default is `mock` | ||
responseDir: './mock', | ||
responseDir: "./mock", | ||
@@ -208,9 +207,9 @@ /** | ||
{ | ||
match: '/b.html', | ||
mock: 'c.html' | ||
match: "/b.html", | ||
mock: "c.html" | ||
}, | ||
{ | ||
match: '/account/getUserInfo', // also support regex and function | ||
match: "/account/getUserInfo", // also support regex and function | ||
mock: { | ||
proxy: 'localhost:9090' // use proxy | ||
proxy: "localhost:9090" // use proxy | ||
} | ||
@@ -221,22 +220,23 @@ }, | ||
// it'will be processed as a node module by builtin js-processor | ||
match: '/user/profile' | ||
match: "/user/profile" | ||
}, | ||
{ | ||
match: '/data/list', | ||
mock: 'data/list.json' | ||
match: "/data/list", | ||
mock: "data/list.json" | ||
}, | ||
{ | ||
match: '/php', | ||
match: "/php", | ||
mock: { | ||
path: '/data/test.php' // rewrite request path | ||
path: "/data/test.php" // rewrite request path | ||
} | ||
}, | ||
{ | ||
match: '/a/b', | ||
mock: 'a/b.php' // mock with php file which is processed by php processor | ||
match: "/a/b", | ||
mock: "a/b.php" // mock with php file which is processed by php processor | ||
}, | ||
'/account/getUserInfo', // specify the match path | ||
function (reqPath, context) { // using function to determine which request to mock | ||
"/account/getUserInfo", // specify the match path | ||
function(reqPath, context) { | ||
// using function to determine which request to mock | ||
return { | ||
match: 'a/b' | ||
match: "a/b" | ||
}; | ||
@@ -254,8 +254,9 @@ } | ||
* install php-cgi | ||
* [for mac](https://gist.github.com/xiangshouding/9359739) | ||
* [for windows](https://gist.github.com/lily-zhangying/9295c5221fa29d429d52) | ||
- install php-cgi | ||
* processor configure | ||
- [for mac](https://gist.github.com/xiangshouding/9359739) | ||
- [for windows](https://gist.github.com/lily-zhangying/9295c5221fa29d429d52) | ||
- processor configure | ||
```javascript | ||
@@ -303,11 +304,11 @@ // add this config to autoresponse | ||
* write smarty json data using js processor | ||
- write smarty json data using js processor | ||
* output html document | ||
- output html document | ||
```javascript | ||
module.exports = function (path, queryParam, postParam) { | ||
module.exports = function(path, queryParam, postParam) { | ||
return { | ||
// of course, you can specify the delay time with a random value between 0 and 100 | ||
_timeout: '0,100', | ||
_timeout: "0,100", | ||
@@ -318,6 +319,6 @@ // if you wanna simulate the special status, you can use this | ||
// tell autoresponse that the json data will be processed by smarty processor | ||
_process: 'smarty', | ||
_process: "smarty", | ||
// the smarty template name will be rendered | ||
_tpl: 'a/b.tpl', | ||
_tpl: "a/b.tpl", | ||
@@ -333,3 +334,3 @@ // define the template data to be applied to smarty template file | ||
* output json with smarty render result | ||
- output json with smarty render result | ||
@@ -359,3 +360,2 @@ | ||
## Using mock helper method <a name="helper"></a> | ||
@@ -367,20 +367,19 @@ | ||
* `mock._`: [lodash](https://lodash.com/docs) variable | ||
- `mock._`: [lodash](https://lodash.com/docs) variable | ||
* `mock.m`: [moment](http://momentjs.com/docs/) variable | ||
- `mock.m`: [dayjs](https://github.com/iamkun/dayjs) variable,using [moment](http://momentjs.com/docs/) before the `0.3.0` version | ||
* `mock.fake(format, locale)`: the encapsulation of [faker](https://github.com/Marak/faker.js/) | ||
- `mock.fake(format, locale)`: the encapsulation of [faker](https://github.com/Marak/faker.js/) | ||
```javascript | ||
// more api and variable name, please refer faker api docs | ||
mock.fake('{{name.firstName}}-{{name.lastName}}'); | ||
mock.fake("{{name.firstName}}-{{name.lastName}}"); | ||
``` | ||
* `mock.fakeCN(format)`: generate chinese locale random information | ||
- `mock.fakeCN(format)`: generate chinese locale random information | ||
* `mock.fakeEN(format)`: is equivalent to `mock.fake(format)`, generate english locale random information | ||
- `mock.fakeEN(format)`: is equivalent to `mock.fake(format)`, generate english locale random information | ||
* `mock.faker(locale)`: get `faker` instance with the specified locale, the locale argument is default english | ||
- `mock.faker(locale)`: get `faker` instance with the specified locale, the locale argument is default english | ||
More details, please refer to the [autoresponse-config.js](https://github.com/wuhy/autoresponse/blob/master/lib/autoresponse-config.js). |
505061
366
+ Addeddayjs@^1.8.19
+ Addedmime-types@^2.1.26
+ Addedansi-styles@4.3.0(transitive)
+ Addedanymatch@3.1.3(transitive)
+ Addedbinary-extensions@2.3.0(transitive)
+ Addedbraces@3.0.3(transitive)
+ Addedchalk@3.0.0(transitive)
+ Addedchokidar@3.6.0(transitive)
+ Addedcolor-convert@2.0.1(transitive)
+ Addedcolor-name@1.1.4(transitive)
+ Addeddayjs@1.11.13(transitive)
+ Addedfill-range@7.1.1(transitive)
+ Addedfsevents@2.3.3(transitive)
+ Addedglob-parent@5.1.2(transitive)
+ Addedhas-flag@4.0.0(transitive)
+ Addedis-binary-path@2.1.0(transitive)
+ Addedis-number@7.0.0(transitive)
+ Addedmime-db@1.52.0(transitive)
+ Addedmime-types@2.1.35(transitive)
+ Addedpath-to-regexp@6.3.0(transitive)
+ Addedpicomatch@2.3.1(transitive)
+ Addedreaddirp@3.6.0(transitive)
+ Addedsupports-color@7.2.0(transitive)
+ Addedto-regex-range@5.0.1(transitive)
- Removedmime@^1.3.4
- Removedmoment@^2.17.1
- Removedansi-styles@3.2.1(transitive)
- Removedanymatch@2.0.0(transitive)
- Removedarr-diff@4.0.0(transitive)
- Removedarr-flatten@1.1.0(transitive)
- Removedarr-union@3.1.0(transitive)
- Removedarray-unique@0.3.2(transitive)
- Removedassign-symbols@1.0.0(transitive)
- Removedasync-each@1.0.6(transitive)
- Removedatob@2.1.2(transitive)
- Removedbase@0.11.2(transitive)
- Removedbinary-extensions@1.13.1(transitive)
- Removedbindings@1.5.0(transitive)
- Removedbraces@2.3.2(transitive)
- Removedcache-base@1.0.1(transitive)
- Removedchalk@2.4.2(transitive)
- Removedchokidar@2.1.8(transitive)
- Removedclass-utils@0.3.6(transitive)
- Removedcollection-visit@1.0.0(transitive)
- Removedcolor-convert@1.9.3(transitive)
- Removedcolor-name@1.1.3(transitive)
- Removedcomponent-emitter@1.3.1(transitive)
- Removedcopy-descriptor@0.1.1(transitive)
- Removedcore-util-is@1.0.3(transitive)
- Removeddebug@2.6.9(transitive)
- Removeddecode-uri-component@0.2.2(transitive)
- Removeddefine-property@0.2.51.0.02.0.2(transitive)
- Removedescape-string-regexp@1.0.5(transitive)
- Removedexpand-brackets@2.1.4(transitive)
- Removedextend-shallow@2.0.13.0.2(transitive)
- Removedextglob@2.0.4(transitive)
- Removedfile-uri-to-path@1.0.0(transitive)
- Removedfill-range@4.0.0(transitive)
- Removedfor-in@1.0.2(transitive)
- Removedfragment-cache@0.2.1(transitive)
- Removedfsevents@1.2.13(transitive)
- Removedget-value@2.0.6(transitive)
- Removedglob-parent@3.1.0(transitive)
- Removedgraceful-fs@4.2.11(transitive)
- Removedhas-flag@3.0.0(transitive)
- Removedhas-value@0.3.11.0.0(transitive)
- Removedhas-values@0.1.41.0.0(transitive)
- Removedinherits@2.0.4(transitive)
- Removedis-accessor-descriptor@1.0.1(transitive)
- Removedis-binary-path@1.0.1(transitive)
- Removedis-buffer@1.1.6(transitive)
- Removedis-data-descriptor@1.0.1(transitive)
- Removedis-descriptor@0.1.71.0.3(transitive)
- Removedis-extendable@0.1.11.0.1(transitive)
- Removedis-glob@3.1.0(transitive)
- Removedis-number@3.0.0(transitive)
- Removedis-plain-object@2.0.4(transitive)
- Removedis-windows@1.0.2(transitive)
- Removedisarray@0.0.11.0.0(transitive)
- Removedisobject@2.1.03.0.1(transitive)
- Removedkind-of@3.2.24.0.06.0.3(transitive)
- Removedmap-cache@0.2.2(transitive)
- Removedmap-visit@1.0.0(transitive)
- Removedmicromatch@3.1.10(transitive)
- Removedmime@1.6.0(transitive)
- Removedmixin-deep@1.3.2(transitive)
- Removedmoment@2.30.1(transitive)
- Removedms@2.0.0(transitive)
- Removednan@2.22.0(transitive)
- Removednanomatch@1.2.13(transitive)
- Removednormalize-path@2.1.1(transitive)
- Removedobject-copy@0.1.0(transitive)
- Removedobject-visit@1.0.1(transitive)
- Removedobject.pick@1.3.0(transitive)
- Removedpascalcase@0.1.1(transitive)
- Removedpath-dirname@1.0.2(transitive)
- Removedpath-is-absolute@1.0.1(transitive)
- Removedpath-to-regexp@1.9.0(transitive)
- Removedposix-character-classes@0.1.1(transitive)
- Removedprocess-nextick-args@2.0.1(transitive)
- Removedreadable-stream@2.3.8(transitive)
- Removedreaddirp@2.2.1(transitive)
- Removedregex-not@1.0.2(transitive)
- Removedremove-trailing-separator@1.1.0(transitive)
- Removedrepeat-element@1.1.4(transitive)
- Removedrepeat-string@1.6.1(transitive)
- Removedresolve-url@0.2.1(transitive)
- Removedret@0.1.15(transitive)
- Removedsafe-buffer@5.1.2(transitive)
- Removedsafe-regex@1.1.0(transitive)
- Removedset-value@2.0.1(transitive)
- Removedsnapdragon@0.8.2(transitive)
- Removedsnapdragon-node@2.1.1(transitive)
- Removedsnapdragon-util@3.0.1(transitive)
- Removedsource-map@0.5.7(transitive)
- Removedsource-map-resolve@0.5.3(transitive)
- Removedsource-map-url@0.4.1(transitive)
- Removedsplit-string@3.1.0(transitive)
- Removedstatic-extend@0.1.2(transitive)
- Removedstring_decoder@1.1.1(transitive)
- Removedsupports-color@5.5.0(transitive)
- Removedto-object-path@0.3.0(transitive)
- Removedto-regex@3.0.2(transitive)
- Removedto-regex-range@2.1.1(transitive)
- Removedunion-value@1.0.1(transitive)
- Removedunset-value@1.0.0(transitive)
- Removedupath@1.2.0(transitive)
- Removedurix@0.1.0(transitive)
- Removeduse@3.1.1(transitive)
- Removedutil-deprecate@1.0.2(transitive)
Updatedchalk@^3.0.0
Updatedchokidar@^3.3.1
Updatedformidable@^1.2.1
Updatedhttp-proxy@^1.18.0
Updatedlodash@^4.17.15
Updatedpath-to-regexp@^6.1.0