Skip to content

Commit 54849b6

Browse files
authored
fix(dart_frog_gen): support repeated nested routes (#246)
1 parent d353ae5 commit 54849b6

File tree

2 files changed

+68
-10
lines changed

2 files changed

+68
-10
lines changed

packages/dart_frog_gen/lib/src/build_route_configuration.dart

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -179,18 +179,28 @@ List<RouteFile> _getRouteFiles({
179179
final filePath = path
180180
.relative(entity.path, from: routesDirectory.path)
181181
.replaceAll(r'\', '/');
182-
final fileRoutePath = pathToRoute(path.join('..', 'routes', filePath))
183-
.split(directoryPath)
184-
.last;
185-
var fileRoute = fileRoutePath.isEmpty ? '/' : fileRoutePath;
186-
fileRoute = prefix + fileRoute;
187-
if (!fileRoute.startsWith('/')) {
188-
fileRoute = '/$fileRoute';
189-
}
190-
if (fileRoute != '/' && fileRoute.endsWith('/')) {
191-
fileRoute = fileRoute.substring(0, fileRoute.length - 1);
182+
183+
String getFileRoute() {
184+
final routePath = pathToRoute(path.join('..', 'routes', filePath));
185+
final index = routePath.indexOf(directoryPath);
186+
final fileRoutePath = index == -1
187+
? routePath
188+
: routePath.substring(index + directoryPath.length);
189+
190+
var fileRoute = fileRoutePath.isEmpty ? '/' : fileRoutePath;
191+
fileRoute = prefix + fileRoute;
192+
193+
if (!fileRoute.startsWith('/')) {
194+
fileRoute = '/$fileRoute';
195+
}
196+
if (fileRoute != '/' && fileRoute.endsWith('/')) {
197+
fileRoute = fileRoute.substring(0, fileRoute.length - 1);
198+
}
199+
200+
return fileRoute;
192201
}
193202

203+
final fileRoute = getFileRoute();
194204
final relativeFilePath = path.join('..', 'routes', filePath);
195205
final route = RouteFile(
196206
name: filePath.toAlias(),

packages/dart_frog_gen/test/src/build_route_configuration_test.dart

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,5 +648,53 @@ void main() {
648648
}),
649649
);
650650
});
651+
652+
test('supports /api/api.dart', () {
653+
const expected = [
654+
{'name': '_', 'route': '/', 'middleware': false, 'files': <dynamic>[]},
655+
{
656+
'name': '_api',
657+
'route': '/api',
658+
'middleware': false,
659+
'files': [
660+
{
661+
'name': 'api_api',
662+
'path': '../routes/api/api.dart',
663+
'route': '/api'
664+
}
665+
]
666+
}
667+
];
668+
final directory = Directory(
669+
path.join(
670+
Directory.current.path,
671+
'test',
672+
'.fixtures',
673+
'dynamic_static_nesting3',
674+
),
675+
)..createSync(recursive: true);
676+
final routes = Directory(path.join(directory.path, 'routes'))
677+
..createSync();
678+
final apiDirectory = Directory(path.join(routes.path, 'api'))
679+
..createSync();
680+
File(path.join(apiDirectory.path, 'api.dart')).createSync();
681+
final configuration = buildRouteConfiguration(directory);
682+
expect(
683+
configuration.directories.map((d) => d.toJson()).toList(),
684+
equals(expected),
685+
);
686+
expect(
687+
configuration.endpoints,
688+
equals({
689+
'/api/api': [
690+
isA<RouteFile>().having(
691+
(r) => r.path,
692+
'path',
693+
'../routes/api/api.dart',
694+
)
695+
],
696+
}),
697+
);
698+
});
651699
});
652700
}

0 commit comments

Comments
 (0)