create project
This commit is contained in:
45
frontend/lib/api/api_client.dart
Normal file
45
frontend/lib/api/api_client.dart
Normal file
@@ -0,0 +1,45 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
||||
|
||||
class ApiClient {
|
||||
late final Dio _dio;
|
||||
final _storage = const FlutterSecureStorage();
|
||||
static const _tokenKey = 'auth_token';
|
||||
|
||||
ApiClient({String? baseUrl}) {
|
||||
_dio = Dio(BaseOptions(
|
||||
baseUrl: baseUrl ?? 'http://localhost:3000/api',
|
||||
connectTimeout: const Duration(seconds: 10),
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
));
|
||||
|
||||
_dio.interceptors.add(InterceptorsWrapper(
|
||||
onRequest: (options, handler) async {
|
||||
final token = await _storage.read(key: _tokenKey);
|
||||
if (token != null) {
|
||||
options.headers['Authorization'] = 'Bearer $token';
|
||||
}
|
||||
handler.next(options);
|
||||
},
|
||||
));
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>> get(String path, {Map<String, dynamic>? params}) async {
|
||||
final res = await _dio.get(path, queryParameters: params);
|
||||
return res.data as Map<String, dynamic>;
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>> post(String path, {dynamic data}) async {
|
||||
final res = await _dio.post(path, data: data);
|
||||
return res.data as Map<String, dynamic>;
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>> put(String path, {dynamic data}) async {
|
||||
final res = await _dio.put(path, data: data);
|
||||
return res.data as Map<String, dynamic>;
|
||||
}
|
||||
|
||||
Future<void> saveToken(String token) => _storage.write(key: _tokenKey, value: token);
|
||||
Future<String?> getToken() => _storage.read(key: _tokenKey);
|
||||
Future<void> clearToken() => _storage.delete(key: _tokenKey);
|
||||
}
|
||||
30
frontend/lib/api/auth_api.dart
Normal file
30
frontend/lib/api/auth_api.dart
Normal file
@@ -0,0 +1,30 @@
|
||||
import 'api_client.dart';
|
||||
|
||||
class AuthApi {
|
||||
final ApiClient _client;
|
||||
AuthApi(this._client);
|
||||
|
||||
Future<Map<String, dynamic>> login(String phone, String password) {
|
||||
return _client.post('/auth/login', data: {'phone': phone, 'password': password});
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>> register(String phone, String password, String nickname) {
|
||||
return _client.post('/auth/register', data: {'phone': phone, 'password': password, 'nickname': nickname});
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>> logout() {
|
||||
return _client.post('/auth/logout');
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>> getProfile() {
|
||||
return _client.get('/user/profile');
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>> bindEmail(String email) {
|
||||
return _client.put('/user/email', data: {'email': email});
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>> switchActiveFamily(String? familyId) {
|
||||
return _client.put('/user/active-family', data: {'familyId': familyId});
|
||||
}
|
||||
}
|
||||
18
frontend/lib/api/bill_api.dart
Normal file
18
frontend/lib/api/bill_api.dart
Normal file
@@ -0,0 +1,18 @@
|
||||
import 'api_client.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
class BillApi {
|
||||
final ApiClient _client;
|
||||
BillApi(this._client);
|
||||
|
||||
Future<Map<String, dynamic>> getBills({String? userId, String? familyId}) {
|
||||
final params = <String, dynamic>{};
|
||||
if (userId != null) params['userId'] = userId;
|
||||
if (familyId != null) params['familyId'] = familyId;
|
||||
return _client.get('/bills', params: params);
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>> createBill(Map<String, dynamic> data) {
|
||||
return _client.post('/bills', data: data);
|
||||
}
|
||||
}
|
||||
26
frontend/lib/api/family_api.dart
Normal file
26
frontend/lib/api/family_api.dart
Normal file
@@ -0,0 +1,26 @@
|
||||
import 'api_client.dart';
|
||||
|
||||
class FamilyApi {
|
||||
final ApiClient _client;
|
||||
FamilyApi(this._client);
|
||||
|
||||
Future<Map<String, dynamic>> getFamilies() {
|
||||
return _client.get('/families');
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>> getFamily(String id) {
|
||||
return _client.get('/families/$id');
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>> createFamily(String name) {
|
||||
return _client.post('/families', data: {'name': name});
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>> joinFamily(String inviteCode) {
|
||||
return _client.post('/families/join', data: {'inviteCode': inviteCode});
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>> leaveFamily(String familyId) {
|
||||
return _client.post('/families/leave', data: {'familyId': familyId});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user