A corbel-composer is a middleware based in nodeJS with express, to offer developers to make his own specific application API bsed in corbel-js
[text]
[image]
-
install
npm install -g bq/corbel-composer
-
run server
corbel-composer
- Get postman
- Import corbel-composer collection:
https://raw.githubusercontent.com/bq/corbel-composer/master/doc/postman.json
- Enjoy!
{
"url": "phraseName",
"get": {
"code": "res.render('index', {title: 'hello world'});",
"doc": {
"description": "Phrase description",
"queryParameters": {
"param1": {
"type": "number",
"description": "Param description",
"default": 0
}
},
"responses": {
"200": {
"body": {
"application/json": {
"schema": "{\n\t"$schema": "http://json-schema.org/schema",\n\t"type": "object",\n\t"description": "A canonical song",\n\t"properties": {\n\t\t"title": {\n\t\t\t"type": "String"\n\t\t},\n\t\t"artist": {\n\t\t\t"type": "String"\n\t\t}\n\t},\n\t"required": ["title", "artist"]\n}"
}
}
}
}
}
}
}
{
"url": "countExample",
"get": {
"code": "CORBEL-JS_SNIPPET"
}
}
where code
should be a string with this corbel-js snippet:
var count;
corbelDriver.resources.collection('test:ComposrTest').get(undefined, {
aggregation: {
$count: '*'
}
}).then(function(response) {
count = response.data.count;
return corbelDriver.resources.collection('test:ComposrTest').get();
}).then(function(response) {
res.send({
data: response.data,
'count': count
});
}).catch(function(error) {
res.send(error);
});
{
"url": "paramsExample/:pathparam",
"get": {
"code": "res.status(200).send('path param: ' + req.params.pathparam + ', query param: ' + req.query.queryparam);"
}
}
- corbel-js API
- Request object
- Response object
- RAML for phrase definition
- Use nouns not verbs
- Use plural nouns
Resource | GET (read) | POST (create) | PUT (update) | DELETE |
---|---|---|---|---|
/cars | Returns a list of cars | Create a new ticket | Bulk update of cars | Delete all cars |
/cars/711 | Returns a specific car | Method not allowed (405) | Updates a specific ticket | Deletes a specific ticket |
/purchase | Get al purchases | Create a new purchase | Bulk update of purschases | Delete all purchases |
/purchase/85 | Returns a purchase | Method not allowed (405) | Updates a specific purchase | Delete all purchases |
Resource GET read POST create PUT update DELETE /cars Returns a list of cars Create a new ticket Bulk update of cars Delete all cars /cars/711 Returns a specific car Method not allowed (405) Deletes a specific ticket
A simple way to achieve this is definning the phrase version in the url, like this
{
"url": "v1/paramsExample/:pathparam",
"get": { ... }
}
A phrase version should change only if the phrase contract is broken
- APIgee
- Principios de diseño de APIs REST
- Best Practices for Designing a Pragmatic RESTful API
- REST API Resoruces
-
clone repo
-
build image
docker build -t <username>/corbel-composer .
-
run container
docker run -d -p 3000:3000 --name="corbel-composer" <username>/corbel-composer
-
start/stop container
docker start/stop corbel-composer
npm test
grunt test:coverage
Requires node-inspector
npm install -g node-inspector
-
Server
npm run debug
-
Tests
npm run test:debug
corbelDriver.iam.token().create().then(function(response) { res.send(response); }) .catch(function(err){ res.status(500).send(err); });
//Extract the clientId from the auth token var jwtDecoded = corbel.jwt.decode(req.get('Authorization')); var clientId = jwtDecoded.clientId; //Claims object for log the user in var claims = { 'iss' : clientId, 'scopes' : req.body.scopes, 'basic_auth.username' : req.body.username, 'basic_auth.password' : req.body.password }; var tokenObject; //Request a session token for the user corbelDriver.iam.token().create({ claims : claims }) .then(function(response){ //Tenemos el token de usuario, asimismo tambien el refresh y el expires tokenObject = response.data; //Recreamos el corbelDriver con los settings del usuario var corbelDriver = corbel.generateDriver({ iamToken : tokenObject }); //Obtain the logged user data return corbelDriver.iam.user('me').get(); }) .then(function(response){ res.send({ tokenObject: tokenObject, user: response.data }); }) .catch(function(err){ res.status(500).send(err); });
corbelDriver.iam.user('me').get();