S1

Introduction


S1 is a service located in soajs.jsconf repository, it's not secured and can be accessed by any user. This service shows a basic API that you can invoke and provide it with inputs via the query string.

It focuses mainly on how the IMFV engine can be configured to check for mandatory inputs or use default values during runtime.

The S1 service is composed of two files:

  • index.js: contains the code of the service that implements the APIs
  • config.js: contains the configuration used by the IMFV

Let's start this S1 and controller services and see what it does:

# go to directory
cd /opt/soajs/node_modules/soajs.jsconf/services/s1/

# export necessary environment variable
export SOAJS_PROFILE=/opt/soajs/node_modules/soajs.utilities/data/jsconf/profile.js
export SOAJS_ENV=test
export SOAJS_SRVIP=127.0.0.1

# run service
node .


Code Walkthrough


This page shows you how to interact with the s1 service after you install it and run it.

The code walkthrough is located in a sub page and explained in depth. Click here to read the explanation.


Interacting with S1 service


We are going to access this service through the controller, therefore the controller needs to be running.

Click here for a quick reminder how to start the controller service. Make sure to provide the same environment variables of the service S1.

Once you have both the controller and the service running, you are ready to make calls to the S1 service APis.

Hello API:

curl -X GET "http://test-api.mydomain.com/jsconf1/hello"

Response:

Response
{"result":false,"errors":{"codes":[172],"details":[{"code":172,"message":"Missing required field: email"}]}}

Why did we get an error? If we check the /wiki/spaces/~mike/pages/52887587 file, we see that this API is configured to receive a mandatory input called email. 

Call the API again but this time provide an email input in the query string.


curl -X GET "http://test-api.mydomain.com/jsconf1/hello?email=team@soajs.org"

Response:

Response
{"result":true,"data":"John Doe <team@soajs.org> "}

This time our request is successful.

Note that we did not provide a name and yet the request was successful and the response contained a name "John Doe", why is that?

Again, the answer is in /wiki/spaces/~mike/pages/52887587, we see that for the same API, the IMFV configuration states that the input name is not set to be "required" and that it also has a default value.

When making a call to this API, if you have an input configured like the name input and you don't provide a value for it, the IMFV will use the default value.

However if you do provide an input, then this input will be used.


curl -X GET "http://test-api.mydomain.com/jsconf1/hello?email=team@soajs.org&name=mike"

Response:

Response
{"result":true,"data":"mike <team@soajs.org> "}


Conclusion


The Input mapping, filtering and validation (IMFV) is an engine provided by the SOAJS framework and is responsible to fetch and clean the inputs arriving to your API at runtime.

It is based on json-schema and requires minimal configuration. The beauty about this engine is that it eliminates the headache and effort that developers need to fetch and clean inputs from arriving http requests so they can pass it later on to their business logic. Configure the IMFV as shown in the config.js and you are done. The IMFV will look for the inputs, clean them and consolidate them in one Javascript object called inputmaskData and hand them over to you to use in your API business logic.