1
1
import express from "express" ;
2
2
import { asAsync } from "@notainc/typed-api-spec/express" ;
3
+ import { ToHandlers , typed } from "@notainc/typed-api-spec/express" ;
3
4
import { pathMap } from "../../spec/valibot" ;
4
- import { ToHandlers , typed } from "@notainc/typed-api-spec/express/valibot" ;
5
5
6
6
const emptyMiddleware = (
7
7
req : express . Request ,
@@ -20,7 +20,7 @@ const newApp = () => {
20
20
// const wApp = app as TRouter<typeof pathMap>;
21
21
// ```
22
22
const wApp = asAsync ( typed ( pathMap , app ) ) ;
23
- wApp . get ( "/users" , emptyMiddleware , ( req , res ) => {
23
+ wApp . get ( "/users" , emptyMiddleware , async ( req , res ) => {
24
24
// eslint-disable-next-line no-constant-condition
25
25
if ( false ) {
26
26
// @ts -expect-error params is not defined because pathMap["/users"]["get"].params is not defined
@@ -29,41 +29,42 @@ const newApp = () => {
29
29
30
30
// validate method is available in res.locals
31
31
// validate(req).query() is equals to pathMap["/users"]["get"].query.safeParse(req.query)
32
- const { data, error } = res . locals . validate ( req ) . query ( ) ;
33
- if ( data !== undefined ) {
34
- // res.status(200).json() accepts only the response schema defined in pathMap["/users"]["get"].res["200"]
35
- res . status ( 200 ) . json ( { userNames : [ `page${ data . page } #user1` ] } ) ;
36
- } else {
32
+ const r = await res . locals . validate ( req ) . query ( ) ;
33
+ if ( r . issues ) {
37
34
// res.status(400).json() accepts only the response schema defined in pathMap["/users"]["get"].res["400"]
38
- res . status ( 400 ) . json ( { errorMessage : error . toString ( ) } ) ;
35
+ return res . status ( 400 ) . json ( { errorMessage : r . issues . toString ( ) } ) ;
39
36
}
37
+ // res.status(200).json() accepts only the response schema defined in pathMap["/users"]["get"].res["200"]
38
+ return res . status ( 200 ) . json ( { userNames : [ `page${ r . value . page } #user1` ] } ) ;
40
39
} ) ;
41
- wApp . post ( "/users" , ( req , res ) => {
42
- // validate(req).body() is equals to pathMap["/users"]["post"].body.safeParse(req.body)
43
- const { data, error } = res . locals . validate ( req ) . body ( ) ;
40
+
41
+ wApp . post ( "/users" , async ( req , res ) => {
44
42
{
45
43
// Request header also can be validated
46
44
res . locals . validate ( req ) . headers ( ) ;
47
45
}
48
- if ( data !== undefined ) {
49
- // res.status(200).json () accepts only the response schema defined in pathMap["/users"]["post"].res["200"]
50
- res . status ( 200 ) . json ( { userId : data . userName + "#0" } ) ;
51
- } else {
46
+
47
+ // validate(req).body () is equals to pathMap["/users"]["post"].body.safeParse(req.body)
48
+ const r = await res . locals . validate ( req ) . body ( ) ;
49
+ if ( r . issues ) {
52
50
// res.status(400).json() accepts only the response schema defined in pathMap["/users"]["post"].res["400"]
53
- res . status ( 400 ) . json ( { errorMessage : error . toString ( ) } ) ;
51
+ return res . status ( 400 ) . json ( { errorMessage : r . issues . toString ( ) } ) ;
54
52
}
53
+ // res.status(200).json() accepts only the response schema defined in pathMap["/users"]["post"].res["200"]
54
+ return res . status ( 200 ) . json ( { userId : r . value . userName + "#0" } ) ;
55
55
} ) ;
56
56
57
- const getUserHandler : Handlers [ "/users/:userId" ] [ "get" ] = ( req , res ) => {
58
- const { data : params , error } = res . locals . validate ( req ) . params ( ) ;
59
-
60
- if ( params !== undefined ) {
61
- // res.status(200).json() accepts only the response schema defined in pathMap["/users/:userId"]["get"].res["200"]
62
- res . status ( 200 ) . json ( { userName : "user#" + params . userId } ) ;
63
- } else {
57
+ const getUserHandler : Handlers [ "/users/:userId" ] [ "get" ] = async (
58
+ req ,
59
+ res ,
60
+ ) => {
61
+ const r = await res . locals . validate ( req ) . params ( ) ;
62
+ if ( r . issues ) {
64
63
// res.status(400).json() accepts only the response schema defined in pathMap["/users/:userId"]["get"].res["400"]
65
- res . status ( 400 ) . json ( { errorMessage : error . toString ( ) } ) ;
64
+ return res . status ( 400 ) . json ( { errorMessage : r . issues . toString ( ) } ) ;
66
65
}
66
+ // res.status(200).json() accepts only the response schema defined in pathMap["/users/:userId"]["get"].res["200"]
67
+ return res . status ( 200 ) . json ( { userName : "user#" + r . value . userId } ) ;
67
68
} ;
68
69
wApp . get ( "/users/:userId" , getUserHandler ) ;
69
70
0 commit comments