Repository navigation
0.6.0
Highlight
Depends
Now, Nexify has a very powerful Dependency Injection system.
def common_parameters(
q: Annotated[str | None, Query()] = None,
skip: Annotated[int, Query()] = 0,
limit: Annotated[int, Query()] = 100,
):
return {"q": q, "skip": skip, "limit": limit}
@app.get("/items")
def read_items(
common: Annotated[dict, Depends(common_parameters)],
):
...Also, we supports dependencies that do some extra steps after finishing.
To do this, use yield instead of return, and write the extra steps (code) in the finally scope.
def get_db():
db = Session()
try:
yield db
finally:
db.close()
@app.get("/user")
def read_user(db: Annotated[Session, Depends(get_db)]):This is very similar to Depends in FastAPI.
Header and Cookie
In Nexify, you can extract values from request headers and cookies using Header and Cookie dependencies.
@app.get("/headers")
def read_headers(user_agent: Annotated[str, Header()], x_token: Annotated[str, Header()]) -> dict:
return {"User-Agent": user_agent, "X-Token": x_token}Similarly, you can extract cookies like this:
@app.get("/cookies")
def read_cookies(session_id: Annotated[str, Cookie()]) -> dict:
return {"Session-ID": session_id}