Add JSONAPI decorator

This commit is contained in:
2026-01-14 09:01:49 +00:00
parent 7b03312fb2
commit 5e1f464ff5

View File

@@ -159,3 +159,18 @@ def JSONResponse(d: dict, status=200):
return HTTPResponse(
json.dumps(d), status=status, content_type="text/json; charset=utf-8"
)
def JSONAPI(func):
def wrapper(*args, **kwargs):
result = func(*args, **kwargs)
if not isinstance(result, dict):
if (
isinstance(result, tuple)
and len(result) == 2
and isinstance(result[1], dict)
and isinstance(result[0], int)
):
return JSONResponse(result[1], result[0])
raise RuntimeError("Return value of JSONAPI route is not a dictionary")
return JSONResponse(result)