Open ChernichenkoStephan opened 5 months ago
Hi there thanks very much for pointing this out. Sry for the late reply. I still want to check if it maybe makes sense to have this typed better + later maybe have an additional response model which then also support other HTMX features (like oob updates), before changing the public API.
Do I understand it correctly, that this would allow to send response headers? I'm especially interested on how to send an HX-Trigger
header in my response
Yes. This feature adds full compatibility with Jinja2 Templates with full backward compatibility. Some htmx functionality uses headers, so i added support to use it in my project. Also this request adds ability to change status_code and media_type.
Example usage with headers:
@router.get("/update_cart/{product_id}/{action}", response_class=HTMLResponse)
@htmx('parts/cart_item', 'parts/cart_item')
def update_cart(
request: fastapi.Request,
response: fastapi.Response,
product_id: int,
action: str,
cart_service = fastapi.Depends(dependencies.get_cart_service)):
session_id = request.session[auth.consts.USER_ID_SESSION_KEY]
if action == 'increment':
item, _ = cart_service.add_to_cart(product_id, session_id)
if action == 'decrement':
item, _ = cart_service.remove_from_cart(product_id, session_id)
return {
'item': item,
'headers': {'HX-Trigger': 'my-custom-event'},
}
Short update: I'm still working on some refactoring I want to merge beforehand. But after that I'm looking forward to this as well!
This change adds support for status_code headers media_type background params