Skip to content

Commit f10004e

Browse files
authored
feat(dart_frog_cli): add dart_frog_new_brick (middleware implementation) (#624)
* feat(dart_frog_cli): add dart_frog_new_brick * Update bricks/dart_frog_new/__brick__/{{~ middleware.dart }}
1 parent be1f512 commit f10004e

File tree

7 files changed

+327
-12
lines changed

7 files changed

+327
-12
lines changed
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
import 'package:dart_frog/dart_frog.dart';
22

3-
{{> route.dart}}
3+
{{#is_route}}
4+
{{> route.dart}}
5+
{{/is_route}}{{#is_middleware}}
6+
{{> middleware.dart}}
7+
{{/is_middleware}}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Handler middleware(Handler handler) {
2+
// TODO: implement middleware
3+
return handler;
4+
}
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{{^params}}Response onRequest(RequestContext context) {
22
{{/params}}{{#params.0}}Response onRequest(
33
RequestContext context,{{#params}}
4-
String {{.}},{{/params}}
4+
String {{#camelCase}}{{.}}{{/camelCase}},{{/params}}
55
) {
6-
{{/params.0}} return Response(body: 'Welcome to Dart Frog!');
6+
{{/params.0}} // TODO: implement route handler
7+
return Response(body: 'This is a new route!');
78
}

bricks/dart_frog_new/hooks/lib/post_gen.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ Future<void> postGen(
1111
io.Directory? directory,
1212
void Function(int exitCode) exit = _defaultExit,
1313
}) async {
14+
final succeeded = context.vars.containsKey('dir_path');
15+
if (!succeeded) {
16+
return exit(1);
17+
}
18+
1419
final dirPath = context.vars['dir_path'] as String;
1520
final currentDirectory = directory ?? io.Directory.current;
1621

bricks/dart_frog_new/hooks/lib/pre_gen.dart

Lines changed: 102 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,31 @@ void preGen(
4646
return exit(1);
4747
}
4848

49-
// The path in which the route will be created
49+
// The path in which the route or middleware will be created
5050
final routePath = normalizeRoutePath(context.vars['route_path'] as String);
5151

52-
return _preGenRoute(
53-
context,
54-
routePath: routePath,
55-
routeConfiguration: routeConfiguration,
56-
projectDirectory: projectDirectory,
57-
exit: exit,
58-
);
52+
if (type == 'route') {
53+
return _preGenRoute(
54+
context,
55+
routePath: routePath,
56+
routeConfiguration: routeConfiguration,
57+
projectDirectory: projectDirectory,
58+
exit: exit,
59+
);
60+
}
61+
62+
if (type == 'middleware') {
63+
return _preGenMiddleware(
64+
context,
65+
routePath: routePath,
66+
routeConfiguration: routeConfiguration,
67+
projectDirectory: projectDirectory,
68+
exit: exit,
69+
);
70+
}
71+
72+
context.logger.err('Unrecognized type: $type');
73+
return exit(1);
5974
}
6075

6176
void _preGenRoute(
@@ -125,7 +140,86 @@ void _preGenRoute(
125140
return exit(1);
126141
}
127142

143+
context.vars['is_route'] = true;
128144
context.vars['dir_path'] = path.dirname(routeFileName);
129145
context.vars['filename'] = path.basename(routeFileName);
130146
context.vars['params'] = parameterNames;
131147
}
148+
149+
void _preGenMiddleware(
150+
HookContext context, {
151+
required String routePath,
152+
required RouteConfiguration routeConfiguration,
153+
required io.Directory projectDirectory,
154+
required void Function(int exitCode) exit,
155+
}) {
156+
final routesDirectoryPath = path.relative(
157+
io.Directory(path.join(projectDirectory.path, 'routes')).path,
158+
);
159+
160+
const middlewareFilename = '_middleware.dart';
161+
162+
// Get the path to directory containing the middleware file
163+
final String middlewareContainingDir;
164+
if (routePath == '/') {
165+
middlewareContainingDir = routesDirectoryPath;
166+
} else {
167+
middlewareContainingDir = path.withoutExtension(
168+
routeToPath(
169+
routePath.toBracketParameterSyntax,
170+
preamble: routesDirectoryPath,
171+
),
172+
);
173+
}
174+
175+
// Verify if the middleware file already exists
176+
final middlewareFilePath =
177+
path.join(middlewareContainingDir, middlewareFilename);
178+
final middlewareExists = io.File(middlewareFilePath).existsSync();
179+
if (middlewareExists) {
180+
context.logger.err('There is already a middleware at $middlewareFilePath');
181+
return exit(1);
182+
}
183+
184+
// Verify if the given route already exists as directory
185+
final routeExistsAsDirectory = io.Directory(
186+
middlewareContainingDir.toBracketParameterSyntax,
187+
).existsSync();
188+
189+
// If the route does not exist as directory, we must check if any of its
190+
// ancestor routes exists as file routes to avoid rogue routes.
191+
if (!routeExistsAsDirectory) {
192+
final fileRoute = routeConfiguration.containingFileRoute(
193+
routePath,
194+
includeSelf: true,
195+
);
196+
197+
if (fileRoute != null) {
198+
final filePath = path.normalize(
199+
path.join(routesDirectoryPath, fileRoute.path),
200+
);
201+
202+
io.Directory(path.withoutExtension(filePath)).createSync();
203+
204+
final newFilepath = filePath.replaceFirst('.dart', '/index.dart');
205+
io.File(filePath).renameSync(newFilepath);
206+
context.logger
207+
.detail('Renamed $filePath to $newFilepath to avoid rogue routes');
208+
}
209+
}
210+
211+
try {
212+
middlewareContainingDir.toBracketParameterSyntax.getParameterNames();
213+
} on FormatException catch (exception) {
214+
context.logger.err('Failed to create middleware: ${exception.message}');
215+
return exit(1);
216+
}
217+
218+
context.logger.detail(
219+
'Creating middleware file: ${middlewareFilePath.toBracketParameterSyntax}',
220+
);
221+
222+
context.vars['is_middleware'] = true;
223+
context.vars['dir_path'] = middlewareContainingDir.toBracketParameterSyntax;
224+
context.vars['filename'] = middlewareFilename;
225+
}

bricks/dart_frog_new/hooks/test/post_gen_test.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ void main() {
4848
);
4949
});
5050

51+
test('exit(1) if dir_path is not defined', () {
52+
final exitCalls = <int>[];
53+
postGen(context, exit: exitCalls.add);
54+
expect(exitCalls, equals([1]));
55+
});
56+
5157
test('moves file to supposed directory', () {
5258
final directory = io.Directory.systemTemp.createTempSync(
5359
'dart_frog_new_hooks_test',

0 commit comments

Comments
 (0)