platform/packages/client/README.md

109 lines
2.6 KiB
Markdown
Raw Permalink Normal View History

# Protevus Client
2021-07-09 14:19:16 +00:00
![Pub Version (including pre-releases)](https://img.shields.io/pub/v/protevus_client?include_prereleases)
2021-05-15 06:01:47 +00:00
[![Null Safety](https://img.shields.io/badge/null-safety-brightgreen)](https://dart.dev/null-safety)
2024-07-07 15:02:49 +00:00
[![Discord](https://img.shields.io/discord/1060322353214660698)](https://discord.gg/3X6bxTUdCM)
[![License](https://img.shields.io/github/license/dart-backend/protevus)](https://github.com/dart-backend/protevus/tree/master/packages/client/LICENSE)
2016-06-24 21:06:57 +00:00
A browser, mobile and command line based client that supports querying Protevus 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
import 'package:protevus_client/io.dart';
import 'package:protevus_client/browser.dart';
import 'package:protevus_client/flutter.dart';
2016-06-24 21:06:57 +00:00
main() async {
Protevus 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-09-25 06:32:32 +00:00
The CLI client also supports reflection via `package:belatuk_json_serializer`. 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<ProtevusAuthResult> reviveJwt(String jwt) {
2017-03-29 01:52:19 +00:00
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
```