angel_client

Pubbuild status

Client library for the Angel framework.This library provides virtually the same API as an Angel server.The client can run in the browser, in Flutter, or on the command-line.In addition, the client supports angel_auth authentication.

Usage

  1. // Choose one or the other, depending on platform
  2. import 'package:angel_client/io.dart';
  3. import 'package:angel_client/browser.dart';
  4. import 'package:angel_client/flutter.dart';
  5.  
  6. main() async {
  7. Angel app = new Rest("http://localhost:3000");
  8. }

You can call service to receive an instance of Service, which acts as a client to aservice on the server at the given path (say that five times fast!).

  1. foo() async {
  2. Service Todos = app.service("todos");
  3. List<Map> todos = await Todos.index();
  4.  
  5. print(todos.length);
  6. }

The CLI client also supports reflection via json_god. There is no need to work with Maps;you can use the same class on the client and the server.

  1. class Todo extends Model {
  2. String text;
  3.  
  4. Todo({String this.text});
  5. }
  6.  
  7. bar() async {
  8. // By the next release, this will just be:
  9. // app.service<Todo>("todos")
  10. Service Todos = app.service("todos", type: Todo);
  11. List<Todo> todos = await Todos.index();
  12.  
  13. print(todos.length);
  14. }

Just like on the server, services support index, read, create, modify, update andremove.

Authentication

Local auth:

  1. var auth = await app.authenticate(type: 'local', credentials: {username: ..., password: ...});
  2. print(auth.token);
  3. print(auth.data); // User object

Revive an existing jwt:

  1. Future<AngelAuthResult> reviveJwt(String jwt) {
  2. return app.authenticate(credentials: {'token': jwt});
  3. }

Via Popup:

  1. app.authenticateViaPopup('/auth/google').listen((jwt) {
  2. // Do something with the JWT
  3. });

Resume a session from localStorage (browser only):

  1. // Automatically checks for JSON-encoded 'token' in localStorage,
  2. // and tries to revive it
  3. await app.authenticate();

Logout:

  1. await app.logout();

Live Updates

Oftentimes, you will want to update a collection based on updates from a service.Use ServiceList for this case:

  1. build(BuildContext context) async {
  2. var list = new ServiceList(app.service('api/todos'));
  3.  
  4. return new StreamBuilder(
  5. stream: list.onChange,
  6. builder: _yourBuildFunction,
  7. );
  8. }