OBSOLETED PACKAGE!!! Please use @leismore/lmos-nodejs-lmresponse instead!!!
Response class - A HTTP Response class for LMOS NodeJS projects.
npm install @leismore/response
npm test
import { LMResponse, LMResponseData } from '@leismore/response';
// In Express.js routing handler
function(req, res, next)
{
const resp = new LMResponse(res);
let data:LMResponseData = {statusCode: '200', headers: {'Content-Type': 'application/json'}, body: {'result': 'OK'}};
resp.send(data);
}
// In Express.js error handler
function(error, req, res, next)
{
const resp = new LMResponse(res);
resp.sendERROR(error);
}
LMResponse
{
protected readonly res:ResExpress; // Response in Express.js
public constructor(res:ResExpress) // Response in Express.js
/**
* Send HTTP response
* @param response - HTTP response data
* @throw {LMResponseError} - 1/2/3
*/
public send(response:LMResponseData):void
public sendERROR(error:Error):void
}
/**
* LMResponseError
* 1 invalid_http_statusCode
* 2 invalid_http_header
* 3 invalid_http_body
*/
LMResponseError extends LMError
{
public constructor(message:string, code:string, previous?:Error)
}
type LMResponseData = { // HTTP response
readonly statusCode: string, // HTTP response status code
headers?: {readonly [key:string]: string}, // HTTP headers
body?: any // HTTP body
};
Refer to LMError / @leismore/lmerror.
GNU Affero General Public License v3.0
- Kyle Chine (Initial Author / Sep 04, 2019)