feat: add api routes (#11)

This commit adds API routes. These routes are located in the
./pages/api/ directory. Each file in there will have it's default
export invoked for HTTP calls to the corresponding route.
This commit is contained in:
Luca Casonato
2021-06-02 00:15:00 +02:00
committed by GitHub
parent 31dbd99bea
commit ff69a0abdf
10 changed files with 136 additions and 37 deletions
+10 -2
View File
@@ -1,11 +1,15 @@
import { extname, oak } from "./deps.ts";
import { Bundler } from "./bundle.ts";
import { render } from "./render.ts";
import { Page } from "./page.ts";
import { ApiRoute, Page } from "./routes.ts";
import { BUILD_ID, INTERNAL_PREFIX, JS_PREFIX } from "./constants.ts";
/** This function installs all routes required by fresh onto an oak router. */
export function installRoutes(router: oak.Router, pages: Page[]) {
export function installRoutes(
router: oak.Router,
pages: Page[],
apiRoutes: ApiRoute[],
) {
for (const page of pages) {
router.get<Record<string, string>>(page.route, (ctx) => {
ctx.response.status = 200;
@@ -14,6 +18,10 @@ export function installRoutes(router: oak.Router, pages: Page[]) {
});
}
for (const apiRoute of apiRoutes) {
router.all(apiRoute.route, apiRoute.handler);
}
const internal = internalRouter(pages);
router.use(