-
Notifications
You must be signed in to change notification settings - Fork 169
/
compile-route.js
133 lines (114 loc) · 3.3 KB
/
compile-route.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
const { getDebug } = require('./debug');
const generateMatcher = require('./generate-matcher');
const matcherProperties = [
'query',
'method',
'headers',
'params',
'body',
'functionMatcher',
'url'
];
const isUrlMatcher = matcher =>
matcher instanceof RegExp ||
typeof matcher === 'string' ||
(typeof matcher === 'object' && 'href' in matcher);
const isFunctionMatcher = matcher => typeof matcher === 'function';
const argsToRoute = args => {
const [matcher, response, options = {}] = args;
const routeConfig = {};
if (isUrlMatcher(matcher) || isFunctionMatcher(matcher)) {
routeConfig.matcher = matcher;
} else {
Object.assign(routeConfig, matcher);
}
if (response) {
routeConfig.response = response;
}
Object.assign(routeConfig, options);
return routeConfig;
};
const sanitizeRoute = route => {
const debug = getDebug('sanitizeRoute()');
debug('Sanitizing route properties');
route = Object.assign({}, route);
if (route.method) {
debug(`Converting method ${route.method} to lower case`);
route.method = route.method.toLowerCase();
}
if (isUrlMatcher(route.matcher)) {
debug('Mock uses a url matcher', route.matcher);
route.url = route.matcher;
delete route.matcher;
}
route.functionMatcher = route.matcher || route.functionMatcher;
debug('Setting route.identifier...');
debug(` route.name is ${route.name}`);
debug(` route.url is ${route.url}`);
debug(` route.functionMatcher is ${route.functionMatcher}`);
route.identifier = route.name || route.url || route.functionMatcher;
debug(` -> route.identifier set to ${route.identifier}`);
return route;
};
const validateRoute = route => {
if (!('response' in route)) {
throw new Error('fetch-mock: Each route must define a response');
}
if (!matcherProperties.some(matcherType => matcherType in route)) {
throw new Error(
"fetch-mock: Each route must specify some criteria for matching calls to fetch. To match all calls use '*'"
);
}
};
const limit = route => {
const debug = getDebug('limit()');
debug('Limiting number of requests to handle by route');
if (!route.repeat) {
debug(
' No `repeat` value set on route. Will match any number of requests'
);
return;
}
debug(` Route set to repeat ${route.repeat} times`);
const matcher = route.matcher;
let timesLeft = route.repeat;
route.matcher = (url, options) => {
const match = timesLeft && matcher(url, options);
if (match) {
timesLeft--;
return true;
}
};
route.reset = () => (timesLeft = route.repeat);
};
const delayResponse = route => {
const debug = getDebug('delayResponse()');
debug(`Applying response delay settings`);
const { delay } = route;
if (delay) {
debug(` Wrapping response in delay of ${delay} miliseconds`);
const response = route.response;
route.response = () => {
debug(`Delaying response by ${delay} miliseconds`);
return new Promise(res => setTimeout(() => res(response), delay));
};
} else {
debug(
` No delay set on route. Will respond 'immediately' (but asynchronously)`
);
}
};
const compileRoute = function(args) {
const debug = getDebug('compileRoute()');
debug('Compiling route');
const route = sanitizeRoute(argsToRoute(args));
validateRoute(route);
route.matcher = generateMatcher(route);
limit(route);
delayResponse(route);
return route;
};
module.exports = {
compileRoute,
sanitizeRoute
};