Files
Docwell/Phone-App/lib/api/docwell_api.dart
T
tajniak81andClaude Opus 5.5 4903bb12dd Scaffold Docwell monorepo with PocketBase authentication
Phase 0 foundation for Docwell, an open-source knowledge base:

- Database: PocketBase v0.40.6 with initial schema migrations; users is
  the built-in auth collection, plus a hook for token checks and
  env-driven app URL/SMTP settings
- Server: Go API (health, version, auth endpoints forwarded to
  PocketBase, admin user list) with an embedded Vue admin panel
- Web-App: Go static server and Vue/Tailwind app with Tiptap editor,
  sign-in, sign-up, password reset and email verification (EN/PL)
- Desktop-App: Wails v3 host reusing the web app
- Phone-App: Flutter app with sign-in
- Shared packages (editor, ui, api-client), OpenAPI spec, Docker
  compose (separate services and all-in-one), GitHub Actions CI
- Docs: feature inventory and architecture/build plan

Licensed under AGPL-3.0-or-later.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-30 22:18:25 +01:00

78 lines
2.5 KiB
Dart

import 'dart:convert';
import 'package:http/http.dart' as http;
/// Minimal docwell-server client. Will be replaced by a client generated
/// from api/openapi.yaml.
class DocwellApi {
DocwellApi(String baseUrl, {http.Client? client})
: baseUrl = baseUrl.replaceAll(RegExp(r'/+$'), ''),
_client = client ?? http.Client();
final String baseUrl;
final http.Client _client;
/// PocketBase auth token of the signed-in user, sent as a bearer token.
String? token;
Future<String> version() async {
final body = await _send('GET', '/api/v1/version');
return body['version'] as String;
}
/// Signs in with email and password and keeps the token for later calls.
Future<User> login(String email, String password) async {
final body = await _send('POST', '/api/v1/auth/login', {'email': email, 'password': password});
token = body['token'] as String;
return User.fromJson(body['user'] as Map<String, dynamic>);
}
Future<User> me() async => User.fromJson(await _send('GET', '/api/v1/auth/me'));
void logout() => token = null;
Future<Map<String, dynamic>> _send(String method, String path, [Object? body]) async {
final req = http.Request(method, Uri.parse('$baseUrl$path'));
req.headers['Accept'] = 'application/json';
if (token != null) req.headers['Authorization'] = 'Bearer $token';
if (body != null) {
req.headers['Content-Type'] = 'application/json';
req.body = jsonEncode(body);
}
final res = await http.Response.fromStream(await _client.send(req).timeout(const Duration(seconds: 15)));
final data = res.body.isEmpty ? <String, dynamic>{} : jsonDecode(res.body) as Map<String, dynamic>;
if (res.statusCode >= 300) {
throw DocwellApiException(res.statusCode, data['error'] as String? ?? 'unknown');
}
return data;
}
}
class User {
User({required this.id, required this.email, required this.name, required this.isAdmin});
factory User.fromJson(Map<String, dynamic> json) => User(
id: json['id'] as String,
email: json['email'] as String,
name: json['name'] as String,
isAdmin: json['isAdmin'] as bool? ?? false,
);
final String id;
final String email;
final String name;
final bool isAdmin;
}
class DocwellApiException implements Exception {
DocwellApiException(this.status, [this.code = 'unknown']);
final int status;
/// Machine-readable error code from the server, e.g. "invalid_credentials".
final String code;
@override
String toString() => 'Docwell API error $status ($code)';
}