Skip to content
Open
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 24 additions & 12 deletions nest/core/decorators/http_method.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
from typing import Callable, List, Union, TypeVar
from enum import Enum
from typing import Any, Callable, List, Union
import sys

if sys.version_info >= (3, 10):
from typing import ParamSpec, TypeAlias
else:
from typing_extensions import ParamSpec, TypeAlias


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

class HTTPMethod(Enum):
GET = "GET"
Expand All @@ -12,20 +22,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 +47,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)