Skip to content

Commit

Permalink
Replace copyWith method with withUrl fluent API
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardoboss committed Feb 12, 2024
1 parent a4ded58 commit 214e980
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 32 deletions.
26 changes: 3 additions & 23 deletions lib/src/base_request_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,7 @@ abstract class BaseRequestBuilder<T extends BaseRequestBuilder<T>> {
/// Clones the current request builder.
T clone();

/// Clones the current request builder using [clone] and applies the provided
/// parameters.
T copyWith({
RequestAdapter? requestAdapter,
String? urlTemplate,
Map<String, dynamic>? pathParameters,
}) {
final clone = this.clone();

if (requestAdapter != null) {
clone.requestAdapter = requestAdapter;
}

if (urlTemplate != null) {
clone.urlTemplate = urlTemplate;
}

if (pathParameters != null) {
clone.pathParameters = pathParameters;
}

return clone;
}
/// Clones the current request builder using [clone] and sets the given
/// [template] as the url template.
T withUrl(String template) => this.clone()..urlTemplate = template;
}
13 changes: 4 additions & 9 deletions test/request_builder_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,15 @@ void main() {
{'id': '1'},
);

final anotherRequestAdapter = MockRequestAdapter();

final newRequestBuilder = requestBuilder.copyWith(
requestAdapter: anotherRequestAdapter,
urlTemplate: 'https://graph.microsoft.com/v2.0/users/{id}',
pathParameters: {'id': '2'},
);
final newRequestBuilder =
requestBuilder.withUrl('https://graph.microsoft.com/v2.0/users/{id}');

expect(newRequestBuilder.id, equals(1));
expect(newRequestBuilder.requestAdapter, equals(anotherRequestAdapter));
expect(newRequestBuilder.requestAdapter, equals(mockRequestAdapter));
expect(
newRequestBuilder.urlTemplate,
equals('https://graph.microsoft.com/v2.0/users/{id}'),
);
expect(newRequestBuilder.pathParameters, equals({'id': '2'}));
expect(newRequestBuilder.pathParameters, equals({'id': '1'}));
});
}

0 comments on commit 214e980

Please sign in to comment.