Skip to content

Commit db12fc3

Browse files
committed
different approach
1 parent 2478c85 commit db12fc3

File tree

4 files changed

+43
-14
lines changed

4 files changed

+43
-14
lines changed

bricks/dart_frog_dev_server/__brick__/server.dart

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bricks/dart_frog_prod_server/__brick__/build/bin/server.dart

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/dart_frog_gen/lib/src/build_route_configuration.dart

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ List<RouteFile> _getRouteFiles({
202202
name: filePath.toAlias(),
203203
path: relativeFilePath.replaceAll(r'\', '/'),
204204
route: fileRoute.toRoute(),
205-
params: isWildcard ? [] : fileRoute.toParams(),
205+
params: fileRoute.toParams(),
206206
wildcard: isWildcard,
207207
);
208208
onRoute(route);
@@ -220,29 +220,44 @@ List<RouteFile> _getRouteFiles({
220220
return files;
221221
}
222222

223-
extension on String {
223+
/// Extension on [String] with helper methods regarding
224+
/// Dart Frog routes.
225+
extension RouteStringX on String {
226+
/// Parses the stirng into a route alias.
224227
String toAlias() {
225228
final alias = path
226229
.withoutExtension(this)
227230
.replaceAll('[', r'$')
228231
.replaceAll(']', '')
229232
.replaceAll('/', '_')
230-
.replaceAll('*', 'wildcard');
233+
.replaceAll('...', 'wildcard_');
231234
if (alias == '') return 'index';
232235
return alias;
233236
}
234237

235-
bool isWildcard() => path.withoutExtension(this).endsWith('[*]');
238+
/// Returns if this value matches a wildcard route.
239+
bool isWildcard() {
240+
final value = endsWith('.dart')
241+
? path.basenameWithoutExtension(this)
242+
: path.basename(this);
236243

244+
return RegExp(r'\[\.\.\.(.*?)\]').hasMatch(value);
245+
}
246+
247+
/// Parses the string into a route path.
237248
String toRoute() {
238249
if (isWildcard()) return '/';
239250
return replaceAll('[', '<').replaceAll(']', '>').replaceAll(r'\', '/');
240251
}
241252

253+
/// Parses the string into a list of route parameters.
242254
List<String> toParams() {
243255
final regexp = RegExp(r'\[(.*?)\]');
244256
final matches = regexp.allMatches(this);
245-
return matches.map((m) => m[0]!.replaceAll(RegExp(r'\[|\]'), '')).toList();
257+
return matches.map((m) {
258+
final match = m[0]!;
259+
return match.replaceAll(RegExp(r'\[|\]'), '').replaceFirst('...', '');
260+
}).toList();
246261
}
247262
}
248263

packages/dart_frog_gen/test/src/build_route_configuration_test.dart

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,18 +1229,18 @@ Future<void>init(InternetAddress ip,int port)async{}
12291229
expect(configuration.rogueRoutes, isEmpty);
12301230
});
12311231

1232-
test('includes wildecard routes', () {
1232+
test('includes wildcard routes', () {
12331233
const expected = [
12341234
{
12351235
'name': '_test',
12361236
'route': '/test',
12371237
'middleware': [],
12381238
'files': [
12391239
{
1240-
'name': r'test_$wildcard',
1241-
'path': '../routes/test/[*].dart',
1240+
'name': r'test_$wildcard_value',
1241+
'path': '../routes/test/[...value].dart',
12421242
'route': '/',
1243-
'file_params': [],
1243+
'file_params': ['value'],
12441244
'wildcard': true,
12451245
}
12461246
],
@@ -1249,7 +1249,7 @@ Future<void>init(InternetAddress ip,int port)async{}
12491249
];
12501250

12511251
final configuration = buildRouteConfiguration(
1252-
createTempDir(files: ['routes/test/[*].dart']),
1252+
createTempDir(files: ['routes/test/[...value].dart']),
12531253
);
12541254

12551255
expect(
@@ -1259,19 +1259,33 @@ Future<void>init(InternetAddress ip,int port)async{}
12591259
expect(
12601260
configuration.endpoints,
12611261
equals({
1262-
'/test': [isARouteFile(path: '../routes/test/[*].dart')]
1262+
'/test': [isARouteFile(path: '../routes/test/[...value].dart')]
12631263
}),
12641264
);
12651265
});
12661266

12671267
test('throws when a folder is mapped to a wildcard', () {
12681268
expect(
12691269
() => buildRouteConfiguration(
1270-
createTempDir(files: ['routes/[*]/index.dart']),
1270+
createTempDir(files: ['routes/[...value]/index.dart']),
12711271
),
12721272
throwsArgumentError,
12731273
);
12741274
});
1275+
1276+
test('isWildcard returns correctly', () {
1277+
expect('/blog/[...slug].dart'.isWildcard(), isTrue);
1278+
expect('/blog/content/[...slug].dart'.isWildcard(), isTrue);
1279+
expect('/blog/content/[slug].dart'.isWildcard(), isFalse);
1280+
expect('/[...value]'.isWildcard(), isTrue);
1281+
1282+
expect(
1283+
'/blog/content/[...slug].dart'.toParams(),
1284+
equals(
1285+
['slug'],
1286+
),
1287+
);
1288+
});
12751289
});
12761290
}
12771291

0 commit comments

Comments
 (0)