Skip to content

Commit 6c2fa1a

Browse files
authored
Release 0.6 (#38)
1 parent 4483d00 commit 6c2fa1a

File tree

7 files changed

+96
-19
lines changed

7 files changed

+96
-19
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
## [0.6.0] - 2019-03-25
10+
### Changed
11+
- JSON:API Document moved out
12+
- Renamed `client.removeToOne` to `client.deleteToOne`
13+
914
## [0.5.0] - 2019-03-21
1015
### Changed
1116
- More BC-breaking changes in the Server
@@ -47,7 +52,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
4752
### Added
4853
- Client: fetch resources, collections, related resources and relationships
4954

50-
[Unreleased]: https://github.com/f3ath/json-api-dart/compare/0.5.0...HEAD
55+
[Unreleased]: https://github.com/f3ath/json-api-dart/compare/0.6.0...HEAD
56+
[0.6.0]: https://github.com/f3ath/json-api-dart/compare/0.5.0...0.6.0
5157
[0.5.0]: https://github.com/f3ath/json-api-dart/compare/0.4.0...0.5.0
5258
[0.4.0]: https://github.com/f3ath/json-api-dart/compare/0.3.0...0.4.0
5359
[0.3.0]: https://github.com/f3ath/json-api-dart/compare/0.2.0...0.3.0

README.md

Lines changed: 62 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,83 @@
22
[JSON:API](http://jsonapi.org) is a specification for building APIs in JSON. This library implements
33
a Client (VM, Flutter, Web), and a Server (VM only).
44

5-
## Supported features
6-
- Fetching single resources and resource collections
5+
## Features
6+
- Fetching single resources, resource collections, related resources
7+
- Fetching/updating relationships
8+
- Creating/updating/deleting resources
79
- Collection pagination
8-
- Fetching relationships and related resources and collections
9-
- Fetching single resources
10-
- Creating resources
11-
- Deleting resources
12-
- Updating resource's attributes
13-
- Updating resource's relationships
14-
- Updating relationships
1510
- Compound documents
16-
- Related collection pagination
1711
- Asynchronous processing
1812

1913
## Usage
20-
In the VM:
14+
### Creating a client instance
15+
JSON:API Client uses the Dart's native HttpClient. Depending on the platform,
16+
you may want to use either the one which comes from `dart:io` or the `BrowserClient`.
17+
18+
In the VM/Flutter you don't need to provide any dependencies:
2119
```dart
2220
import 'package:json_api/client.dart';
2321
2422
final client = JsonApiClient();
2523
```
2624

27-
In a browser:
25+
In a browser use the `BrowserClient`:
2826
```dart
2927
import 'package:json_api/client.dart';
3028
import 'package:http/browser_client.dart';
3129
3230
final client = JsonApiClient(factory: () => BrowserClient());
3331
```
3432

35-
For usage examples see the [functional tests](https://github.com/f3ath/json-api-dart/tree/master/test/functional).
33+
### Making requests
34+
The client provides a set of methods to manipulate resources and relationships.
35+
- Fetching
36+
- `fetchCollection` - resource collection, either primary or related
37+
- `fetchResource` - a single resource, either primary or related
38+
- `fetchRelationship` - a generic relationship (either to-one, to-many or even incomplete)
39+
- `fetchToOne` - a to-one relationship
40+
- `fetchToMany` - a to-many relationship
41+
- Manipulating resources
42+
- `createResource` - creates a new primary resource
43+
- `updateResource` - updates the existing resource by its type and id
44+
- `deleteResource` - deletes the existing resource
45+
- Manipulating relationships
46+
- `replaceToOne` - replaces the existing to-one relationship with a new resource identifier
47+
- `deleteToOne` - deletes the existing to-one relationship by setting the resrouce identifier to null
48+
- `replaceToMany` - replaces the existing to-many relationship with the given set of resource identifiers
49+
- `addToMany` - adds the given identifiers to the existing to-many relationship
50+
51+
These methods accept the target URI and the object to update (except for fetch and delete requests).
52+
You can also pass an optional map of HTTP headers e.g. for authentication. The return value
53+
is `Response` object bearing the HTTP response status and headers and the JSON:API
54+
document with the primary data according to the type of the request.
55+
56+
Here's a collection fetching example:
57+
58+
```dart
59+
import 'package:json_api/client.dart';
60+
61+
void main() async {
62+
final client = JsonApiClient();
63+
final companiesUri = Uri.parse('http://localhost:8080/companies');
64+
final response = await client.fetchCollection(companiesUri);
65+
66+
print('Status: ${response.status}');
67+
print('Headers: ${response.headers}');
68+
69+
print('The collection page size is ${response.data.collection.length}');
70+
71+
final resource = response.data.collection.first.toResource();
72+
print('The first element is ${resource}');
73+
74+
print('Attributes:');
75+
resource.attributes.forEach((k, v) => print('$k=$v'));
76+
77+
print('Relationships:');
78+
resource.toOne.forEach((k, v) => print('$k=$v'));
79+
resource.toMany.forEach((k, v) => print('$k=$v'));
80+
}
81+
```
82+
83+
84+
For more usage examples refer to the [functional tests](https://github.com/f3ath/json-api-dart/tree/master/test/functional).

example/README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,9 @@ You can run it locally to play around.
77
- In you console run `dart example/cars_server.dart`, this will start the server at port 8080.
88
- Open http://localhost:8080/companies in the browser.
99

10-
## [Cars Client](./cars_client.dart)
11-
A simple client for Cars Server API. It is also used in the tests.
10+
## [Fetch example](./fetch_collection.dart)
11+
With the server running, call
12+
```
13+
dart example/fetch_collection.dart
14+
```
15+
This will make a `fetchCollection()` call and print the response.

example/fetch_collection.dart

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import 'package:json_api/client.dart';
2+
3+
void main() async {
4+
final client = JsonApiClient();
5+
final companiesUri = Uri.parse('http://localhost:8080/companies');
6+
final response = await client.fetchCollection(companiesUri);
7+
print('Status: ${response.status}');
8+
print('Headers: ${response.headers}');
9+
10+
final resource = response.data.collection.first.toResource();
11+
12+
print('The collection page size is ${response.data.collection.length}');
13+
print('The first element is ${resource}');
14+
print('Attributes:');
15+
resource.attributes.forEach((k, v) => print('$k=$v'));
16+
print('Relationships:');
17+
resource.toOne.forEach((k, v) => print('$k=$v'));
18+
resource.toMany.forEach((k, v) => print('$k=$v'));
19+
}

lib/src/client/client.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ class JsonApiClient {
8888

8989
/// Removes a to-one relationship. This is equivalent to calling [replaceToOne]
9090
/// with id = null.
91-
Future<Response<ToOne>> removeToOne(Uri uri, {Map<String, String> headers}) =>
91+
Future<Response<ToOne>> deleteToOne(Uri uri, {Map<String, String> headers}) =>
9292
replaceToOne(uri, null, headers: headers);
9393

9494
/// Replaces a to-many relationship with the given set of [ids].

test/browser_compat_test.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ void main() async {
88
final channel = spawnHybridUri('test_server.dart');
99
final client = JsonApiClient(factory: () => BrowserClient());
1010
final port = await channel.stream.first;
11-
print('Port: $port');
1211
final r = await client
1312
.fetchCollection(Uri.parse('http://localhost:$port/companies'));
1413
expect(r.status, 200);

test/functional/update_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ void main() async {
183183
final original = r0.document.data.toIdentifier();
184184
expect(original.id, '2');
185185

186-
final r1 = await client.removeToOne(url);
186+
final r1 = await client.deleteToOne(url);
187187
expect(r1.status, 204);
188188

189189
final r2 = await client.fetchToOne(url);

0 commit comments

Comments
 (0)