feat(server): allow single method API routes (#24)

This commit is contained in:
Leo K
2021-06-10 23:26:16 +02:00
committed by GitHub
parent 572b8123c4
commit ae1e311eb8
7 changed files with 74 additions and 15 deletions
+11
View File
@@ -0,0 +1,11 @@
export function GET() {
return new Response("Get fresh!", {
headers: {
"Content-Type": "application/json; charset=utf-8",
},
});
}
export function NOTAMETHOD() {
return new Response("unreachable");
}
+8
View File
@@ -5,3 +5,11 @@ export default () => {
},
});
};
export function GET() {
return new Response("Get fresh!", {
headers: {
"Content-Type": "application/json; charset=utf-8",
},
});
}
+8 -6
View File
@@ -2,15 +2,17 @@
// This file SHOULD be checked into source version control.
// To update this file, run `fresh routes`.
import * as $0 from "./pages/api/name.ts";
import * as $1 from "./pages/[name].tsx";
import * as $2 from "./pages/index.tsx";
import * as $0 from "./pages/index.tsx";
import * as $1 from "./pages/api/get_only.ts";
import * as $2 from "./pages/api/name.ts";
import * as $3 from "./pages/[name].tsx";
const routes = {
pages: {
"./pages/api/name.ts": $0,
"./pages/[name].tsx": $1,
"./pages/index.tsx": $2,
"./pages/index.tsx": $0,
"./pages/api/get_only.ts": $1,
"./pages/api/name.ts": $2,
"./pages/[name].tsx": $3,
},
baseUrl: import.meta.url,
};
+27 -1
View File
@@ -32,10 +32,22 @@ Deno.test("/[name] page prerender", async () => {
);
});
Deno.test("/api/name", async () => {
Deno.test("/api/name - GET", async () => {
const resp = await router(new Request("https://fresh.deno.dev/api/name"));
assert(resp);
assertEquals(resp.status, 200);
const body = await resp.text();
assertEquals(body, "Get fresh!");
});
Deno.test("/api/name - default", async () => {
const resp = await router(
new Request("https://fresh.deno.dev/api/name", {
method: "POST",
}),
);
assert(resp);
assertEquals(resp.status, 200);
assertEquals(
resp.headers.get("content-type"),
"application/json; charset=utf-8",
@@ -44,6 +56,20 @@ Deno.test("/api/name", async () => {
assertEquals(body, { name: "fresh" });
});
Deno.test("/api/get_only - NOTAMETHOD", async () => {
const resp = await router(
new Request("https://fresh.deno.dev/api/get_only", {
method: "NOTAMETHOD",
}),
);
assert(resp);
assertEquals(resp.status, 405);
assertEquals(
resp.headers.get("accept"),
"GET",
);
});
Deno.test("/api/xyz not found", async () => {
const resp = await router(new Request("https://fresh.deno.dev/api/xyz"));
assert(resp);