Compare commits

...

1 commit

Author SHA1 Message Date
Simon Willison
e0a84691c2 Prototyp of link rel=alternate header for tables
Refs https://github.com/simonw/datasette-notebook/issues/2
2021-11-27 12:08:42 -08:00
3 changed files with 19 additions and 2 deletions

View file

@ -1094,3 +1094,4 @@ async def derive_named_parameters(db, sql):
def add_cors_headers(headers): def add_cors_headers(headers):
headers["Access-Control-Allow-Origin"] = "*" headers["Access-Control-Allow-Origin"] = "*"
headers["Access-Control-Allow-Headers"] = "Authorization" headers["Access-Control-Allow-Headers"] = "Authorization"
headers["Access-Control-Expose-Headers"] = "Link"

View file

@ -137,10 +137,18 @@ class BaseView:
], ],
}, },
} }
# Hacky cheat to add extra headers
headers = {}
if "_extra_headers" in context:
headers.update(context["_extra_headers"])
return Response.html( return Response.html(
await self.ds.render_template( await self.ds.render_template(
template, template_context, request=request, view_name=self.name template,
) template_context,
request=request,
view_name=self.name,
),
headers=headers,
) )
@classmethod @classmethod

View file

@ -17,6 +17,7 @@ from datasette.utils import (
is_url, is_url,
path_from_row_pks, path_from_row_pks,
path_with_added_args, path_with_added_args,
path_with_format,
path_with_removed_args, path_with_removed_args,
path_with_replaced_args, path_with_replaced_args,
to_css_class, to_css_class,
@ -958,6 +959,13 @@ class TableView(RowTableShared):
"metadata": metadata, "metadata": metadata,
"view_definition": await db.get_view_definition(table), "view_definition": await db.get_view_definition(table),
"table_definition": await db.get_table_definition(table), "table_definition": await db.get_table_definition(table),
"_extra_headers": {
"Link": '{}; rel="alternate"; type="application/json+datasette"'.format(
self.ds.absolute_url(
request, path_with_format(request=request, format="json")
),
)
},
} }
return ( return (