Skip to content

Commit ba5831f

Browse files
authored
feat: Helper functions for custom responses
Helper functions for custom responses
2 parents 20291c2 + 154224e commit ba5831f

File tree

19 files changed

+138
-85
lines changed

19 files changed

+138
-85
lines changed

.editorconfig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Editor configuration, see http://editorconfig.org
2+
root = true
3+
4+
[*]
5+
charset = utf-8
6+
indent_style = space
7+
indent_size = 2
8+
insert_final_newline = true
9+
trim_trailing_whitespace = true
10+
11+
[*.md]
12+
max_line_length = off
13+
trim_trailing_whitespace = false

README.md

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -226,23 +226,23 @@ module.exports = function (request, response) {
226226
`POST.js` file for non ExpressJS server:
227227

228228
```js
229-
var fs = require('fs');
230-
var path = require('path');
229+
const fs = require('fs');
230+
const path = require('path');
231231

232-
module.exports = function (request, response) {
232+
module.exports = (request, response) => {
233233
if (!request.get('X-Auth-Key')) {
234234
response.statusCode = 403;
235235
response.end();
236236
} else {
237-
var filePath = path.join(__dirname, 'POST.json');
238-
var stat = fs.statSync(filePath);
237+
const filePath = path.join(__dirname, 'POST.json');
238+
const stat = fs.statSync(filePath);
239239

240240
response.writeHead(200, {
241241
'Content-Type': 'application/json',
242242
'Content-Length': stat.size
243243
});
244244

245-
var readStream = fs.createReadStream(filePath);
245+
const readStream = fs.createReadStream(filePath);
246246
// We replaced all the event handlers with a simple call to readStream.pipe()
247247
readStream.pipe(response);
248248
}
@@ -260,7 +260,7 @@ const fs = require('fs');
260260
const path = require('path');
261261

262262
module.exports = function (request, response) {
263-
var targetFileName = 'GET.json';
263+
let targetFileName = 'GET.json';
264264

265265
// Check is a type parameter exist
266266
if (request.query.type) {
@@ -281,13 +281,13 @@ module.exports = function (request, response) {
281281
```
282282
`GET.js` file for non ExpressJS server:
283283
```js
284-
var url = require('url');
285-
var fs = require('fs');
286-
var path = require('path');
284+
const url = require('url');
285+
const fs = require('fs');
286+
const path = require('path');
287287

288288
module.exports = function (request, response) {
289-
var targetFileName = 'GET.json';
290-
var typeQueryParam = url.parse(request.url, true).query.type;
289+
let targetFileName = 'GET.json';
290+
const typeQueryParam = url.parse(request.url, true).query.type;
291291
// Check is a type parameter exist
292292
if (typeQueryParam) {
293293
// Generate a new targetfilename with that type parameter
@@ -306,18 +306,46 @@ module.exports = function (request, response) {
306306
return;
307307
}
308308

309-
var stat = fs.statSync(filePath);
309+
const stat = fs.statSync(filePath);
310310
response.writeHead(200, {
311311
'Content-Type': 'application/json',
312312
'Content-Length': stat.size
313313
});
314314

315-
var readStream = fs.createReadStream(filePath);
315+
const readStream = fs.createReadStream(filePath);
316316
// We replaced all the event handlers with a simple call to readStream.pipe()
317317
readStream.pipe(response);
318318
}
319319
```
320320

321+
## Helper functions for custom responses
322+
323+
Connect-Api-Mocker also presents a bunch of helper functions to speed up writing simple custom responses. There are:
324+
325+
- `status(statusCode)`: Set status code of response
326+
- `notFound(message?)`: Set status code as 404 and optionally sends message
327+
- `created()`: Sets status code as 201
328+
- `success()`: Sets status code as 200
329+
- `delay(duration)`: Delays the request by given duration(in ms).
330+
- `json(data)`: Send given JSON object as response.
331+
- `end(body)`: Ends request and optionally sends the string output
332+
333+
You can use these functions in custom responses, like:
334+
335+
```js
336+
const { notFound } = require('connect-api-mocker/helpers');
337+
338+
module.exports = notFound('Page is not found');
339+
```
340+
341+
Also you can combine multiple functions:
342+
343+
```js
344+
const { delay, created, json } = require('connect-api-mocker/helpers');
345+
346+
module.exports = [delay(500), created(), json({success: true})];
347+
```
348+
321349
## Wildcards in requests
322350

323351
You can use wildcards for paths to handle multiple urls(like for IDs). If you create a folder structure like `api/users/__user_id__/GET.js`, all requests like `/api/users/321` or `/api/users/1` will be responded by custom middleware that defined in your `GET.js`. Also id part of the path will be passed as a request parameter named as `user_id` to your middleware. So you can write a middleware like that:

examples/redefine-default-mounting/example.js

Lines changed: 0 additions & 12 deletions
This file was deleted.

examples/redefine-default-mounting/states/base/profile/GET.js

Lines changed: 0 additions & 14 deletions
This file was deleted.

examples/redefine-default-mounting/states/my-own-state/profile/GET.json

Lines changed: 0 additions & 6 deletions
This file was deleted.

helpers/created.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const status = require('./status');
2+
3+
module.exports = () => status(201);

helpers/delay.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = (duration) => (req, res, next) => {
2+
setTimeout(next, duration);
3+
};

helpers/end.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = message => (req, res) => {
2+
res.end(message);
3+
};

helpers/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module.exports = {
2+
delay: require('./delay'),
3+
json: require('./json'),
4+
status: require('./status'),
5+
notFound: require('./not-found'),
6+
created: require('./created'),
7+
success: require('./success'),
8+
end: require('./end')
9+
};

helpers/json.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = data => (req, res) => {
2+
res.setHeader('content-type', 'application/json');
3+
res.end(JSON.stringify(data));
4+
};

0 commit comments

Comments
 (0)