diff --git a/CHANGELOG.md b/CHANGELOG.md index 866e5ba8..b78b1bb3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +# 2.0.0-alpha.1 +* Refactor `params` to `Map`. + # 2.0.0-alpha * Depend on Dart 2. * Depend on Angel 2. diff --git a/lib/angel_client.dart b/lib/angel_client.dart index 2f89bd8f..fa1ff0bf 100644 --- a/lib/angel_client.dart +++ b/lib/angel_client.dart @@ -8,13 +8,13 @@ import 'package:http/src/response.dart' as http; export 'package:angel_http_exception/angel_http_exception.dart'; /// A function that configures an [Angel] client in some way. -typedef Future AngelConfigurer(Angel app); +typedef FutureOr AngelConfigurer(Angel app); /// A function that deserializes data received from the server. /// /// This is only really necessary in the browser, where `json_god` /// doesn't work. -typedef AngelDeserializer(x); +typedef T AngelDeserializer(x); /// Represents an Angel server that we are querying. abstract class Angel { @@ -45,7 +45,8 @@ abstract class Angel { /// Logs the current user out of the application. Future logout(); - Service service(String path, {Type type, AngelDeserializer deserializer}); + Service service(String path, + {Type type, AngelDeserializer deserializer}); Future delete(String url, {Map headers}); @@ -92,24 +93,24 @@ class AngelAuthResult { } /// Queries a service on an Angel server, with the same API. -abstract class Service { +abstract class Service { /// Fired on `indexed` events. Stream get onIndexed; /// Fired on `read` events. - Stream get onRead; + Stream get onRead; /// Fired on `created` events. - Stream get onCreated; + Stream get onCreated; /// Fired on `modified` events. - Stream get onModified; + Stream get onModified; /// Fired on `updated` events. - Stream get onUpdated; + Stream get onUpdated; /// Fired on `removed` events. - Stream get onRemoved; + Stream get onRemoved; /// The Angel instance powering this service. Angel get app; @@ -117,26 +118,26 @@ abstract class Service { Future close(); /// Retrieves all resources. - Future index([Map params]); + Future index([Map params]); /// Retrieves the desired resource. - Future read(id, [Map params]); + Future read(Id id, [Map params]); /// Creates a resource. - Future create(data, [Map params]); + Future create(Data data, [Map params]); /// Modifies a resource. - Future modify(id, data, [Map params]); + Future modify(Id id, Data data, [Map params]); /// Overwrites a resource. - Future update(id, data, [Map params]); + Future update(Id id, Data data, [Map params]); /// Removes the given resource. - Future remove(id, [Map params]); + Future remove(Id id, [Map params]); } /// A [List] that automatically updates itself whenever the referenced [service] fires an event. -class ServiceList extends DelegatingList { +class ServiceList extends DelegatingList { /// A field name used to compare [Map] by ID. final String idField; @@ -150,9 +151,10 @@ class ServiceList extends DelegatingList { /// Defaults to comparing the [idField] of `Map` instances. final Equality _compare; - final Service service; + final Service service; - final StreamController _onChange = new StreamController(); + final StreamController> _onChange = + new StreamController(); final List _subs = []; ServiceList(this.service, @@ -175,7 +177,7 @@ class ServiceList extends DelegatingList { })); // Modified/Updated - handleModified(item) { + handleModified(Data item) { var indices = []; for (int i = 0; i < length; i++) { @@ -202,7 +204,7 @@ class ServiceList extends DelegatingList { } /// Fires whenever the underlying [service] fires a change event. - Stream get onChange => _onChange.stream; + Stream> get onChange => _onChange.stream; Future close() async { _onChange.close(); diff --git a/lib/base_angel_client.dart b/lib/base_angel_client.dart index 96fb0308..13f7f79b 100644 --- a/lib/base_angel_client.dart +++ b/lib/base_angel_client.dart @@ -16,7 +16,7 @@ const Map _writeHeaders = const { 'Content-Type': 'application/json' }; -_buildQuery(Map params) { +_buildQuery(Map params) { if (params == null || params.isEmpty || params['query'] is! Map) return ""; List query = []; @@ -191,9 +191,10 @@ abstract class BaseAngelClient extends Angel { } @override - Service service(String path, {Type type, AngelDeserializer deserializer}) { + Service service(String path, + {Type type, AngelDeserializer deserializer}) { String uri = path.toString().replaceAll(straySlashes, ""); - var s = new BaseAngelService(client, this, '$basePath/$uri', + var s = new BaseAngelService(client, this, '$basePath/$uri', deserializer: deserializer); _services.add(s); return s; @@ -240,15 +241,15 @@ abstract class BaseAngelClient extends Angel { } } -class BaseAngelService extends Service { +class BaseAngelService extends Service { @override final BaseAngelClient app; final String basePath; final http.BaseClient client; - final AngelDeserializer deserializer; + final AngelDeserializer deserializer; - final StreamController _onIndexed = new StreamController(), - _onRead = new StreamController(), + final StreamController _onIndexed = new StreamController(); + final StreamController _onRead = new StreamController(), _onCreated = new StreamController(), _onModified = new StreamController(), _onUpdated = new StreamController(), @@ -258,19 +259,19 @@ class BaseAngelService extends Service { Stream get onIndexed => _onIndexed.stream; @override - Stream get onRead => _onRead.stream; + Stream get onRead => _onRead.stream; @override - Stream get onCreated => _onCreated.stream; + Stream get onCreated => _onCreated.stream; @override - Stream get onModified => _onModified.stream; + Stream get onModified => _onModified.stream; @override - Stream get onUpdated => _onUpdated.stream; + Stream get onUpdated => _onUpdated.stream; @override - Stream get onRemoved => _onRemoved.stream; + Stream get onRemoved => _onRemoved.stream; @override Future close() async { @@ -284,8 +285,8 @@ class BaseAngelService extends Service { BaseAngelService(this.client, this.app, this.basePath, {this.deserializer}); - deserialize(x) { - return deserializer != null ? deserializer(x) : x; + Data deserialize(x) { + return deserializer != null ? deserializer(x) : x as Data; } makeBody(x) { @@ -301,7 +302,7 @@ class BaseAngelService extends Service { } @override - Future index([Map params]) async { + Future index([Map params]) async { final response = await app.sendUnstreamed( 'GET', '$basePath${_buildQuery(params)}', _readHeaders); @@ -316,7 +317,7 @@ class BaseAngelService extends Service { final v = json.decode(response.body); if (v is! List) { - _onIndexed.add(v); + _onIndexed.add(v as Data); return v; } @@ -332,7 +333,7 @@ class BaseAngelService extends Service { } @override - Future read(id, [Map params]) async { + Future read(id, [Map params]) async { final response = await app.sendUnstreamed( 'GET', '$basePath/$id${_buildQuery(params)}', _readHeaders); @@ -356,7 +357,7 @@ class BaseAngelService extends Service { } @override - Future create(data, [Map params]) async { + Future create(data, [Map params]) async { final response = await app.sendUnstreamed('POST', '$basePath/${_buildQuery(params)}', _writeHeaders, makeBody(data)); @@ -380,7 +381,7 @@ class BaseAngelService extends Service { } @override - Future modify(id, data, [Map params]) async { + Future modify(id, data, [Map params]) async { final response = await app.sendUnstreamed('PATCH', '$basePath/$id${_buildQuery(params)}', _writeHeaders, makeBody(data)); @@ -404,7 +405,7 @@ class BaseAngelService extends Service { } @override - Future update(id, data, [Map params]) async { + Future update(id, data, [Map params]) async { final response = await app.sendUnstreamed('POST', '$basePath/$id${_buildQuery(params)}', _writeHeaders, makeBody(data)); @@ -428,7 +429,7 @@ class BaseAngelService extends Service { } @override - Future remove(id, [Map params]) async { + Future remove(id, [Map params]) async { final response = await app.sendUnstreamed( 'DELETE', '$basePath/$id${_buildQuery(params)}', _readHeaders); diff --git a/lib/io.dart b/lib/io.dart index 538d9fe6..cb50df37 100644 --- a/lib/io.dart +++ b/lib/io.dart @@ -15,9 +15,10 @@ class Rest extends BaseAngelClient { Rest(String path) : super(new http.Client() as http.BaseClient, path); @override - Service service(String path, {Type type, AngelDeserializer deserializer}) { + Service service(String path, + {Type type, AngelDeserializer deserializer}) { String uri = path.replaceAll(straySlashes, ""); - var s = new RestService(client, this, "$basePath/$uri", type); + var s = new RestService(client, this, "$basePath/$uri", type); _services.add(s); return s; } @@ -37,7 +38,7 @@ class Rest extends BaseAngelClient { } /// Queries an Angel service via REST. -class RestService extends BaseAngelService { +class RestService extends BaseAngelService { final Type type; RestService( @@ -45,14 +46,14 @@ class RestService extends BaseAngelService { : super(client, app, url); @override - deserialize(x) { + Data deserialize(x) { if (type != null) { return x.runtimeType == type - ? x - : god.deserializeDatum(x, outputType: type); + ? x as Data + : god.deserializeDatum(x, outputType: type) as Data; } - return x; + return x as Data; } @override diff --git a/pubspec.yaml b/pubspec.yaml index 2870cf10..dbab4f54 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,5 +1,5 @@ name: angel_client -version: 2.0.0-alpha +version: 2.0.0-alpha.1 description: Client library for the Angel framework. author: Tobe O homepage: https://github.com/angel-dart/angel_client