feat(server): allow single method API routes (#24)
This commit is contained in:
+15
-3
@@ -44,7 +44,11 @@ export class ServerContext {
|
|||||||
route,
|
route,
|
||||||
url,
|
url,
|
||||||
name,
|
name,
|
||||||
handler: module.default as router.MatchHandler,
|
handlers: Object.fromEntries(
|
||||||
|
Object.entries(module).filter(([method]) =>
|
||||||
|
router.METHODS.includes(method) || method === "default"
|
||||||
|
),
|
||||||
|
),
|
||||||
};
|
};
|
||||||
apiRoutes.push(apiRoute);
|
apiRoutes.push(apiRoute);
|
||||||
} else if (!path.startsWith("/_")) {
|
} else if (!path.startsWith("/_")) {
|
||||||
@@ -92,8 +96,16 @@ export class ServerContext {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const apiRoute of this.#apiRoutes) {
|
for (const { route, handlers } of this.#apiRoutes) {
|
||||||
routes[apiRoute.route] = apiRoute.handler;
|
for (const [method, handler] of Object.entries(handlers)) {
|
||||||
|
if (handler) {
|
||||||
|
if (method === "default") {
|
||||||
|
routes[route] = handler;
|
||||||
|
} else {
|
||||||
|
routes[`${method}@${route}`] = handler;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
routes[`${INTERNAL_PREFIX}${JS_PREFIX}/${BUILD_ID}/:path*`] = this
|
routes[`${INTERNAL_PREFIX}${JS_PREFIX}/${BUILD_ID}/:path*`] = this
|
||||||
|
|||||||
+1
-1
@@ -6,7 +6,7 @@ export { extname } from "https://deno.land/std@0.95.0/path/mod.ts";
|
|||||||
export { generate as generateUuid } from "https://deno.land/std@0.95.0/uuid/v4.ts";
|
export { generate as generateUuid } from "https://deno.land/std@0.95.0/uuid/v4.ts";
|
||||||
|
|
||||||
// -- router --
|
// -- router --
|
||||||
export * as router from "https://crux.land/router@0.0.2";
|
export * as router from "https://crux.land/router@0.0.4";
|
||||||
|
|
||||||
// -- media types --
|
// -- media types --
|
||||||
export { lookup as mediaTypeLookup } from "https://deno.land/x/media_types@v2.8.4/mod.ts";
|
export { lookup as mediaTypeLookup } from "https://deno.land/x/media_types@v2.8.4/mod.ts";
|
||||||
|
|||||||
+4
-4
@@ -13,13 +13,13 @@ export interface Page {
|
|||||||
component: ComponentType<PageProps>;
|
component: ComponentType<PageProps>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ApiRouteModule {
|
export type ApiRouteModule = {
|
||||||
default: router.MatchHandler;
|
[K in "default" | typeof router.METHODS[number]]?: router.MatchHandler;
|
||||||
}
|
};
|
||||||
|
|
||||||
export interface ApiRoute {
|
export interface ApiRoute {
|
||||||
route: string;
|
route: string;
|
||||||
url: string;
|
url: string;
|
||||||
name: string;
|
name: string;
|
||||||
handler: router.MatchHandler;
|
handlers: Record<string, router.MatchHandler>;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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");
|
||||||
|
}
|
||||||
@@ -5,3 +5,11 @@ export default () => {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export function GET() {
|
||||||
|
return new Response("Get fresh!", {
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json; charset=utf-8",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,15 +2,17 @@
|
|||||||
// This file SHOULD be checked into source version control.
|
// This file SHOULD be checked into source version control.
|
||||||
// To update this file, run `fresh routes`.
|
// To update this file, run `fresh routes`.
|
||||||
|
|
||||||
import * as $0 from "./pages/api/name.ts";
|
import * as $0 from "./pages/index.tsx";
|
||||||
import * as $1 from "./pages/[name].tsx";
|
import * as $1 from "./pages/api/get_only.ts";
|
||||||
import * as $2 from "./pages/index.tsx";
|
import * as $2 from "./pages/api/name.ts";
|
||||||
|
import * as $3 from "./pages/[name].tsx";
|
||||||
|
|
||||||
const routes = {
|
const routes = {
|
||||||
pages: {
|
pages: {
|
||||||
"./pages/api/name.ts": $0,
|
"./pages/index.tsx": $0,
|
||||||
"./pages/[name].tsx": $1,
|
"./pages/api/get_only.ts": $1,
|
||||||
"./pages/index.tsx": $2,
|
"./pages/api/name.ts": $2,
|
||||||
|
"./pages/[name].tsx": $3,
|
||||||
},
|
},
|
||||||
baseUrl: import.meta.url,
|
baseUrl: import.meta.url,
|
||||||
};
|
};
|
||||||
|
|||||||
+27
-1
@@ -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"));
|
const resp = await router(new Request("https://fresh.deno.dev/api/name"));
|
||||||
assert(resp);
|
assert(resp);
|
||||||
assertEquals(resp.status, 200);
|
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(
|
assertEquals(
|
||||||
resp.headers.get("content-type"),
|
resp.headers.get("content-type"),
|
||||||
"application/json; charset=utf-8",
|
"application/json; charset=utf-8",
|
||||||
@@ -44,6 +56,20 @@ Deno.test("/api/name", async () => {
|
|||||||
assertEquals(body, { name: "fresh" });
|
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 () => {
|
Deno.test("/api/xyz not found", async () => {
|
||||||
const resp = await router(new Request("https://fresh.deno.dev/api/xyz"));
|
const resp = await router(new Request("https://fresh.deno.dev/api/xyz"));
|
||||||
assert(resp);
|
assert(resp);
|
||||||
|
|||||||
Reference in New Issue
Block a user