@tinyhttp/unless
TypeScript icon, indicating that this package has built-in type declarations

3.0.0 • Public • Published

Unless middleware for tinyhttp that executes a middleware conditionally.

Install

pnpm i @tinyhttp/unless

API

unless(middleware, (UnlessMiddlewareOptions | CustomUnless))

The UnlessMiddlewareOptions object can include:

  • method - string or array of strings that describe forbidden http methods such as GET, POST, PUT etc...
  • path - array of strings, Regex and objects that include url and methods properties, which will be compared against the request.
  • ext - string or array of strings that describe forbidden path ends (e.g. in /user/123 it will check against /123).

The CustomUnless is a function that receives a Request object and returns a boolean. The result of the function will determine if the execution of the middleware is skipped.

Example

import { App } from '@tinyhttp/app'
import { unless } from '@tinyhttp/unless'
import { cors } from '@tinyhttp/cors'

const app = new App()

app
  .use(unless(cors(),  { method: ['GET', 'POST'] }))
  .use(unless(cors(), { ext: '/public' }))
  .use(unless(cors(), (req) => req.method === 'GET')
  .use(unless(cors(), { path: ['/content/public', /user/, { url: '/public', methods: ['GET'] }] })
  .listen(3000)

Package Sidebar

Install

npm i @tinyhttp/unless

Weekly Downloads

5

Version

3.0.0

License

MIT

Unpacked Size

8.09 kB

Total Files

5

Last publish

Collaborators

  • dropthebeatbro