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
* Depend on Dart 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';
/// 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<T>(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<Id, Data> service<Id, Data>(String path,
{Type type, AngelDeserializer<Data> deserializer});
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.
abstract class Service {
abstract class Service<Id, Data> {
/// Fired on `indexed` events.
Stream get onIndexed;
/// Fired on `read` events.
Stream get onRead;
Stream<Data> get onRead;
/// Fired on `created` events.
Stream get onCreated;
Stream<Data> get onCreated;
/// Fired on `modified` events.
Stream get onModified;
Stream<Data> get onModified;
/// Fired on `updated` events.
Stream get onUpdated;
Stream<Data> get onUpdated;
/// Fired on `removed` events.
Stream get onRemoved;
Stream<Data> 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<String, dynamic> params]);
/// Retrieves the desired resource.
Future read(id, [Map params]);
Future read(Id id, [Map<String, dynamic> params]);
/// Creates a resource.
Future create(data, [Map params]);
Future create(Data data, [Map<String, dynamic> params]);
/// Modifies a resource.
Future modify(id, data, [Map params]);
Future modify(Id id, Data data, [Map<String, dynamic> params]);
/// Overwrites a resource.
Future update(id, data, [Map params]);
Future update(Id id, Data data, [Map<String, dynamic> params]);
/// 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.
class ServiceList extends DelegatingList {
class ServiceList<Id, Data> 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<Id, Data> service;
final StreamController<ServiceList> _onChange = new StreamController();
final StreamController<ServiceList<Id, Data>> _onChange =
new StreamController();
final List<StreamSubscription> _subs = [];
ServiceList(this.service,
@ -175,7 +177,7 @@ class ServiceList extends DelegatingList {
}));
// Modified/Updated
handleModified(item) {
handleModified(Data item) {
var indices = <int>[];
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<ServiceList> get onChange => _onChange.stream;
Stream<ServiceList<Id, Data>> get onChange => _onChange.stream;
Future close() async {
_onChange.close();

View file

@ -16,7 +16,7 @@ const Map<String, String> _writeHeaders = const {
'Content-Type': 'application/json'
};
_buildQuery(Map params) {
_buildQuery(Map<String, dynamic> params) {
if (params == null || params.isEmpty || params['query'] is! Map) return "";
List<String> query = [];
@ -191,9 +191,10 @@ abstract class BaseAngelClient extends Angel {
}
@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, "");
var s = new BaseAngelService(client, this, '$basePath/$uri',
var s = new BaseAngelService<Id, Data>(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<Id, Data> extends Service<Id, Data> {
@override
final BaseAngelClient app;
final String basePath;
final http.BaseClient client;
final AngelDeserializer deserializer;
final AngelDeserializer<Data> deserializer;
final StreamController _onIndexed = new StreamController(),
_onRead = new StreamController(),
final StreamController _onIndexed = new StreamController();
final StreamController<Data> _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<Data> get onRead => _onRead.stream;
@override
Stream get onCreated => _onCreated.stream;
Stream<Data> get onCreated => _onCreated.stream;
@override
Stream get onModified => _onModified.stream;
Stream<Data> get onModified => _onModified.stream;
@override
Stream get onUpdated => _onUpdated.stream;
Stream<Data> get onUpdated => _onUpdated.stream;
@override
Stream get onRemoved => _onRemoved.stream;
Stream<Data> 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<String, dynamic> 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<String, dynamic> 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<String, dynamic> 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<String, dynamic> 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<String, dynamic> 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<String, dynamic> params]) async {
final response = await app.sendUnstreamed(
'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);
@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, "");
var s = new RestService(client, this, "$basePath/$uri", type);
var s = new RestService<Id, Data>(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<Id, Data> extends BaseAngelService<Id, Data> {
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

View file

@ -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 <thosakwe@gmail.com>
homepage: https://github.com/angel-dart/angel_client