@@ -765,6 +765,72 @@ describe('ts-rest-nest-handler', () => {
765765 } ,
766766 } ) ;
767767 } ) ;
768+
769+ it ( 'should route correctly for optional params' , async ( ) => {
770+ const c = initContract ( ) ;
771+
772+ const contract = c . router ( {
773+ getPosts : {
774+ path : '/posts/:year?/:month?' ,
775+ method : 'GET' ,
776+ responses : {
777+ 200 : z . object ( {
778+ rangeSearched : z . string ( ) ,
779+ } ) ,
780+ } ,
781+ } ,
782+ } ) ;
783+
784+ @Controller ( )
785+ class TestController {
786+ @TsRestHandler ( contract )
787+ async handler ( ) {
788+ return tsRestHandler ( contract , {
789+ getPosts : async ( { params } ) => ( {
790+ status : 200 ,
791+ body : { rangeSearched : `${ params . year } -${ params . month } ` } ,
792+ } ) ,
793+ } ) ;
794+ }
795+ }
796+
797+ const moduleRef = await Test . createTestingModule ( {
798+ controllers : [ TestController ] ,
799+ } ) . compile ( ) ;
800+
801+ const app = moduleRef . createNestApplication ( ) ;
802+ await app . init ( ) ;
803+
804+ await supertest ( app . getHttpServer ( ) )
805+ . get ( '/posts' )
806+ . send ( )
807+ . expect ( 200 )
808+ . then ( ( res ) =>
809+ expect ( res . body ) . toStrictEqual ( {
810+ rangeSearched : 'undefined-undefined' ,
811+ } ) ,
812+ ) ;
813+
814+ await supertest ( app . getHttpServer ( ) )
815+ . get ( '/posts/yyyy' )
816+ . send ( )
817+ . expect ( 200 )
818+ . then ( ( res ) =>
819+ expect ( res . body ) . toStrictEqual ( {
820+ rangeSearched : 'yyyy-undefined' ,
821+ } ) ,
822+ ) ;
823+
824+ await supertest ( app . getHttpServer ( ) )
825+ . get ( '/posts/yyyy/mm' )
826+ . send ( )
827+ . expect ( 200 )
828+ . then ( ( res ) =>
829+ expect ( res . body ) . toStrictEqual ( {
830+ rangeSearched : 'yyyy-mm' ,
831+ } ) ,
832+ ) ;
833+ } ) ;
768834 } ) ;
769835
770836 describe ( 'single-handler api' , ( ) => {
@@ -1584,6 +1650,70 @@ describe('ts-rest-nest-handler', () => {
15841650 expect ( res . header . location ) . toBe ( '/redirected' ) ;
15851651 } ) ;
15861652 } ) ;
1653+
1654+ it ( 'should route correctly for optional params' , async ( ) => {
1655+ const c = initContract ( ) ;
1656+
1657+ const contract = c . router ( {
1658+ getPosts : {
1659+ path : '/posts/:year?/:month?' ,
1660+ method : 'GET' ,
1661+ responses : {
1662+ 200 : z . object ( {
1663+ rangeSearched : z . string ( ) ,
1664+ } ) ,
1665+ } ,
1666+ } ,
1667+ } ) ;
1668+
1669+ @Controller ( )
1670+ class TestController {
1671+ @TsRestHandler ( contract . getPosts )
1672+ async handler ( ) {
1673+ return tsRestHandler ( contract . getPosts , async ( { params } ) => ( {
1674+ status : 200 ,
1675+ body : { rangeSearched : `${ params . year } -${ params . month } ` } ,
1676+ } ) ) ;
1677+ }
1678+ }
1679+
1680+ const moduleRef = await Test . createTestingModule ( {
1681+ controllers : [ TestController ] ,
1682+ } ) . compile ( ) ;
1683+
1684+ const app = moduleRef . createNestApplication ( ) ;
1685+ await app . init ( ) ;
1686+
1687+ await supertest ( app . getHttpServer ( ) )
1688+ . get ( '/posts' )
1689+ . send ( )
1690+ . expect ( 200 )
1691+ . then ( ( res ) =>
1692+ expect ( res . body ) . toStrictEqual ( {
1693+ rangeSearched : 'undefined-undefined' ,
1694+ } ) ,
1695+ ) ;
1696+
1697+ await supertest ( app . getHttpServer ( ) )
1698+ . get ( '/posts/yyyy' )
1699+ . send ( )
1700+ . expect ( 200 )
1701+ . then ( ( res ) =>
1702+ expect ( res . body ) . toStrictEqual ( {
1703+ rangeSearched : 'yyyy-undefined' ,
1704+ } ) ,
1705+ ) ;
1706+
1707+ await supertest ( app . getHttpServer ( ) )
1708+ . get ( '/posts/yyyy/mm' )
1709+ . send ( )
1710+ . expect ( 200 )
1711+ . then ( ( res ) =>
1712+ expect ( res . body ) . toStrictEqual ( {
1713+ rangeSearched : 'yyyy-mm' ,
1714+ } ) ,
1715+ ) ;
1716+ } ) ;
15871717 } ) ;
15881718
15891719 it ( 'should be able to combine single-handler, multi-handler and vanilla nest controllers' , async ( ) => {
0 commit comments