Skip to content

Commit f1beb85

Browse files
committed
feat(kit): add context
1 parent daedd2a commit f1beb85

5 files changed

Lines changed: 59 additions & 14 deletions

File tree

apps/docs/app/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ const waveKitServerCode = dedent`
1818
const waveKitAppCode = dedent`
1919
// app/index.ts
2020
import { wave } from "@wavekit/wave";
21-
import { WaveKitResponse } from "@wavekit/kit";
21+
import type { WaveKitHandler } from "@wavekit/kit";
2222
23-
export function GET(req: BunRequest) {
24-
return WaveKitResponse.html(
23+
export const GET: WaveKitHandler = (c) => {
24+
return c.html(
2525
wave.article({ class: "container mx-auto" }, (article) => {
2626
article
2727
.h1("WaveKit is awesome.")
@@ -33,7 +33,7 @@ const waveKitAppCode = dedent`
3333
`;
3434

3535
const waveKitWaveCode = dedent`
36-
// src/components/form.ts
36+
// src/components/form.tsx
3737
import { wave } from "@wavekit/wave";
3838
3939
export const SignUpForm = wave.form({
@@ -104,7 +104,7 @@ export async function GET(req: BunRequest) {
104104
.code("bun add @wavekit/wave @wavekit/kit")
105105
.p({ style: "margin-top: 1rem;" }, "Nightly release:")
106106
.code(
107-
"bun add https://pkg.pr.new/getgrinta/wavekit/wave https://pkg.pr.new/getgrinta/wavekit/kit",
107+
"bun add https://pkg.pr.new/getgrinta/wavekit/wave@main https://pkg.pr.new/getgrinta/wavekit/kit@main",
108108
)
109109
.h3("Usage")
110110
.div(

packages/kit/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@wavekit/kit",
33
"type": "module",
4-
"version": "0.0.2",
4+
"version": "0.0.3",
55
"module": "dist/index.js",
66
"types": "dist/index.d.ts",
77
"exports": {

packages/kit/src/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,8 @@
33
*/
44
export { createWaveKit, ssgRender } from "./server";
55
export { WaveKitResponse } from "./response";
6-
export type { SsgRenderProps, CreateWaveKitProps } from "./server";
6+
export type {
7+
SsgRenderProps,
8+
CreateWaveKitProps,
9+
WaveKitHandler,
10+
} from "./server";

packages/kit/src/server.ts

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import path from "node:path";
2-
import type { RouterTypes } from "bun";
2+
import type { BunRequest, RouterTypes, Server } from "bun";
3+
import { WaveKitResponse } from "./response";
34

45
const isDev = process.env.NODE_ENV === "development";
56

@@ -9,6 +10,17 @@ const defaultRoutesDir = isDev
910

1011
const defaultOutDir = path.join(process.cwd(), "build");
1112

13+
type WaveKitContext = {
14+
req: BunRequest;
15+
res: WaveKitResponse;
16+
html: typeof WaveKitResponse.html;
17+
json: typeof WaveKitResponse.json;
18+
};
19+
20+
export type WaveKitHandler = (
21+
c: WaveKitContext,
22+
) => Response | Promise<Response>;
23+
1224
type BuildRoutesProps = {
1325
routes: Record<string, string>;
1426
};
@@ -18,14 +30,43 @@ export async function buildRoutes({
1830
}: BuildRoutesProps): Promise<
1931
Record<string, RouterTypes.RouteHandlerObject<string>>
2032
> {
33+
const contextStore = new Map();
2134
const rawRoutes = Object.entries(routes);
2235
const routesWithHandlers = rawRoutes.map(async ([path, handlerPath]) => {
2336
const handler = (await import(
2437
handlerPath
2538
)) as RouterTypes.RouteHandlerObject<string>;
26-
return [path, handler];
39+
const contextHandler: RouterTypes.RouteHandlerObject<string> = {};
40+
for (const method of Object.keys(handler)) {
41+
const methodHandler = handler[
42+
method as RouterTypes.HTTPMethod
43+
] as unknown as WaveKitHandler;
44+
if (!methodHandler) return;
45+
contextHandler[method as RouterTypes.HTTPMethod] = (
46+
req: BunRequest,
47+
server: Server,
48+
) => {
49+
const res = new WaveKitResponse();
50+
const context = {
51+
req,
52+
res,
53+
html: WaveKitResponse.html,
54+
json: WaveKitResponse.json,
55+
redirect: WaveKitResponse.redirect,
56+
set: contextStore.set,
57+
get: contextStore.get,
58+
};
59+
return methodHandler(context);
60+
};
61+
}
62+
return [path, contextHandler];
2763
});
28-
return Object.fromEntries(await Promise.all(routesWithHandlers));
64+
return Object.fromEntries(
65+
(await Promise.all(routesWithHandlers)).filter(Boolean) as [
66+
string,
67+
RouterTypes.RouteHandlerObject<string>,
68+
][],
69+
);
2970
}
3071

3172
export type CreateWaveKitProps = {
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { WaveKitResponse } from "@wavekit/kit";
1+
import type { WaveKitHandler } from "@wavekit/kit";
22
import { wave } from "@wavekit/wave";
33

4-
export function GET() {
5-
return WaveKitResponse.html(wave.p("Hello World!"));
6-
}
4+
export const GET: WaveKitHandler = (c) => {
5+
return c.html(wave.p("Hello World!"));
6+
};

0 commit comments

Comments
 (0)