Skip to content

Commit 33a3224

Browse files
authored
ENG-1825 Primitive content negotiation to expose our schema (#1098)
1 parent 6a94b6e commit 33a3224

2 files changed

Lines changed: 44 additions & 7 deletions

File tree

apps/website/next.config.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import nextra from "nextra";
22
import type { NextConfig } from "next";
3+
import type { RouteHas } from "next/dist/lib/load-custom-routes";
34
import { config } from "@repo/database/dbDotEnv";
45
import { DOCS_REDIRECTS } from "./docsRouteMap";
56

@@ -19,6 +20,28 @@ const nextConfig: NextConfig = {
1920
async redirects() {
2021
return DOCS_REDIRECTS;
2122
},
23+
async rewrites() {
24+
function negotiateSchema(prefix: string) {
25+
return {
26+
source: `${prefix}`,
27+
has: [
28+
{
29+
type: "header",
30+
key: "accept",
31+
value: "(.*)(\\btext/turtle\\b|\\btext/\\*|\\*/\\*)(.*)",
32+
} as RouteHas,
33+
],
34+
destination: `${prefix}.ttl`,
35+
};
36+
}
37+
return {
38+
beforeFiles: [
39+
negotiateSchema("/schema/dg_base"),
40+
negotiateSchema("/schema/dg_core"),
41+
],
42+
};
43+
},
44+
2245
turbopack: {
2346
resolveAlias: {
2447
"next-mdx-import-source-file": "./mdx-components.tsx",

apps/website/proxy.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,26 @@
1-
import { type NextRequest } from "next/server";
1+
import { NextResponse, type NextRequest } from "next/server";
22
import { updateSession } from "~/utils/supabase/proxy";
33

4-
export const proxy = async (request: NextRequest) =>
5-
await updateSession(request);
4+
const NEGOTIATED_PATHS = new Set(["/schema/dg_base", "/schema/dg_core"]);
5+
const ACCEPTABLE = /(\btext\/turtle\b|\btext\/\*|\*\/\*)/;
6+
7+
export const proxy = async (request: NextRequest): Promise<NextResponse> => {
8+
const { pathname } = request.nextUrl;
9+
10+
if (NEGOTIATED_PATHS.has(pathname)) {
11+
const accept = request.headers.get("accept") ?? "";
12+
if (!ACCEPTABLE.test(accept)) {
13+
return new NextResponse("You have to Accept text/turtle", {
14+
status: 406,
15+
headers: { "Content-Type": "text/plain" },
16+
});
17+
}
18+
return NextResponse.next();
19+
}
20+
21+
return await updateSession(request);
22+
};
623

724
export const config = {
8-
matcher: [
9-
/* Only apply to /auth paths */
10-
"/(auth/.*)",
11-
],
25+
matcher: ["/schema/dg_base", "/schema/dg_core", "/(auth/.*)"],
1226
};

0 commit comments

Comments
 (0)