2021.4.13 • Published 2 years ago

express-endpoint-wrapper v2021.4.13

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

express-endpoint-wrapper

This package provides a wrapper to avoid try-catch blocks in each express-endpoint. It detects errors byInstance or byMessage.

basic usage

const express = require("express");
const router = express.Router({mergeParams: true});
const eew = require("express-endpoint-wrapper");

module.exports = function () {
    router.get("/", eew({
        callback: (req, res) => {
            // controller belongs here - anything you would do inside a normal express-route
        },
    }));

    return router;
};

usage with errors detected by message

const express = require("express");
const router = express.Router({mergeParams: true});
const eew = require("express-endpoint-wrapper");

module.exports = function () {
    router.get("/", eew({
        callback: (req, res) => {
            // controller belongs here - anything you would do inside a normal express-route
        },
        errors: [
            {
                status: 400, byMessage: "message-that-should-be-detected",
                errorCallback: ({res, status, message}) => {    // is fully optional; if not provide -> console.log
                    // do something with the error...
                    res.status(status).json({ error: message }); // for example
                }
            }
        ]
    }));

    return router;
};

usage with errors detected by instance

const express = require("express");
const router = express.Router({mergeParams: true});
const eew = require("express-endpoint-wrapper");

module.exports = function () {
    router.get("/", eew({
        callback: (req, res) => {
            // controller belongs here - anything you would do inside a normal express-route
        },
        errors: [
            {
                status: 400, byInstance: SuperFancyCustomError,
                errorCallback: ({res, status, message}) => {    // is fully optional; if not provide -> console.log
                    // do something with the error...
                    res.status(status).json({ error: message }); // for example
                }
            }
        ]
    }));

    return router;
};
2021.4.13

2 years ago

2021.4.12

2 years ago

2021.4.11

2 years ago

2021.4.10

2 years ago

2021.5.0

2 years ago

2021.4.8

2 years ago

2021.4.7

2 years ago

2021.4.6

2 years ago

2021.4.5

2 years ago

2021.4.4

2 years ago

2021.4.3

2 years ago

2021.4.2

2 years ago

2021.4.1

2 years ago