1.0.2 • Published 3 months ago

@leismore/lmos-nodejs-lmresponse v1.0.2

Weekly downloads
-
License
AGPL-3.0-only
Repository
github
Last release
3 months ago

lmos-nodejs-lmresponse

A general HTTP Response class for LMOS for Node.js project

Features

  • Sending HTTP responses
  • Sending LMError and other Errors as HTTP responses

Environment

Installation

npm install @leismore/lmos-nodejs-lmresponse

Test

npm test

Build

npm run build

Example

import { LMResponse, LMResponseData } from '@leismore/lmos-nodejs-lmresponse';

// 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 Class

Data Types

type LMResponseData = {                                 // HTTP response
  readonly statusCode:  string,                         // HTTP response status code
           headers?:   {readonly [key:string]: string}, // HTTP headers
           body?:       any                             // HTTP body
};

LMResponseError

/**
 * LMResponseError
 *   1  invalid_http_statusCode
 *   2  invalid_http_header
 *   3  invalid_http_body
 */

class LMResponseError extends LMError
{
  public constructor(message:string, code:string, previous?:Error)
}

Refer to LMError

LMResponse Class

Properties

import {Response as ResExpress} from 'express';

protected readonly res:ResExpress;

Methods

import {Response as ResExpress} from 'express';


public constructor(res:ResExpress)


/**
 * Send HTTP response
 * @param   {LMResponseData}   response   - HTTP response data
 * @throws  {LMResponseError}             - 1/2/3
 */
public send(response:LMResponseData):void


/**
 * Server-side error -> Stop this whole application
 * 
 * Exit Code: 1
 */
public sendERROR(error:Error):void

Refer to LMError

Donation

Donate with PayPal button

License

© Leismore 2024

GNU AFFERO GENERAL PUBLIC LICENSE v3

Authors

Credits


Leismore Logo

Product of Leismore OpenSource

Leismore (Australian Business Number: 25 935 862 619) is your affordable and reliable business software provider since 2021