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(http/unstable): add capability to attach handlers by methods, implement sensible defaults #6305

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

kerezsiz42
Copy link

Hi everyone!

I would like to propose a new implementation of the route() function that adds the capability to define handlers on a per-method basis (in a style similar to Fresh) or to use a single handler that handles all methods.

I envision the route() function being used only once when we pass it to Deno.serve(). In the case of a REST API, one would:

  • Export an array of routes in user.ts with URL patterns such as /api/users and /api/users/:id.
  • Export an array of routes in products.ts with URL patterns such as /api/products and /api/products/:id.
  • Import these in main.ts, then call Deno.serve(route([...userRoutes, ...productRoutes, notFoundRoute], errorHandler)).

The current defaultHandler() does not seem very useful, as a wildcard route can provide the same functionality (see tests). I believe an optional errorHandler() would be more important.

While my PR adds some logic to the route() function, it conforms to the relevant specifications (RFC2616) and practices regarding the "Method Not Allowed" response, error handling, and the default "Not Found" response, at least to my understanding. Furthermore, all of the new default behavior remains customizable by the user.

Accepting this PR in its current form would essentially mean that the URLPattern would be the only factor determining whether a Route can handle the request or not.

@kerezsiz42 kerezsiz42 requested a review from kt3k as a code owner December 25, 2024 10:18
@CLAassistant
Copy link

CLAassistant commented Dec 25, 2024

CLA assistant check
All committers have signed the CLA.

@github-actions github-actions bot added the http label Dec 25, 2024
Copy link

codecov bot commented Dec 25, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.34%. Comparing base (c40f593) to head (6f1d3e6).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #6305   +/-   ##
=======================================
  Coverage   96.33%   96.34%           
=======================================
  Files         547      547           
  Lines       41667    41681   +14     
  Branches     6314     6316    +2     
=======================================
+ Hits        40140    40157   +17     
+ Misses       1484     1482    -2     
+ Partials       43       42    -1     

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

@kerezsiz42 kerezsiz42 changed the title feat(http/route): add capability to attach handlers by methods, implement sensible defaults feat(http/unstable): add capability to attach handlers by methods, implement sensible defaults Dec 25, 2024
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