@ords/core
Version:
Microservice architecture with service proposals in nodejs and typescript
81 lines (80 loc) • 2.48 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const proposals_1 = require("./proposals");
class ShortenAct {
/**
* Convert body output from act depending on the meta flag set
*/
static convertOutput(body, flag) {
switch (flag) {
case proposals_1.main.flag.dataType.RAW:
return body['0'];
case proposals_1.main.flag.dataType.MULTIPLE:
// return in sorted array
return Object.keys(body).map((key) => {
return body[key];
});
case proposals_1.main.flag.dataType.OBJECT:
return body;
default:
return body;
}
}
/**
* Perform try catch on acting
*/
static tryCatch(registry, root, name, request, handler) {
// try to perform action
let errflag = false;
// setup holder
let body = {};
let meta = {};
// flag indicating send
let done = false;
// operation flag
let flag = proposals_1.main.flag.dataType.RAW;
// carry out operation and send back results
let resp = registry.act(root, name, request);
// subscribe to body
resp.package.subscribe(value => {
body[value[0]] = value[1];
}, err => {
if (errflag === false) {
handler(err);
errflag = true;
}
}, () => {
// send the response if done
if (done) {
handler(null, ShortenAct.convertOutput(body, flag), meta);
}
else {
// set done flag to true
done = true;
}
});
// subscribe to headers
resp.meta.subscribe(value => {
// save flag if it is send
if (value[0] === proposals_1.main.flag.FLAGSEND) {
flag = value[1];
}
else {
meta[value[0]] = value[1];
}
}, err => {
if (errflag === false) {
handler(err);
errflag = true;
}
}, () => {
// send the response if done
if (done) {
handler(null, ShortenAct.convertOutput(body, flag), meta);
}
// set done flag to true
done = true;
});
}
}
exports.ShortenAct = ShortenAct;