BREAKING: merge API routes and pages concept (#53)

This commit merges API routes and pages into a single concept. The
pages/api folder is now not special anymore.
This commit is contained in:
Luca Casonato
2021-09-25 10:49:53 +02:00
committed by GitHub
parent aeaddb1eaa
commit f5b3faf0d8
12 changed files with 150 additions and 121 deletions
+12 -11
View File
@@ -1,11 +1,12 @@
export function GET() {
return new Response("Get fresh!", {
headers: {
"Content-Type": "application/json; charset=utf-8",
},
});
}
export function NOTAMETHOD() {
return new Response("unreachable");
}
export const handler = {
GET() {
return new Response("Get fresh!", {
headers: {
"Content-Type": "application/json; charset=utf-8",
},
});
},
NOTAMETHOD() {
throw new Error("unreachable");
},
};
-15
View File
@@ -1,15 +0,0 @@
export default () => {
return new Response(JSON.stringify({ name: "fresh" }), {
headers: {
"Content-Type": "application/json; charset=utf-8",
},
});
};
export function GET() {
return new Response("Get fresh!", {
headers: {
"Content-Type": "application/json; charset=utf-8",
},
});
}
+21
View File
@@ -0,0 +1,21 @@
/** @jsx h */
import { h } from "../deps.ts";
import { HandlerContext } from "../../../server.ts";
export default function Page() {
return <div>This is HTML</div>;
}
export const handler = {
GET({ req, render }: HandlerContext) {
if (req.headers.get("accept")?.includes("text/html")) {
return render!();
} else {
return new Response("This is plain text");
}
},
POST() {
return new Response("POST response");
},
};