Skip to content

feat(dart_frog): expose captured request params #1818

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/dart_frog/lib/src/request.dart
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@ class Request {
);
}

/// Returns the url parameters captured by the [Router].
/// Returns an empty map if no parameters are captured.
///
/// The returned map is unmodifiable.
Copy link
Contributor

@alestiago alestiago Aug 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: the signature doesn't guarantee is unmodifiable and there doesn't seem to be any test in place to guarantee params is unmodifiable. shall we add a test or update the signature to reflect the statement?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah good call. I'll adjust -- also want to think a bit about whether we can consolidate this and mountedParams which was added a while ago.

Will make some more adjustments later today, thanks for the review!

Map<String, String> get params => _request.params;

/// Returns a [Stream] representing the body.
Stream<List<int>> bytes() => _request.read();

Expand Down
5 changes: 5 additions & 0 deletions packages/dart_frog/test/src/request_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ ${HttpMethod.values.map((m) => m.value.toUpperCase()).join(', ')}.'''),
expect(request.headers['foo'], equals(headers['foo']));
});

test('has correct params (empty)', () {
final request = Request('GET', localhost);
expect(request.params, isEmpty);
});

test('body can be read multiple times (sync)', () {
final body = json.encode({'test': 'body'});
final request = Request('GET', localhost, body: body);
Expand Down
71 changes: 70 additions & 1 deletion packages/dart_frog/test/src/router_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -291,10 +291,79 @@ void main() {
expect(response.body, equals('hello'));
});

test('request exposes captured params (empty)', () async {
final context = _MockRequestContext();
final app = Router()
..get('/hello', (RequestContext context) {
expect(context.request.params, isEmpty);
return Response(body: 'hello world');
});

server.mount((request) async {
when(() => context.request).thenReturn(
Request(request.method, request.requestedUri),
);
final response = await app(context);
final body = await response.body();
return shelf.Response(response.statusCode, body: body);
});

final response = await http.get(Uri.parse('${server.url}/hello'));
expect(response.statusCode, equals(HttpStatus.ok));
expect(response.body, equals('hello world'));
});

test('request exposes captured params (single)', () async {
final context = _MockRequestContext();
final app = Router()
..get('/users/<id>/greet', (RequestContext context, String id) {
expect(context.request.params['id'], equals(id));
return Response(body: 'hello $id');
});

server.mount((request) async {
when(() => context.request).thenReturn(
Request(request.method, request.requestedUri),
);
final response = await app(context);
final body = await response.body();
return shelf.Response(response.statusCode, body: body);
});

final response = await http.get(Uri.parse('${server.url}/users/42/greet'));
expect(response.statusCode, equals(HttpStatus.ok));
expect(response.body, equals('hello 42'));
});

test('request exposes captured params (multiple)', () async {
final context = _MockRequestContext();
final app = Router()
..get('/users/<id>/greet/<name>',
(RequestContext context, String id, String name) {
expect(context.request.params['id'], equals(id));
expect(context.request.params['name'], equals(name));
return Response(body: 'hello $name ($id)');
});

server.mount((request) async {
when(() => context.request).thenReturn(
Request(request.method, request.requestedUri),
);
final response = await app(context);
final body = await response.body();
return shelf.Response(response.statusCode, body: body);
});

final response =
await http.get(Uri.parse('${server.url}/users/42/greet/felangel'));
expect(response.statusCode, equals(HttpStatus.ok));
expect(response.body, equals('hello felangel (42)'));
});

test('mount(Router)', () async {
final context = _MockRequestContext();
final api = Router()
..all('/user/<user>/info', (RequestContext request, String user) {
..all('/user/<user>/info', (RequestContext context, String user) {
return Response(body: 'Hello $user');
});

Expand Down