https://github.com/tinyhttp/unless
🚦 Unless middleware for tinyhttp
https://github.com/tinyhttp/unless
http middleware node-js nodejs tinyhttp unless unless-middleware
Last synced: 2 months ago
JSON representation
🚦 Unless middleware for tinyhttp
- Host: GitHub
- URL: https://github.com/tinyhttp/unless
- Owner: tinyhttp
- License: mit
- Created: 2021-07-30T09:49:38.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2023-10-27T13:54:01.000Z (over 1 year ago)
- Last Synced: 2025-03-24T08:47:05.583Z (3 months ago)
- Topics: http, middleware, node-js, nodejs, tinyhttp, unless, unless-middleware
- Language: TypeScript
- Homepage: https://tinyhttp.v1rtl.site/mw/@tinyhttp/unless
- Size: 173 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# @tinyhttp/unless
[![npm][npm-img]][npm-url] [![GitHub Workflow Status][gh-actions-img]][github-actions] [![Coverage][cov-img]][cov-url]
Unless middleware for tinyhttp that executes a middleware conditionally.
## Install
```sh
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
```ts
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)
```[npm-url]: https://npmjs.com/package/@tinyhttp/unless
[github-actions]: https://github.com/tinyhttp/unless/actions
[gh-actions-img]: https://img.shields.io/github/actions/workflow/status/tinyhttp/unless/ci.yml?branch=master&style=for-the-badge&logo=github&label=&color=hotpink
[cov-img]: https://img.shields.io/coveralls/github/tinyhttp/unless?style=for-the-badge&color=hotpink&
[cov-url]: https://coveralls.io/github/tinyhttp/unless
[npm-img]: https://img.shields.io/npm/dt/@tinyhttp/unless?style=for-the-badge&color=hotpink