Socket
Socket
Sign inDemoInstall

express-mock-api-middleware

Package Overview
Dependencies
122
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.0.5 to 1.0.7

56

lib/createMockMiddleware.js

@@ -6,3 +6,3 @@ "use strict";

});
exports.default = getMockMiddleware;
exports["default"] = getMockMiddleware;

@@ -25,8 +25,16 @@ var _path = require("path");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
function _createForOfIteratorHelper(o) { if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) { if (Array.isArray(o) || (o = _unsupportedIterableToArray(o))) { var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var it, normalCompletion = true, didErr = false, err; return { s: function s() { it = o[Symbol.iterator](); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

@@ -48,3 +56,3 @@

var watcher = _chokidar.default.watch([absMockPath], {
var watcher = _chokidar["default"].watch([absMockPath], {
ignoreInitial: true

@@ -58,3 +66,3 @@ });

if (!errors.length) {
_signale.default.success("Mock file parse success");
_signale["default"].success("Mock file parse success");
}

@@ -70,3 +78,3 @@ });

var mockFiles = _glob.default.sync('**/*.[jt]s', {
var mockFiles = _glob["default"].sync('**/*.[jt]s', {
cwd: absMockPath,

@@ -85,3 +93,3 @@ ignore: (options || {}).ignore || []

memo = _objectSpread({}, memo, m.default || m);
memo = _objectSpread(_objectSpread({}, memo), m["default"] || m);
return memo;

@@ -92,3 +100,3 @@ }, {});

_signale.default.error("Mock file parse failed: ".concat(e.message));
_signale["default"].error("Mock file parse failed: ".concat(e.message));
}

@@ -123,7 +131,7 @@

if (BODY_PARSED_METHODS.includes(method)) {
_bodyParser.default.json({
_bodyParser["default"].json({
limit: '5mb',
strict: false
})(req, res, function () {
_bodyParser.default.urlencoded({
_bodyParser["default"].urlencoded({
limit: '5mb',

@@ -141,3 +149,3 @@ extended: true

if (typeof handler === 'function') {
(0, _multer.default)().any()(req, res, function () {
(0, _multer["default"])().any()(req, res, function () {
handler(req, res, next);

@@ -158,3 +166,3 @@ });

(0, _assert.default)(type === 'function' || type === 'object', "mock value of ".concat(key, " should be function or object, but got ").concat(type));
(0, _assert["default"])(type === 'function' || type === 'object', "mock value of ".concat(key, " should be function or object, but got ").concat(type));

@@ -170,3 +178,3 @@ var _parseKey = parseKey(key),

};
var re = (0, _pathToRegexp.default)(path, keys, pathOptions); // debug(re);
var re = (0, _pathToRegexp["default"])(path, keys, pathOptions); // debug(re);

@@ -196,8 +204,7 @@ memo.push({

var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
var _iterator = _createForOfIteratorHelper(mockData),
_step;
try {
for (var _iterator = mockData[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var mock = _step.value;

@@ -230,14 +237,5 @@ var method = mock.method,

} catch (err) {
_didIteratorError = true;
_iteratorError = err;
_iterator.e(err);
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return != null) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
_iterator.f();
}

@@ -244,0 +242,0 @@

@@ -5,6 +5,6 @@ "use strict";

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
module.exports = function (mockDir, options) {
return (0, _createMockMiddleware.default)(mockDir, options);
return (0, _createMockMiddleware["default"])(mockDir, options);
};
{
"name": "express-mock-api-middleware",
"version": "1.0.5",
"version": "1.0.7",
"description": "Express middleware for mocking restful APIs",
"main": "lib/index.js",
"files": [
"lib/"
],
"scripts": {
"build": "babel src -d lib",
"test": "exit 0",
"prepack": "npm run build"

@@ -27,24 +31,24 @@ },

"dependencies": {
"assert": "^1.4.1",
"body-parser": "^1.18.3",
"chokidar": "^2.1.2",
"assert": "^2.0.0",
"body-parser": "^1.19.0",
"chokidar": "^3.4.0",
"debug": "^4.1.1",
"glob": "^7.1.3",
"multer": "^1.4.1",
"path-to-regexp": "^3.0.0",
"glob": "^7.1.6",
"multer": "^1.4.2",
"path-to-regexp": "^6.1.0",
"signale": "^1.4.0"
},
"devDependencies": {
"@babel/cli": "^7.2.3",
"@babel/core": "^7.4.0",
"@babel/preset-env": "^7.4.2",
"babel-eslint": "^10.0.1",
"eslint": "^5.15.3",
"eslint-config-airbnb-base": "^13.1.0",
"eslint-config-prettier": "^4.1.0",
"@babel/cli": "^7.8.4",
"@babel/core": "^7.9.6",
"@babel/preset-env": "^7.9.6",
"babel-eslint": "^10.1.0",
"eslint": "^7.0.0",
"eslint-config-airbnb-base": "^14.1.0",
"eslint-config-prettier": "^6.11.0",
"eslint-plugin-babel": "^5.3.0",
"eslint-plugin-import": "^2.16.0",
"eslint-plugin-prettier": "^3.0.1",
"prettier": "^1.16.4"
"eslint-plugin-import": "^2.20.2",
"eslint-plugin-prettier": "^3.1.3",
"prettier": "^2.0.5"
}
}
# express-mock-api-middleware
Express middleware for mocking restful APIs
![express-mock-api-middleware CI](https://github.com/TechStark/express-mock-api-middleware/workflows/express-mock-api-middleware%20CI/badge.svg)
# Install

@@ -22,3 +24,14 @@ ```

);
app.use(mockApiMiddleware);
app.use('/', mockApiMiddleware);
const host = 'localhost';
const port = 4000;
app.listen(port, host, error => {
if (error) {
console.error(error);
return;
}
console.info(`http://${host}:${port}`);
});
```

@@ -32,3 +45,3 @@ `path.resolve(__dirname, 'mock')` is the folder where mock API files are.

// you can write your own mock logic here
export default {
module.exports = {
'GET /api/user/info': {

@@ -41,2 +54,3 @@ id: 101,

},
'POST /api/user/login': (req, res) => {

@@ -53,5 +67,11 @@ const { userName, password } = req.body;

}
},
'GET /api/product/:id': (req, res) => {
const { id } = req.params;
res.sendFile(path.join(__dirname, `products/${id}.json`));
}
};
```
Then you can access the mock API from `http://localhost:4000/api/user/info`.

@@ -62,2 +82,2 @@ ## Show debug log

DEBUG=mock-api
```
```
SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc