Skip to content

Commit

Permalink
Refactor endpoints for GETing a dataset/narrative to make room for ad…
Browse files Browse the repository at this point in the history
…ditional media types

Only sends Auspice's entrypoint for text/html-accepting clients (i.e.
all browsers) and moves existence check into the endpoint itself.  Other
media types won't need the existence check as they don't need to prevent
Auspice from hitting a 404 when it tries to load the dataset/narrative.
  • Loading branch information
tsibley committed Dec 14, 2021
1 parent 3d65697 commit 86190b7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
24 changes: 12 additions & 12 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ const {
setDataset,
setNarrative,
canonicalizeDataset,
ifDatasetExists,
ifNarrativeExists,
getDataset,
getNarrative,
} = endpoints.sources;

const esc = encodeURIComponent;
Expand Down Expand Up @@ -124,12 +124,12 @@ app.use([coreBuildRoutes, "/narratives/*"], setSource("core"));

app.routeAsync(coreBuildRoutes)
.all(setDataset(req => req.path), canonicalizeDataset(path => `/${path}`))
.getAsync(ifDatasetExists, endpoints.static.sendAuspiceEntrypoint)
.getAsync(getDataset)
;

app.routeAsync("/narratives/*")
.all(setNarrative(req => req.params[0]))
.getAsync(ifNarrativeExists, endpoints.static.sendAuspiceEntrypoint)
.getAsync(getNarrative)
;


Expand All @@ -146,12 +146,12 @@ app.routeAsync("/staging/narratives")

app.routeAsync("/staging/narratives/*")
.all(setNarrative(req => req.params[0]))
.getAsync(ifNarrativeExists, endpoints.static.sendAuspiceEntrypoint)
.getAsync(getNarrative)
;

app.routeAsync("/staging/*")
.all(setDataset(req => req.params[0]), canonicalizeDataset(path => `/staging/${path}`))
.getAsync(ifDatasetExists, endpoints.static.sendAuspiceEntrypoint)
.getAsync(getDataset)
;


Expand All @@ -174,12 +174,12 @@ app.use(["/community/narratives/:user/:repo", "/community/:user/:repo"],
*/
app.routeAsync(["/community/narratives/:user/:repo", "/community/narratives/:user/:repo/*"])
.all(setNarrative(req => req.params[0]))
.getAsync(ifNarrativeExists, endpoints.static.sendAuspiceEntrypoint)
.getAsync(getNarrative)
;

app.routeAsync(["/community/:user/:repo", "/community/:user/:repo/*"])
.all(setDataset(req => req.params[0]))
.getAsync(ifDatasetExists, endpoints.static.sendAuspiceEntrypoint)
.getAsync(getDataset)
;


Expand All @@ -192,14 +192,14 @@ app.routeAsync("/fetch/narratives/:authority/*")
.all(setNarrative(req => req.params[0]))

// Assume existence; little benefit to checking when we don't have a fallback page.
.getAsync(endpoints.static.sendAuspiceEntrypoint)
.getAsync(getNarrative)
;

app.routeAsync("/fetch/:authority/*")
.all(setDataset(req => req.params[0]))

// Assume existence; little benefit to checking when we don't have a fallback page.
.getAsync(endpoints.static.sendAuspiceEntrypoint)
.getAsync(getDataset)
;


Expand Down Expand Up @@ -229,12 +229,12 @@ app.routeAsync("/groups/:groupName/narratives")

app.routeAsync("/groups/:groupName/narratives/*")
.all(setNarrative(req => req.params[0]))
.getAsync(ifNarrativeExists, endpoints.static.sendAuspiceEntrypoint)
.getAsync(getNarrative)
;

app.routeAsync("/groups/:groupName/*")
.all(setDataset(req => req.params[0]))
.getAsync(ifDatasetExists, endpoints.static.sendAuspiceEntrypoint)
.getAsync(getDataset)
;


Expand Down
18 changes: 16 additions & 2 deletions src/endpoints/sources.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const {Forbidden, NotFound} = require("http-errors");

const {contentTypesProvided} = require("../negotiate");
const sources = require("../sources");
const utils = require("../utils");
const {sendAuspiceEntrypoint} = require("./static");


/**
Expand Down Expand Up @@ -110,10 +112,15 @@ const canonicalizeDataset = (pathBuilder) => (req, res, next) => {
*/
const ifDatasetExists = async (req, res, next) => {
if (!(await req.context.dataset.exists())) throw new NotFound();
next();
return next();
};


const getDataset = contentTypesProvided([
["text/html", ifDatasetExists, sendAuspiceEntrypoint],
]);


/* Narratives
*/

Expand All @@ -138,10 +145,15 @@ const setNarrative = (pathExtractor) => (req, res, next) => {
*/
const ifNarrativeExists = async (req, res, next) => {
if (!(await req.context.narrative.exists())) throw new NotFound();
next();
return next();
};


const getNarrative = contentTypesProvided([
["text/html", ifNarrativeExists, sendAuspiceEntrypoint],
]);


/**
* Split a dataset or narrative `path` into an array of parts.
*
Expand Down Expand Up @@ -212,7 +224,9 @@ module.exports = {
setDataset,
canonicalizeDataset,
ifDatasetExists,
getDataset,

setNarrative,
ifNarrativeExists,
getNarrative,
};

0 comments on commit 86190b7

Please sign in to comment.