-
Notifications
You must be signed in to change notification settings - Fork 35
/
mod.ts
37 lines (30 loc) · 942 Bytes
/
mod.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import type { Handler, HandlerMapping, Pipeline, Routes } from "wren/types.ts";
import type { Manifest, PageProps } from "./types.ts";
import * as Response from "wren/response.ts";
import render from "preact-render-to-string";
import * as Middleware from "./middleware/page.ts";
export const setup = (manifest: Manifest) => {
const routes: Routes = [];
for (const [pathname, { default: page, handler }] of Object.entries(manifest.routes)) {
if (handler && page) {
routes.push([
pathname,
[Middleware.Page(page), handler] as Pipeline,
]);
} else if (handler) {
routes.push([pathname, handler as Handler | HandlerMapping | Pipeline]);
} else if (page) {
routes.push([pathname, {
GET: (request) => {
const { params } = request;
// FIXME
return Response.HTML(render(page({ params } as PageProps)!));
},
}]);
} else {
// FIXME
}
}
return routes;
};
export * from "./types.ts";