mirror of
https://github.com/typicode/json-server.git
synced 2025-07-27 20:23:34 +08:00
Rollback to _start and _end query params and update README.md
This commit is contained in:
14
README.md
14
README.md
@ -128,7 +128,19 @@ PATCH /:resource/:id
|
|||||||
DEL /:resource/:id
|
DEL /:resource/:id
|
||||||
```
|
```
|
||||||
|
|
||||||
To slice resources, add `_start` and `_end` to query parameters.
|
To slice resources, add `_start` and `_end`.
|
||||||
|
|
||||||
|
```
|
||||||
|
GET /:resource?_start=&end=
|
||||||
|
GET /:resource?filter=&filter=&_start=&end=
|
||||||
|
GET /:parent/:parentId/:resource?_start=&end=
|
||||||
|
```
|
||||||
|
|
||||||
|
To make a full-text search on resources, add `q`.
|
||||||
|
|
||||||
|
```
|
||||||
|
GET /:resource?q=
|
||||||
|
```
|
||||||
|
|
||||||
For routes usage information, have a look at [JSONPlaceholder](https://github.com/typicode/jsonplaceholder) code examples.
|
For routes usage information, have a look at [JSONPlaceholder](https://github.com/typicode/jsonplaceholder) code examples.
|
||||||
|
|
||||||
|
@ -13,8 +13,8 @@ routes.db = function(req, res, next) {
|
|||||||
// GET /:resource?q=
|
// GET /:resource?q=
|
||||||
// GET /:resource?attr=&attr=
|
// GET /:resource?attr=&attr=
|
||||||
// GET /:parent/:parentId/:resource?attr=&attr=
|
// GET /:parent/:parentId/:resource?attr=&attr=
|
||||||
// GET /*?*&limit=
|
// GET /*?*&_end=
|
||||||
// GET /*?*&offset=&limit=
|
// GET /*?*&_start=&_end=
|
||||||
routes.list = function(req, res, next) {
|
routes.list = function(req, res, next) {
|
||||||
|
|
||||||
// Filters list
|
// Filters list
|
||||||
@ -23,16 +23,17 @@ routes.list = function(req, res, next) {
|
|||||||
// Result array
|
// Result array
|
||||||
var array
|
var array
|
||||||
|
|
||||||
// Remove offset and limit from req.query to avoid filtering using those
|
// Remove _start and _end from req.query to avoid filtering using those
|
||||||
// parameters
|
// parameters
|
||||||
var offset = req.query.offset
|
var _start = req.query._start
|
||||||
var limit = req.query.limit
|
var _end = req.query._end
|
||||||
|
|
||||||
delete req.query.offset
|
delete req.query._start
|
||||||
delete req.query.limit
|
delete req.query._end
|
||||||
|
|
||||||
if (req.query.q) {
|
if (req.query.q) {
|
||||||
|
|
||||||
|
// Full-text search
|
||||||
var q = req.query.q.toLowerCase()
|
var q = req.query.q.toLowerCase()
|
||||||
|
|
||||||
array = low(req.params.resource).where(function(obj) {
|
array = low(req.params.resource).where(function(obj) {
|
||||||
@ -67,14 +68,14 @@ routes.list = function(req, res, next) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Slicing result
|
// Slice result
|
||||||
if (limit) {
|
if (_end) {
|
||||||
res.setHeader('X-Total-Count', array.length)
|
res.setHeader('X-Total-Count', array.length)
|
||||||
res.setHeader('Access-Control-Expose-Headers', 'X-Total-Count')
|
res.setHeader('Access-Control-Expose-Headers', 'X-Total-Count')
|
||||||
|
|
||||||
offset = offset || 0
|
_start = _start || 0
|
||||||
|
|
||||||
array = array.slice(offset, limit)
|
array = array.slice(_start, _end)
|
||||||
}
|
}
|
||||||
|
|
||||||
res.jsonp(array)
|
res.jsonp(array)
|
||||||
|
@ -79,10 +79,10 @@ describe('Server', function() {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('GET /:resource?limit=', function() {
|
describe('GET /:resource?_end=', function() {
|
||||||
it('should respond with a sliced array', function(done) {
|
it('should respond with a sliced array', function(done) {
|
||||||
request(server)
|
request(server)
|
||||||
.get('/comments?limit=2')
|
.get('/comments?_end=2')
|
||||||
.expect('Content-Type', /json/)
|
.expect('Content-Type', /json/)
|
||||||
.expect('x-total-count', low.db.comments.length.toString())
|
.expect('x-total-count', low.db.comments.length.toString())
|
||||||
.expect('Access-Control-Expose-Headers', 'X-Total-Count')
|
.expect('Access-Control-Expose-Headers', 'X-Total-Count')
|
||||||
@ -91,10 +91,10 @@ describe('Server', function() {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('GET /:resource?offset=&limit=', function() {
|
describe('GET /:resource?_start=&_end=', function() {
|
||||||
it('should respond with a sliced array', function(done) {
|
it('should respond with a sliced array', function(done) {
|
||||||
request(server)
|
request(server)
|
||||||
.get('/comments?offset=1&limit=2')
|
.get('/comments?_start=1&_end=2')
|
||||||
.expect('Content-Type', /json/)
|
.expect('Content-Type', /json/)
|
||||||
.expect('x-total-count', low.db.comments.length.toString())
|
.expect('x-total-count', low.db.comments.length.toString())
|
||||||
.expect('Access-Control-Expose-Headers', 'X-Total-Count')
|
.expect('Access-Control-Expose-Headers', 'X-Total-Count')
|
||||||
|
Reference in New Issue
Block a user