1.5.4 • Published 2 years ago

express-wrapper-simple v1.5.4

Weekly downloads
-
License
ISC
Repository
-
Last release
2 years ago

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)
1.5.4

2 years ago

1.5.3

3 years ago

1.5.2

3 years ago

1.5.1

3 years ago

1.5.0

3 years ago

1.4.5

3 years ago

1.4.4

3 years ago

1.4.3

3 years ago

1.4.2

3 years ago

1.4.1

4 years ago

1.4.0

4 years ago

1.3.1

4 years ago

1.3.0

4 years ago

1.2.4

4 years ago

1.2.3

4 years ago

1.2.2

4 years ago

1.2.0

4 years ago

1.2.1

4 years ago

1.1.12

4 years ago

1.1.11

4 years ago

1.1.10

4 years ago

1.1.9

4 years ago

1.1.8

4 years ago

1.1.7

4 years ago

1.1.6

4 years ago

1.1.5

4 years ago

1.1.4

4 years ago

1.1.3

4 years ago

1.1.2

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago