-
Notifications
You must be signed in to change notification settings - Fork 169
/
index.js
150 lines (124 loc) · 3.85 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
const builtInMatchers = require('./matchers');
const { debug, setDebugNamespace, getDebug } = require('../lib/debug');
const isUrlMatcher = (matcher) =>
matcher instanceof RegExp ||
typeof matcher === 'string' ||
(typeof matcher === 'object' && 'href' in matcher);
const isFunctionMatcher = (matcher) => typeof matcher === 'function';
class Route {
constructor(args, fetchMock) {
this.fetchMock = fetchMock;
const debug = getDebug('compileRoute()');
debug('Compiling route');
this.init(args);
this.sanitize();
this.validate();
this.generateMatcher();
this.limit();
this.delayResponse();
}
validate() {
if (!('response' in this)) {
throw new Error('fetch-mock: Each route must define a response');
}
if (!Route.registeredMatchers.some(({ name }) => name in this)) {
throw new Error(
"fetch-mock: Each route must specify some criteria for matching calls to fetch. To match all calls use '*'"
);
}
}
init(args) {
const [matcher, response, options = {}] = args;
const routeConfig = {};
if (isUrlMatcher(matcher) || isFunctionMatcher(matcher)) {
routeConfig.matcher = matcher;
} else {
Object.assign(routeConfig, matcher);
}
if (typeof response !== 'undefined') {
routeConfig.response = response;
}
Object.assign(routeConfig, options);
Object.assign(this, routeConfig);
}
sanitize() {
const debug = getDebug('sanitize()');
debug('Sanitizing route properties');
if (this.method) {
debug(`Converting method ${this.method} to lower case`);
this.method = this.method.toLowerCase();
}
if (isUrlMatcher(this.matcher)) {
debug('Mock uses a url matcher', this.matcher);
this.url = this.matcher;
delete this.matcher;
}
this.functionMatcher = this.matcher || this.functionMatcher;
debug('Setting route.identifier...');
debug(` route.name is ${this.name}`);
debug(` route.url is ${this.url}`);
debug(` route.functionMatcher is ${this.functionMatcher}`);
this.identifier = this.name || this.url || this.functionMatcher;
debug(` -> route.identifier set to ${this.identifier}`);
}
generateMatcher() {
setDebugNamespace('generateMatcher()');
debug('Compiling matcher for route');
const activeMatchers = Route.registeredMatchers
.map(
({ name, matcher, usesBody }) =>
this[name] && { matcher: matcher(this, this.fetchMock), usesBody }
)
.filter((matcher) => Boolean(matcher));
this.usesBody = activeMatchers.some(({ usesBody }) => usesBody);
debug('Compiled matcher for route');
setDebugNamespace();
this.matcher = (url, options = {}, request) =>
activeMatchers.every(({ matcher }) => matcher(url, options, request));
}
limit() {
const debug = getDebug('limit()');
debug('Limiting number of requests to handle by route');
if (!this.repeat) {
debug(
' No `repeat` value set on route. Will match any number of requests'
);
return;
}
debug(` Route set to repeat ${this.repeat} times`);
const matcher = this.matcher;
let timesLeft = this.repeat;
this.matcher = (url, options) => {
const match = timesLeft && matcher(url, options);
if (match) {
timesLeft--;
return true;
}
};
this.reset = () => (timesLeft = this.repeat);
}
delayResponse() {
const debug = getDebug('delayResponse()');
debug(`Applying response delay settings`);
if (this.delay) {
debug(` Wrapping response in delay of ${this.delay} miliseconds`);
const response = this.response;
this.response = () => {
debug(`Delaying response by ${this.delay} miliseconds`);
return new Promise((res) =>
setTimeout(() => res(response), this.delay)
);
};
} else {
debug(
` No delay set on route. Will respond 'immediately' (but asynchronously)`
);
}
}
static addMatcher(matcher) {
Route.registeredMatchers.push(matcher);
}
}
Route.registeredMatchers = [];
builtInMatchers.forEach(Route.addMatcher);
module.exports = Route;