5.0.2 • Published 2 years ago
@leismore/response v5.0.2
Response Class
Response class - A HTTP Response class for LMOS NodeJS projects.
Donation
Installation
npm install @leismore/response
Test
npm test
Example
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);
}
API
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.
License
GNU Affero General Public License v3.0
Authors
- Kyle Chine (Initial Author / Sep 04, 2019)