lib/middleware/middleware.js

/**
 * Contains all middleware for expressjs endpoints.
 * @module middleware
 */

/**
 * Wraps async expressjs endpoint functions so that they resolve and have errors caught properly.
 * @function wrapAsync
 * @param {Function} fn - An async function to wrap that accepts (req, res, next) or (req, res) as parameters.
 * @returns {Function} A function that wraps fn, forcing it to resolve and catching any errors.
 * @example
 * router.get('/', wrapAsync(async (req, res, next) => {
 *  const responseData = await someAsyncFunction();
 *  res.send(responseData);
 *  }));
 */
const wrapAsync = require('./asyncWrap');

/**
 * Middleware to authorize devices that have completed mutual TLS authentication.
 * @function authorized
 * @param {Object} req - an expressjs request object.
 * @param {Object} res - an expressjs response object.
 * @param {Function} next - the expressjs next function.
 *
 * @example
 * router.post('/', authorized, (req, res, next) => {
 *  res.send("Device authenticated successfully!")
 * });
 */
const authorized = require('./authorized');

/**
 * Middleware to authenticate JWT from the spool spa that are generated by Auth0.
 * @function secured
 * @name module:middleware#secured
 * @example
 * router.get('/', secured, (req, res, next) => {
 *  res.send("This user is authorized to access this API endpoint");
 * });
 */
const secured = require('./secured');

/**
 * Middleware to provide the user in api endpoints that are protected with {@link module:middleware#secured secured()}.
 * @function userInApi
 * @see secured
 */
const userInApi = require('./userInApi');


/**
 * Middleware to wrap request to the database and provide automatic error response handling.
 * @function databaseWrap
 * @param {Function} fn - The function to wrap.
 */
const databaseWrap = require('./databaseWrap');

module.exports = {
	wrapAsync: wrapAsync,
	authorized: authorized,
	secured: secured,
	databaseWrap: databaseWrap
};