Proposal: Provides the hono/decorators
package that allows you to define routes using ECMA Decorators
#3941
Labels
enhancement
New feature or request.
What is the feature you are proposing?
Description
Not long ago, I came into contact with Hono. I chose Hono because of its great api and Web standards. Just last month, I implemented a
nest-hono-adapter
, but I found that there is a better way to make the decorator and Hono work together.My English is not good, so the following sentence may seem a little strange
ECMA decorators are currently at Stage 3. In the future, they will become a standard part of JavaScript syntax. For now, we can use this syntax through TypeScript.
We can leverage decorators and decorator metadata to achieve functionality similar to Nest's decorators. Since Stage 3 decorators do not include parameter decorators, here we only consider using decorators for route definitions and do not consider dependency injection.
Because it is a JavaScript standard, this will not introduce third-party packages.
If you accept the proposal, I am willing to contribute a PR.
Related links
typescript: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-0.html#decorators
tc39: https://github.com/tc39/proposal-decorators
A simple example
API Design
After applying the decorator, it actually just adds metadata to this class. When
applyController()
is called, it reads the metadata of this class and then sets up routes and middleware based on the metadata.Endpoint Decorators
Endpoint decorators are the foundation of all decorators. Before applying other decorators, an endpoint decorator must be applied, and a method or property can only have one endpoint decorator applied to it.
Controller Decorators
Controller decorators can define certain behaviors for a group of routes, such as applying middleware, etc.
Middleware Decorators
Middleware decorators can be applied to classes, methods, or properties. The order in which requests pass through middleware is from the outside to the inside (opposite to the order in which the decorators are called), which makes it easier to intuitively understand the process from the request to the route handler.
The sequence of requests passed through: A > B > C > D > E > F > method() > F > E > D > C > B > A
Transform Decorator
The transform decorator can transform the Hono's Context object into the parameters required by the controller method, and also convert the object returned by the controller method into a Response object.
Custom Decorators
Based on the decorators mentioned above, we can define specific responsibility decorators, such as setting variables, parameter validation, and permission validation, etc.
An example of permission validation:
Extends
If a subclass controller class declares
@Controller({ extends: true })
, then the subclass will inherit the route and middleware configurations of the parent class, otherwise it will ignore all decorators of the parent class.If
applyController(hono, new Bird())
is called, only/fly
will be added, and the middleware defined onAnimal
will not take effect.If
applyController(hono, new Dog())
is called, only/animal/sleep
,/animal/eat
, and/animal/speak
will be added, and all these requests will pass through thebodyLimit
decorator applied onAnimal
.We can also modify some settings of the parent class in the subclass.
This example rewrites the
basePath
, theeat()
method, and the/speak
route.If
applyController(hono, new Cat())
is called, only/run
,/eat
, and/speak
will be added.GET /eat response
Cat eat
GET /speak response
Cat speak
The above is my preliminary design
The text was updated successfully, but these errors were encountered: