Objective
When you build your service, SOAJS adds its functionality and properties to the request and response of that service. These features empower the developer to have all the tools needed to develop the API quickly and offer common ground between team members.
This space will show you:
SOAJS Request
Property | Purpose |
---|---|
req.soajs.inputmaskData | soajs makes it easy by consolidating all the api params from multiple source(post, get, cookie, headers, session, provisioning, …) under one object after formatting and validation. |
req.soajs.registry | soajs gives you easy access to the specified environment configuration. |
req.soajs.log | soajs gives you the ability to have unified and standardized api logging (error, debug, info, …). With the power of soajs.agent, the unified logging will be fed into elastic search and the soajs.dashboard.ui to give you amazing analytic information. |
req.soajs.buildResponse | this function builds the standardized json response returned by the api. |
req.soajs.validator | soajs makes it easy for you to validate json schemas. |
req.soajs.meta | multitenancy meta configuration. |
req.soajs.servicesConfig | soajs gives the power to configure each service differently from one tenant to the other as well as empowers you to even have it configured differently down to the user. This configuration is provided by this entry. |
req.soajs.tenant | contains the tenant information. |
req.soajs.session | object to read/write data from the multitenant session. |
//SOAJS adds to the req the following req.soajs = { // object containing all the input specified in the schema of inputmask mw "inputmaskData": {}, // the registry "registry" : {}, // the logger object "log" : {"error" : function(){}}, // build a response and returns it "buildResponse": function (error, data) {}, // the validator object "validator" : {}, // the meta object "meta" : {}, // the services config from key or URAC "servicesConfig": {}, //tenant info "tenant": { //tenant id "id": '', //tenant code "code": '', //tenant internal and external keys "key": {}, //application info "application": { //application id "appId": '', //product code "product": '', //package code "package": '', //application acl "acl": '', //package acl "package_acl": '' } }, // Access to the session data "session": { // to set service data into the session "setSERVICE": function (obj, cb) {}, // to get the service data from the session "getSERVICE": function () {}, } };
Usage
req.soajs can be invoked from any API at runtime to fetch the running configuration and use SOAJS's core methods.
Active Configuration
You can use req.soajs to:
Read the current registry information: req.soajs.registry
service.get("/hello", function(req, res) { console.log(req.soajs.registry); });
Read the IMFV inputs: req.soajs.inputmaskData
service.get("/hello", function(req, res) { console.log(req.soajs.inputmaskData); });
Read the service configuration: req.soajs.servicesConfig
service.get("/hello", function(req, res) { console.log(req.soajs.servicesConfig); });
Read the current tenant information: req.soajs.tenant
service.get("/hello", function(req, res) { console.log(req.soajs.tenant); });
SOAJS functionality
You can use req.soajs to:
use the logger to output logs on the console based on log type: req.soajs.log
service.get("/hello", function(req, res) { req.soajs.log.error("any message, object, array .... "); req.soajs.log.info("any message, object, array .... "); req.soajs.log.warn("any message, object, array .... "); req.soajs.log.debug("any message, object, array .... "); });
use buildResponse to construct a unified and valid REST response: req.soajs.buildResponse
service.get("/hello", function(req, res) { var response1 = req.soajs.buildResponse(new Error("Something went wrong..."); res.json(response1); var response2 = req.soajs.buildResponse({"code": 401, "msg": "Something went wrong..." }); res.json(response2); var response3 = req.soajs.buildResponse(null, { "a": "b" }); res.json(response3); });
use the meta to fetch a multitenant database configuration based on the current tenant: req.soajs.meta
service.get("/hello", function(req, res) { var config = req.soajs.meta.tenantDB(req.soajs.registry.tenantMetaDB, "myMTdbName", req.soajs.tenant.code); var mongo = new soajs.mongo(config); });
use the validator to perform JSON schema validation: req.soajs.validator
service.get("/hello", function(req, res) { var data = 4; var schema = {"type": "number"}; var x = new req.soajs.validator.Validator(); x.validate(data, schema); req.soajs.log.debug(x); });
Add Comment