Skip to content

Commit d8f335b

Browse files
committed
minor cleanup
1 parent 626f596 commit d8f335b

File tree

4 files changed

+50
-50
lines changed

4 files changed

+50
-50
lines changed

README.md

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ If you've used the Google APIs, provided a `?fields=` query-string to get a
1010
and wanted to do the same for your own server, now you can do so with this
1111
middleware.
1212

13-
*Underneath, this middleware uses [json-mask](https://github.com/nemtsov/json-mask).
14-
Use it directly without this middleware if you need more flexibility.*
13+
_Underneath, this middleware uses [json-mask](https://github.com/nemtsov/json-mask).
14+
Use it directly without this middleware if you need more flexibility._
1515

1616
# Installation
1717

@@ -22,26 +22,29 @@ npm install express-partial-response
2222
# Usage
2323

2424
```js
25-
const express = require('express')
26-
const partialResponse = require('express-partial-response')
27-
const app = express()
25+
const express = require('express');
26+
const partialResponse = require('express-partial-response');
27+
const app = express();
2828

29-
app.use(partialResponse())
29+
app.use(partialResponse());
3030

31-
app.get('/', function (req, res, next) {
31+
app.get('/', (req, res) => {
3232
res.json({
33-
firstName: 'Mohandas'
34-
, lastName: 'Gandhi'
35-
, aliases: [{
36-
firstName: 'Mahatma'
37-
, lastName: 'Gandhi'
38-
}, {
39-
firstName: 'Bapu'
40-
}]
41-
})
42-
})
43-
44-
app.listen(4000)
33+
firstName: 'Mohandas',
34+
lastName: 'Gandhi',
35+
aliases: [
36+
{
37+
firstName: 'Mahatma',
38+
lastName: 'Gandhi'
39+
},
40+
{
41+
firstName: 'Bapu'
42+
}
43+
]
44+
});
45+
});
46+
47+
app.listen(4000);
4548
```
4649

4750
Let's test it:
@@ -70,9 +73,11 @@ Look at [json-mask](https://github.com/nemtsov/json-mask) for the available synt
7073
`query` specifies the query-string to use. Defaults to `fields`
7174

7275
```js
73-
app.use(partialResponse({
74-
query: 'filter'
75-
}))
76+
app.use(
77+
partialResponse({
78+
query: 'filter'
79+
})
80+
);
7681
```
7782

7883
# License

example/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
{
22
"name": "example",
33
"version": "1.0.0",
4-
"description": "",
54
"main": "server.js",
65
"scripts": {
76
"start": "node server.js"

example/server.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const app = express();
44

55
app.use(partialResponse());
66

7-
app.get('/', function(req, res, next) {
7+
app.get('/', (req, res) => {
88
res.json({
99
firstName: 'Mohandas',
1010
lastName: 'Gandhi',
@@ -20,7 +20,7 @@ app.get('/', function(req, res, next) {
2020
});
2121
});
2222

23-
app.listen(4000, function() {
23+
app.listen(4000, () => {
2424
const prefix = "curl 'http://localhost:4000?fields=%s'";
2525
console.log('Server running on :4000, try the following:');
2626
console.log(prefix, '*');

index.js

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,34 @@
11
const jsonMask = require('json-mask');
22

3-
const badCode = code => code >= 300 || code < 200;
3+
function isBadCode(statusCode) {
4+
return statusCode < 200 || statusCode >= 300;
5+
}
46

5-
module.exports = function(opt) {
6-
opt = opt || {};
7+
module.exports = function(options = {}) {
8+
options = { query: 'fields', ...options };
79

8-
function partialResponse(obj, fields) {
9-
if (!fields) return obj;
10-
return jsonMask(obj, fields);
11-
}
12-
13-
function wrap(orig) {
10+
function wrap(resJson) {
1411
return function(obj) {
15-
const param = this.req.query[opt.query || 'fields'];
16-
if (1 === arguments.length) {
17-
if (badCode(this.statusCode)) {
18-
return orig(this.statusCode, arguments[0]);
19-
} else {
20-
return orig(partialResponse(obj, param));
12+
const fields = this.req.query[options.query];
13+
14+
// deprecated API that are still supported in v4
15+
if (arguments.length > 1) {
16+
// res.json(obj, status)
17+
if ('number' === typeof arguments[1] && !isBadCode(arguments[1])) {
18+
return resJson(jsonMask(obj, fields), arguments[1]);
2119
}
22-
}
2320

24-
if ('number' === typeof arguments[1] && !badCode(arguments[1])) {
25-
// res.json(body, status) backwards compat
26-
return orig(partialResponse(obj, param), arguments[1]);
27-
}
21+
// res.json(status, obj)
22+
if ('number' === typeof obj && !isBadCode(obj)) {
23+
return resJson(obj, jsonMask(arguments[1], fields));
24+
}
2825

29-
if ('number' === typeof obj && !badCode(obj)) {
30-
// res.json(status, body) backwards compat
31-
return orig(obj, partialResponse(arguments[1], param));
26+
return resJson(...arguments);
3227
}
3328

34-
// The original actually returns this.send(body)
35-
return orig(obj, arguments[1]);
29+
return isBadCode(this.statusCode)
30+
? resJson(...arguments)
31+
: resJson(jsonMask(obj, fields));
3632
};
3733
}
3834

0 commit comments

Comments
 (0)