Skip to content

Commit

Permalink
Prototyp of link rel=alternate header for tables
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Nov 27, 2021
1 parent 48f1199 commit e0a8469
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
1 change: 1 addition & 0 deletions datasette/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1094,3 +1094,4 @@ async def derive_named_parameters(db, sql):
def add_cors_headers(headers):
headers["Access-Control-Allow-Origin"] = "*"
headers["Access-Control-Allow-Headers"] = "Authorization"
headers["Access-Control-Expose-Headers"] = "Link"
12 changes: 10 additions & 2 deletions datasette/views/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,18 @@ async def render(self, templates, request, context=None):
],
},
}
# Hacky cheat to add extra headers
headers = {}
if "_extra_headers" in context:
headers.update(context["_extra_headers"])
return Response.html(
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
Expand Down
8 changes: 8 additions & 0 deletions datasette/views/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
is_url,
path_from_row_pks,
path_with_added_args,
path_with_format,
path_with_removed_args,
path_with_replaced_args,
to_css_class,
Expand Down Expand Up @@ -958,6 +959,13 @@ async def table_actions():
"metadata": metadata,
"view_definition": await db.get_view_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 (
Expand Down

0 comments on commit e0a8469

Please sign in to comment.