Skip to content

Commit 8fb462b

Browse files
authored
Add meta parameter for createResourceAt() (#106)
1 parent dff52b7 commit 8fb462b

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

lib/src/client/json_api_client.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ class JsonApiClient {
6666
///
6767
/// https://jsonapi.org/format/#crud-creating
6868
Future<Response<ResourceData>> createResourceAt(Uri uri, Resource resource,
69-
{Map<String, String> headers}) =>
70-
_call(_post(uri, headers, _resourceDoc(resource)), ResourceData.fromJson);
69+
{Map<String, String> headers, Map<String, Object> meta}) =>
70+
_call(_post(uri, headers, _resourceDoc(resource, meta: meta)), ResourceData.fromJson);
7171

7272
/// Deletes the resource.
7373
///
@@ -127,8 +127,8 @@ class JsonApiClient {
127127

128128
final _api = Api(version: '1.0');
129129

130-
Document<ResourceData> _resourceDoc(Resource resource) =>
131-
Document(ResourceData.fromResource(resource), api: _api);
130+
Document<ResourceData> _resourceDoc(Resource resource, {Map<String, Object> meta}) =>
131+
Document(ResourceData.fromResource(resource), meta: meta, api: _api);
132132

133133
Document<ToMany> _toManyDoc(Iterable<Identifier> identifiers) =>
134134
Document(ToMany.fromIdentifiers(identifiers), api: _api);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import 'package:json_api/client.dart';
2+
import 'package:json_api/document.dart';
3+
import 'package:json_api/http.dart';
4+
import 'package:test/test.dart';
5+
6+
import '../../helper/test_http_handler.dart';
7+
8+
void main() async {
9+
test('request actually has the meta property', () async {
10+
final handler = TestHttpHandler();
11+
final client = JsonApiClient(handler);
12+
13+
final uri = Uri.parse('https://github.com/f3ath/json-api-dart');
14+
final person =
15+
Resource('people', '123', attributes: {'name': 'Te Cheng Hung'});
16+
final meta = {'friend': 'Martin Fowler'};
17+
18+
handler.nextResponse = HttpResponse(201);
19+
await client.createResourceAt(uri, person, meta: meta);
20+
21+
final request = handler.requestLog.first;
22+
expect(request.body,
23+
'{"data":{"type":"people","id":"123","attributes":{"name":"Te Cheng Hung"}},"meta":{"friend":"Martin Fowler"}}');
24+
});
25+
}

0 commit comments

Comments
 (0)