🚦 Unless middleware for tinyhttp
Go to file
v1rtl 9a779770fb 3.0.0 2023-10-27 16:53:30 +03:00
.github Update FUNDING.yml 2023-10-27 13:52:04 +00:00
.husky chore: use bun for husky 2023-10-27 16:42:29 +03:00
src fix: add router as explicit dependency 2023-10-26 22:10:10 +03:00
tests test: make use of generics 2023-10-26 22:05:10 +03:00
.eslintrc chore: enable ts eslint rules 2023-10-27 16:04:14 +03:00
.gitignore fix: ci 2023-10-26 21:59:29 +03:00
.prettierrc Initial commit 2021-07-30 12:49:39 +03:00
LICENSE Initial commit 2021-07-30 12:49:39 +03:00
README.md chore: readme badge 2023-10-26 22:03:33 +03:00
bun.lockb chore: cleanup 2023-10-26 22:10:57 +03:00
commitlint.config.cjs Initial commit 2021-07-30 12:49:39 +03:00
package.json 3.0.0 2023-10-27 16:53:30 +03:00
tsconfig.eslint.json feat: move to bun, make types more friendly 2023-10-26 21:53:37 +03:00
tsconfig.json chore: drop support for Node.js 12 and 14 before 14.18 2023-10-27 16:51:11 +03:00

README.md

@tinyhttp/unless

npm GitHub Workflow Status Coverage

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)