Skip to content

Commit

Permalink
Merge pull request #24 from francescovallone/feat/16
Browse files Browse the repository at this point in the history
feat(#16): add factory constructors to Route and remove abstract modifier
  • Loading branch information
francescovallone authored Jun 9, 2024
2 parents 3d809e4 + 42af888 commit 4404949
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 3 deletions.
19 changes: 18 additions & 1 deletion .website/overview/routes.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,19 @@ They only exposes the endpoint and the method that the route will respond to so

## Create a route

To add routes you first need to create a class that extends the `Route` class and then add it to the controller using the `on` method.
To add routes you can either create a class that extends the `Route` class or use the following methods to create one.

- `Route.get`
- `Route.post`
- `Route.put`
- `Route.delete`
- `Route.patch`

All this methods has a required parameter `path` that is the path of the route and the method signature corresponds to the method that the route will respond to.

This change was made to reduce the boilerplate code needed to create a route and to make the code more readable.

Then you can add it to the controller using the `on` method.

::: code-group

Expand All @@ -20,6 +32,11 @@ class MyController extends Controller {
data: 'Hello World!',
);
});
on(Route.get(path: '/'), (context) { // This is the same as the previous route
return Response.text(
data: 'Hello World!',
);
});
}
}
```
Expand Down
3 changes: 3 additions & 0 deletions packages/serinus/bin/serinus.dart
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ class HomeController extends Controller {
return Response.text(
'${context.request.getData('test')} ${context.pathParameters}');
});
on(Route.get('/test'), (context) async {
return Response.text('Hello world from test');
});
}
}

Expand Down
28 changes: 27 additions & 1 deletion packages/serinus/lib/src/core/route.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'guard.dart';
import 'pipe.dart';

/// The [Route] class is used to define the routes of the application.
abstract class Route {
class Route {
/// The path of the route.
final String path;

Expand All @@ -28,4 +28,30 @@ abstract class Route {
required this.method,
this.queryParameters = const {},
});

/// The [Route.get] factory constructor is used to create a new instance of the [Route] class with the GET method.
factory Route.get(String path) {
return Route(path: path, method: HttpMethod.get);
}

/// The [Route.post] factory constructor is used to create a new instance of the [Route] class with the POST method.
factory Route.post(String path) {
return Route(path: path, method: HttpMethod.post);
}

/// The [Route.put] factory constructor is used to create a new instance of the [Route] class with the PUT method.
factory Route.put(String path) {
return Route(path: path, method: HttpMethod.put);
}

/// The [Route.delete] factory constructor is used to create a new instance of the [Route] class with the DELETE method.
factory Route.delete(String path) {
return Route(path: path, method: HttpMethod.delete);
}

/// The [Route.patch] factory constructor is used to create a new instance of the [Route] class with the PATCH method.
factory Route.patch(String path) {
return Route(path: path, method: HttpMethod.patch);
}

}
1 change: 0 additions & 1 deletion packages/serinus/test/commons/body_size_limit_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ void main() {
request.add(utf8.encode(jsonEncode({'id': 'json-obj'})));
final response = await request.close();
expect(response, isA<HttpClientResponse>());
print(await utf8.decoder.bind(response).join());
expect(response.statusCode, 413);
});
});
Expand Down
40 changes: 40 additions & 0 deletions packages/serinus/test/core/route_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import 'package:serinus/serinus.dart';
import 'package:test/test.dart';

void main() {

group('$Route', () {

test('when a route is created, then it should have the correct path and method', () {
final route = Route(path: '/test', method: HttpMethod.get);
expect(route.path, equals('/test'));
expect(route.method, equals(HttpMethod.get));
});

test('when a route is created with the GET factory constructor, then it should have the correct method', () {
final route = Route.get('/test');
expect(route.method, equals(HttpMethod.get));
});

test('when a route is created with the POST factory constructor, then it should have the correct method', () {
final route = Route.post('/test');
expect(route.method, equals(HttpMethod.post));
});

test('when a route is created with the PUT factory constructor, then it should have the correct method', () {
final route = Route.put('/test');
expect(route.method, equals(HttpMethod.put));
});

test('when a route is created with the DELETE factory constructor, then it should have the correct method', () {
final route = Route.delete('/test');
expect(route.method, equals(HttpMethod.delete));
});

test('when a route is created with the PATCH factory constructor, then it should have the correct method', () {
final route = Route.patch('/test');
expect(route.method, equals(HttpMethod.patch));
});
});

}

0 comments on commit 4404949

Please sign in to comment.