Skip to content

Commit

Permalink
add annotation autocomplete
Browse files Browse the repository at this point in the history
  • Loading branch information
dev0Guy committed Feb 8, 2025
1 parent daaced3 commit 1ace89b
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions nest/core/decorators/http_method.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from typing import Callable, List, Union, TypeVar, ParamSpec, TypeAlias
from enum import Enum
from typing import Any, Callable, List, Union

P = ParamSpec("P")
R = TypeVar("R")
Func: TypeAlias = Callable[[P], R]

class HTTPMethod(Enum):
GET = "GET"
Expand All @@ -12,20 +15,22 @@ class HTTPMethod(Enum):
OPTIONS = "OPTIONS"


def route(http_method: HTTPMethod, route_path: Union[str, List[str]] = "/", **kwargs):
def route(http_method: HTTPMethod, route_path: Union[str, List[str]] = "/", **kwargs) -> Callable[[Func], Func]:
"""
Decorator that defines a route for the controller.
Args:
http_method (HTTPMethod): The HTTP method for the route (GET, POST, DELETE, PUT, PATCH).
route_path (Union[str, List[str]]): The route path for the route. example: "/users"
route_path (Union[str, List[str]]): The route path for the route.
**kwargs: Additional keyword arguments to configure the route.
Returns:
function: The decorated function.
function: The decorated function, preserving the signature.
"""

def decorator(func):
def decorator(func: Func) -> Func:

# Add custom route metadata
func.__http_method__ = http_method
func.__route_path__ = route_path
func.__kwargs__ = kwargs
Expand All @@ -35,29 +40,29 @@ def decorator(func):
return decorator


def Get(route_path: Union[str, List[str]] = "/", **kwargs) -> Callable[..., Any]:
def Get(route_path: Union[str, List[str]] = "/", **kwargs):
return route(HTTPMethod.GET, route_path, **kwargs)


def Post(route_path: Union[str, List[str]] = "/", **kwargs) -> Callable[..., Any]:
def Post(route_path: Union[str, List[str]] = "/", **kwargs):
return route(HTTPMethod.POST, route_path, **kwargs)


def Delete(route_path: Union[str, List[str]] = "/", **kwargs) -> Callable[..., Any]:
def Delete(route_path: Union[str, List[str]] = "/", **kwargs):
return route(HTTPMethod.DELETE, route_path, **kwargs)


def Put(route_path: Union[str, List[str]] = "/", **kwargs) -> Callable[..., Any]:
def Put(route_path: Union[str, List[str]] = "/", **kwargs):
return route(HTTPMethod.PUT, route_path, **kwargs)


def Patch(route_path: Union[str, List[str]] = "/", **kwargs) -> Callable[..., Any]:
def Patch(route_path: Union[str, List[str]] = "/", **kwargs):
return route(HTTPMethod.PATCH, route_path, **kwargs)


def Head(route_path: Union[str, List[str]] = "/", **kwargs) -> Callable[..., Any]:
def Head(route_path: Union[str, List[str]] = "/", **kwargs):
return route(HTTPMethod.HEAD, route_path, **kwargs)


def Options(route_path: Union[str, List[str]] = "/", **kwargs) -> Callable[..., Any]:
def Options(route_path: Union[str, List[str]] = "/", **kwargs):
return route(HTTPMethod.OPTIONS, route_path, **kwargs)

0 comments on commit 1ace89b

Please sign in to comment.