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.
Here is a list of what SOAJS adds to the request object:
SOAJS RequestThis space will show you:
SOAJS Request
Anchor | ||||
---|---|---|---|---|
|
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.urac | soajs gives you easy access to the logged in user record in case urac is set to true |
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 |
multi-tenant session. | |
req.soajs.awareness | soajs allows services on the same cloud to get connected internally |
Code Block | ||||
---|---|---|---|---|
| ||||
//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" : {}, // This is added in case urac property is set to true, it contains the logged in record "urac" : {}, // 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 () {}, }, "awareness": { "getHost": function(serviceName, cb) {} } }; |
Usage
Anchor | ||||
---|---|---|---|---|
|
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
Code Block language js service.get("/hello", function(req, res) { console.log(req.soajs.registry); });
Read the IMFV inputs: req.soajs.inputmaskData
Code Block language js service.get("/hello", function(req, res) { console.log(req.soajs.inputmaskData); });
Read the service configuration: req.soajs.servicesConfig
Code Block language js service.get("/hello", function(req, res) { console.log(req.soajs.servicesConfig); });
Read the current tenant information: req.soajs.tenant
Code Block language js service.get("/hello", function(req, res) { console.log(req.soajs.tenant); });
Get the host of a service
Code Block language js service.get("/hello", function(req, res) { req.soajs.awareness.getHost('controller', (host) => { let uri = 'http://' + host + ':' + req.soajs.registry.services.controller.port + req.soajs.inputmaskData.config.route; // hit the new route }); });
SOAJS functionality
Anchor | ||||
---|---|---|---|---|
|
You can use req.soajs to:
use the logger to output logs on the console based on log type: req.soajs.log
Code Block language js 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
Code Block language js 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
Code Block language js 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
Code Block language js 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); });