-
Notifications
You must be signed in to change notification settings - Fork 3
Plugin API
Plugin API is normal way to extend Colibri.
Colibri request handlers are named "methods" and each method consists of several "steps" which are Express route middleware.
Each step may be extended with Resource#use().
Gets single document.
Steps:
-
begin- initialize -
input- gets_idfrom request and stores it inreq.rest._id -
load- loads document using_idand stores it inreq.rest.document -
serialize- storesreq.rest.documenttoreq.rest.response -
output- outputsreq.rest.metaandreq.rest.response
Updates document.
Steps:
-
begin- initialize -
input- gets_idfrom request and stores it inreq.rest._id. Also gets fields fromreq.bodyand stores them inreq.rest.fieldValues -
load- loads document using_idand stores it inreq.rest.document -
update- updatesreq.rest.documentfields fromreq.rest.fieldValues -
save- savesreq.rest.document -
serialize- storesreq.rest.documenttoreq.rest.response -
output- outputsreq.rest.metaandreq.rest.response
Deletes document.
Steps:
-
begin- initialize -
input- gets_idfrom request and stores it inreq.rest._id -
load- loads document using_idand stores it inreq.rest.document -
remove- destroysreq.rest.document -
output- outputsreq.rest.metaandreq.rest.response
Creates document.
Steps:
-
begin- initialize -
input- gets fields fromreq.bodyand stores them inreq.rest.fieldValues -
create- creates new document, populates it withreq.rest.fieldValuesand stores it inreq.rest.document -
save- savesreq.rest.document -
serialize- storesreq.rest.documenttoreq.rest.response -
output- outputsreq.rest.metaandreq.rest.response
Gets list of documents.
Steps:
-
begin- initialize -
input- does nothing -
query- creates Mongoose query and stores it inreq.rest.query -
load- loads documents list usingreq.rest.queryand stores it inreq.rest.documents -
serialize- storesreq.rest.documentstoreq.rest.response -
output- outputsreq.rest.metaandreq.rest.response
By calling Resource#use() we specify middlewares that will be added after the corresponding method/step.
In our middleware we can access and/or modify any variables under req.rest namespace, used by Colibri to pass data across steps.
For example, if we want to restrict creating new documents for unauthorized users, we need to use some middleware that would check authentification after post.begin (assuming that req.session.currentUser is set for authenticated users only):
resource.use({
post:{
begin : function (req, res, next) {
if(req.session.currentUser) {
next();
return;
}
res.send(403);
}
}
});This way we can control lots of things.
Another good example is changing list query. If we want to add sorting and limitation and filtering to our list results, we may create the following plugin:
resource.use({
list:{
query : function (req, res, next) {
//list.query step has created Mongoose query for us
var query = req.rest.query;
query.sort('mtime', -1);
query.limit(100);
query.where('userName').equals(req.param('userName'));
next();
}
}
});TODO