Skip to content

feat(cors): allowedMethods by function #4060

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

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

Conversation

Kanahiro
Copy link
Contributor

@Kanahiro Kanahiro commented Apr 9, 2025

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

Proposal

In my usecase, it is needed to set different Access-Control-Allow-Methods by origins.
Then, I modified hono/cors for 'allowedMethods' to accept function like origins.
How do you think?

Usecase

GET or HEAD can be performed from any origins, but others only from specific origin.

app.use(
  '/api7/*',
  cors({
    origin: (origin) => (origin === 'http://example.com' ? origin : '*'),
    allowMethods: (origin) =>
      origin === 'http://example.com'
        ? ['GET', 'HEAD', 'POST', 'PATCH', 'DELETE']
        : ['GET', 'HEAD'],
  })
)

Copy link

codecov bot commented Apr 9, 2025

Codecov Report

Attention: Patch coverage is 83.33333% with 2 lines in your changes missing coverage. Please review.

Project coverage is 91.27%. Comparing base (16a9a94) to head (dfe8998).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
src/middleware/cors/index.ts 83.33% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4060      +/-   ##
==========================================
- Coverage   91.28%   91.27%   -0.02%     
==========================================
  Files         168      168              
  Lines       10769    10779      +10     
  Branches     3029     3155     +126     
==========================================
+ Hits         9831     9839       +8     
- Misses        937      939       +2     
  Partials        1        1              

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@yusukebe
Copy link
Member

Hi @Kanahiro

In my usecase, it is needed to set different Access-Control-Allow-Methods by origins. Then, I modified hono/cors for 'allowedMethods' to accept function like origins. How do you think?

I think this is a good feature! I'll review the details later.

@yusukebe yusukebe changed the title feat: allowedMethods by function feat(cors): allowedMethods by function Apr 12, 2025
Copy link
Member

@yusukebe yusukebe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@yusukebe
Copy link
Member

Hi @Kanahiro

I've reviewed this PR. It looks good. This is a new feature, but it's a slight change. So, I'm wondering which minor or patch release we should make. So, I'll decide and merge it later. Thanks!

@Kanahiro
Copy link
Contributor Author

Kanahiro commented Apr 12, 2025

Sounds great!

I've found and added a tiny improvement to remove one char ?, it won't change any behavior.

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

Successfully merging this pull request may close these issues.

2 participants