Refactor params to Map<String, dynamic>.

This commit is contained in:
Tobe O 2018-10-02 11:42:26 -04:00
parent ddd5271d1f
commit 8b67f8d4bb
5 changed files with 56 additions and 49 deletions

View file

@ -1,3 +1,6 @@
# 2.0.0-alpha.1
* Refactor `params` to `Map<String, dynamic>`.
# 2.0.0-alpha # 2.0.0-alpha
* Depend on Dart 2. * Depend on Dart 2.
* Depend on Angel 2. * Depend on Angel 2.

View file

@ -8,13 +8,13 @@ import 'package:http/src/response.dart' as http;
export 'package:angel_http_exception/angel_http_exception.dart'; export 'package:angel_http_exception/angel_http_exception.dart';
/// A function that configures an [Angel] client in some way. /// 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. /// A function that deserializes data received from the server.
/// ///
/// This is only really necessary in the browser, where `json_god` /// This is only really necessary in the browser, where `json_god`
/// doesn't work. /// doesn't work.
typedef AngelDeserializer(x); typedef T AngelDeserializer<T>(x);
/// Represents an Angel server that we are querying. /// Represents an Angel server that we are querying.
abstract class Angel { abstract class Angel {
@ -45,7 +45,8 @@ abstract class Angel {
/// Logs the current user out of the application. /// Logs the current user out of the application.
Future logout(); Future logout();
Service service(String path, {Type type, AngelDeserializer deserializer}); Service<Id, Data> service<Id, Data>(String path,
{Type type, AngelDeserializer<Data> deserializer});
Future<http.Response> delete(String url, {Map<String, String> headers}); Future<http.Response> delete(String url, {Map<String, String> headers});
@ -92,24 +93,24 @@ class AngelAuthResult {
} }
/// Queries a service on an Angel server, with the same API. /// Queries a service on an Angel server, with the same API.
abstract class Service { abstract class Service<Id, Data> {
/// Fired on `indexed` events. /// Fired on `indexed` events.
Stream get onIndexed; Stream get onIndexed;
/// Fired on `read` events. /// Fired on `read` events.
Stream get onRead; Stream<Data> get onRead;
/// Fired on `created` events. /// Fired on `created` events.
Stream get onCreated; Stream<Data> get onCreated;
/// Fired on `modified` events. /// Fired on `modified` events.
Stream get onModified; Stream<Data> get onModified;
/// Fired on `updated` events. /// Fired on `updated` events.
Stream get onUpdated; Stream<Data> get onUpdated;
/// Fired on `removed` events. /// Fired on `removed` events.
Stream get onRemoved; Stream<Data> get onRemoved;
/// The Angel instance powering this service. /// The Angel instance powering this service.
Angel get app; Angel get app;
@ -117,26 +118,26 @@ abstract class Service {
Future close(); Future close();
/// Retrieves all resources. /// Retrieves all resources.
Future index([Map params]); Future index([Map<String, dynamic> params]);
/// Retrieves the desired resource. /// Retrieves the desired resource.
Future read(id, [Map params]); Future read(Id id, [Map<String, dynamic> params]);
/// Creates a resource. /// Creates a resource.
Future create(data, [Map params]); Future create(Data data, [Map<String, dynamic> params]);
/// Modifies a resource. /// Modifies a resource.
Future modify(id, data, [Map params]); Future modify(Id id, Data data, [Map<String, dynamic> params]);
/// Overwrites a resource. /// Overwrites a resource.
Future update(id, data, [Map params]); Future update(Id id, Data data, [Map<String, dynamic> params]);
/// Removes the given resource. /// Removes the given resource.
Future remove(id, [Map params]); Future remove(Id id, [Map<String, dynamic> params]);
} }
/// A [List] that automatically updates itself whenever the referenced [service] fires an event. /// A [List] that automatically updates itself whenever the referenced [service] fires an event.
class ServiceList extends DelegatingList { class ServiceList<Id, Data> extends DelegatingList {
/// A field name used to compare [Map] by ID. /// A field name used to compare [Map] by ID.
final String idField; final String idField;
@ -150,9 +151,10 @@ class ServiceList extends DelegatingList {
/// Defaults to comparing the [idField] of `Map` instances. /// Defaults to comparing the [idField] of `Map` instances.
final Equality _compare; final Equality _compare;
final Service service; final Service<Id, Data> service;
final StreamController<ServiceList> _onChange = new StreamController(); final StreamController<ServiceList<Id, Data>> _onChange =
new StreamController();
final List<StreamSubscription> _subs = []; final List<StreamSubscription> _subs = [];
ServiceList(this.service, ServiceList(this.service,
@ -175,7 +177,7 @@ class ServiceList extends DelegatingList {
})); }));
// Modified/Updated // Modified/Updated
handleModified(item) { handleModified(Data item) {
var indices = <int>[]; var indices = <int>[];
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
@ -202,7 +204,7 @@ class ServiceList extends DelegatingList {
} }
/// Fires whenever the underlying [service] fires a change event. /// Fires whenever the underlying [service] fires a change event.
Stream<ServiceList> get onChange => _onChange.stream; Stream<ServiceList<Id, Data>> get onChange => _onChange.stream;
Future close() async { Future close() async {
_onChange.close(); _onChange.close();

View file

@ -16,7 +16,7 @@ const Map<String, String> _writeHeaders = const {
'Content-Type': 'application/json' 'Content-Type': 'application/json'
}; };
_buildQuery(Map params) { _buildQuery(Map<String, dynamic> params) {
if (params == null || params.isEmpty || params['query'] is! Map) return ""; if (params == null || params.isEmpty || params['query'] is! Map) return "";
List<String> query = []; List<String> query = [];
@ -191,9 +191,10 @@ abstract class BaseAngelClient extends Angel {
} }
@override @override
Service service(String path, {Type type, AngelDeserializer deserializer}) { Service<Id, Data> service<Id, Data>(String path,
{Type type, AngelDeserializer<Data> deserializer}) {
String uri = path.toString().replaceAll(straySlashes, ""); String uri = path.toString().replaceAll(straySlashes, "");
var s = new BaseAngelService(client, this, '$basePath/$uri', var s = new BaseAngelService<Id, Data>(client, this, '$basePath/$uri',
deserializer: deserializer); deserializer: deserializer);
_services.add(s); _services.add(s);
return s; return s;
@ -240,15 +241,15 @@ abstract class BaseAngelClient extends Angel {
} }
} }
class BaseAngelService extends Service { class BaseAngelService<Id, Data> extends Service<Id, Data> {
@override @override
final BaseAngelClient app; final BaseAngelClient app;
final String basePath; final String basePath;
final http.BaseClient client; final http.BaseClient client;
final AngelDeserializer deserializer; final AngelDeserializer<Data> deserializer;
final StreamController _onIndexed = new StreamController(), final StreamController _onIndexed = new StreamController();
_onRead = new StreamController(), final StreamController<Data> _onRead = new StreamController(),
_onCreated = new StreamController(), _onCreated = new StreamController(),
_onModified = new StreamController(), _onModified = new StreamController(),
_onUpdated = new StreamController(), _onUpdated = new StreamController(),
@ -258,19 +259,19 @@ class BaseAngelService extends Service {
Stream get onIndexed => _onIndexed.stream; Stream get onIndexed => _onIndexed.stream;
@override @override
Stream get onRead => _onRead.stream; Stream<Data> get onRead => _onRead.stream;
@override @override
Stream get onCreated => _onCreated.stream; Stream<Data> get onCreated => _onCreated.stream;
@override @override
Stream get onModified => _onModified.stream; Stream<Data> get onModified => _onModified.stream;
@override @override
Stream get onUpdated => _onUpdated.stream; Stream<Data> get onUpdated => _onUpdated.stream;
@override @override
Stream get onRemoved => _onRemoved.stream; Stream<Data> get onRemoved => _onRemoved.stream;
@override @override
Future close() async { Future close() async {
@ -284,8 +285,8 @@ class BaseAngelService extends Service {
BaseAngelService(this.client, this.app, this.basePath, {this.deserializer}); BaseAngelService(this.client, this.app, this.basePath, {this.deserializer});
deserialize(x) { Data deserialize(x) {
return deserializer != null ? deserializer(x) : x; return deserializer != null ? deserializer(x) : x as Data;
} }
makeBody(x) { makeBody(x) {
@ -301,7 +302,7 @@ class BaseAngelService extends Service {
} }
@override @override
Future index([Map params]) async { Future index([Map<String, dynamic> params]) async {
final response = await app.sendUnstreamed( final response = await app.sendUnstreamed(
'GET', '$basePath${_buildQuery(params)}', _readHeaders); 'GET', '$basePath${_buildQuery(params)}', _readHeaders);
@ -316,7 +317,7 @@ class BaseAngelService extends Service {
final v = json.decode(response.body); final v = json.decode(response.body);
if (v is! List) { if (v is! List) {
_onIndexed.add(v); _onIndexed.add(v as Data);
return v; return v;
} }
@ -332,7 +333,7 @@ class BaseAngelService extends Service {
} }
@override @override
Future read(id, [Map params]) async { Future read(id, [Map<String, dynamic> params]) async {
final response = await app.sendUnstreamed( final response = await app.sendUnstreamed(
'GET', '$basePath/$id${_buildQuery(params)}', _readHeaders); 'GET', '$basePath/$id${_buildQuery(params)}', _readHeaders);
@ -356,7 +357,7 @@ class BaseAngelService extends Service {
} }
@override @override
Future create(data, [Map params]) async { Future create(data, [Map<String, dynamic> params]) async {
final response = await app.sendUnstreamed('POST', final response = await app.sendUnstreamed('POST',
'$basePath/${_buildQuery(params)}', _writeHeaders, makeBody(data)); '$basePath/${_buildQuery(params)}', _writeHeaders, makeBody(data));
@ -380,7 +381,7 @@ class BaseAngelService extends Service {
} }
@override @override
Future modify(id, data, [Map params]) async { Future modify(id, data, [Map<String, dynamic> params]) async {
final response = await app.sendUnstreamed('PATCH', final response = await app.sendUnstreamed('PATCH',
'$basePath/$id${_buildQuery(params)}', _writeHeaders, makeBody(data)); '$basePath/$id${_buildQuery(params)}', _writeHeaders, makeBody(data));
@ -404,7 +405,7 @@ class BaseAngelService extends Service {
} }
@override @override
Future update(id, data, [Map params]) async { Future update(id, data, [Map<String, dynamic> params]) async {
final response = await app.sendUnstreamed('POST', final response = await app.sendUnstreamed('POST',
'$basePath/$id${_buildQuery(params)}', _writeHeaders, makeBody(data)); '$basePath/$id${_buildQuery(params)}', _writeHeaders, makeBody(data));
@ -428,7 +429,7 @@ class BaseAngelService extends Service {
} }
@override @override
Future remove(id, [Map params]) async { Future remove(id, [Map<String, dynamic> params]) async {
final response = await app.sendUnstreamed( final response = await app.sendUnstreamed(
'DELETE', '$basePath/$id${_buildQuery(params)}', _readHeaders); 'DELETE', '$basePath/$id${_buildQuery(params)}', _readHeaders);

View file

@ -15,9 +15,10 @@ class Rest extends BaseAngelClient {
Rest(String path) : super(new http.Client() as http.BaseClient, path); Rest(String path) : super(new http.Client() as http.BaseClient, path);
@override @override
Service service(String path, {Type type, AngelDeserializer deserializer}) { Service<Id, Data> service<Id, Data>(String path,
{Type type, AngelDeserializer deserializer}) {
String uri = path.replaceAll(straySlashes, ""); String uri = path.replaceAll(straySlashes, "");
var s = new RestService(client, this, "$basePath/$uri", type); var s = new RestService<Id, Data>(client, this, "$basePath/$uri", type);
_services.add(s); _services.add(s);
return s; return s;
} }
@ -37,7 +38,7 @@ class Rest extends BaseAngelClient {
} }
/// Queries an Angel service via REST. /// Queries an Angel service via REST.
class RestService extends BaseAngelService { class RestService<Id, Data> extends BaseAngelService<Id, Data> {
final Type type; final Type type;
RestService( RestService(
@ -45,14 +46,14 @@ class RestService extends BaseAngelService {
: super(client, app, url); : super(client, app, url);
@override @override
deserialize(x) { Data deserialize(x) {
if (type != null) { if (type != null) {
return x.runtimeType == type return x.runtimeType == type
? x ? x as Data
: god.deserializeDatum(x, outputType: type); : god.deserializeDatum(x, outputType: type) as Data;
} }
return x; return x as Data;
} }
@override @override

View file

@ -1,5 +1,5 @@
name: angel_client name: angel_client
version: 2.0.0-alpha version: 2.0.0-alpha.1
description: Client library for the Angel framework. description: Client library for the Angel framework.
author: Tobe O <thosakwe@gmail.com> author: Tobe O <thosakwe@gmail.com>
homepage: https://github.com/angel-dart/angel_client homepage: https://github.com/angel-dart/angel_client