Skip to content

Commit f1f2781

Browse files
committed
Fixed 33 problem and solution
1 parent a7a301b commit f1f2781

File tree

2 files changed

+34
-34
lines changed

2 files changed

+34
-34
lines changed

src/07-challenges/33-zod-with-express.problem.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ import { Equal, Expect } from "../helpers/type-utils";
55

66
const makeTypeSafeHandler = (
77
config: {
8-
params?: unknown;
9-
body?: unknown;
8+
query?: z.Schema;
9+
body?: z.Schema;
1010
},
11-
handler: unknown,
12-
): unknown => {
11+
handler: RequestHandler
12+
): RequestHandler => {
1313
return (req, res, next) => {
14-
const { params, body } = req;
15-
if (config.params) {
14+
const { query, body } = req;
15+
if (config.query) {
1616
try {
17-
config.params.parse(params);
17+
config.query.parse(query);
1818
} catch (e) {
1919
return res.status(400).send((e as ZodError).message);
2020
}
@@ -32,12 +32,12 @@ const makeTypeSafeHandler = (
3232

3333
const app = express();
3434

35-
it("Should make the params AND body type safe", () => {
35+
it("Should make the query AND body type safe", () => {
3636
app.get(
3737
"/users",
3838
makeTypeSafeHandler(
3939
{
40-
params: z.object({
40+
query: z.object({
4141
id: z.string(),
4242
}),
4343
body: z.object({
@@ -46,11 +46,11 @@ it("Should make the params AND body type safe", () => {
4646
},
4747
(req, res) => {
4848
type tests = [
49-
Expect<Equal<typeof req.params, { id: string }>>,
50-
Expect<Equal<typeof req.body, { name: string }>>,
49+
Expect<Equal<typeof req.query, { id: string }>>,
50+
Expect<Equal<typeof req.body, { name: string }>>
5151
];
52-
},
53-
),
52+
}
53+
)
5454
);
5555
});
5656

@@ -59,9 +59,9 @@ it("Should default them to any if not passed in config", () => {
5959
"/users",
6060
makeTypeSafeHandler({}, (req, res) => {
6161
type tests = [
62-
Expect<Equal<typeof req.params, any>>,
63-
Expect<Equal<typeof req.body, any>>,
62+
Expect<Equal<typeof req.query, any>>,
63+
Expect<Equal<typeof req.body, any>>
6464
];
65-
}),
65+
})
6666
);
6767
});

src/07-challenges/33-zod-with-express.solution.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,20 @@ import { z, ZodError } from "zod";
44
import { Equal, Expect } from "../helpers/type-utils";
55

66
const makeTypeSafeHandler = <
7-
TParams extends z.ZodType,
8-
TBody extends z.ZodType,
7+
TQuery extends Record<string, string> = any,
8+
TBody extends Record<string, string> = any
99
>(
1010
config: {
11-
params?: TParams;
12-
body?: TBody;
11+
query?: z.Schema<TQuery>;
12+
body?: z.Schema<TBody>;
1313
},
14-
handler: RequestHandler<z.infer<TParams>, any, z.infer<TBody>>,
15-
): RequestHandler<z.infer<TParams>, any, z.infer<TBody>> => {
14+
handler: RequestHandler<any, any, TBody, TQuery>
15+
): RequestHandler<any, any, TBody, TQuery> => {
1616
return (req, res, next) => {
17-
const { params, body } = req;
18-
if (config.params) {
17+
const { query, body } = req;
18+
if (config.query) {
1919
try {
20-
config.params.parse(params);
20+
config.query.parse(query);
2121
} catch (e) {
2222
return res.status(400).send((e as ZodError).message);
2323
}
@@ -35,12 +35,12 @@ const makeTypeSafeHandler = <
3535

3636
const app = express();
3737

38-
it("Should make the params AND body type safe", () => {
38+
it("Should make the query AND body type safe", () => {
3939
app.get(
4040
"/users",
4141
makeTypeSafeHandler(
4242
{
43-
params: z.object({
43+
query: z.object({
4444
id: z.string(),
4545
}),
4646
body: z.object({
@@ -49,11 +49,11 @@ it("Should make the params AND body type safe", () => {
4949
},
5050
(req, res) => {
5151
type tests = [
52-
Expect<Equal<typeof req.params, { id: string }>>,
53-
Expect<Equal<typeof req.body, { name: string }>>,
52+
Expect<Equal<typeof req.query, { id: string }>>,
53+
Expect<Equal<typeof req.body, { name: string }>>
5454
];
55-
},
56-
),
55+
}
56+
)
5757
);
5858
});
5959

@@ -62,9 +62,9 @@ it("Should default them to any if not passed in config", () => {
6262
"/users",
6363
makeTypeSafeHandler({}, (req, res) => {
6464
type tests = [
65-
Expect<Equal<typeof req.params, any>>,
66-
Expect<Equal<typeof req.body, any>>,
65+
Expect<Equal<typeof req.query, any>>,
66+
Expect<Equal<typeof req.body, any>>
6767
];
68-
}),
68+
})
6969
);
7070
});

0 commit comments

Comments
 (0)