Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(middleware/predicate): Introduce predicate middleware #2941

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

usualoma
Copy link
Member

@usualoma usualoma commented Jun 9, 2024

Proposes middleware to compose multiple middleware. This middleware provides the following three functions.

  • some : Create a composed middleware that runs the first middleware that returns true.
  • every : Create a composed middleware that runs all middleware and throws an error if any of them fail.
  • except : Create a composed middleware that runs all middleware except when the condition is met.

Usage is as indicated in comments and tests.

This change will allow users to write code similar to that illustrated in the comments below.

#2813 (comment)

import { Hono } from 'hono'
import { ipLimit } from 'hono/ip-limit'
import { bearerAuth } from 'hono/bearer-auth'
import { rateLimit } from '@/my-rate-limit'
import { some, every } from 'hono/predicate' // New middleware
import { getConnInfo } from 'hono/...'

const app = new Hono()

// Very complex access restriction rules.
app.use(
  '*',
  some([
    every([
      ipLimit(getConnInfo, { allow: [ '192.168.0.2' ] },
      bearerAuth({ token })
    ]), // If both are satisfied, rateLimit is not necessary.
    rateLimit()
  ])
)
app.get('/', (c) => c.text('Hello world!'))

Name of middleware

I named it "predicate", but I am not particularly particular about it, so it could be named something else. I also thought of "compose", but it is now "predicate" because it is confusing because it is covered by "compose", which we use internally.

The author should do the following, if applicable

  • Add tests
  • Run tests
  • bun run format:fix && bun run lint:fix to format the code
  • Add TSDoc/JSDoc to document the code

Copy link

codecov bot commented Jun 9, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 94.48%. Comparing base (fe7cfcf) to head (0ee0075).
Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2941      +/-   ##
==========================================
+ Coverage   94.42%   94.48%   +0.06%     
==========================================
  Files         136      137       +1     
  Lines       13343    13502     +159     
  Branches     2219     2282      +63     
==========================================
+ Hits        12599    12758     +159     
  Misses        744      744              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@yusukebe
Copy link
Member

Hey @usualoma .

This middleware is unique and exciting, and implementations like except are very interesting. I think there are other use cases like this where we want to control middleware conditions, so let me think about that for a while!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants