-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathindex.js
245 lines (212 loc) · 7.67 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
const fs = require('fs');
const path = require('path');
const nodeUrl = require('url');
const bodyParser = require('body-parser');
const chalk = require('chalk');
function trimSlashes(text) {
return text.replace(/\/$/, '').replace(/^\//, '');
}
function escapeRegExp(str) {
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&');
}
function isJsFile(str) {
return !!str.match(/\.c?js$/);
}
function defaultLogger(params) {
console.log(
`${chalk.bgYellow.black('api-mocker')} ${
chalk.green(params.req.method.toUpperCase())} ${
chalk.blue(params.req.originalUrl)} => ${
chalk.cyan(params.filePath)}`
);
}
function logger(params) {
if (params.config.verbose === true) {
defaultLogger(params);
} else if (typeof params.config.verbose === 'function') {
params.config.verbose(params);
}
}
/**
* Find possible folder matches for current path
* @param {string} parentPath path to current level
* @param {string} testPath folder to locate on current level
* @param {object[]} existingParams list of params found on the parentPath (key, value)
*/
function findMatchingFolderOnLevel(parentPath, testPath, existingParams) {
const pathOptions = [];
if (parentPath === false || !fs.existsSync(parentPath)) {
return pathOptions;
}
if (fs.existsSync(path.join(parentPath, testPath))) {
pathOptions.push({
path: path.join(parentPath, testPath),
params: existingParams.concat([])
});
}
fs.readdirSync(parentPath)
.filter((file) => fs.lstatSync(path.join(parentPath, file)).isDirectory())
.filter((folder_name) => folder_name.slice(0, 2) === '__' && folder_name.slice(-2) === '__')
.map((wildcardFolder) => ({
param: wildcardFolder.slice(2, -2),
folder: wildcardFolder
}))
.forEach((wildcardFolder) => {
const pathOption = {
path: path.join(parentPath, wildcardFolder.folder),
params: existingParams.concat({ key: wildcardFolder.param, value: testPath })
};
pathOptions.push(pathOption);
});
return pathOptions;
}
/**
* Recursively loop through path to find all possible path matches including wildcards
* @param {string[]} basePath rootPath to traverse down form
* @param {string[]} lookupPath section of path to traverse
* @param {object[]} existingParams list of params found on the basepath (key, value)
*/
function recurseLookup(basePath, lookupPath, existingParams) {
let paths = [];
const matchingFolders = findMatchingFolderOnLevel(basePath.join('/'), lookupPath[0], existingParams);
if (lookupPath.length < 2) { return matchingFolders; }
matchingFolders.forEach((folder) => {
paths = paths.concat(recurseLookup(folder.path.split('/'), lookupPath.slice(1), folder.params));
});
return paths;
}
/**
* Locate all possible options to match a file request, select the first one (most relevant)
* @param {string} requestPath Requst path to API mock file.
* @param {string[]} requestMethodFiles A list of files that match the request
*/
function findMatchingPath(requestPath, requestMethodFiles) {
const pathParts = requestPath.split('/');
const pathOptions = recurseLookup([pathParts.shift()], pathParts, []);
let result = false;
pathOptions.some((pathOption) => requestMethodFiles.some((requestMethodFile) => {
if (fs.existsSync(path.join(pathOption.path, requestMethodFile))) {
result = {
path: path.resolve(path.join(pathOption.path, requestMethodFile)),
params: pathOption.params
};
return true;
}
return false;
}));
return result;
}
/**
* @param {string|object} urlRoot Base path for API url or full config object
* @param {string|object} pathRoot Base path of API mock files. eg: ./mock/api or
* config object for urlRoot
*/
module.exports = function (urlRoot, pathRoot) {
return function (req, res, next) {
let config = {
target: ''
};
let baseUrl;
if (typeof urlRoot === 'string') {
if (!pathRoot) {
config.target = urlRoot;
baseUrl = req.baseUrl;
} else {
baseUrl = urlRoot;
if (typeof pathRoot === 'object') {
config = pathRoot;
} else {
config.target = pathRoot;
}
}
}
if (typeof urlRoot === 'object') {
config = urlRoot;
baseUrl = req.baseUrl;
}
// trim leading slash from baseUrl
baseUrl = trimSlashes(baseUrl);
// if requested url is in our interest
if (!req.baseUrl && req.url.indexOf(`/${baseUrl}`) !== 0) {
return next();
}
// Ignore querystrings
let url = nodeUrl.parse(req.url).pathname;
// remove baseUrl
url = url.replace(new RegExp(`^(\/)?${escapeRegExp(baseUrl)}`), '');
// trim trailing and leading slashes from url again
url = trimSlashes(url);
const targetPath = `${trimSlashes(config.target)}/${url}`;
const targetFullPath = path.resolve(targetPath);
const returnNotFound = function () {
if (config.nextOnNotFound) {
return next();
}
res.writeHead(404, { 'Content-Type': 'text/html' });
return res.end(`Endpoint not found on mock files: ${url}`);
};
const returnForPath = function (filePath, requestParams) {
if (isJsFile(filePath)) {
logger({
req, filePath, fileType: 'js', config
});
delete require.cache[require.resolve(path.resolve(filePath))];
let customMiddleware = require(path.resolve(filePath));
if (requestParams) {
req.params = requestParams;
}
const executeMiddleware = (request, response) => {
if (typeof customMiddleware === 'function') {
customMiddleware = [customMiddleware];
}
// flatten middlewares
customMiddleware = [].concat(...customMiddleware);
customMiddleware.reduce((chain, middleware) => chain.then(
() => new Promise((resolve) => {
middleware(request, response, resolve);
})
), Promise.resolve()).then(next);
};
if (config.bodyParser === false) {
executeMiddleware(req, res);
} else {
const bodyParserType = (config.bodyParser && config.bodyParser.type) || 'json';
const bodyParserOptions = (config.bodyParser && config.bodyParser.options) || {};
bodyParser[bodyParserType](bodyParserOptions)(req, res, () => executeMiddleware(req, res));
}
} else {
let fileType = config.type || 'json';
if (fileType === 'auto') {
fileType = req.accepts(['json', 'xml']);
}
logger({
req, filePath, fileType, config
});
const buf = fs.readFileSync(filePath);
res.setHeader('Content-Type', `application/${fileType}`);
return res.end(buf);
}
};
let methodFileExtension = config.type || 'json';
if (methodFileExtension === 'auto') {
methodFileExtension = req.accepts(['json', 'xml']);
}
const fileExtensions = [methodFileExtension, 'cjs', 'js'];
const jsMockFiles = fileExtensions.map((ext) => `${req.method}.${ext}`);
const wildcardJsMockFiles = fileExtensions.map((ext) => `ANY.${ext}`);
const methodFiles = [...jsMockFiles, ...wildcardJsMockFiles];
const matchedMethodFile = methodFiles.find((methodFile) => fs.existsSync(path.join(targetFullPath, methodFile)));
if (matchedMethodFile) {
return returnForPath(path.resolve(path.join(targetFullPath, matchedMethodFile)));
}
const newTarget = findMatchingPath(targetPath, methodFiles);
if (newTarget) {
const requestParams = {};
newTarget.params.forEach((param) => {
requestParams[param.key] = param.value;
});
return returnForPath(newTarget.path, requestParams);
}
return returnNotFound();
};
};