Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

autoresponse

Package Overview
Dependencies
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

autoresponse - npm Package Compare versions

Comparing version 0.3.0 to 0.4.0

4

examples/autoresponse-config.js

@@ -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 @@ },

@@ -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).
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc