2021-07-09 14:19:16 +00:00
# Angel3 Client
2021-07-15 08:11:54 +00:00
[![version ](https://img.shields.io/badge/pub-v4.0.2-brightgreen )](https://pub.dartlang.org/packages/angel3_client)
2021-05-15 06:01:47 +00:00
[![Null Safety ](https://img.shields.io/badge/null-safety-brightgreen )](https://dart.dev/null-safety)
[![Gitter ](https://img.shields.io/gitter/room/angel_dart/discussion )](https://gitter.im/angel_dart/discussion)
2016-12-09 00:24:07 +00:00
2021-05-15 06:01:47 +00:00
[![License ](https://img.shields.io/github/license/dukefirehawk/angel )](https://github.com/dukefirehawk/angel/tree/angel3/packages/client/LICENSE)
2016-06-24 21:06:57 +00:00
2021-07-15 08:11:54 +00:00
A browser, mobile and command line based client that supports querying Angel3 backend.
2021-07-09 14:19:16 +00:00
## Usage
2016-06-24 21:06:57 +00:00
```dart
2016-06-25 18:37:49 +00:00
// Choose one or the other, depending on platform
2021-05-15 06:01:47 +00:00
import 'package:angel3_client/io.dart';
import 'package:angel3_client/browser.dart';
import 'package:angel3_client/flutter.dart';
2016-06-24 21:06:57 +00:00
main() async {
2021-05-15 06:01:47 +00:00
Angel app = Rest("http://localhost:3000");
2016-06-24 21:06:57 +00:00
}
```
2021-07-09 14:19:16 +00:00
You can call `service` to receive an instance of `Service` , which acts as a client to a service on the server at the given path (say that five times fast!).
2016-06-24 21:06:57 +00:00
```dart
foo() async {
Service Todos = app.service("todos");
List< Map > todos = await Todos.index();
print(todos.length);
}
```
2021-07-09 14:19:16 +00:00
The CLI client also supports reflection via `angel3_json_god` . There is no need to work with Maps; you can use the same class on the client and the server.
2016-06-24 21:06:57 +00:00
```dart
class Todo extends Model {
String text;
Todo({String this.text});
}
bar() async {
2016-12-10 14:45:22 +00:00
// By the next release, this will just be:
// app.service< Todo > ("todos")
2016-06-24 21:06:57 +00:00
Service Todos = app.service("todos", type: Todo);
List< Todo > todos = await Todos.index();
print(todos.length);
}
```
2021-07-09 14:19:16 +00:00
Just like on the server, services support `index` , `read` , `create` , `modify` , `update` and `remove` .
2017-03-29 01:52:19 +00:00
## Authentication
2021-07-09 14:19:16 +00:00
2017-03-29 01:52:19 +00:00
Local auth:
2021-07-09 14:19:16 +00:00
2017-03-29 01:52:19 +00:00
```dart
var auth = await app.authenticate(type: 'local', credentials: {username: ..., password: ...});
print(auth.token);
2017-03-30 19:00:02 +00:00
print(auth.data); // User object
2017-03-29 01:52:19 +00:00
```
Revive an existing jwt:
2021-07-09 14:19:16 +00:00
2017-03-29 01:52:19 +00:00
```dart
Future< AngelAuthResult > reviveJwt(String jwt) {
return app.authenticate(credentials: {'token': jwt});
}
```
Via Popup:
2021-07-09 14:19:16 +00:00
2017-03-29 01:52:19 +00:00
```dart
app.authenticateViaPopup('/auth/google').listen((jwt) {
// Do something with the JWT
});
```
Resume a session from localStorage (browser only):
2021-07-09 14:19:16 +00:00
2017-03-29 01:52:19 +00:00
```dart
// Automatically checks for JSON-encoded 'token' in localStorage,
// and tries to revive it
await app.authenticate();
```
Logout:
2021-07-09 14:19:16 +00:00
2017-03-29 01:52:19 +00:00
```dart
await app.logout();
2017-03-30 19:00:02 +00:00
```
2017-12-10 05:13:31 +00:00
2021-07-09 14:19:16 +00:00
## Live Updates
Oftentimes, you will want to update a collection based on updates from a service. Use `ServiceList` for this case:
2017-12-10 05:13:31 +00:00
```dart
build(BuildContext context) async {
2021-05-15 06:01:47 +00:00
var list = ServiceList(app.service('api/todos'));
2017-12-10 05:13:31 +00:00
2021-05-15 06:01:47 +00:00
return StreamBuilder(
2017-12-10 05:13:31 +00:00
stream: list.onChange,
builder: _yourBuildFunction,
);
}
2021-07-09 14:19:16 +00:00
```