UNPKG

mocker-api

Version:

This is dev support mock RESTful API.

50 lines 1.49 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = delay; /** * You can use functional tool to enhance mock. [#17](https://github.com/jaywcjlove/webpack-api-mocker/issues/17) * * ```js * const delay = require('mocker-api/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)); * ``` */ module.exports = delay; function delay(proxy, timer) { if (timer === void 0) { timer = 0; } var mockApi = {}; Object.keys(proxy).forEach(function (key) { var result = proxy[key]; if ((Object.prototype.toString.call(result) === '[object String]' && /^http/.test(result)) || key === '_proxy' || timer === 0) { mockApi[key] = proxy[key]; } else { mockApi[key] = function (req, res) { var foo; if (Object.prototype.toString.call(result) === '[object Function]') { foo = result; } else { foo = function (_req, _res) { return _res.json(result); }; } setTimeout(function () { foo(req, res); }, timer); }; } }); return mockApi; } //# sourceMappingURL=delay.js.map