/// Client library for the Angel framework. library angel_client; import 'dart:async'; import 'package:collection/collection.dart'; import 'dart:convert'; 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); /// A function that deserializes data received from the server. /// /// This is only really necessary in the browser, where `json_god` /// doesn't work. typedef T AngelDeserializer(x); /// Represents an Angel server that we are querying. abstract class Angel { String authToken; String basePath; Angel(String this.basePath); /// Fired whenever a WebSocket is successfully authenticated. Stream get onAuthenticated; Future authenticate( {String type, credentials, String authEndpoint: '/auth', String reviveEndpoint: '/auth/token'}); /// Opens the [url] in a new window, and returns a [Stream] that will fire a JWT on successful authentication. Stream authenticateViaPopup(String url, {String eventName: 'token'}); Future close(); /// Applies an [AngelConfigurer] to this instance. Future configure(AngelConfigurer configurer) async { await configurer(this); } /// Logs the current user out of the application. Future logout(); Service service(String path, {Type type, AngelDeserializer deserializer}); Future delete(String url, {Map headers}); Future get(String url, {Map headers}); Future head(String url, {Map headers}); Future patch(String url, {body, Map headers}); Future post(String url, {body, Map headers}); Future put(String url, {body, Map headers}); } /// Represents the result of authentication with an Angel server. class AngelAuthResult { String _token; final Map data = {}; String get token => _token; AngelAuthResult({String token, Map data: const {}}) { _token = token; this.data.addAll(data ?? {}); } factory AngelAuthResult.fromMap(Map data) { final result = new AngelAuthResult(); if (data is Map && data.containsKey('token') && data['token'] is String) result._token = data['token'].toString(); if (data is Map) result.data.addAll((data['data'] as Map) ?? {}); return result; } factory AngelAuthResult.fromJson(String s) => new AngelAuthResult.fromMap(json.decode(s) as Map); Map toJson() { return {'token': token, 'data': data}; } } /// Queries a service on an Angel server, with the same API. abstract class Service { /// Fired on `indexed` events. Stream> get onIndexed; /// Fired on `read` events. Stream get onRead; /// Fired on `created` events. Stream get onCreated; /// Fired on `modified` events. Stream get onModified; /// Fired on `updated` events. Stream get onUpdated; /// Fired on `removed` events. Stream get onRemoved; /// The Angel instance powering this service. Angel get app; Future close(); /// Retrieves all resources. Future> index([Map params]); /// Retrieves the desired resource. Future read(Id id, [Map params]); /// Creates a resource. Future create(Data data, [Map params]); /// Modifies a resource. Future modify(Id id, Data data, [Map params]); /// Overwrites a resource. Future update(Id id, Data data, [Map params]); /// Removes the given resource. Future remove(Id id, [Map params]); /// Creates a [Service] that wraps over this one, and maps input and output using two converter functions. /// /// Handy utility for handling data in a type-safe manner. Service map(U Function(Data) encoder, Data Function(U) decoder) { return new _MappedService(this, encoder, decoder); } } class _MappedService extends Service { final Service inner; final U Function(Data) encoder; final Data Function(U) decoder; _MappedService(this.inner, this.encoder, this.decoder); @override Angel get app => inner.app; @override Future close() => new Future.value(); @override Future create(U data, [Map params]) { return inner.create(decoder(data)).then(encoder); } @override Future> index([Map params]) { return inner.index(params).then((l) => l.map(encoder).toList()); } @override Future modify(Id id, U data, [Map params]) { return inner.modify(id, decoder(data), params).then(encoder); } @override Stream get onCreated => inner.onCreated.map(encoder); @override Stream> get onIndexed => inner.onIndexed.map((l) => l.map(encoder).toList()); @override Stream get onModified => inner.onModified.map(encoder); @override Stream get onRead => inner.onRead.map(encoder); @override Stream get onRemoved => inner.onRemoved.map(encoder); @override Stream get onUpdated => inner.onUpdated.map(encoder); @override Future read(Id id, [Map params]) { return inner.read(id, params).then(encoder); } @override Future remove(Id id, [Map params]) { return inner.remove(id, params).then(encoder); } @override Future update(Id id, U data, [Map params]) { return inner.update(id, decoder(data), params).then(encoder); } } /// A [List] that automatically updates itself whenever the referenced [service] fires an event. class ServiceList extends DelegatingList { /// A field name used to compare [Map] by ID. final String idField; /// A function used to compare the ID's two items for equality. /// /// Defaults to comparing the [idField] of `Map` instances. Equality get equality => _equality; Equality _equality; final Service service; final StreamController> _onChange = new StreamController(); final List _subs = []; ServiceList(this.service, {this.idField, Equality equality}) : super([]) { _equality = equality; _equality ??= new EqualityBy((map) { if (map is Map) return map[idField ?? 'id'] as Id; else throw new UnsupportedError( 'ServiceList only knows how to find the id from a Map object. Provide a custom `Equality` in your call to the constructor.'); }); // Index _subs.add(service.onIndexed.where(_notNull).listen((data) { this ..clear() ..addAll(data); _onChange.add(this); })); // Created _subs.add(service.onCreated.where(_notNull).listen((item) { add(item); _onChange.add(this); })); // Modified/Updated handleModified(Data item) { var indices = []; for (int i = 0; i < length; i++) { if (_equality.equals(item, this[i])) indices.add(i); } if (indices.isNotEmpty) { for (var i in indices) this[i] = item; _onChange.add(this); } } _subs.addAll([ service.onModified.where(_notNull).listen(handleModified), service.onUpdated.where(_notNull).listen(handleModified), ]); // Removed _subs.add(service.onRemoved.where(_notNull).listen((item) { removeWhere((x) => _equality.equals(item, x)); _onChange.add(this); })); } static bool _notNull(x) => x != null; /// Fires whenever the underlying [service] fires a change event. Stream> get onChange => _onChange.stream; Future close() async { _onChange.close(); } }