What is @types/express-unless?
@types/express-unless is a TypeScript type definition package for the express-unless middleware. The express-unless middleware is used to conditionally add middleware to express routes based on certain conditions.
What are @types/express-unless's main functionalities?
Conditional Middleware Application
This feature allows you to conditionally apply middleware to routes. In this example, the middleware is skipped for the '/skip' route.
const express = require('express');
const unless = require('express-unless');
const app = express();
const middleware = (req, res, next) => {
res.send('Middleware applied');
};
middleware.unless = unless;
app.use(middleware.unless({ path: ['/skip'] }));
app.get('/', (req, res) => res.send('Hello World!'));
app.get('/skip', (req, res) => res.send('Skipped Middleware'));
app.listen(3000, () => console.log('Server running on port 3000'));
Conditional Middleware with Custom Condition
This feature allows you to use a custom condition to determine whether to apply the middleware. In this example, the middleware is skipped if the request contains a specific custom header.
const express = require('express');
const unless = require('express-unless');
const app = express();
const middleware = (req, res, next) => {
res.send('Middleware applied');
};
middleware.unless = unless;
const customCondition = (req) => req.headers['x-custom-header'] === 'skip';
app.use(middleware.unless(customCondition));
app.get('/', (req, res) => res.send('Hello World!'));
app.listen(3000, () => console.log('Server running on port 3000'));
Other packages similar to @types/express-unless
express-conditional-middleware
express-conditional-middleware is a package that allows you to conditionally apply middleware to routes based on custom conditions. It is similar to express-unless but provides a more flexible API for defining conditions.
Installation
npm install --save @types/express-unless
Summary
This package contains type definitions for express-unless (https://www.npmjs.org/package/express-unless).
Details
Files were exported from https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/express-unless.
import express = require("express");
declare function unless(options: unless.Options): express.RequestHandler;
declare function unless(options: unless.Options["custom"]): express.RequestHandler;
declare namespace unless {
type pathFilter = string | RegExp | { url: string | RegExp, methods?: string[] | undefined, method?: string | string[] | undefined };
export interface Options {
custom?: ((req: express.Request) => boolean) | undefined;
path?: pathFilter | pathFilter[] | undefined;
ext?: string | string[] | undefined;
method?: string | string[] | undefined;
useOriginalUrl?: boolean | undefined;
}
export interface RequestHandler extends express.RequestHandler {
unless?: typeof unless | undefined;
}
}
export = unless;
Additional Details
- Last updated: Tue, 06 Jul 2021 20:32:51 GMT
- Dependencies: @types/express
- Global values: none
Credits
These definitions were written by Wonshik Kim, Joao Vieira, and Michal Kaminski.