Skip to content

Commit 078ca3f

Browse files
committedFeb 3, 2023
feat: added useParams hook
1 parent 7bc9219 commit 078ca3f

File tree

5 files changed

+51
-2
lines changed

5 files changed

+51
-2
lines changed
 

Diff for: ‎.changeset/weak-ghosts-exist.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@backhooks/http": patch
3+
---
4+
5+
Added useParams hook

Diff for: ‎packages/http/src/hooks/headers.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export interface HeadersHookState {
55
fetch?: () => Record<string, string>;
66
}
77

8-
const [useHeaders, configureHeadersHook] = createHook({
8+
export const [useHeaders, configureHeadersHook] = createHook({
99
data(): HeadersHookState {
1010
return {
1111
headers: undefined as undefined | Record<string, string>,
@@ -25,4 +25,4 @@ const [useHeaders, configureHeadersHook] = createHook({
2525
},
2626
});
2727

28-
export { useHeaders, configureHeadersHook };
28+
export const setHeaders = configureHeadersHook;

Diff for: ‎packages/http/src/hooks/params.ts

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

Diff for: ‎packages/http/src/middlewares/server.ts

+8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { runHookContext } from "@backhooks/core";
22
import { configureHeadersHook } from "../hooks/headers";
33
import { configureBodyHook } from "../hooks/body";
44
import { setQuery } from "../hooks/query";
5+
import { setParams } from "../hooks/params";
56

67
export const hooksMiddleware = () => {
78
return (req, res, next) => {
@@ -25,6 +26,13 @@ export const hooksMiddleware = () => {
2526
query: req.query,
2627
};
2728
});
29+
setParams(() => {
30+
return {
31+
fetch() {
32+
return req.params;
33+
},
34+
};
35+
});
2836
next();
2937
}).catch((error) => {
3038
next(error);

Diff for: ‎packages/http/tests/server-middleware.spec.ts

+10
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { useBody } from "../src/hooks/body";
66
import { useHeaders } from "../src/hooks/headers";
77
import { hooksMiddleware } from "../src/middlewares/server";
88
import { useQuery } from "../src/hooks/query";
9+
import { useParams } from "../src/hooks/params";
910

1011
test("it should work with express", async () => {
1112
const app = express();
@@ -25,6 +26,12 @@ test("it should work with express", async () => {
2526
query,
2627
});
2728
});
29+
app.get("/test-param/:foo", (req, res) => {
30+
const params = useParams();
31+
res.send({
32+
params,
33+
});
34+
});
2835
const res = await request(app)
2936
.post("/")
3037
.send({
@@ -38,6 +45,9 @@ test("it should work with express", async () => {
3845

3946
const res2 = await request(app).get("/bar?some=thing");
4047
expect(res2.body.query.some).toBe("thing");
48+
49+
const res3 = await request(app).get("/test-param/bar");
50+
expect(res3.body.params.foo).toBe("bar");
4151
});
4252

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

0 commit comments

Comments
 (0)
Please sign in to comment.