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
+15 -3
View File
@@ -44,7 +44,11 @@ export class ServerContext {
route,
url,
name,
handler: module.default as router.MatchHandler,
handlers: Object.fromEntries(
Object.entries(module).filter(([method]) =>
router.METHODS.includes(method) || method === "default"
),
),
};
apiRoutes.push(apiRoute);
} else if (!path.startsWith("/_")) {
@@ -92,8 +96,16 @@ export class ServerContext {
};
}
for (const apiRoute of this.#apiRoutes) {
routes[apiRoute.route] = apiRoute.handler;
for (const { route, handlers } of this.#apiRoutes) {
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
+1 -1
View File
@@ -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";
// -- 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 --
export { lookup as mediaTypeLookup } from "https://deno.land/x/media_types@v2.8.4/mod.ts";
+4 -4
View File
@@ -13,13 +13,13 @@ export interface Page {
component: ComponentType<PageProps>;
}
export interface ApiRouteModule {
default: router.MatchHandler;
}
export type ApiRouteModule = {
[K in "default" | typeof router.METHODS[number]]?: router.MatchHandler;
};
export interface ApiRoute {
route: string;
url: string;
name: string;
handler: router.MatchHandler;
handlers: Record<string, router.MatchHandler>;
}