-
-
Notifications
You must be signed in to change notification settings - Fork 229
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ add fetching grapher configs by UUID
- Loading branch information
Showing
3 changed files
with
103 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { Env } from "../../_common/env.js" | ||
import { fetchGrapherConfig } from "../../_common/grapherRenderer.js" | ||
import { IRequestStrict, Router, error, StatusError } from "itty-router" | ||
|
||
const router = Router<IRequestStrict, [URL, Env, string]>() | ||
router | ||
.get( | ||
"/grapher/by-uuid/:uuid.config.json", | ||
async ({ params: { uuid } }, { searchParams }, env, etag) => | ||
handleConfigRequest(uuid, searchParams, env, etag) | ||
) | ||
.all("*", () => error(404, "Route not defined")) | ||
|
||
export const onRequest: PagesFunction = async (context) => { | ||
const { request, env } = context | ||
const url = new URL(request.url) | ||
|
||
return router | ||
.fetch( | ||
request, | ||
url, | ||
{ ...env, url }, | ||
request.headers.get("if-none-match") | ||
) | ||
.catch((e) => { | ||
if (e instanceof StatusError) { | ||
return error(e.status, e.message) | ||
} | ||
|
||
return error(500, e) | ||
}) | ||
} | ||
|
||
async function handleConfigRequest( | ||
uuid: string, | ||
searchParams: URLSearchParams, | ||
env: Env, | ||
etag: string | undefined | ||
) { | ||
const shouldCache = searchParams.get("nocache") === null | ||
console.log("Preparing json response for uuid ", uuid) | ||
|
||
const grapherPageResp = await fetchGrapherConfig( | ||
{ type: "uuid", id: uuid }, | ||
env, | ||
etag | ||
) | ||
|
||
if (grapherPageResp.status === 304) { | ||
return new Response(null, { status: 304 }) | ||
} | ||
|
||
console.log("Grapher page response", grapherPageResp.grapherConfig.title) | ||
|
||
const cacheControl = shouldCache | ||
? "public, s-maxage=3600, max-age=0, must-revalidate" | ||
: "public, s-maxage=0, max-age=0, must-revalidate" | ||
|
||
return new Response(JSON.stringify(grapherPageResp.grapherConfig), { | ||
headers: { | ||
"content-type": "application/json", | ||
"Cache-Control": cacheControl, | ||
ETag: grapherPageResp.etag, | ||
}, | ||
}) | ||
} |