@@ -424,6 +424,38 @@ describe('Basic - $url()', () => {
424424 } ) . href
425425 ) . toBe ( 'http://fake/content/search?page=123&limit=20' )
426426 } )
427+
428+ it . each ( [ 'http://fake' , 'http://fake/' , 'http://fake//' , 'http://fake/api' ] ) (
429+ 'Should return a correct path via $path() regardless of %s' ,
430+ async ( baseURL ) => {
431+ const client = hc < typeof app > ( baseURL )
432+ expect ( client . index . $path ( ) ) . toBe ( '/' )
433+ expect (
434+ client . index . $path ( {
435+ query : {
436+ page : '123' ,
437+ limit : '20' ,
438+ } ,
439+ } )
440+ ) . toBe ( '/?page=123&limit=20' )
441+ expect ( client . api . $path ( ) ) . toBe ( '/api' )
442+ expect (
443+ client . api . posts [ ':id' ] . $path ( {
444+ param : {
445+ id : '123' ,
446+ } ,
447+ } )
448+ ) . toBe ( '/api/posts/123' )
449+ expect (
450+ client . content . search . $path ( {
451+ query : {
452+ page : '123' ,
453+ limit : '20' ,
454+ } ,
455+ } )
456+ ) . toBe ( '/content/search?page=123&limit=20' )
457+ }
458+ )
427459} )
428460
429461describe ( 'Form - Multiple Values' , ( ) => {
@@ -760,6 +792,10 @@ describe('Merge path with `app.route()`', () => {
760792 const url = client . api . bar . $url ( )
761793 expect ( url . href ) . toBe ( 'http://localhost/api/bar' )
762794 } )
795+ it ( 'Should work with $path' , async ( ) => {
796+ const path = client . api . bar . $path ( )
797+ expect ( path ) . toBe ( '/api/bar' )
798+ } )
763799 } )
764800
765801 describe ( 'With a blank path' , ( ) => {
@@ -780,6 +816,35 @@ describe('Merge path with `app.route()`', () => {
780816 const url = client . api . v1 . me . $url ( )
781817 expectTypeOf < URL > ( url )
782818 expect ( url . href ) . toBe ( 'http://localhost/api/v1/me' )
819+
820+ const path = client . api . v1 . me . $path ( )
821+ expectTypeOf < '/api/v1/me' > ( path )
822+ expect ( path ) . toBe ( '/api/v1/me' )
823+ } )
824+ } )
825+
826+ describe ( 'With endpoint pathname' , ( ) => {
827+ const app = new Hono ( ) . basePath ( '/api/v1' )
828+ const routes = app . route (
829+ '/me' ,
830+ new Hono ( ) . route (
831+ '' ,
832+ new Hono ( ) . get ( '' , async ( c ) => {
833+ return c . json ( { name : 'hono' } )
834+ } )
835+ )
836+ )
837+ const client = hc < typeof routes > ( 'http://localhost/proxy' )
838+
839+ it ( 'Should infer paths correctly' , async ( ) => {
840+ // Should not a throw type error
841+ const url = client . api . v1 . me . $url ( )
842+ expectTypeOf < URL > ( url )
843+ expect ( url . href ) . toBe ( 'http://localhost/proxy/api/v1/me' )
844+
845+ const path = client . api . v1 . me . $path ( )
846+ expectTypeOf < '/api/v1/me' > ( path )
847+ expect ( path ) . toBe ( '/api/v1/me' )
783848 } )
784849 } )
785850} )
@@ -1055,40 +1120,43 @@ describe('Infer the response types from middlewares', () => {
10551120 } )
10561121} )
10571122
1058- describe ( '$url() with a param option' , ( ) => {
1123+ const pathname = < T extends URL | string > ( value : T ) : string =>
1124+ value instanceof URL ? value . pathname : value
1125+
1126+ describe . each ( [ '$path' , '$url' ] as const ) ( '%s() with a param option' , ( cmd ) => {
10591127 const app = new Hono ( )
10601128 . get ( '/posts/:id/comments' , ( c ) => c . json ( { ok : true } ) )
10611129 . get ( '/something/:firstId/:secondId/:version?' , ( c ) => c . json ( { ok : true } ) )
10621130 type AppType = typeof app
10631131 const client = hc < AppType > ( 'http://localhost' )
10641132
1065- it ( 'Should return the correct path - /posts/123/comments' , async ( ) => {
1066- const url = client . posts [ ':id' ] . comments . $url ( {
1133+ it ( 'Should return the correct url path - /posts/123/comments' , async ( ) => {
1134+ const value = client . posts [ ':id' ] . comments [ cmd ] ( {
10671135 param : {
10681136 id : '123' ,
10691137 } ,
10701138 } )
1071- expect ( url . pathname ) . toBe ( '/posts/123/comments' )
1139+ expect ( pathname ( value ) ) . toBe ( '/posts/123/comments' )
10721140 } )
10731141
10741142 it ( 'Should return the correct path - /posts/:id/comments' , async ( ) => {
1075- const url = client . posts [ ':id' ] . comments . $url ( )
1076- expect ( url . pathname ) . toBe ( '/posts/:id/comments' )
1143+ const value = client . posts [ ':id' ] . comments [ cmd ] ( )
1144+ expect ( pathname ( value ) ) . toBe ( '/posts/:id/comments' )
10771145 } )
10781146
10791147 it ( 'Should return the correct path - /something/123/456' , async ( ) => {
1080- const url = client . something [ ':firstId' ] [ ':secondId' ] [ ':version?' ] . $url ( {
1148+ const value = client . something [ ':firstId' ] [ ':secondId' ] [ ':version?' ] [ cmd ] ( {
10811149 param : {
10821150 firstId : '123' ,
10831151 secondId : '456' ,
10841152 version : undefined ,
10851153 } ,
10861154 } )
1087- expect ( url . pathname ) . toBe ( '/something/123/456' )
1155+ expect ( pathname ( value ) ) . toBe ( '/something/123/456' )
10881156 } )
10891157} )
10901158
1091- describe ( '$url() with a query option' , ( ) => {
1159+ describe ( '$url() / $path() with a query option' , ( ) => {
10921160 const app = new Hono ( ) . get (
10931161 '/posts' ,
10941162 validator ( 'query' , ( ) => {
@@ -1106,6 +1174,13 @@ describe('$url() with a query option', () => {
11061174 } ,
11071175 } )
11081176 expect ( url . search ) . toBe ( '?filter=test' )
1177+
1178+ const path = client . posts . $path ( {
1179+ query : {
1180+ filter : 'test' ,
1181+ } ,
1182+ } )
1183+ expect ( path ) . toBe ( '/posts?filter=test' )
11091184 } )
11101185} )
11111186
0 commit comments