#CompoSR Core
The core package for Corbel's Composr. NPM page
Usage
npm install --save composr-core
Read:
What are Phrases or Snippets?
Setup for fetching remote data from Corbel
var composr = require('composr-core');
var options = {
credentials: {
clientId: 'demo',
clientSecret: 'demo',
scopes: 'demo'
},
urlBase: 'https://remote-corbel.com',
timeout: 3000
};
composr.init(options)
.then(function() {
});
Setup with local data
var composr = require('composr-core');
var phrasesLoaded = composr.Phrase.register(domain, phrases);
var snippetsLoaded = composr.Snippet.register(domain, snippets);
Promise.all([phrasesLoaded, snippetsLoaded])
.then(function(){
});
Suscribe to log events
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);
});
Phrases execution
Standalone execution
It will use internal "mocked" req, res, next objects
var path = '/user/1231/test';
var method = 'get';
var params = {
timeout: 10000
};
composr.Phrase.runByPath(domain, url, method, params, function(err, response){
});
Tunneling "restify" req, res, next
var path = '/user/1231/test';
var method = 'get';
var params = {
req: req,
res: res,
timeout: 10000
};
composr.Phrase.runByPath(domain, url, method, params, function(err, response){
});
Phrase parameters
Using mocked handlers
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
}
Using restify handlers
Whoa, just execute your dinamics endpoints.
var params = {
req,
res,
next
}
Injecting a custom corbel-js driver instance
var params = {
corbelDriver
}
Phrase execution timeout
var params = {
timeout: 10000
}
Other parameters
var params = {
domain,
functionMode : false,
}
Restify example
router.get('/user/me', function(req, res, next) {
var params = {
req: req,
res: res,
next: next,
timeout: 10000
};
var uid = req.get('User-ID')
composr.Phrase.runByPath(domain, 'userdetail/' + uid, 'get', params,
function(err, response){
});
});
Debugging phrases
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.
API
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