angel_client 2.0.2

  • Readme
  • Changelog
  • Example
  • Installing
  • 73

angel_client #

Pub build 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 #

// Choose one or the other, depending on platform
import 'package:angel_client/io.dart';
import 'package:angel_client/browser.dart';
import 'package:angel_client/flutter.dart';

main() async {
  Angel app = new Rest("http://localhost:3000");
}

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!).

foo() async {
  Service Todos = app.service("todos");
  List<Map> todos = await Todos.index();

  print(todos.length);
}

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.

class Todo extends Model {
  String text;

  Todo({String this.text});
}

bar() async {
  // By the next release, this will just be:
  // app.service<Todo>("todos")
  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 remove.

Authentication #

Local auth:

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

Revive an existing jwt:

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

Via Popup:

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

Resume a session from localStorage (browser only):

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

Logout:

await app.logout();

Live Updates #

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

build(BuildContext context) async {
  var list = new ServiceList(app.service('api/todos'));
  
  return new StreamBuilder(
    stream: list.onChange,
    builder: _yourBuildFunction,
  );
}

2.0.2 #

  • _join previously discarded quer parameters, etc.
  • Allow any Map<String, dynamic> as body, not just Map<String, String>.

2.0.1 #

  • Change BaseAngelClient constructor to accept dynamic instead of String for `baseUrl.

2.0.0 #

  • Deprecate basePath in favor of baseUrl.
  • Angel now extends http.Client.
  • Deprecate auth_types.

2.0.0-alpha.2 #

  • Make Service index always return List<Data>.
  • Add Service.map.

2.0.0-alpha.1 #

  • Refactor params to Map<String, dynamic>.

2.0.0-alpha #

  • Depend on Dart 2.
  • Depend on Angel 2.
  • Remove dart2_constant.

1.2.0+2 #

  • Code cleanup + housekeeping, update to dart2_constant, and ensured build works with 2.0.0-dev.64.1.

1.2.0+1 #

  • Removed a type annotation in authenticateViaPopup to prevent breaking with DDC.

1.2.0 #

  • ServiceList now uses Equality from package:collection to compare items.
  • Services will now add service errors to corresponding streams if there is a listener.

1.1.0+3 #

  • ServiceList no longer ignores empty index events.

1.1.0+2 #

  • Updated ServiceList to also fire on index.

1.1.0+1 #

  • Added ServiceList.

example/main.dart

import 'dart:async';
import 'package:angel_client/angel_client.dart';

Future doSomething(Angel app) async {
  var userService = app
      .service<String, Map<String, dynamic>>('api/users')
      .map(User.fromMap, User.toMap);

  var users = await userService.index();
  print('Name: ${users.first.name}');
}

class User {
  final String name;

  User({this.name});

  static User fromMap(Map data) => User(name: data['name'] as String);

  static Map<String, String> toMap(User user) => {'name': user.name};
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  angel_client: ^2.0.2

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:angel_client/angel_client.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
60
Health:
Code health derived from static analysis. [more]
75
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
73
Learn more about scoring.

We analyzed this package on Feb 27, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.5

Health suggestions

Fix lib/base_angel_client.dart. (-19.79 points)

Analysis of lib/base_angel_client.dart reported 44 hints, including:

line 13 col 42: Avoid const keyword.

line 14 col 43: Avoid const keyword.

line 20 col 32: Unnecessary new keyword.

line 34 col 14: Unnecessary new keyword.

line 36 col 14: Unnecessary new keyword.

Fix lib/angel_client.dart. (-6.78 points)

Analysis of lib/angel_client.dart reported 14 hints, including:

line 126 col 20: Unnecessary new keyword.

line 129 col 7: DO use curly braces for all flow control structures.

line 132 col 7: DO use curly braces for all flow control structures.

line 135 col 13: Unnecessary new keyword.

line 138 col 13: Unnecessary new keyword.

Fix lib/browser.dart. (-3.45 points)

Analysis of lib/browser.dart reported 7 hints, including:

line 16 col 33: Unnecessary new keyword.

line 25 col 15: Unnecessary new keyword.

line 43 col 16: Unnecessary new keyword.

line 48 col 9: Unnecessary new keyword.

line 48 col 28: Unnecessary new keyword.

Fix lib/io.dart. (-1.49 points)

Analysis of lib/io.dart reported 3 hints:

line 16 col 29: Unnecessary new keyword.

line 22 col 13: Unnecessary new keyword.

line 30 col 11: Unnecessary new keyword.

Fix lib/flutter.dart. (-1 points)

Analysis of lib/flutter.dart reported 2 hints:

line 11 col 33: Unnecessary new keyword.

line 16 col 11: Unnecessary new keyword.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev <3.0.0
angel_http_exception ^1.0.0 1.1.0
collection ^1.0.0 1.14.12
http ^0.12.0 0.12.0+4
json_god >=2.0.0-beta <3.0.0 2.0.0-beta+3
meta ^1.0.0 1.1.8
path ^1.0.0 1.6.4
Transitive dependencies
charcode 1.1.3
dart2_constant 1.0.2+dart2
http_parser 3.1.3
logging 0.11.4
source_span 1.6.0
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6
Dev dependencies
angel_framework ^2.0.0-alpha
angel_model ^1.0.0
async ^2.0.0 2.4.0
build_runner ^1.0.0
build_web_compilers ^1.0.0
mock_request ^1.0.0
pedantic ^1.0.0 1.9.0
test ^1.0.0