platform/packages/client/README.md

106 lines
2.5 KiB
Markdown
Raw Normal View History

2016-06-23 19:13:52 +00:00
# angel_client
2016-12-09 00:24:07 +00:00
2017-06-30 22:57:01 +00:00
[![Pub](https://img.shields.io/pub/v/angel_client.svg)](https://pub.dartlang.org/packages/angel_client)
2017-02-22 22:20:53 +00:00
[![build status](https://travis-ci.org/angel-dart/client.svg)](https://travis-ci.org/angel-dart/client)
2016-12-09 00:24:07 +00:00
2016-06-23 19:13:52 +00:00
Client library for the Angel framework.
2017-02-22 22:20:30 +00:00
This library provides virtually the same API as an Angel server.
2017-06-30 22:57:01 +00:00
The client can run in the browser, in Flutter, or on the command-line.
2017-02-22 22:20:30 +00:00
In addition, the client supports `angel_auth` authentication.
2016-06-24 21:06:57 +00:00
# Usage
```dart
2016-06-25 18:37:49 +00:00
// Choose one or the other, depending on platform
2017-03-04 02:19:10 +00:00
import 'package:angel_client/io.dart';
2016-06-25 18:37:49 +00:00
import 'package:angel_client/browser.dart';
2017-07-13 17:52:42 +00:00
import 'package:angel_client/flutter.dart';
2016-06-24 21:06:57 +00:00
main() async {
2017-03-04 02:18:53 +00:00
Angel app = new Rest("http://localhost:3000");
2016-06-24 21:06:57 +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!).
```dart
foo() async {
Service Todos = app.service("todos");
List<Map> todos = await Todos.index();
print(todos.length);
}
```
2016-09-03 12:04:00 +00:00
The CLI client also supports reflection via `json_god`. There is no need to work with Maps;
2016-06-24 21:06:57 +00:00
you can use the same class on the client and the server.
```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);
}
```
Just like on the server, services support `index`, `read`, `create`, `modify`, `update` and
2016-09-03 12:04:00 +00:00
`remove`.
2017-03-29 01:52:19 +00:00
## Authentication
Local auth:
```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:
```dart
Future<AngelAuthResult> reviveJwt(String jwt) {
return app.authenticate(credentials: {'token': jwt});
}
```
Via Popup:
```dart
app.authenticateViaPopup('/auth/google').listen((jwt) {
// Do something with the JWT
});
```
Resume a session from localStorage (browser only):
```dart
// Automatically checks for JSON-encoded 'token' in localStorage,
// and tries to revive it
await app.authenticate();
```
Logout:
```dart
await app.logout();
2017-03-30 19:00:02 +00:00
```
2017-12-10 05:13:31 +00:00
# Live Updates
Oftentimes, you will want to update a collection based on updates from a service.
Use `ServiceList` for this case:
```dart
build(BuildContext context) async {
var list = new ServiceList(app.service('api/todos'));
return new StreamBuilder(
stream: list.onChange,
builder: _yourBuildFunction,
);
}
```