The core package for Corbel's Composr. NPM page
npm install --save composr-core
var composr = require('composr-core');
var options = {
credentials: {
clientId: 'demo',
clientSecret: 'demo',
scopes: 'demo'
},
urlBase: 'https://remote-corbel.com',
timeout: 3000
};
//Trigger the load of data from the "remote corbel"
composr.init(options)
.then(function() {
//Ready to go
});
var composr = require('composr-core');
//Register phrases (Returns a promise)
var phrasesLoaded = composr.Phrase.register(domain, phrases);
//Register snippets (Returns a promise)
var snippetsLoaded = composr.Snippet.register(domain, snippets);
Promise.all([phrasesLoaded, snippetsLoaded])
.then(function(){
//Ready to go
});
//Suscribe to core log events (Optional)
composr.events.on('debug', 'myProject', function(){
console.log.apply(console, arguments);
});
composr.events.on('info', 'myProject', function(){
console.info.apply(console, arguments);
});
composr.events.on('warn', 'myProject', function(){
console.warn.apply(console, arguments);
});
composr.events.on('warn', 'myProject', function(){
console.warn.apply(console, arguments);
});
It will use internal "mocked" req, res, next objects
var path = '/user/1231/test';
var method = 'get'; //get, post, put, delete
var params = {
timeout: 10000
};
composr.Phrase.runByPath(domain, url, method, params, function(err, response){
//if err, phrase missing
//response.status, response.body
});
var path = '/user/1231/test';
var method = 'get'; //get, post, put, delete
var params = {
req: req,
res: res,
timeout: 10000
};
composr.Phrase.runByPath(domain, url, method, params, function(err, response){
//if err, phrase missing
//response.status, response.body
});
You can send an object containing the request headers and other for the body.
Use params
if you don't want to extract the params from the url or if you are using composr.Phrase.runByID
var headers = {
'Authorization' : 'Token'
};
var body = {
'name' : 'test'
};
var params = {
'user' : '123'
};
var query = {
'foo' : 'bar'
};
var params = {
headers,
body,
query,
params
}
Whoa, just execute your dinamics endpoints.
var params = {
req,
res,
next
}
var params = {
corbelDriver
}
var params = {
timeout: 10000 //miliseconds
}
var params = {
domain,
functionMode : false, //In case you want to execute phrases inside vm contexts (memory intensive)
}
router.get('/user/me', function(req, res, next) {
var params = {
req: req,
res: res,
next: next,
timeout: 10000
};
var uid = req.get('User-ID')//for example
composr.Phrase.runByPath(domain, 'userdetail/' + uid, 'get', params,
function(err, response){
//Executed and already sent to client.
});
});
When registering some phrase models, pass the url to the "code" file in order to allow vm.Script
to find the reference:
phraseModel.debug = {
'get' : '/myAbsolute/path/phrase.code.js'
};
composr.Phrase.register(phraseModel);
Later on, from your project, you can launch node-inspector
debug and add breackpoints in the phrase.code.js
file.
composr.Phrase.validate
composr.Phrase.compile
composr.Phrase.register
composr.Phrase.unregister
composr.Phrase.runById
composr.Phrase.runByPath
composr.Phrase.getPhrases
composr.Phrase.getById
composr.Phrase.getByMatchingPath
composr.Snippet.validate
composr.Snippet.compile
composr.Snippet.register
composr.Snippet.unregister
composr.Snippet.runByName
composr.Snippet.getByName
composr.Snippet.getSnippets