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:
@@ -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");
|
||||
},
|
||||
};
|
||||
|
||||
@@ -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",
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -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");
|
||||
},
|
||||
};
|
||||
@@ -6,7 +6,7 @@ import * as $0 from "./pages/index.tsx";
|
||||
import * as $1 from "./pages/static.tsx";
|
||||
import * as $2 from "./pages/props/[id].tsx";
|
||||
import * as $3 from "./pages/api/get_only.ts";
|
||||
import * as $4 from "./pages/api/name.ts";
|
||||
import * as $4 from "./pages/intercept.tsx";
|
||||
import * as $5 from "./pages/[name].tsx";
|
||||
|
||||
const routes = {
|
||||
@@ -15,7 +15,7 @@ const routes = {
|
||||
"./pages/static.tsx": $1,
|
||||
"./pages/props/[id].tsx": $2,
|
||||
"./pages/api/get_only.ts": $3,
|
||||
"./pages/api/name.ts": $4,
|
||||
"./pages/intercept.tsx": $4,
|
||||
"./pages/[name].tsx": $5,
|
||||
},
|
||||
baseUrl: import.meta.url,
|
||||
|
||||
+33
-19
@@ -54,28 +54,46 @@ Deno.test("/[name] page prerender", async () => {
|
||||
);
|
||||
});
|
||||
|
||||
Deno.test("/api/name - GET", async () => {
|
||||
const resp = await router(new Request("https://fresh.deno.dev/api/name"));
|
||||
Deno.test("/intercept - GET html", async () => {
|
||||
const req = new Request("https://fresh.deno.dev/intercept", {
|
||||
headers: { "accept": "text/html" },
|
||||
});
|
||||
const resp = await router(req);
|
||||
assert(resp);
|
||||
assertEquals(resp.status, 200);
|
||||
const body = await resp.text();
|
||||
assertEquals(body, "Get fresh!");
|
||||
assert(body.includes("<div>This is HTML</div>"));
|
||||
});
|
||||
|
||||
Deno.test("/api/name - default", async () => {
|
||||
const resp = await router(
|
||||
new Request("https://fresh.deno.dev/api/name", {
|
||||
method: "POST",
|
||||
}),
|
||||
);
|
||||
Deno.test("/intercept - GET text", async () => {
|
||||
const req = new Request("https://fresh.deno.dev/intercept", {
|
||||
headers: { "accept": "text/plain" },
|
||||
});
|
||||
const resp = await router(req);
|
||||
assert(resp);
|
||||
assertEquals(resp.status, 200);
|
||||
assertEquals(
|
||||
resp.headers.get("content-type"),
|
||||
"application/json; charset=utf-8",
|
||||
);
|
||||
const body = await resp.json();
|
||||
assertEquals(body, { name: "fresh" });
|
||||
const body = await resp.text();
|
||||
assertEquals(body, "This is plain text");
|
||||
});
|
||||
|
||||
Deno.test("/intercept - POST", async () => {
|
||||
const req = new Request("https://fresh.deno.dev/intercept", {
|
||||
method: "POST",
|
||||
});
|
||||
const resp = await router(req);
|
||||
assert(resp);
|
||||
assertEquals(resp.status, 200);
|
||||
const body = await resp.text();
|
||||
assertEquals(body, "POST response");
|
||||
});
|
||||
|
||||
Deno.test("/intercept - DELETE", async () => {
|
||||
const req = new Request("https://fresh.deno.dev/intercept", {
|
||||
method: "DELETE",
|
||||
});
|
||||
const resp = await router(req);
|
||||
assert(resp);
|
||||
assertEquals(resp.status, 405);
|
||||
});
|
||||
|
||||
Deno.test("/api/get_only - NOTAMETHOD", async () => {
|
||||
@@ -86,10 +104,6 @@ Deno.test("/api/get_only - NOTAMETHOD", async () => {
|
||||
);
|
||||
assert(resp);
|
||||
assertEquals(resp.status, 405);
|
||||
assertEquals(
|
||||
resp.headers.get("accept"),
|
||||
"GET",
|
||||
);
|
||||
});
|
||||
|
||||
Deno.test("/api/xyz not found", async () => {
|
||||
|
||||
Reference in New Issue
Block a user