Files
Docwell/Phone-App/lib/api/docwell_api.dart
T
tajniak81andClaude Opus 5.5 ba394b8fac Finish Phase 0: lint, codegen, contributor docs and the two remaining spikes
Repository basics:
- CONTRIBUTING.md and CODE_OF_CONDUCT.md (Contributor Covenant 2.1)
- SPDX AGPL-3.0-or-later headers on all hand-written sources, checked in CI
- golangci-lint (shared .golangci.yml), ESLint and Prettier, all run in CI

API codegen from api/openapi.yaml (npm run generate, drift fails CI):
- Go types via oapi-codegen in Server/internal/api
- a contract test that the router and the spec list the same routes
- Dart models via Phone-App/tool/generate_api.dart; the app uses them

Spike: shared Tiptap editor in a Flutter WebView (ADR-4)
- @docwell/editor-bridge builds the editor into one HTML page with a
  versioned JSON protocol; Phone-App hosts it with webview_flutter and a
  native toolbar. Integration test passes on an Android API 35 emulator.
- flutter_inappwebview dropped: its Android plugin fails on AGP 9.

Spike: Wails v3 desktop (ADR-11)
- system tray, docwell:// deep links (page and space only) with
  single-instance hand-off, and the built-in updater with a required
  ed25519 signing key. Verified on Windows.
- regenerated build assets register the scheme and drop template
  placeholders.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 16:11:07 +01:00

88 lines
2.5 KiB
Dart

// SPDX-License-Identifier: AGPL-3.0-or-later
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'generated/models.dart';
export 'generated/models.dart';
/// Minimal docwell-server client. Requests are written by hand; the models
/// are generated from api/openapi.yaml (tool/generate_api.dart).
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;
}
}
extension UserRoles on User {
/// Admins manage their organization; superadmins manage everything.
bool get isManager => role == Role.admin || role == Role.superadmin;
}
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)';
}