11import express from "express" ;
22import { asAsync } from "@notainc/typed-api-spec/express" ;
3+ import { ToHandlers , typed } from "@notainc/typed-api-spec/express" ;
34import { pathMap } from "../../spec/valibot" ;
4- import { ToHandlers , typed } from "@notainc/typed-api-spec/express/valibot" ;
55
66const emptyMiddleware = (
77 req : express . Request ,
@@ -20,7 +20,7 @@ const newApp = () => {
2020 // const wApp = app as TRouter<typeof pathMap>;
2121 // ```
2222 const wApp = asAsync ( typed ( pathMap , app ) ) ;
23- wApp . get ( "/users" , emptyMiddleware , ( req , res ) => {
23+ wApp . get ( "/users" , emptyMiddleware , async ( req , res ) => {
2424 // eslint-disable-next-line no-constant-condition
2525 if ( false ) {
2626 // @ts -expect-error params is not defined because pathMap["/users"]["get"].params is not defined
@@ -29,41 +29,42 @@ const newApp = () => {
2929
3030 // validate method is available in res.locals
3131 // 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 ) {
3734 // 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 ( ) } ) ;
3936 }
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` ] } ) ;
4039 } ) ;
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 ) => {
4442 {
4543 // Request header also can be validated
4644 res . locals . validate ( req ) . headers ( ) ;
4745 }
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 ) {
5250 // 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 ( ) } ) ;
5452 }
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" } ) ;
5555 } ) ;
5656
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 ) {
6463 // 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 ( ) } ) ;
6665 }
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 } ) ;
6768 } ;
6869 wApp . get ( "/users/:userId" , getUserHandler ) ;
6970
0 commit comments