@@ -24,26 +24,26 @@ class JsonApiClient {
2424 final api = Api ('1.0' );
2525
2626 /// Fetches a [Document] containing resource(s) from the given [url] .
27- /// Pass a [Map] of [headers] to add extra headers to the request.
2827 ///
28+ /// Pass a [Map] of [headers] to add extra headers to the request.
2929 /// More details: https://jsonapi.org/format/#fetching-resources
3030 Future <Response > fetchResource (String url,
3131 {Map <String , String > headers = const {}}) async =>
3232 Response (await _exec ((_) => _.get (_url (url), headers: _headers (headers))),
3333 preferResource: true );
3434
3535 /// Fetches a [Document] containing identifier(s) from the given [url] .
36- /// Pass a [Map] of [headers] to add extra headers to the request.
3736 ///
37+ /// Pass a [Map] of [headers] to add extra headers to the request.
3838 /// More details: https://jsonapi.org/format/#fetching-relationships
3939 Future <Response > fetchRelationship (String url,
4040 {Map <String , String > headers = const {}}) async =>
4141 Response (
4242 await _exec ((_) => _.get (_url (url), headers: _headers (headers))));
4343
4444 /// Creates a new [resource] sending a POST request to the [url] .
45- /// Pass a [Map] of [headers] to add extra headers to the request.
4645 ///
46+ /// Pass a [Map] of [headers] to add extra headers to the request.
4747 /// More details: https://jsonapi.org/format/#crud-creating
4848 Future <Response > createResource (String url, Resource resource,
4949 {Map <String , String > headers = const {}}) async =>
@@ -54,17 +54,17 @@ class JsonApiClient {
5454 preferResource: true );
5555
5656 /// Deletes the resource sending a DELETE request to the [url] .
57- /// Pass a [Map] of [headers] to add extra headers to the request.
5857 ///
58+ /// Pass a [Map] of [headers] to add extra headers to the request.
5959 /// More details: https://jsonapi.org/format/#crud-deleting
6060 Future <Response > deleteResource (String url,
6161 {Map <String , String > headers = const {}}) async =>
6262 Response (
6363 await _exec ((_) => _.delete (_url (url), headers: _headers (headers))));
6464
6565 /// Updates the [resource] sending a PATCH request to the [url] .
66- /// Pass a [Map] of [headers] to add extra headers to the request.
6766 ///
67+ /// Pass a [Map] of [headers] to add extra headers to the request.
6868 /// More details: https://jsonapi.org/format/#crud-updating
6969 Future <Response > updateResource (String url, Resource resource,
7070 {Map <String , String > headers = const {}}) async =>
@@ -76,8 +76,8 @@ class JsonApiClient {
7676
7777 /// Creates or updates a to-one relationship sending a corresponding
7878 /// [identifier] via PATCH request to the [url] .
79- /// Pass a [Map] of [headers] to add extra headers to the request.
8079 ///
80+ /// Pass a [Map] of [headers] to add extra headers to the request.
8181 /// More details: https://jsonapi.org/format/#crud-updating-to-one-relationships
8282 Future <Response > setToOne (String url, Identifier identifier,
8383 {Map <String , String > headers = const {}}) async =>
@@ -87,27 +87,58 @@ class JsonApiClient {
8787
8888 /// Removes a to-one relationship sending PATCH request with "null" data
8989 /// to the [url] .
90- /// Pass a [Map] of [headers] to add extra headers to the request.
9190 ///
91+ /// Pass a [Map] of [headers] to add extra headers to the request.
9292 /// More details: https://jsonapi.org/format/#crud-updating-to-one-relationships
9393 Future <Response > deleteToOne (String url,
9494 {Map <String , String > headers = const {}}) async =>
9595 Response (await _exec ((_) => _.patch (_url (url),
9696 body: json.encode (DataDocument .fromNull (api: api)),
9797 headers: _headers (headers, withContentType: true ))));
9898
99- /// Updates a to-many relationship sending the
99+ /// Updates (replaces!) a to-many relationship sending the
100100 /// [identifiers] via PATCH request to the [url] .
101- /// Pass a [Map] of [headers] to add extra headers to the request.
102101 ///
102+ /// This call **completely replaces** the set of relationships. E.g. when given
103+ /// an empty array, it will remove all relationships.
104+ /// Pass a [Map] of [headers] to add extra headers to the request.
103105 /// More details: https://jsonapi.org/format/#crud-updating-to-many-relationships
104- setToMany (String url, List <Identifier > identifiers,
106+ Future < Response > setToMany (String url, List <Identifier > identifiers,
105107 {Map <String , String > headers = const {}}) async =>
106108 Response (await _exec ((_) => _.patch (_url (url),
107109 body: json
108110 .encode (DataDocument .fromIdentifierList (identifiers, api: api)),
109111 headers: _headers (headers, withContentType: true ))));
110112
113+ /// Adds the [identifiers] to the to-many relationship via POST request to the [url] .
114+ ///
115+ /// The existing members of the relationships will be kept.
116+ /// Pass a [Map] of [headers] to add extra headers to the request.
117+ /// More details: https://jsonapi.org/format/#crud-updating-to-many-relationships
118+ Future <Response > addToMany (String url, List <Identifier > identifiers,
119+ {Map <String , String > headers = const {}}) async =>
120+ Response (await _exec ((_) => _.post (_url (url),
121+ body: json
122+ .encode (DataDocument .fromIdentifierList (identifiers, api: api)),
123+ headers: _headers (headers, withContentType: true ))));
124+
125+ /// Deletes the [identifiers] from the to-many relationship via DELETE request to the [url] .
126+ ///
127+ /// The other existing members of the relationships will be kept.
128+ /// Pass a [Map] of [headers] to add extra headers to the request.
129+ /// More details: https://jsonapi.org/format/#crud-updating-to-many-relationships
130+ Future <Response > deleteToMany (String url, List <Identifier > identifiers,
131+ {Map <String , String > headers = const {}}) async {
132+ // http.Client at version 0.12.0 does not support delete() with a body
133+ // So we will have to make the Request object
134+ final request = http.Request ('DELETE' , Uri .parse (_url (url)));
135+ request.body =
136+ json.encode (DataDocument .fromIdentifierList (identifiers, api: api));
137+ request.headers.addAll (_headers (headers, withContentType: true ));
138+ return Response (await _exec (
139+ (_) async => http.Response .fromStream (await _.send (request))));
140+ }
141+
111142 String _url (String url) => '${baseUrl }${url }' ;
112143
113144 Map <String , String > _headers (Map <String , String > headers,
0 commit comments