msh
v0.2.4
Published
Node Microservices Shell
Downloads
107
Readme
= NodeMSH: A NodeJS implementation of Micro Service Shell
In http://asciidoctor.org/docs/asciidoc-syntax-quick-reference/[Asciidoc] format.
:toc:
== Quick Start Examples
=== GETing POSTing PUTing and PIPEing
[source,javascript]
var msh = require('msh');
var h = 'localhost';
var p = mockMshServicePort;
var url = '/some/path';
var callback = function(actions) {
// Great, msh finished successfully now what?
console.log("action 0" + JSON.stringify(actions[0]));
console.log("action 1" + JSON.stringify(actions[1]));
console.log("action 2" + JSON.stringify(actions[2]));
console.log("action 3" + JSON.stringify(actions[3]));
console.log("action 4" + JSON.stringify(actions[4]));
console.log("action 5" + JSON.stringify(actions[5]));
assert.equal(actions[0].type, 'http'); // every action has a type
assert.equal(actions[0].method, 'GET'); // every http type has a method
assert.equal(actions[1].type, 'pipe'); // this means we piped data from one action to another
assert.equal(actions[2].statusCode, 201); // get http response status codes
assert.equal(actions[5].response.message, 'some test data'); // put payload/response data
assert.equal(true, actions.allOk()); // all http return code > 399
done();
};
var errCallback = function(status, host, data) {
// Oh noes! An error! Do something good to restore your Karma
};
var testTransformer = function(data) {return '{"message": "this was transformed from:", "oldmessage": ' + data + ' }'};
var putData = '{"data": "putData"}';
var host = "localhost";
var port = 8080;
// Here's the magic...
// Eh? what happened to all the nested callbacks?
msh.init(callback, errCallback)
.get(h, p, '/path1')
.pipe(testTransformer)
.post(h, p, '/path2')
.del(h, p, '/path3')
.get(h, p, '/path4')
.put(h, p, '/path5', putData)
.end();
=== Using predicates to halt execution
var msh = require('msh');
var h = 'localhost';
var p = mockMshServicePort;
var url = '/some/path';
var payload = {"id":"sentiment","image":"sp_platform/uber-any","env":{"GIT_REPO_URL":"https://github.com/fuzzy-logic/sentiment.git", "DNS": "sentiment.muoncore.io"}};
var errCallback = function(status, host, data) {
console.log('errCallback status=%s, host=%s data=%s', status, host, data);
assert.ok(false);
};
var callback = function(actions) {
console.log('msh callback...');
// console.dir(actions);
var getStatus = actions[0].statusCode;
var postStatus = actions[2].statusCode;
console.log('action0: ' + JSON.stringify(actions[0]));
assert.equal(200, actions[0].statusCode);
assert.equal('http', actions[0].type);
assert.equal('GET', actions[0].method);
assert.ok(actions[0].response);
console.log('action1: ' + JSON.stringify(actions[1]));
assert.equal('stopPredicate', actions[1].type);
assert.equal(true, actions[1].response); //predicate returned false
console.log('action2: ' + JSON.stringify(actions[2]));
assert.equal('http', actions[2].type);
assert.equal('POST', actions[2].method);
assert.equal(undefined, actions[2].response);
done();
};
var predicate = function(prevAction) {
//console.log("predicate prevAction=%s", JSON.stringify(prevAction));
return true
};
console.log('msh starting...');
msh.init(callback, errCallback).get(h, p, url, payload).stop(predicate).post(h, p, url).end();
=== Using URL Templating
var h = 'localhost';
var p = 80;
var url = '/some/path';
var url2 = '/{first}/{second}/{third}';
var errCallback = function(status, host, data) {
console.log('errCallback status=%s, host=%s data=%s', status, host, data);
};
var callback = function(actions) {
var get1Id = actions[0].response.id;
assert.equal(url2, actions[2].pathTemplate);
assert.equal('/foo/bar/' + get1Id, actions[2].path);
done();
};
var processor = function (result) {
return {
first: "foo",
second: "bar",
third: result.id // result = {id: 3}
}
}
msh.init(callback, errCallback).get(h, p, url).template(processor).get(h, p, url2).end();