Skip to content

Commit

Permalink
Update Link Header in list()
Browse files Browse the repository at this point in the history
  • Loading branch information
sebelga committed Jul 17, 2016
1 parent 7194724 commit 543aab8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,18 @@ It is built on top of the [gstore-node](https://github.com/sebelga/gstore-node)
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->


- [Motivation](#motivation)
- [Installation](#installation)
- [What do I get from it](#what-do-i-get-from-it)
- [Getting started](#getting-started)
- [Initiate library](#initiate-library)
- [Create an Entity API](#create-an-entity-api)
- [Create an API for an Entity](#create-an-api-for-an-entity)
- [settings](#settings)
- [path](#path)
- [ancestors](#ancestors)
- [op](#op)
- [op settings](#op-settings)

<!-- END doctoc generated TOC please keep comment here to allow auto update -->

Expand Down Expand Up @@ -223,7 +228,7 @@ router.use('/private/', yourAuthMiddelware);
Then all the POST, PUT, PATCH and DELETE routes will automatically be routed through your Auth middelware.


## Create an Entity API
## Create an API for an Entity

To its simplest form, to create an API for a Model you just need to create a new instance of the gstoreApi and pass the gstore-node Model.

Expand Down Expand Up @@ -313,15 +318,16 @@ new gstoreApi(Comment, {

Operations can be any of

- list (GET all entities) --> call the list() query shortcut on Model
- list (GET all entities) --> call the list() query shortcut on Model. [Documentation here](https://github.com/sebelga/gstore-node#list).
- get (GET one entity)
- create (POST new entity)
- updatePatch (PATCH update entity) --> only update properties sent
- updateReplace (PUT to update entity) --> replace all data for entity
- delete (DELETE one entity)
- deleteAll (DELETE all entities)

The **list** operation calls the same method from the gstore-node shortcut query "list" as explained in the [documentation here](https://github.com/sebelga/gstore-node#list).
**Link Header**
The **list** operation adds a [Link Header](https://tools.ietf.org/html/rfc5988#page-6) (rel="next") with the link to the next page to fetch if there are more result.


##### op settings
Expand Down Expand Up @@ -396,6 +402,7 @@ Extra options:
- **ancestors** (except if already defined in global init())
- **filters**


**path**
You can add here some custom prefix or suffix to the path.

Expand Down
11 changes: 8 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ class DatastoreApi {
}

this.defaultSettings = {
host : '',
contexts: {
public : '',
private : ''
Expand Down Expand Up @@ -116,13 +115,19 @@ class DatastoreApi {
settings.ancestors = ancestors;
}

if (req.query.pageCursor) {
settings.start = req.query.pageCursor;
}

this.Model.list(settings, (err, result) => {
if (err) {
return errorsHandler.rpcError(err, res);
}
if (result.nextPageCursor) {
// var fullUrl = req.protocol + '://' + req.get('host') + req.originalUrl;
res.set('Link', '<' + _this.settings.host + req.originalUrl + '?pageCursor=' + result.nextPageCursor + '>; rel=next');
var linkNextPage = _this.settings.host || req.protocol + '://' + req.get('host');
req.originalUrl = req.originalUrl.replace(/\?pageCursor=[^&]+/, '');
linkNextPage += req.originalUrl + '?pageCursor=' + result.nextPageCursor;
res.set('Link', '<' + linkNextPage + '>; rel="next"');
}
res.json(result.entities);
});
Expand Down
16 changes: 15 additions & 1 deletion test/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,12 @@ describe('Datastore API', function() {

Model = gstore.model('BlogPost', schema);

req = {params:{id:123, anc0ID:'ancestor1', anc1ID:'ancestor2'}, body:{title:'Blog Title'}};
req = {
params:{id:123, anc0ID:'ancestor1', anc1ID:'ancestor2'},
query : {},
body:{title:'Blog Title'},
get : () => 'http://localhost'
};
res = {
status:() => {
return {json:() => {}}
Expand Down Expand Up @@ -226,6 +231,15 @@ describe('Datastore API', function() {
res.set.restore();
});

it('should add start setting if pageCursor in request query', () => {
req.query.pageCursor = 'abcd1234';
var dsApi = new gstoreApi(Model);

dsApi.list(req, res);

expect(Model.list.getCall(0).args[0]).deep.equal({simplifyResult:false, start:'abcd1234'});
});

it('should read options', () => {
var settings = {
path:'/users',
Expand Down

0 comments on commit 543aab8

Please sign in to comment.