Skip to content

Commit 06e3157

Browse files
committed
updated h3 example
1 parent 391c60e commit 06e3157

File tree

3 files changed

+18
-15
lines changed

3 files changed

+18
-15
lines changed

packages/examples/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"scripts": {
66
"test": "jest",
77
"start:express": "ts-node src/runtimes/express",
8-
"start:fastify": "ts-node src/runtimes/fastify"
8+
"start:fastify": "ts-node src/runtimes/fastify",
9+
"start:h3": "ts-node src/runtimes/h3"
910
},
1011
"dependencies": {
1112
"@backhooks/core": "^0.2.0",

packages/examples/src/handlers.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ export const mainHandler = async () => {
55
const logger = useLogger();
66
logger.debug("Executing main handler.");
77
const headers = useHeaders();
8+
logger.debug(headers);
89
return headers;
910
};

packages/examples/src/runtimes/h3.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import {
44
toNodeListener,
55
getHeaders,
66
readBody,
7+
H3Event,
8+
App,
79
} from "h3";
810
import { listen } from "listhen";
911
import {
@@ -16,33 +18,32 @@ import { mainHandler } from "../handlers";
1618

1719
const app = createApp();
1820

19-
const hookableEventHandler: typeof eventHandler = (handler) => {
20-
return eventHandler(async (e) => {
21-
return runHookContext(async () => {
21+
const makeHookableApp = (h3App: App) => {
22+
const originalHandler = h3App.handler;
23+
h3App.handler = async (event: H3Event) => {
24+
return runHookContext(() => {
25+
const headers = getHeaders(event);
2226
configureHeadersHook((state) => {
2327
return {
2428
...state,
25-
fetch() {
26-
const headers = getHeaders(e);
27-
return headers as Record<string, string>;
28-
},
29+
headers,
2930
};
3031
});
3132
configureBodyHook((state) => {
3233
return {
3334
...state,
3435
fetch() {
35-
const body = readBody(e);
36-
return body;
36+
return readBody(event);
3737
},
3838
};
3939
});
40-
const response = await handler(e);
41-
return response;
42-
}) as ReturnType<typeof handler>;
43-
});
40+
return originalHandler(event);
41+
});
42+
};
4443
};
4544

46-
app.use("/", hookableEventHandler(mainHandler));
45+
makeHookableApp(app);
46+
47+
app.use("/", eventHandler(mainHandler));
4748

4849
listen(toNodeListener(app));

0 commit comments

Comments
 (0)