Skip to content

Commit a07ef8e

Browse files
committed
Add rawResponse
1 parent a1b2f09 commit a07ef8e

9 files changed

+236
-207
lines changed

lib/src/client/response/collection_fetched.dart

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
1-
import 'package:http_interop/http_interop.dart';
1+
import 'package:http_interop/http_interop.dart' as i;
22
import 'package:json_api/document.dart';
3+
import 'package:json_api/src/client/response.dart';
34

45
class CollectionFetched {
5-
CollectionFetched(this.httpResponse, Map json) {
6-
final document = InboundDocument(json);
6+
CollectionFetched(this.rawResponse) {
7+
final document = InboundDocument(rawResponse.document ??
8+
(throw ArgumentError('The document must not be empty')));
79
collection.addAll(document.dataAsCollection());
810
included.addAll(document.included());
911
meta.addAll(document.meta());
1012
links.addAll(document.links());
1113
}
1214

13-
final Response httpResponse;
15+
/// The raw HTTP response
16+
@Deprecated('Use rawResponse.httpResponse instead')
17+
i.Response get httpResponse => rawResponse.httpResponse;
18+
19+
/// The raw JSON:API response
20+
final Response rawResponse;
1421

1522
/// The resource collection fetched from the server
1623
final collection = <Resource>[];

lib/src/client/response/related_resource_fetched.dart

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,29 @@
1-
import 'package:http_interop/http_interop.dart';
1+
import 'package:http_interop/http_interop.dart' as i;
22
import 'package:json_api/document.dart';
3+
import 'package:json_api/src/client/response.dart';
34

45
/// A related resource response.
56
///
67
/// https://jsonapi.org/format/#fetching-resources-responses
78
class RelatedResourceFetched {
8-
RelatedResourceFetched(this.httpResponse, Map json)
9-
: resource = InboundDocument(json).dataAsResourceOrNull() {
10-
final document = InboundDocument(json);
9+
RelatedResourceFetched(this.rawResponse) {
10+
final document = InboundDocument(rawResponse.document ??
11+
(throw ArgumentError('The document must not be empty')));
12+
resource = document.dataAsResourceOrNull();
1113
included.addAll(document.included());
1214
meta.addAll(document.meta());
1315
links.addAll(document.links());
1416
}
1517

16-
final Response httpResponse;
18+
/// The raw HTTP response
19+
@Deprecated('Use rawResponse.httpResponse instead')
20+
i.Response get httpResponse => rawResponse.httpResponse;
21+
22+
/// The raw JSON:API response
23+
final Response rawResponse;
1724

1825
/// Related resource. May be null
19-
final Resource? resource;
26+
late final Resource? resource;
2027

2128
/// Included resources
2229
final included = <Resource>[];
Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,32 @@
1-
import 'package:http_interop/http_interop.dart';
1+
import 'package:http_interop/http_interop.dart' as i;
22
import 'package:json_api/document.dart';
3+
import 'package:json_api/src/client/response.dart';
34

45
/// A response to a relationship fetch request.
56
class RelationshipFetched<R extends Relationship> {
6-
RelationshipFetched(this.httpResponse, this.relationship);
7+
RelationshipFetched(this.rawResponse, this.relationship);
78

8-
static RelationshipFetched<ToMany> many(Response httpResponse, Map json) =>
9-
RelationshipFetched(httpResponse, InboundDocument(json).asToMany())
10-
..included.addAll(InboundDocument(json).included());
9+
static RelationshipFetched<ToMany> many(Response response) {
10+
final document = InboundDocument(response.document ??
11+
(throw ArgumentError('The document must not be empty')));
12+
return RelationshipFetched(response, document.asToMany())
13+
..included.addAll(document.included());
14+
}
1115

12-
static RelationshipFetched<ToOne> one(Response httpResponse, Map json) =>
13-
RelationshipFetched(httpResponse, InboundDocument(json).asToOne())
14-
..included.addAll(InboundDocument(json).included());
16+
static RelationshipFetched<ToOne> one(Response response) {
17+
final document = InboundDocument(response.document ??
18+
(throw ArgumentError('The document must not be empty')));
19+
return RelationshipFetched(response, document.asToOne())
20+
..included.addAll(document.included());
21+
}
22+
23+
/// The raw HTTP response
24+
@Deprecated('Use rawResponse.httpResponse instead')
25+
i.Response get httpResponse => rawResponse.httpResponse;
26+
27+
/// The raw JSON:API response
28+
final Response rawResponse;
1529

16-
final Response httpResponse;
1730
final R relationship;
1831
final included = <Resource>[];
1932
}

lib/src/client/response/relationship_updated.dart

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,29 @@
1-
import 'package:http_interop/http_interop.dart';
1+
import 'package:http_interop/http_interop.dart' as i;
22
import 'package:json_api/document.dart';
3+
import 'package:json_api/src/client/response.dart';
34

45
/// A response to a relationship request.
56
class RelationshipUpdated<R extends Relationship> {
6-
RelationshipUpdated(this.httpResponse, this.relationship);
7+
RelationshipUpdated(this.rawResponse, this.relationship);
78

8-
static RelationshipUpdated<ToMany> many(Response httpResponse, Map? json) =>
9-
RelationshipUpdated(
10-
httpResponse, json == null ? null : InboundDocument(json).asToMany());
9+
static RelationshipUpdated<ToMany> many(Response response) {
10+
final json = response.document;
11+
return RelationshipUpdated(
12+
response, json == null ? null : InboundDocument(json).asToMany());
13+
}
1114

12-
static RelationshipUpdated<ToOne> one(Response httpResponse, Map? json) =>
13-
RelationshipUpdated(
14-
httpResponse, json == null ? null : InboundDocument(json).asToOne());
15+
static RelationshipUpdated<ToOne> one(Response response) {
16+
final json = response.document;
17+
return RelationshipUpdated(
18+
response, json == null ? null : InboundDocument(json).asToOne());
19+
}
1520

16-
final Response httpResponse;
21+
/// The raw HTTP response
22+
@Deprecated('Use rawResponse.httpResponse instead')
23+
i.Response get httpResponse => rawResponse.httpResponse;
24+
25+
/// The raw JSON:API response
26+
final Response rawResponse;
1727

1828
/// Updated relationship. Null if "204 No Content" is returned.
1929
final R? relationship;

lib/src/client/response/request_failure.dart

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
1-
import 'package:http_interop/http_interop.dart';
1+
import 'package:http_interop/http_interop.dart' as i;
22
import 'package:json_api/document.dart';
3+
import 'package:json_api/src/client/response.dart';
34

45
/// Thrown when the server returns a non-successful response.
56
class RequestFailure implements Exception {
6-
RequestFailure(this.httpResponse, Map? document) {
7-
if (document != null) {
8-
errors.addAll(InboundDocument(document).errors());
9-
meta.addAll(InboundDocument(document).meta());
10-
}
7+
RequestFailure(this.rawResponse) {
8+
final json = rawResponse.document;
9+
if (json == null) return;
10+
final document = InboundDocument(json);
11+
errors.addAll(document.errors());
12+
meta.addAll(document.meta());
1113
}
1214

13-
final Response httpResponse;
15+
/// The raw HTTP response
16+
@Deprecated('Use rawResponse.httpResponse instead')
17+
i.Response get httpResponse => rawResponse.httpResponse;
18+
19+
/// The raw JSON:API response
20+
final Response rawResponse;
1421

1522
/// Error objects returned by the server
1623
final errors = <ErrorObject>[];

lib/src/client/response/resource_created.dart

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,30 @@
1-
import 'package:http_interop/http_interop.dart';
1+
import 'package:http_interop/http_interop.dart' as i;
22
import 'package:json_api/document.dart';
3+
import 'package:json_api/src/client/response.dart';
34

45
/// A response to a new resource creation request.
56
/// This is always a "201 Created" response.
67
///
78
/// https://jsonapi.org/format/#crud-creating-responses-201
89
class ResourceCreated {
9-
ResourceCreated(this.httpResponse, Map json)
10-
: resource = InboundDocument(json).dataAsResource() {
11-
meta.addAll(InboundDocument(json).meta());
12-
links.addAll(InboundDocument(json).links());
13-
included.addAll(InboundDocument(json).included());
10+
ResourceCreated(this.rawResponse) {
11+
final document = InboundDocument(rawResponse.document ??
12+
(throw ArgumentError('The document must not be empty')));
13+
resource = document.dataAsResource();
14+
included.addAll(document.included());
15+
meta.addAll(document.meta());
16+
links.addAll(document.links());
1417
}
1518

16-
final Response httpResponse;
19+
/// The raw HTTP response
20+
@Deprecated('Use rawResponse.httpResponse instead')
21+
i.Response get httpResponse => rawResponse.httpResponse;
22+
23+
/// The raw JSON:API response
24+
final Response rawResponse;
1725

1826
/// Created resource.
19-
final Resource resource;
27+
late final Resource resource;
2028

2129
/// Top-level meta data
2230
final meta = <String, Object?>{};

lib/src/client/response/resource_fetched.dart

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,26 @@
1-
import 'package:http_interop/http_interop.dart';
1+
import 'package:http_interop/http_interop.dart' as i;
22
import 'package:json_api/document.dart';
3+
import 'package:json_api/src/client/response.dart';
34

45
/// A response to fetch a primary resource request
56
class ResourceFetched {
6-
ResourceFetched(this.httpResponse, Map json)
7-
: resource = InboundDocument(json).dataAsResource() {
8-
included.addAll(InboundDocument(json).included());
9-
meta.addAll(InboundDocument(json).meta());
10-
links.addAll(InboundDocument(json).links());
7+
ResourceFetched(this.rawResponse) {
8+
final document = InboundDocument(rawResponse.document ??
9+
(throw ArgumentError('The document must not be empty')));
10+
resource = document.dataAsResource();
11+
included.addAll(document.included());
12+
meta.addAll(document.meta());
13+
links.addAll(document.links());
1114
}
1215

13-
final Response httpResponse;
14-
final Resource resource;
16+
/// The raw HTTP response
17+
@Deprecated('Use rawResponse.httpResponse instead')
18+
i.Response get httpResponse => rawResponse.httpResponse;
19+
20+
/// The raw JSON:API response
21+
final Response rawResponse;
22+
23+
late final Resource resource;
1524

1625
/// Top-level meta data
1726
final meta = <String, Object?>{};

lib/src/client/response/resource_updated.dart

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
import 'package:http_interop/http_interop.dart';
1+
import 'package:http_interop/http_interop.dart' as i;
22
import 'package:json_api/document.dart';
3+
import 'package:json_api/src/client/response.dart';
34

45
class ResourceUpdated {
5-
ResourceUpdated(this.httpResponse, Map? json) : resource = _resource(json) {
6-
if (json != null) {
7-
included.addAll(InboundDocument(json).included());
8-
meta.addAll(InboundDocument(json).meta());
9-
links.addAll(InboundDocument(json).links());
6+
ResourceUpdated(this.rawResponse)
7+
: resource = _resource(rawResponse.document) {
8+
final document = rawResponse.document;
9+
if (document != null) {
10+
included.addAll(InboundDocument(document).included());
11+
meta.addAll(InboundDocument(document).meta());
12+
links.addAll(InboundDocument(document).links());
1013
}
1114
}
1215

@@ -20,7 +23,12 @@ class ResourceUpdated {
2023
return null;
2124
}
2225

23-
final Response httpResponse;
26+
/// The raw HTTP response
27+
@Deprecated('Use rawResponse.httpResponse instead')
28+
i.Response get httpResponse => rawResponse.httpResponse;
29+
30+
/// The raw JSON:API response
31+
final Response rawResponse;
2432

2533
/// The created resource. Null for "204 No Content" responses.
2634
late final Resource? resource;

0 commit comments

Comments
 (0)