Skip to content

Commit b13b758

Browse files
authored
feat: adding wildcard detection to dart frog gen (#691)
1 parent ec77e97 commit b13b758

File tree

6 files changed

+182
-33
lines changed

6 files changed

+182
-33
lines changed

packages/dart_frog_gen/lib/src/build_route_configuration.dart

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ List<RouteDirectory> _getRouteDirectories({
123123
onEndpoint(endpoint, file);
124124
}
125125

126+
if (directoryPath.isWildcard()) {
127+
throw ArgumentError('Only files can be named with a wildcard alias.');
128+
}
129+
126130
directories.add(
127131
RouteDirectory(
128132
name: directoryPath.toAlias(),
@@ -191,11 +195,15 @@ List<RouteFile> _getRouteFiles({
191195

192196
final fileRoute = getFileRoute();
193197
final relativeFilePath = path.join('..', 'routes', filePath);
198+
199+
final isWildcard = filePath.isWildcard();
200+
194201
final route = RouteFile(
195202
name: filePath.toAlias(),
196203
path: relativeFilePath.replaceAll(r'\', '/'),
197204
route: fileRoute.toRoute(),
198205
params: fileRoute.toParams(),
206+
wildcard: isWildcard,
199207
);
200208
onRoute(route);
201209
files.add(route);
@@ -212,25 +220,44 @@ List<RouteFile> _getRouteFiles({
212220
return files;
213221
}
214222

215-
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.
216227
String toAlias() {
217228
final alias = path
218229
.withoutExtension(this)
219230
.replaceAll('[', r'$')
220231
.replaceAll(']', '')
221-
.replaceAll('/', '_');
232+
.replaceAll('/', '_')
233+
.replaceAll('...', 'wildcard_');
222234
if (alias == '') return 'index';
223235
return alias;
224236
}
225237

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);
243+
244+
return RegExp(r'\[\.\.\.(.*?)\]').hasMatch(value);
245+
}
246+
247+
/// Parses the string into a route path.
226248
String toRoute() {
249+
if (isWildcard()) return '/';
227250
return replaceAll('[', '<').replaceAll(']', '>').replaceAll(r'\', '/');
228251
}
229252

253+
/// Parses the string into a list of route parameters.
230254
List<String> toParams() {
231255
final regexp = RegExp(r'\[(.*?)\]');
232256
final matches = regexp.allMatches(this);
233-
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();
234261
}
235262
}
236263

@@ -388,6 +415,7 @@ class RouteFile {
388415
required this.path,
389416
required this.route,
390417
required this.params,
418+
required this.wildcard,
391419
});
392420

393421
/// The alias for the current file.
@@ -402,18 +430,23 @@ class RouteFile {
402430
/// The dynamic route params associated with the file.
403431
final List<String> params;
404432

433+
/// Whether the route is a wildcard route.
434+
final bool wildcard;
435+
405436
/// Create a copy of the current instance and override zero or more values.
406437
RouteFile copyWith({
407438
String? name,
408439
String? path,
409440
String? route,
410441
List<String>? params,
442+
bool? wildcard,
411443
}) {
412444
return RouteFile(
413445
name: name ?? this.name,
414446
path: path ?? this.path,
415447
route: route ?? this.route,
416448
params: params ?? this.params,
449+
wildcard: wildcard ?? this.wildcard,
417450
);
418451
}
419452

@@ -424,6 +457,7 @@ class RouteFile {
424457
'path': path,
425458
'route': route,
426459
'file_params': params,
460+
'wildcard': wildcard,
427461
};
428462
}
429463
}

0 commit comments

Comments
 (0)