1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.default = void 0;
|
7 |
|
8 | require("core-js/modules/es6.array.sort");
|
9 |
|
10 | require("core-js/modules/es6.regexp.split");
|
11 |
|
12 | require("core-js/modules/es6.regexp.replace");
|
13 |
|
14 | require("core-js/modules/es6.regexp.match");
|
15 |
|
16 | var _fs = _interopRequireDefault(require("fs"));
|
17 |
|
18 | var _mockjs = _interopRequireWildcard(require("mockjs"));
|
19 |
|
20 | var _nodeWalkdir = _interopRequireDefault(require("node-walkdir"));
|
21 |
|
22 | function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
|
23 |
|
24 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
25 |
|
26 | var RE = /^\s*\/\*[*\s]+?([^\r\n]+)[\s\S]+?@url\s+([^\n]+)[\s\S]+?\*\//im;
|
27 |
|
28 | function mock(_opts) {
|
29 | var routes = {};
|
30 |
|
31 | _opts.modules.map(function (_dir) {
|
32 | _fs.default.exists(_dir, function (exists) {
|
33 | if (exists) {
|
34 | (0, _nodeWalkdir.default)(_dir, /\.js(on)?$/i, function (filepath) {
|
35 | var content = String(_fs.default.readFileSync(filepath, 'utf8')).trim() || '{}';
|
36 | var url = filepath;
|
37 | var describe = 'no description';
|
38 | var m = content.match(RE);
|
39 |
|
40 | if (m) {
|
41 | url = m[2].trim();
|
42 | describe = m[1].replace(/(^[\s*]+|[\s*]+$)/g, '');
|
43 | }
|
44 |
|
45 | if (url[0] !== '/') {
|
46 | url = "/".concat(url);
|
47 | }
|
48 |
|
49 | var pathname = url;
|
50 |
|
51 | if (pathname.indexOf('?') > -1) {
|
52 | pathname = pathname.split('?')[0];
|
53 | }
|
54 |
|
55 | if (mock.debug && routes[pathname]) {
|
56 | console.warn("[Mock Warn]: [".concat(filepath, ": ").concat(pathname, "] already exists and has been covered with new data."));
|
57 | }
|
58 |
|
59 | routes[pathname] = {
|
60 | url: url,
|
61 | filepath: filepath,
|
62 | describe: describe
|
63 | };
|
64 |
|
65 | if (/\.js$/.test(filepath)) {
|
66 | routes[pathname].data = require(filepath);
|
67 | } else {
|
68 | try {
|
69 | routes[pathname].data = new Function("return (".concat(content, ")"))();
|
70 | } catch (e) {
|
71 | delete routes[pathname];
|
72 | mock.debug && console.warn('[Mock Warn]:', e);
|
73 | }
|
74 | }
|
75 | });
|
76 | }
|
77 | });
|
78 | });
|
79 |
|
80 | return function handle(req, res, next) {
|
81 | res.set('Access-Control-Allow-Origin', '*');
|
82 | res.set('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
|
83 | res.set('Access-Control-Allow-Methods', 'PUT,POST,GET,DELETE,OPTIONS');
|
84 |
|
85 | if (req.method === 'OPTIONS') {
|
86 | return res.send('');
|
87 | }
|
88 |
|
89 | var url = req.originalUrl.split('?')[0];
|
90 |
|
91 | if (url === '/api') {
|
92 | var host = "".concat(req.protocol, "://").concat(req.headers.host).concat(req.baseUrl);
|
93 | var list = Object.keys(routes).sort().map(function (path) {
|
94 | var route = routes[path];
|
95 | return {
|
96 | title: route.describe,
|
97 | url: host + route.url,
|
98 | file: route.filepath
|
99 | };
|
100 | });
|
101 | res.json(list);
|
102 | }
|
103 |
|
104 | var data = (routes[url] || 0).data;
|
105 |
|
106 | if (data) {
|
107 | if (typeof data === 'function') {
|
108 | data = data(req, res, _mockjs.default);
|
109 | }
|
110 |
|
111 | var _mockData = _mockjs.default.mock(data);
|
112 |
|
113 | if (req.query.callback) {
|
114 | res.end("".concat(req.query.callback, "(").concat(JSON.stringify(_mockData), ")"));
|
115 | } else {
|
116 | res.json(_mockData);
|
117 | }
|
118 | } else {
|
119 | next();
|
120 | }
|
121 | };
|
122 | }
|
123 |
|
124 | var _default = mock;
|
125 | exports.default = _default; |
\ | No newline at end of file |