amk-wrap

0.2.0 • Public • Published

AMK-WRAP

Github Actions codecov Known Vulnerabilities

function wrapper to catch errors in express controllers

Usage

to install: npm i amk-wrap

can pass a function or a class method.

on the router file:

const express = require('express');
const router = express.Router();
const wrap = require('amk-wrap');

class Controller {
  constructor() {
    this.something = "something"
  }
  async get(req, res) {
    this.something; // access this
    res.send('hello world');
  }
}
const controller = new Controller();
module.exports = function (controller) {
  router.get('/', wrap(controller, 'get')); // pass a class method
  return router;
}

on the index.js:

const wrap = require('amk-wrap');

// some other code

app.get('/', wrap(getFunction)); // pass a function

or

const wrap = require('amk-wrap');

// some other code

app.get('/', wrap((req, res) => {
	res.send('hello world');
}));

Tests

  1. install dependencies using npm install
  2. run npm test

Feedback

All bugs, feature requests, pull requests, feedback, etc., are welcome. Create an issue.

License

MIT

Readme

Keywords

none

Package Sidebar

Install

npm i amk-wrap

Weekly Downloads

9

Version

0.2.0

License

MIT

Unpacked Size

9.22 kB

Total Files

12

Last publish

Collaborators

  • heinrich10