Skip to content

Commit faae2e5

Browse files
authored
v7 (#138)
1 parent e5d1aed commit faae2e5

24 files changed

+238
-175
lines changed

.github/workflows/dart.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@ jobs:
2525
- name: Tests
2626
run: dart test --coverage=.coverage -j1
2727
- name: Coverage
28-
run: dart run coverage:format_coverage -l -c -i .coverage --report-on=lib | dart run check_coverage:check_coverage 98
28+
run: dart run coverage:format_coverage -l -c -i .coverage --report-on=lib | dart run check_coverage:check_coverage

.github/workflows/publish.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
name: Publish to pub.dev
2+
3+
on:
4+
push:
5+
tags:
6+
- '[0-9]+.[0-9]+.[0-9]+*'
7+
8+
jobs:
9+
publish:
10+
permissions:
11+
id-token: write # Required for authentication using OIDC
12+
uses: dart-lang/setup-dart/.github/workflows/publish.yml@v1
13+
# with:
14+
# working-directory: path/to/package/within/repository

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [7.0.0] - 2023-11-12
8+
### Changed
9+
- Migrated to `http_interop` v1.
10+
711
## [6.0.1] - 2023-09-11
812
### Fixed
913
- `NewRelationship` was not exported
@@ -242,6 +246,7 @@ the Document model.
242246
### Added
243247
- Client: fetch resources, collections, related resources and relationships
244248

249+
[7.0.0]: https://github.com/f3ath/json-api-dart/compare/6.0.1...7.0.0
245250
[6.0.1]: https://github.com/f3ath/json-api-dart/compare/6.0.0...6.0.1
246251
[6.0.0]: https://github.com/f3ath/json-api-dart/compare/5.4.0...6.0.0
247252
[5.4.0]: https://github.com/f3ath/json-api-dart/compare/5.3.0...5.4.0

example/server.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ Future<void> main() async {
2222
ControllerRouter(controller, StandardUriDesign.matchTarget);
2323
handler = TryCatchHandler(handler,
2424
onError: ErrorConverter(onError: (e, stack) async {
25-
stderr.writeln(e);
26-
return Response(500,
27-
document: OutboundErrorDocument(
28-
[ErrorObject(title: 'Internal Server Error')]));
29-
}));
25+
stderr.writeln(e);
26+
return Response(500,
27+
document: OutboundErrorDocument(
28+
[ErrorObject(title: 'Internal Server Error')]));
29+
}).call);
3030
handler = LoggingHandler(handler,
3131
onRequest: (r) => print('${r.method} ${r.uri}'),
3232
onResponse: (r) => print('${r.statusCode}'));

example/server/cors_handler.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ class CorsHandler implements Handler {
1313
'Access-Control-Expose-Headers': ['Location'],
1414
};
1515

16-
if (request.method.equals('OPTIONS')) {
16+
if (request.method == 'options') {
1717
const methods = ['POST', 'GET', 'DELETE', 'PATCH', 'OPTIONS'];
1818
return Response(
1919
204,
20-
Body.empty(),
21-
Headers({
20+
Body(),
21+
Headers.from({
2222
...headers,
2323
'Access-Control-Allow-Methods':
2424
request.headers['Access-Control-Request-Method'] ?? methods,

lib/http.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import 'dart:convert';
2-
31
import 'package:http_interop/http_interop.dart';
42

53
class StatusCode {
@@ -29,7 +27,7 @@ class StatusCode {
2927
}
3028

3129
class Json extends Body {
32-
Json(Map<String, Object?> json) : super(jsonEncode(json), utf8);
30+
Json(Map<String, Object?> super.object) : super.json();
3331
}
3432

3533
class LoggingHandler implements Handler {

lib/src/client/client.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,17 @@ class Client {
2323
/// Sends the [request] to the given [uri].
2424
Future<Response> send(Uri uri, Request request) async {
2525
final json = await _encode(request.document);
26-
final body = http.Body(json, utf8);
27-
final headers = http.Headers({
26+
final body = http.Body.text(json, utf8);
27+
final headers = http.Headers.from({
2828
'Accept': [mediaType],
2929
if (json.isNotEmpty) 'Content-Type': [mediaType],
3030
...request.headers
3131
});
3232
final url = request.query.isEmpty
3333
? uri
3434
: uri.replace(queryParameters: request.query.toQuery());
35-
final response = await _handler
36-
.handle(http.Request(http.Method(request.method), url, body, headers));
35+
final response =
36+
await _handler.handle(http.Request(request.method, url, body, headers));
3737

3838
final document = await _decode(response);
3939
return Response(response, document);

lib/src/client/request.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@ class Request {
3131
final query = Query();
3232

3333
/// HTTP headers.
34-
final headers = Headers({});
34+
final headers = Headers();
3535
}

lib/src/document/inbound_document.dart

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,11 @@ class _Parser {
147147

148148
/// Decodes Link from [json]. Returns the decoded object.
149149
/// If the [json] has incorrect format, throws [FormatException].
150-
Link _link(Object json) {
151-
if (json is String) return Link(Uri.parse(json));
152-
if (json is Map) {
153-
return Link(Uri.parse(json['href']))..meta.addAll(meta(json));
154-
}
155-
throw FormatException('Invalid JSON');
156-
}
150+
Link _link(Object json) => switch (json) {
151+
String() => Link(Uri.parse(json)),
152+
Map() => Link(Uri.parse(json['href']))..meta.addAll(meta(json)),
153+
_ => throw FormatException('Invalid JSON')
154+
};
157155

158156
Map<String, Object?> _getAttributes(Map json) =>
159157
json.get<Map<String, Object?>>('attributes', orGet: () => {});
@@ -166,25 +164,19 @@ class _Parser {
166164
.get<Map>('relationships', orGet: () => {})
167165
.map((key, value) => MapEntry(key, newRelationship(value)));
168166

169-
Relationship _rel(data) {
170-
if (data == null) return ToOne.empty();
171-
if (data is Map) return ToOne(identifier(data));
172-
if (data is List) return ToMany(data.whereType<Map>().map(identifier));
173-
throw FormatException('Invalid relationship object');
174-
}
175-
176-
NewRelationship _newRel(data) {
177-
if (data == null) {
178-
return NewToOne.empty();
179-
}
180-
if (data is Map) {
181-
return NewToOne(newIdentifier(data));
182-
}
183-
if (data is List) {
184-
return NewToMany(data.whereType<Map>().map(newIdentifier));
185-
}
186-
throw FormatException('Invalid relationship object');
187-
}
167+
Relationship _rel(data) => switch (data) {
168+
null => ToOne.empty(),
169+
Map() => ToOne(identifier(data)),
170+
List() => ToMany(data.whereType<Map>().map(identifier)),
171+
_ => throw FormatException('Invalid relationship object')
172+
};
173+
174+
NewRelationship _newRel(data) => switch (data) {
175+
null => NewToOne.empty(),
176+
Map() => NewToOne(newIdentifier(data)),
177+
List() => NewToMany(data.whereType<Map>().map(newIdentifier)),
178+
_ => throw FormatException('Invalid relationship object')
179+
};
188180
}
189181

190182
extension _TypedGeter on Map {

lib/src/document/new_relationship.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ class NewRelationship with IterableMixin<NewIdentifier> {
1212
if (meta.isNotEmpty) 'meta': meta,
1313
};
1414

15-
// coverage:ignore-start
1615
@override
1716
Iterator<NewIdentifier> get iterator => <NewIdentifier>[].iterator;
18-
// coverage:ignore-end
1917
}

0 commit comments

Comments
 (0)