Skip to content

Commit 62db9e0

Browse files
fix trailing slashes handling in nest multi-handler routing (#286)
* fix trailing slashes handling in fastify nest * chore: changeset --------- Co-authored-by: Oliver Butler <dev@oliverbutler.uk>
1 parent 32d7167 commit 62db9e0

3 files changed

Lines changed: 18 additions & 1 deletion

File tree

‎.changeset/loud-jars-work.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@ts-rest/nest': patch
3+
---
4+
5+
fix: add support for trailing slashes in the nest multi-handler routing

‎libs/ts-rest/nest/src/lib/ts-rest-nest-handler.spec.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ describe('doesUrlMatchContractPath', () => {
4141
${'/'} | ${'/'} | ${true}
4242
${'/'} | ${'/api'} | ${false}
4343
${'/api'} | ${'/api'} | ${true}
44+
${'/api/'} | ${'/api'} | ${true}
45+
${'/api'} | ${'/api/'} | ${true}
46+
${'/api/'} | ${'/api/'} | ${true}
4447
${'/posts/:id'} | ${'/posts/1'} | ${true}
4548
${'/posts/:id'} | ${'/posts/1'} | ${true}
4649
${'/posts/:id'} | ${'/posts'} | ${false}

‎libs/ts-rest/nest/src/lib/ts-rest-nest-handler.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,15 @@ export const doesUrlMatchContractPath = (
165165
*/
166166
url: string
167167
): boolean => {
168+
// strip trailing slash
169+
if (contractPath !== '/' && contractPath.endsWith('/')) {
170+
contractPath = contractPath.slice(0, -1);
171+
}
172+
173+
if (url !== '/' && url.endsWith('/')) {
174+
url = url.slice(0, -1);
175+
}
176+
168177
const contractPathParts = contractPath.split('/');
169178

170179
const urlParts = url.split('/');
@@ -221,7 +230,7 @@ export class TsRestHandlerInterceptor implements NestInterceptor {
221230
return (
222231
doesUrlMatchContractPath(
223232
value.path,
224-
'path' in req ? req.path : req.url.split('?')[0]
233+
'path' in req ? req.path : req.routerPath
225234
) && req.method === value.method
226235
);
227236
}

0 commit comments

Comments
 (0)