Skip to content

Commit e16decc

Browse files
committed
alpha.5
1 parent 13a14df commit e16decc

File tree

7 files changed

+56
-30
lines changed

7 files changed

+56
-30
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ 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

77
## [Unreleased]
8+
### Added
9+
- Automatically encode DateTime objects as an ISO-8601 string
10+
811
### Changed
912
- URL Design matching now respects the base URL
1013
- Allo null to be returned by error interceptors

lib/src/client/payload_codec.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import 'dart:async';
22
import 'dart:convert';
33

4+
import 'package:json_api/src/document/document_encoder.dart';
5+
46
/// Encodes/decodes JSON payload.
57
///
68
/// The methods are designed to be asynchronous to allow for conversion to be
@@ -16,5 +18,6 @@ class PayloadCodec {
1618
}
1719

1820
/// Encodes a JSON:API document into a JSON string.
19-
FutureOr<String> encode(Object document) => jsonEncode(document);
21+
FutureOr<String> encode(Object document) =>
22+
jsonEncode(document, toEncodable: documentEncoder);
2023
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import 'package:json_api/document.dart';
2+
3+
Object? documentEncoder(Object? v) => switch (v) {
4+
OutboundDocument() => v.toJson(),
5+
DateTime() => v.toIso8601String(),
6+
_ => throw UnsupportedError('Cannot convert to JSON: $v'),
7+
};

lib/src/server/controller.dart

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,36 @@
1-
import 'package:http_interop/http_interop.dart' as http;
1+
import 'package:http_interop/http_interop.dart';
22
import 'package:json_api/routing.dart';
33

44
/// JSON:API controller
55
abstract class Controller {
66
/// Fetch a primary resource collection
7-
Future<http.Response> fetchCollection(http.Request request, Target target);
7+
Future<Response> fetchCollection(Request request, Target target);
88

99
/// Create resource
10-
Future<http.Response> createResource(http.Request request, Target target);
10+
Future<Response> createResource(Request request, Target target);
1111

1212
/// Fetch a single primary resource
13-
Future<http.Response> fetchResource(
14-
http.Request request, ResourceTarget target);
13+
Future<Response> fetchResource(Request request, ResourceTarget target);
1514

1615
/// Updates a primary resource
17-
Future<http.Response> updateResource(
18-
http.Request request, ResourceTarget target);
16+
Future<Response> updateResource(Request request, ResourceTarget target);
1917

2018
/// Deletes the primary resource
21-
Future<http.Response> deleteResource(
22-
http.Request request, ResourceTarget target);
19+
Future<Response> deleteResource(Request request, ResourceTarget target);
2320

2421
/// Fetches a relationship
25-
Future<http.Response> fetchRelationship(
26-
http.Request rq, RelationshipTarget target);
22+
Future<Response> fetchRelationship(Request rq, RelationshipTarget target);
2723

2824
/// Add new entries to a to-many relationship
29-
Future<http.Response> addMany(
30-
http.Request request, RelationshipTarget target);
25+
Future<Response> addMany(Request request, RelationshipTarget target);
3126

3227
/// Updates the relationship
33-
Future<http.Response> replaceRelationship(
34-
http.Request request, RelationshipTarget target);
28+
Future<Response> replaceRelationship(
29+
Request request, RelationshipTarget target);
3530

3631
/// Deletes the members from the to-many relationship
37-
Future<http.Response> deleteMany(
38-
http.Request request, RelationshipTarget target);
32+
Future<Response> deleteMany(Request request, RelationshipTarget target);
3933

4034
/// Fetches related resource or collection
41-
Future<http.Response> fetchRelated(
42-
http.Request request, RelatedTarget target);
35+
Future<Response> fetchRelated(Request request, RelatedTarget target);
4336
}

lib/src/server/response.dart

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
import 'package:http_interop/http_interop.dart';
22
import 'package:json_api/document.dart';
33
import 'package:json_api/http.dart';
4+
import 'package:json_api/src/document/document_encoder.dart';
45
import 'package:json_api/src/media_type.dart';
56

67
/// JSON:API response
7-
Response response(int statusCode, {OutboundDocument? document}) {
8-
final r = Response(
9-
statusCode, document != null ? Body.json(document) : Body(), Headers());
10-
if (document != null) {
11-
r.headers['Content-Type'] = [mediaType];
12-
}
13-
return r;
14-
}
8+
Response response(int statusCode, {OutboundDocument? document}) => Response(
9+
statusCode,
10+
document != null
11+
? Body.json(document, toEncodable: documentEncoder)
12+
: Body(),
13+
Headers())
14+
..headers.addAll({
15+
if (document != null) 'Content-Type': [mediaType]
16+
});
1517

1618
Response ok(OutboundDocument document) =>
1719
response(StatusCode.ok, document: document);

pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
name: json_api
2-
version: 9.0.0-alpha.4
2+
version: 9.0.0-alpha.5
33
homepage: https://github.com/f3ath/json-api-dart
44
description: A framework-agnostic implementations of JSON:API Client and Server. Supports JSON:API v1.0 (https://jsonapi.org)
55
environment:
66
sdk: '>=3.5.0 <4.0.0'
77

88
dependencies:
99
http_parser: ^4.0.0
10-
http_interop: ^2.0.0
10+
http_interop: ^2.1.0
1111
http_interop_middleware: ^0.1.0
1212

1313
dev_dependencies:

test/unit/server/response_test.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import 'package:http_interop/extensions.dart';
2+
import 'package:json_api/document.dart';
3+
import 'package:json_api/server.dart';
4+
import 'package:test/test.dart';
5+
6+
void main() {
7+
group('Response', () {
8+
test('converts DateTime to ISO-8601', () async {
9+
final r = response(200,
10+
document: OutboundDocument()..meta['date'] = DateTime(2021));
11+
expect(
12+
await r.body.decodeJson(),
13+
equals({
14+
'meta': {'date': '2021-01-01T00:00:00.000'}
15+
}));
16+
});
17+
});
18+
}

0 commit comments

Comments
 (0)