@@ -13,29 +13,30 @@ import 'package:json_api/document.dart';
13
13
///
14
14
/// ```
15
15
/// @JsonSerializable()
16
- /// @DataRepository ([JSONAPIAdapter, MyJSONAPIAdapter])
16
+ /// @DataAdapter ([JSONAPIAdapter, MyJSONAPIAdapter])
17
17
/// class User with DataModel<User> {
18
18
/// @override
19
19
/// final int id;
20
20
/// final String name;
21
21
/// User({this.id, this.name});
22
22
/// }
23
23
///
24
- /// mixin MyJSONAPIAdapter on RemoteAdapter <User> {
24
+ /// mixin MyJSONAPIAdapter on Adapter <User> {
25
25
/// @override
26
26
/// String get baseUrl => "https://my.jsonapi.server/api";
27
27
///
28
28
/// // other customizations
29
29
/// }
30
30
/// ```
31
- mixin JSONAPIAdapter <T extends DataModel <T >> on RemoteAdapter <T > {
31
+ mixin JSONAPIAdapter <T extends DataModelMixin <T >> on Adapter <T > {
32
32
@override
33
33
FutureOr <Map <String , String >> get defaultHeaders async {
34
- return await super .defaultHeaders &
35
- {
36
- 'Content-Type' : 'application/vnd.api+json' ,
37
- 'Accept' : 'application/vnd.api+json' ,
38
- };
34
+ final superHeaders = await super .defaultHeaders;
35
+ return {
36
+ ...superHeaders,
37
+ 'Content-Type' : 'application/vnd.api+json' ,
38
+ 'Accept' : 'application/vnd.api+json' ,
39
+ };
39
40
}
40
41
41
42
/// Transforms native format into JSON:API
@@ -49,7 +50,7 @@ mixin JSONAPIAdapter<T extends DataModel<T>> on RemoteAdapter<T> {
49
50
// relationships
50
51
final relationships = < String , NewRelationship > {};
51
52
52
- for (final relEntry in localAdapter. relationshipMetas.entries) {
53
+ for (final relEntry in relationshipMetas.entries) {
53
54
final key = relEntry.key;
54
55
final type = _typeFor (relEntry.value.type);
55
56
@@ -68,12 +69,12 @@ mixin JSONAPIAdapter<T extends DataModel<T>> on RemoteAdapter<T> {
68
69
final id = map.remove ('id' );
69
70
70
71
// attributes
71
- final attributes = Map .fromEntries (
72
+ final attributes = Map < String , Object ?> .fromEntries (
72
73
map.entries.map ((e) => MapEntry (e.key, e.value)),
73
74
);
74
75
75
76
// assemble type, id, attributes, relationships in `Resource`
76
- final resource = NewResource (_typeFor (internalType ), id: id? .toString ());
77
+ final resource = NewResource (_typeFor (this .type ), id: id? .toString ());
77
78
resource.attributes.addAll (attributes);
78
79
resource.relationships.addAll (relationships);
79
80
@@ -87,7 +88,7 @@ mixin JSONAPIAdapter<T extends DataModel<T>> on RemoteAdapter<T> {
87
88
88
89
/// Transforms JSON:API into native format (with included resources)
89
90
@override
90
- Future < DeserializedData <T >> deserialize (Object ? data, {String ? key}) async {
91
+ DeserializedData <T > deserialize (Object ? data, {String ? key}) {
91
92
final result = DeserializedData <T >([], included: []);
92
93
final collection = < Resource > [];
93
94
@@ -102,18 +103,9 @@ mixin JSONAPIAdapter<T extends DataModel<T>> on RemoteAdapter<T> {
102
103
collection.addAll (inbound.dataAsCollection ());
103
104
}
104
105
105
- // group by type
106
- final grouped = groupBy <Resource , String >(
107
- inbound.included (), (r) => _internalTypeFor (r.type));
108
-
109
- for (final e in grouped.entries) {
110
- final internalType = e.key;
111
- if (adapters.containsKey (internalType)) {
112
- final data = await adapters[internalType]! .deserialize (e.value);
113
- result.included
114
- .addAll (List <DataModel <DataModel >>.from (data.models));
115
- }
116
- }
106
+ // for included resources, we'll just skip since we can't
107
+ // easily access other adapters in the new version
108
+ // we'll process the main collection
117
109
} catch (err, stack) {
118
110
throw DataException ('Invalid JSON:API, $err ' , stackTrace: stack);
119
111
}
@@ -148,29 +140,23 @@ mixin JSONAPIAdapter<T extends DataModel<T>> on RemoteAdapter<T> {
148
140
mapOut[attrEntry.key] = attrEntry.value;
149
141
}
150
142
151
- final data = await super .deserialize (mapOut);
143
+ final data = super .deserialize (mapOut);
152
144
result.models.addAll (data.models);
153
145
}
154
146
155
147
return result;
156
148
}
157
149
158
150
String _internalTypeFor (String type) {
159
- final adapterForType = adapters.values
160
- .where ((adapter) =>
161
- adapter.type == type ||
162
- adapter.type == DataHelpers .internalTypeFor (type))
163
- // ignore: invalid_use_of_visible_for_testing_member
164
- .safeFirst;
165
- return adapterForType? .internalType ?? DataHelpers .internalTypeFor (type);
151
+ // In v2, we don't have direct access to other adapters
152
+ // so we'll just return the type
153
+ return type.toLowerCase ();
166
154
}
167
155
168
156
String _typeFor (String internalType) {
169
- final adapterForType = adapters.values
170
- .where ((adapter) => adapter.internalType == internalType)
171
- // ignore: invalid_use_of_visible_for_testing_member
172
- .safeFirst;
173
- return adapterForType? .type ?? internalType;
157
+ // In v2, we don't have direct access to other adapters
158
+ // so we'll just return the type
159
+ return internalType;
174
160
}
175
161
}
176
162
0 commit comments