Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagejs
titleconfig.js
'use strict';
/**
module.exports =* {@license
 * Copyright SOAJS serviceName: "example01", 		//service name
    servicePort: 4010, 				//service port
    extKeyRequired: false, 			//set to true if the service is multi-tenant
    "errors": { 					//object that contains the error codes of the service
        "900": "firstName not found"
    },
    "schema": {						//object that contains the APIs schema
        "/testGet": {				//one API
            "_apiInfo":{			//API labels
                "l": "Test Get"
            },
            "firstName": {			//API Input
                All Rights Reserved.
 *
 * Use of this source code is governed by an Apache license that can be
 * found in the LICENSE file at the root of this repository
 */

module.exports = {
	"type": 'service',
	"prerequisites": {
		cpu: '',
		memory: ''
	},
	"serviceVersion": 1,
	"serviceName": "example01",
	"serviceGroup": "SOAJS Example Service",
	"requestTimeout": 30,
	"requestTimeoutRenewal": 5,
	"servicePort": 4010,
	
	"extKeyRequired": false,
	"oauth": false,
	
	"errors": {
		"900": "firstName not found"
	},
	"schema": {
		"get": {
			"/testGet": {
				"_apiInfo": {
					"l": "Test Get",
					"group": "Example"
				},
				"firstName": {
					"source": ['query.firstName'],
                					"required": true,
					"validation":                "validation{
						"type": "string"
					}
				},
				"lastName": {
					"source":                    ['query.lastName'],
					"required": true,
					"validation": {
						"type": "string"
					}
				},
				"email":              }
            },
            "lastName": {			//API Input
                "source": ['query.lastName'],
                "required": true,
                "validation": {
                    "type": "string"
                }
            },
            "email": {				//API Input
                {
					"source": ['query.email'],
					"required": false,
					"validation": {
						"type": "string",
						"format": "email"
					}
				}
			},
			"/buildName": {
				"_apiInfo": {
					"l": "Build Name",
					"group": "Example"
				},
				"firstName": {
					"source": ['query.firstName'],
					"required": true,
					"default": "John",
					"validation": {
						"type": "string"
					}
				},
				"lastName": {
					"source": ['query.emaillastName'],
                					"required": false,
                true,
					"validation": {
                    						"type": "string",
					}
				}
			}
		},
		"delete": {
			"/testDel":              "format{
				"_apiInfo": {
					"l": "Test Delete",
					"group": "emailExample"
				},
				"firstName": {
					"source": ['query.firstName'],
					"required": false,
					"validation": {
						"type": "string"
     }
            }
        },
        "/testDel": {
            "_apiInfo":{
                					}
				},
				"lastName": {
					"source": ['query.lastName'],
					"required": false,
					"validation": {
						"type": "string"
					}
				}
			}
		},
		"post": {
			"/testPost": {
				"_apiInfo": {
					"l": "Test DeletePost",
					"group":            },
            "Example"
				},
				"firstName": {
                					"source": ['querybody.firstName'],
                					"required": false,
                true,
					"validation": {
                    						"type": "string"
					}
               }
            },
            "lastName": {
                "source": ['query.lastName'],
                "required": false,
                "validation": {
                    "type": "string"
                }
            }
        },
        "/buildName": {
            "_apiInfo":{
                "l": "Build Name"
            },
            "firstName": {
                "source": ['query.firstName'],
                "required": true,
                "default": "John",
                "validation": {
                    "type": "string"
                }
            },
            "lastName": {
                "source": ['query.lastName'],
                "required": true,
                "validation": {
                    "type": "string"
                }
            }
        },
        "/testPost": {
            "_apiInfo":{
                "l": "Test Post"
            },
            "firstName": {
                "source": ['body.firstName'],
                "required": true,
                "validation": {
                    "type": "string"
                }
            },
            "lastName": {
                "source": ['body.lastName'],
                "required": true,
                "validation": {
                    "type": "string"
                }
            },
            "email": {
                "source": ['body.email'],
                "required": false,
                "validation": {
                    "type": "string",
                    "format": "email"
                }
            }
        },
        "/testPut": {
            "_apiInfo":{
                "l": "Test Put"
            },
            "firstName": {
                "source": ['body.firstName'],
                "required": true,
                "validation": {
                    "type": "string"
                }
            },
            "lastName": {
                "source": ['body.lastName'],
                "required": true,
                "validation": {
                    "type": "string"
                }
            },
            "email": {
                "source": ['body.email'],
                "required": false,
                "validation": {
                    "type": "string",
                    "format": "email"
                }
            }
        }
    }
};

As we mentioned above, the "config.js" contains all the metadata of the service, including its APIs.

In the sample above, there exists information about the service: its name, the port it listens to, the errors, and finally a schema containing the APIs.

Let us analyze the "testGet" API:

Each API is referenced by a label, which is, in this case, "Test Get", under the "_apiInfo" JSON object.

This API expects three input parameters (firstName, lastName, and email) of type String.

These parameters are supplied through the query (i.e. the URL passed in the browser).

More information related to the IMFV can be found under the IMFV section in the documentation.

In this example, the firstName, and lastName are required, whereas the email is not.

Similar to the explained API, the user must add in the schema all the needed APIs, along with any additional information needed.

Code Block
languagejs
titleindex.js
'use strict';
var soajs = require('soajs');						//require soajs
var				},
				"lastName": {
					"source": ['body.lastName'],
					"required": true,
					"validation": {
						"type": "string"
					}
				},
				"email": {
					"source": ['body.email'],
					"required": false,
					"validation": {
						"type": "string",
						"format": "email"
					}
				}
			}
		},
		"put": {
			"/testPut": {
				"_apiInfo": {
					"l": "Test Put",
					"group": "Example"
				},
				"firstName": {
					"source": ['body.firstName'],
					"required": true,
					"validation": {
						"type": "string"
					}
				},
				"lastName": {
					"source": ['body.lastName'],
					"required": true,
					"validation": {
						"type": "string"
					}
				},
				"email": {
					"source": ['body.email'],
					"required": false,
					"validation": {
						"type": "string",
						"format": "email"
					}
				}
			}
		}
	}
};

As we mentioned above, the "config.js" contains all the metadata of the service, including its APIs.

In the sample above, there exists information about the service: its name, the port it listens to, the errors, and finally a schema containing the APIs.

Let us analyze the "testGet" API:

Each API is referenced by a label, which is, in this case, "Test Get", under the "_apiInfo" JSON object.

This API expects three input parameters (firstName, lastName, and email) of type String.

These parameters are supplied through the query (i.e. the URL passed in the browser).

More information related to the IMFV can be found under the IMFV section in the documentation.

In this example, the firstName, and lastName are required, whereas the email is not.

Similar to the explained API, the user must add in the schema all the needed APIs, along with any additional information needed.

Code Block
languagejs
titleindex.js
'use strict';
/**
 * @license
 * Copyright SOAJS All Rights Reserved.
 *
 * Use of this source code is governed by an Apache license that can be
 * found in the LICENSE file at the root of this repository
 */

const soajs = require('soajs');
const config = require('./config.js');

varlet service = new soajs.server.service(config);		//create new service instance

service.init(function () {								//initialize the service
     soajs.server.service(config);

service.init(function () {
	service.get("/testGet", function (req, res) {	//implement the API business logic
        
		res.json(req.soajs.buildResponse(null, {	//generate API response
            buildResponse(null, {
			firstName: req.soajs.inputmaskData.firstName,
            			lastName: req.soajs.inputmaskData.lastName,
            			email: req.soajs.inputmaskData.email
        		}));
    	});

;
	
  	service.get("/buildName", function (req, res) {
        		//write your business logic here
        var 		let fullName = req.soajs.inputmaskData.firstName + ' ' + req.soajs.inputmaskData.lastName;
        		res.json(req.soajs.buildResponse(null, {
            fullName: fullName
        
			fullName: fullName
		}));
    	});
	
    	service.delete("/testDel", function (req, res) {
        		// some business logic
        		res.json(req.soajs.buildResponse(null, true));
    	});
	
    	service.post("/testPost", function (req, res) {
         res) {
		if (req.soajs.inputmaskData.firstName !== 'John') {
            			//EXAMPLE: to simulate error response return
            			res.json(req.soajs.buildResponse({"code": 900, "msg": config.errors[900]}));
        		} else {
            			res.json(req.soajs.buildResponse(null, {
                				firstName: req.soajs.inputmaskData.firstName,
                				lastName: req.soajs.inputmaskData.lastName,
                				email: req.soajs.inputmaskData.email
            			}));
        		}
    	});
	
    	service.put("/testPut", function (req, res) {
        		res.json(req.soajs.buildResponse(null, {
            .buildResponse(null, {
			firstName: req.soajs.inputmaskData.firstName,
            			lastName: req.soajs.inputmaskData.lastName
        		}));
    	});
	
    	service.start();	//start the
service
});

The sampleĀ above contains the implementation of each of the five APIs that form the service.

...