mirror of
https://github.com/typicode/json-server.git
synced 2025-08-01 09:13:32 +08:00
Ignore unknown query parameters
This commit is contained in:
13
CHANGELOG.md
13
CHANGELOG.md
@ -1,5 +1,18 @@
|
|||||||
# Change Log
|
# Change Log
|
||||||
|
|
||||||
|
## [Unreleased][unreleased]
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
|
||||||
|
* Automatically ignore unknown query parameters
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Before
|
||||||
|
GET /products?author=typicode&foo=bar # []
|
||||||
|
# After
|
||||||
|
GET /products?author=typicode&foo=bar # [{...}, {...}]
|
||||||
|
```
|
||||||
|
|
||||||
## [0.7.28][2015-09-09]
|
## [0.7.28][2015-09-09]
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
|
@ -63,6 +63,23 @@ module.exports = function (db, name) {
|
|||||||
delete req.query._embed
|
delete req.query._embed
|
||||||
delete req.query._expand
|
delete req.query._expand
|
||||||
|
|
||||||
|
// Automatically delete query parameters that can't be found
|
||||||
|
// in the database
|
||||||
|
Object.keys(req.query).forEach(function (query) {
|
||||||
|
var arr = db(name).value()
|
||||||
|
for (var i in arr) {
|
||||||
|
if (
|
||||||
|
_.has(arr[i], query) ||
|
||||||
|
query === 'callback' ||
|
||||||
|
query === '_' ||
|
||||||
|
query.indexOf('_lte') !== -1 ||
|
||||||
|
query.indexOf('_gte') !== -1
|
||||||
|
) return
|
||||||
|
}
|
||||||
|
delete req.query[query]
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
if (q) {
|
if (q) {
|
||||||
|
|
||||||
// Full-text search
|
// Full-text search
|
||||||
|
@ -118,6 +118,14 @@ describe('Server', function () {
|
|||||||
.expect(new RegExp(db.comments[0].body)) // JSONP returns text
|
.expect(new RegExp(db.comments[0].body)) // JSONP returns text
|
||||||
.expect(200, done)
|
.expect(200, done)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should ignore unknown query parameters', function (done) {
|
||||||
|
request(server)
|
||||||
|
.get('/comments?foo=1&bar=2')
|
||||||
|
.expect('Content-Type', /json/)
|
||||||
|
.expect(db.comments)
|
||||||
|
.expect(200, done)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('GET /:resource?q=', function () {
|
describe('GET /:resource?q=', function () {
|
||||||
|
Reference in New Issue
Block a user