2016-06-24 00:25:11 +00:00
|
|
|
/// Client library for the Angel framework.
|
|
|
|
library angel_client;
|
|
|
|
|
|
|
|
import 'dart:async';
|
2016-12-09 00:24:07 +00:00
|
|
|
import 'dart:convert';
|
2016-12-10 17:15:54 +00:00
|
|
|
import 'package:http/src/response.dart' as http;
|
2016-12-09 00:24:07 +00:00
|
|
|
export 'package:angel_framework/src/http/angel_http_exception.dart';
|
2016-06-24 19:02:35 +00:00
|
|
|
|
2016-06-24 21:06:57 +00:00
|
|
|
/// A function that configures an [Angel] client in some way.
|
|
|
|
typedef Future AngelConfigurer(Angel app);
|
2016-06-24 19:02:35 +00:00
|
|
|
|
2016-12-13 16:35:35 +00:00
|
|
|
/// 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);
|
|
|
|
|
2016-06-24 19:02:35 +00:00
|
|
|
/// Represents an Angel server that we are querying.
|
|
|
|
abstract class Angel {
|
2017-02-28 21:56:59 +00:00
|
|
|
String authToken;
|
2016-06-24 19:02:35 +00:00
|
|
|
String basePath;
|
|
|
|
|
|
|
|
Angel(String this.basePath);
|
2016-06-24 21:06:57 +00:00
|
|
|
|
2017-06-03 17:43:01 +00:00
|
|
|
/// Fired whenever a WebSocket is successfully authenticated.
|
|
|
|
Stream<AngelAuthResult> get onAuthenticated;
|
|
|
|
|
2016-11-29 00:42:02 +00:00
|
|
|
Future<AngelAuthResult> authenticate(
|
2016-12-03 18:21:44 +00:00
|
|
|
{String type,
|
2016-11-29 00:42:02 +00:00
|
|
|
credentials,
|
|
|
|
String authEndpoint: '/auth',
|
|
|
|
String reviveEndpoint: '/auth/token'});
|
2016-11-28 03:28:41 +00:00
|
|
|
|
2017-02-28 21:56:59 +00:00
|
|
|
/// Opens the [url] in a new window, and returns a [Stream] that will fire a JWT on successful authentication.
|
|
|
|
Stream<String> authenticateViaPopup(String url, {String eventName: 'token'});
|
|
|
|
|
2016-12-10 14:50:05 +00:00
|
|
|
Future close();
|
|
|
|
|
2016-06-24 21:06:57 +00:00
|
|
|
/// Applies an [AngelConfigurer] to this instance.
|
|
|
|
Future configure(AngelConfigurer configurer) async {
|
|
|
|
await configurer(this);
|
|
|
|
}
|
|
|
|
|
2017-03-29 01:52:19 +00:00
|
|
|
/// Logs the current user out of the application.
|
|
|
|
Future logout();
|
|
|
|
|
2017-06-03 17:43:01 +00:00
|
|
|
Service<T> service<T>(String path, {Type type, AngelDeserializer deserializer});
|
2016-12-10 17:15:54 +00:00
|
|
|
|
2017-02-28 21:56:59 +00:00
|
|
|
Future<http.Response> delete(String url, {Map<String, String> headers});
|
2016-12-10 17:15:54 +00:00
|
|
|
|
|
|
|
Future<http.Response> get(String url, {Map<String, String> headers});
|
|
|
|
|
|
|
|
Future<http.Response> head(String url, {Map<String, String> headers});
|
|
|
|
|
2017-02-28 21:56:59 +00:00
|
|
|
Future<http.Response> patch(String url, {body, Map<String, String> headers});
|
2016-12-10 17:15:54 +00:00
|
|
|
|
2017-02-28 21:56:59 +00:00
|
|
|
Future<http.Response> post(String url, {body, Map<String, String> headers});
|
2016-12-10 17:15:54 +00:00
|
|
|
|
2017-02-28 21:56:59 +00:00
|
|
|
Future<http.Response> put(String url, {body, Map<String, String> headers});
|
2016-06-24 19:02:35 +00:00
|
|
|
}
|
2016-06-24 00:25:11 +00:00
|
|
|
|
2016-11-28 03:28:41 +00:00
|
|
|
/// Represents the result of authentication with an Angel server.
|
2016-12-09 00:24:07 +00:00
|
|
|
class AngelAuthResult {
|
|
|
|
String _token;
|
|
|
|
final Map<String, dynamic> data = {};
|
|
|
|
String get token => _token;
|
|
|
|
|
|
|
|
AngelAuthResult({String token, Map<String, dynamic> 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'];
|
|
|
|
|
|
|
|
if (data is Map) result.data.addAll(data['data'] ?? {});
|
2016-12-03 18:21:44 +00:00
|
|
|
|
2016-12-09 00:24:07 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
factory AngelAuthResult.fromJson(String json) =>
|
|
|
|
new AngelAuthResult.fromMap(JSON.decode(json));
|
|
|
|
|
|
|
|
Map<String, dynamic> toJson() {
|
|
|
|
return {'token': token, 'data': data};
|
|
|
|
}
|
2016-11-28 03:28:41 +00:00
|
|
|
}
|
|
|
|
|
2016-06-24 00:25:11 +00:00
|
|
|
/// Queries a service on an Angel server, with the same API.
|
2017-06-03 17:43:01 +00:00
|
|
|
abstract class Service<T> {
|
|
|
|
/// Fired on `indexed` events.
|
|
|
|
Stream<T> get onIndexed;
|
|
|
|
|
|
|
|
/// Fired on `read` events.
|
|
|
|
Stream<T> get onRead;
|
|
|
|
|
|
|
|
/// Fired on `created` events.
|
|
|
|
Stream<T> get onCreated;
|
|
|
|
|
|
|
|
/// Fired on `modified` events.
|
|
|
|
Stream<T> get onModified;
|
|
|
|
|
|
|
|
/// Fired on `updated` events.
|
|
|
|
Stream<T> get onUpdated;
|
|
|
|
|
|
|
|
/// Fired on `removed` events.
|
|
|
|
Stream<T> get onRemoved;
|
|
|
|
|
2016-06-24 21:06:57 +00:00
|
|
|
/// The Angel instance powering this service.
|
2016-11-28 03:28:41 +00:00
|
|
|
Angel get app;
|
2016-06-24 21:06:57 +00:00
|
|
|
|
2017-06-03 17:43:01 +00:00
|
|
|
Future close();
|
|
|
|
|
2016-06-24 00:25:11 +00:00
|
|
|
/// Retrieves all resources.
|
2017-02-12 19:58:18 +00:00
|
|
|
Future index([Map params]);
|
2016-06-24 00:25:11 +00:00
|
|
|
|
|
|
|
/// Retrieves the desired resource.
|
|
|
|
Future read(id, [Map params]);
|
|
|
|
|
|
|
|
/// Creates a resource.
|
|
|
|
Future create(data, [Map params]);
|
|
|
|
|
|
|
|
/// Modifies a resource.
|
|
|
|
Future modify(id, data, [Map params]);
|
|
|
|
|
|
|
|
/// Overwrites a resource.
|
|
|
|
Future update(id, data, [Map params]);
|
|
|
|
|
|
|
|
/// Removes the given resource.
|
|
|
|
Future remove(id, [Map params]);
|
2016-09-03 12:02:32 +00:00
|
|
|
}
|