jsend-api
Model based JSON API module, implementing JSend specification for Express.js
Installation
npm install jsend-api --save
About
This package provides convenient way to shape, use and maintain JSON API responses from a single file. It's created for Express.js and implements JSend specification.
Usage
There're two possible ways of how the package may be used:
- Modeling API responses as an object without compiling it. The package is used as a function.
- Modeling API responses as an object and compiling it for use with Express.js.
First approach is more IntelliSense friendly.
Both approaches requirement is to have API modeled as a single object:
// Example API modelvar model = AUTHORIZATION: UNAUTHORIZED_USER: status: 'error' message: 'User not authorized. Access denied.' statusCode: 401 USER_NOT_FOUND: status: 'error' message: 'User not found. Access denied.' statusCode: 401 SOME_SERVICE: SERVICE_SUCCESS_ADD: status: 'success' message: 'Data added.' statusCode: 201 SERVICE_NOT_FOUND_ERROR: status: 'error' message: 'Something went wrong.' statusCode: 500
Each API response in the model should consist of at least required keys (according to JSend specification)
status
is always required and may be:success
,fail
,error
.message
is required only forerror
response, but may be supplied for all statuses.statusCode
is a HTTP status code which will be send by Express.js in response.
Approach 1 - model based API without compilation
api-model.js
var model = // the same model as above // exporting raw objectmoduleexports = model;
app.js
const app = ;const API = ; // require package as API and use as a functionconst RESPONSE = ; // import model as a RESPONSE app; app;
Approach 2 - compile based API model
api-model.js
var API = ; // import package and use as a class var model = // the same model as above // this will compile your model and save it in RESPONSE // field of object created by 'new APiModel()'moduleexports = ;
app.js
const app = ;const API = ; // import compiled model as 'API' app; app;
Output
Both aproaches will produce below output and Express.js send HTTP status 201
(as written in the model):
data
with JSON payload
Sending Because of data
field is always maintained by application and cannot be placed as const
inside model, package provide additional method withData()
which you can use for both described approaches.
var data = dummyData: 'qwerty' moreDummyData: 'ytrewq'// approach 1return ;// approach 2return APIRESPONSESOME_SERVICESERVICE_SUCCESS_ADD ;
The output will be as follow:
Adding optional fields to JSON payload
It is possible to send more data fields than it's specified by JSend. This option may be set in middleware API.config()
:
// Approach 1 import:const API = ; // <-- just package// Approach 2 import:const API = ; // <-- compiled model app;
When allowOptional
option is set more data may be appended to your model in optional
object:
var model = AUTHORIZATION: UNAUTHORIZED_USER: status: 'error' message: 'User not authorized. Access denied.' statusCode: 401 optional: // <-- must be an object optionalData1: 123 optionalData2: 'qwerty'
Output will be:
For dynamically created data withOptional()
function may be used:
var opts = optionalData3: 'option from withOptional() method' optionalData4: 'option from withOptional() method'// approach 1return ;// approach 2return APIRESPONSESOME_SERVICESERVICE_SUCCESS_ADD ;
Output will be:
Adding HTTP status code to JSON payload
It is possible to send HTTP status code in response. This option may be set in middleware API.config()
. Status code is taken directly from model:
// Approach 1 import:const API = ; // <-- just package// Approach 2 import:const API = ; // <-- compiled model app;
Output will be:
MIT License
Copyright (c) 2019 lookasc
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.