express-wrapper-simple
TypeScript icon, indicating that this package has built-in type declarations

1.5.4 • Public • Published

Express Wrapper Simple

Simple express wrapper to provide standard formatted request and response handling. Sample provided in typescript.


Installation

npm i express-wrapper-simple

Quick Start

  • Import express wrapper as main express library
import { Express } from 'express-wrapper-simple';

...

// use express-wrapper-simple Express app:
// include default middlewares - body-parser (express built-in), helmet, cors, compression, morgan (log)
// include expressWrapper middleware
// include expressPagingWrapper middleware
this.express = Express({
  log: {
    enable: true,
    maxFileSize: '50M',
    interval: '1d'
  },
  cors: {
    methods: ['OPTIONS', 'GET', 'PUT', 'PATCH', 'POST', 'DELETE'],
    exposedHeaders: ['X-Auth-Token']
  },
  bodyParser: {
    // optionsJson: {
    //   strict: true
    // },
    optionsUrlencoded: {
      extended: false
    },
  },
});

...

// use imported express normally
this.express.use(otherMiddleWare());
  • Use as express wrapper as middleware
import * as express from 'express';
import { expressWrapper, expressPagingWrapper } from 'express-wrapper-simple';

...

// use default Express app
public express: express.Application = express();

// add expressWrapper middleware - only include express request(req) and response(res) wrapping
this.express.use(expressWrapper);

// add expressPagingWrapper middleware - only include request paging wrapping
this.express.use(expressPagingWrapper);

// use express normally
this.express.use(otherMiddleWare());

Features

  • express application auto include middlewares:
  • expressWrapper
    • extends express request, response, nextFunction to req, resp, next
    • resp implements sendSuccess and sendError function
    • express response wrap with default success or error object
  • expressPagingWrapper
    • auto convert query.* into query.paging.* for easier pagination handling

Examples (expressWrapper)

  • Using ExpressRouter, Req, Resp, response
import { ExpressRouter, Req, Resp, response } from 'express-wrapper-simple';

router: ExpressRouter = ExpressRouter();

...

//
// use built-in 'res.sendSuccess' & 'res.sendError' function
//
this.router.use('/login', (req: Req, res: Resp) => {
  if (!req.body.username) return res.sendError.badRequest('missing body.username');
  if (!req.body.password) return res.sendError.badRequest('missing body.password');

  //
  // login handling logic
  //

  if (unauthorized) return res.sendError.unauthorized('invalid username / password', errorDetail);
  return res.sendSuccess('login successfully', userDetails);
});

//
// different way of 'response.success' & 'response.error' usage
//
this.router.use('/another-login',(req: Req, res: Resp) => {
  if (!req.body.token) return res.status(403).json(response.error('Unauthorized', errorDetails, 'login-error-code'));

  //
  // login handling logic
  //

  return res.status(200).json(response.success('Welcome', userData));
});

...

this.expressApp.use('/api/auth', this.router);

References

  • resp implements:

    • sendSuccess (200)
    • sendError
      • badrequest (400)
      • unauthorized (401)
      • forbidden (403)
      • notfound (404)
      • toomanyrequest (429)
      • unknown (500)
      • maintenance (500)
  • express response wrap with default success and error object

    • success object
      • success (boolean) = true
      • message (string)
      • data (any)
      • paging (any)
    • error object
      • success (boolean) = false
      • message (string)
      • error (any)
      • code (string)
      • maintenance (boolean)
  • req handles:

    • query
      • limit (int) - max limit to 500
      • sortDir (string) - value: ['asc', 'desc']
      • sortBy (string)
      • pagingMode (string)
      • page (int)
      • nextToken (string)
    • [NEW] query.paging (convert query above into query.paging)
      • limit (int) - default: 15
      • sortDir (string) - default: asc
      • sortBy (string) - default: _id
      • pagingMode (string) - default: nopaging
      • page (int) - default: 1
      • sort (list of [sortBy]: [sortDir], key/value pairs)
      • nextToken (string)

Package Sidebar

Install

npm i express-wrapper-simple

Weekly Downloads

1

Version

1.5.4

License

ISC

Unpacked Size

10.6 kB

Total Files

4

Last publish

Collaborators

  • kimmanwky