Create a routes constants file and a router for getting routes in app
This commit is contained in:
parent
790e0a1ce5
commit
5a4f9dc250
5 changed files with 93 additions and 10 deletions
0
src/constants/__init__.py
Normal file
0
src/constants/__init__.py
Normal file
47
src/constants/routes.py
Normal file
47
src/constants/routes.py
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
import abc
|
||||
from typing import Any, TypedDict
|
||||
from dataclasses import dataclass, asdict
|
||||
|
||||
|
||||
class ParamCodec[Params]:
|
||||
@abc.abstractmethod
|
||||
def parse(self, path) -> Params:
|
||||
pass
|
||||
|
||||
|
||||
class EmptyParams(ParamCodec[None]):
|
||||
def parse(self, path) -> None:
|
||||
pass
|
||||
|
||||
|
||||
@dataclass
|
||||
class Route[Params: Any]:
|
||||
path: str
|
||||
codec: ParamCodec[Params]
|
||||
|
||||
def interpolate(self, params: Params) -> str:
|
||||
interpolated = self.path
|
||||
if params:
|
||||
for k, v in asdict(params).items():
|
||||
interpolated.replace(f':{k}', str(v))
|
||||
|
||||
return interpolated
|
||||
|
||||
|
||||
def static(path: str) -> Route[None]:
|
||||
return Route(path, EmptyParams())
|
||||
|
||||
|
||||
class Routes(TypedDict):
|
||||
index: Route[None]
|
||||
confirm_email: Route[None]
|
||||
signup: Route[None]
|
||||
dashboard: Route[None]
|
||||
|
||||
|
||||
ROUTES: Routes = {
|
||||
'index': static('/'),
|
||||
'confirm_email': static('/confirm-email'),
|
||||
'signup': static('/register'),
|
||||
'dashboard': static('/dashboard'),
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue