Skip to content

Commit 387e765

Browse files
authored
fix(dart_frog_gen): nested dynamic directory resolution (#138)
1 parent 41eaea3 commit 387e765

File tree

3 files changed

+194
-3
lines changed

3 files changed

+194
-3
lines changed

packages/dart_frog_gen/lib/src/build_route_configuration.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,9 @@ List<RouteFile> _getRouteFilesForDynamicDirectories(
109109
.listSync()
110110
.sorted()
111111
.whereType<Directory>()
112-
.where((d) => d.isDynamicRoute)
112+
.where((d) => prefix.isNotEmpty || d.isDynamicRoute)
113113
.forEach((dynamicDirectory) {
114-
final newPrefix = '/${path.basename(dynamicDirectory.path)}$prefix';
114+
final newPrefix = '$prefix/${path.basename(dynamicDirectory.path)}';
115115
final subset = _getRouteFiles(
116116
dynamicDirectory,
117117
onRoute: onRoute,

packages/dart_frog_gen/test/src/build_route_configuration_test.dart

Lines changed: 189 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ void main() {
271271
r'.._test_.fixtures_dynamic_nested_routes_$user_$id_index',
272272
'path':
273273
'../test/.fixtures/dynamic_nested/routes/[user]/[id]/index.dart',
274-
'route': '/<id>/<user>'
274+
'route': '/<user>/<id>'
275275
}
276276
]
277277
}
@@ -299,5 +299,193 @@ void main() {
299299
equals(expected),
300300
);
301301
});
302+
303+
test('supports /[id]/api/index.dart', () {
304+
const expected = [
305+
{
306+
'name': '_',
307+
'route': '/',
308+
'middleware': false,
309+
'files': [
310+
{
311+
'name': '.._test_.fixtures_dynamic_static_nesting1_routes_index',
312+
'path':
313+
'../test/.fixtures/dynamic_static_nesting1/routes/index.dart',
314+
'route': '/routes'
315+
},
316+
{
317+
'name':
318+
r'''.._test_.fixtures_dynamic_static_nesting1_routes_$id_api_index''',
319+
'path':
320+
'../test/.fixtures/dynamic_static_nesting1/routes/[id]/api/index.dart',
321+
'route': '/<id>/api'
322+
}
323+
]
324+
}
325+
];
326+
final directory = Directory(
327+
path.join(
328+
Directory.current.path,
329+
'test',
330+
'.fixtures',
331+
'dynamic_static_nesting1',
332+
),
333+
)..createSync(recursive: true);
334+
final routes = Directory(path.join(directory.path, 'routes'))
335+
..createSync();
336+
File(path.join(routes.path, 'index.dart')).createSync();
337+
final idDirectory = Directory(path.join(routes.path, '[id]'))
338+
..createSync();
339+
final apiDirectory = Directory(path.join(idDirectory.path, 'api'))
340+
..createSync();
341+
File(path.join(apiDirectory.path, 'index.dart')).createSync();
342+
final configuration = buildRouteConfiguration(directory);
343+
expect(
344+
configuration.directories.map((d) => d.toJson()).toList(),
345+
equals(expected),
346+
);
347+
});
348+
349+
test('supports /[id]/api/test.dart', () {
350+
const expected = [
351+
{
352+
'name': '_',
353+
'route': '/',
354+
'middleware': false,
355+
'files': [
356+
{
357+
'name': '.._test_.fixtures_dynamic_static_nesting2_routes_index',
358+
'path':
359+
'../test/.fixtures/dynamic_static_nesting2/routes/index.dart',
360+
'route': '/routes'
361+
},
362+
{
363+
'name':
364+
r'''.._test_.fixtures_dynamic_static_nesting2_routes_$id_api_test''',
365+
'path':
366+
'../test/.fixtures/dynamic_static_nesting2/routes/[id]/api/test.dart',
367+
'route': '/<id>/api/test'
368+
}
369+
]
370+
}
371+
];
372+
final directory = Directory(
373+
path.join(
374+
Directory.current.path,
375+
'test',
376+
'.fixtures',
377+
'dynamic_static_nesting2',
378+
),
379+
)..createSync(recursive: true);
380+
final routes = Directory(path.join(directory.path, 'routes'))
381+
..createSync();
382+
File(path.join(routes.path, 'index.dart')).createSync();
383+
final idDirectory = Directory(path.join(routes.path, '[id]'))
384+
..createSync();
385+
final apiDirectory = Directory(path.join(idDirectory.path, 'api'))
386+
..createSync();
387+
File(path.join(apiDirectory.path, 'test.dart')).createSync();
388+
final configuration = buildRouteConfiguration(directory);
389+
expect(
390+
configuration.directories.map((d) => d.toJson()).toList(),
391+
equals(expected),
392+
);
393+
});
394+
395+
test('supports /[id]/api/[name]/index.dart', () {
396+
const expected = [
397+
{
398+
'name': '_',
399+
'route': '/',
400+
'middleware': false,
401+
'files': [
402+
{
403+
'name': '.._test_.fixtures_dynamic_static_nesting3_routes_index',
404+
'path':
405+
'../test/.fixtures/dynamic_static_nesting3/routes/index.dart',
406+
'route': '/routes'
407+
},
408+
{
409+
'name':
410+
r'''.._test_.fixtures_dynamic_static_nesting3_routes_$id_api_$name_index''',
411+
'path':
412+
'../test/.fixtures/dynamic_static_nesting3/routes/[id]/api/[name]/index.dart',
413+
'route': '/<id>/api/<name>'
414+
}
415+
]
416+
},
417+
];
418+
final directory = Directory(
419+
path.join(
420+
Directory.current.path,
421+
'test',
422+
'.fixtures',
423+
'dynamic_static_nesting3',
424+
),
425+
)..createSync(recursive: true);
426+
final routes = Directory(path.join(directory.path, 'routes'))
427+
..createSync();
428+
File(path.join(routes.path, 'index.dart')).createSync();
429+
final idDirectory = Directory(path.join(routes.path, '[id]'))
430+
..createSync();
431+
final apiDirectory = Directory(path.join(idDirectory.path, 'api'))
432+
..createSync();
433+
final nameDirectory = Directory(path.join(apiDirectory.path, '[name]'))
434+
..createSync();
435+
File(path.join(nameDirectory.path, 'index.dart')).createSync();
436+
final configuration = buildRouteConfiguration(directory);
437+
expect(
438+
configuration.directories.map((d) => d.toJson()).toList(),
439+
equals(expected),
440+
);
441+
});
442+
443+
test('supports /[id]/api/[name]/test.dart', () {
444+
const expected = [
445+
{
446+
'name': '_',
447+
'route': '/',
448+
'middleware': false,
449+
'files': [
450+
{
451+
'name': '.._test_.fixtures_dynamic_static_nesting4_routes_index',
452+
'path':
453+
'../test/.fixtures/dynamic_static_nesting4/routes/index.dart',
454+
'route': '/routes'
455+
},
456+
{
457+
'name':
458+
r'''.._test_.fixtures_dynamic_static_nesting4_routes_$id_api_$name_test''',
459+
'path':
460+
'../test/.fixtures/dynamic_static_nesting4/routes/[id]/api/[name]/test.dart',
461+
'route': '/<id>/api/<name>/test'
462+
}
463+
]
464+
},
465+
];
466+
final directory = Directory(
467+
path.join(
468+
Directory.current.path,
469+
'test',
470+
'.fixtures',
471+
'dynamic_static_nesting4',
472+
),
473+
)..createSync(recursive: true);
474+
final routes = Directory(path.join(directory.path, 'routes'))
475+
..createSync();
476+
File(path.join(routes.path, 'index.dart')).createSync();
477+
final idDirectory = Directory(path.join(routes.path, '[id]'))
478+
..createSync();
479+
final apiDirectory = Directory(path.join(idDirectory.path, 'api'))
480+
..createSync();
481+
final nameDirectory = Directory(path.join(apiDirectory.path, '[name]'))
482+
..createSync();
483+
File(path.join(nameDirectory.path, 'test.dart')).createSync();
484+
final configuration = buildRouteConfiguration(directory);
485+
expect(
486+
configuration.directories.map((d) => d.toJson()).toList(),
487+
equals(expected),
488+
);
489+
});
302490
});
303491
}

packages/dart_frog_gen/test/src/path_to_route_test.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ void main() {
88
'../routes/hello.dart': '/hello',
99
'../routes/hello/world.dart': '/hello/world',
1010
'../routes/hello/[name].dart': '/hello/[name]',
11+
'../routes/[id]/item.dart': '/[id]/item',
12+
'../routes/[id]/part/item.dart': '/[id]/part/item',
13+
'../routes/[id]/part/index.dart': '/[id]/part',
1114
'../routes/api/v1/index.dart': '/api/v1',
1215
r'..\routes\index.dart': '/',
1316
r'..\routes\hello.dart': '/hello',

0 commit comments

Comments
 (0)