Skip to content

Commit f66d513

Browse files
committed
feat: added useQuery hook, updated examples
1 parent 06e3157 commit f66d513

File tree

8 files changed

+75
-29
lines changed

8 files changed

+75
-29
lines changed

.changeset/happy-hairs-return.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@backhooks/examples": patch
3+
"@backhooks/http": patch
4+
---
5+
6+
- Updated types for headers and body hooks
7+
- Added useQuery hook

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "backhooks",
33
"scripts": {
44
"build": "npm run build --workspaces --if-present",
5-
"test": "npm run test --workspaces --if-present",
5+
"test": "npm run build && npm run test --workspaces --if-present",
66
"publish": "npm run build && changeset publish",
77
"make:toc": "markdown-toc -i README.md",
88
"format:code": "prettier -w .",

packages/examples/src/runtimes/h3.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,7 @@ import {
88
App,
99
} from "h3";
1010
import { listen } from "listhen";
11-
import {
12-
configureBodyHook,
13-
configureHeadersHook,
14-
useHeaders,
15-
} from "@backhooks/http";
11+
import { configureBodyHook, configureHeadersHook } from "@backhooks/http";
1612
import { runHookContext } from "@backhooks/core";
1713
import { mainHandler } from "../handlers";
1814

@@ -23,15 +19,13 @@ const makeHookableApp = (h3App: App) => {
2319
h3App.handler = async (event: H3Event) => {
2420
return runHookContext(() => {
2521
const headers = getHeaders(event);
26-
configureHeadersHook((state) => {
22+
configureHeadersHook(() => {
2723
return {
28-
...state,
2924
headers,
3025
};
3126
});
32-
configureBodyHook((state) => {
27+
configureBodyHook(() => {
3328
return {
34-
...state,
3529
fetch() {
3630
return readBody(event);
3731
},

packages/http/src/hooks/body.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
import { createHook } from "@backhooks/core";
22

3+
interface BodyHookState {
4+
body?: any;
5+
fetch?: () => any;
6+
}
7+
38
const [useBody, configureBodyHook] = createHook({
4-
data() {
9+
data(): BodyHookState {
510
return {
6-
body: undefined as any,
11+
body: undefined,
712
fetch() {
8-
return undefined as any;
13+
return undefined;
914
},
1015
};
1116
},

packages/http/src/hooks/headers.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
import { createHook } from "@backhooks/core";
22

3-
const useHeadersKey = "@backhooks/http/useHeaders";
4-
5-
export interface HeadersHookOptions {
3+
export interface HeadersHookState {
64
headers?: Record<string, string>;
75
fetch?: () => Record<string, string>;
86
}
97

108
const [useHeaders, configureHeadersHook] = createHook({
11-
data() {
9+
data(): HeadersHookState {
1210
return {
13-
headers: undefined as Record<string, string>,
11+
headers: undefined as undefined | Record<string, string>,
1412
fetch: () => {
1513
return {};
1614
},

packages/http/src/hooks/query.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { createHook } from "@backhooks/core";
2+
3+
export interface QueryHookState {
4+
query?: Record<string, string>;
5+
fetch?: () => Record<string, string>;
6+
}
7+
8+
const [useQuery, setQuery] = createHook({
9+
data(): QueryHookState {
10+
return {
11+
query: undefined as undefined | Record<string, string>,
12+
fetch: () => {
13+
return {};
14+
},
15+
};
16+
},
17+
execute(state) {
18+
if (state.query) {
19+
return state.query;
20+
}
21+
if (state.fetch) {
22+
state.query = state.fetch();
23+
}
24+
return (state.query || {}) as Record<string, string>;
25+
},
26+
});
27+
28+
export { useQuery, setQuery };

packages/http/src/middlewares/server.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,30 @@
11
import { runHookContext } from "@backhooks/core";
22
import { configureHeadersHook } from "../hooks/headers";
33
import { configureBodyHook } from "../hooks/body";
4+
import { setQuery } from "../hooks/query";
45

56
export const hooksMiddleware = () => {
67
return (req, res, next) => {
78
runHookContext(async () => {
8-
configureHeadersHook((currentState) => {
9+
configureHeadersHook(() => {
910
return {
10-
...currentState,
1111
fetch() {
1212
return req.headers;
1313
},
1414
};
15-
}),
16-
configureBodyHook((currentState) => {
17-
return {
18-
...currentState,
19-
fetch() {
20-
return req.body;
21-
},
22-
};
23-
});
15+
});
16+
configureBodyHook(() => {
17+
return {
18+
fetch() {
19+
return req.body;
20+
},
21+
};
22+
});
23+
setQuery(() => {
24+
return {
25+
query: req.query,
26+
};
27+
});
2428
next();
2529
}).catch((error) => {
2630
next(error);

packages/http/tests/server-middleware.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { json } from "body-parser";
55
import { useBody } from "../src/hooks/body";
66
import { useHeaders } from "../src/hooks/headers";
77
import { hooksMiddleware } from "../src/middlewares/server";
8+
import { useQuery } from "../src/hooks/query";
89

910
test("it should work with express", async () => {
1011
const app = express();
@@ -18,6 +19,12 @@ test("it should work with express", async () => {
1819
body,
1920
});
2021
});
22+
app.get("/bar", (req, res) => {
23+
const query = useQuery();
24+
res.send({
25+
query,
26+
});
27+
});
2128
const res = await request(app)
2229
.post("/")
2330
.send({
@@ -28,6 +35,9 @@ test("it should work with express", async () => {
2835
});
2936
expect(res.body.headers.foo).toBe("bar");
3037
expect(res.body.body.hello).toBe("world");
38+
39+
const res2 = await request(app).get("/bar?some=thing");
40+
expect(res2.body.query.some).toBe("thing");
3141
});
3242

3343
test("it should work with fastify", async () => {

0 commit comments

Comments
 (0)