longvan_dart_sdk 1.0.0
longvan_dart_sdk: ^1.0.0 copied to clipboard
A comprehensive Dart SDK for LongVan services with native GraphQL and REST support. Features lazy loading, multi-tenant context management, and type-safe API access.
LongVan Dart SDK #
A comprehensive Dart SDK for LongVan services with native GraphQL and REST support.
🎯 Features #
- Native GraphQL Support: Direct GraphQL queries and mutations without conversion
- REST API Support: Full REST client for REST endpoints
- Lazy Loading: Services are created only when needed for optimal performance
- Multi-tenant Ready: Flexible context management for any application type
- Type Safe: Full Dart type safety and compile-time checks
- Service-Specific Endpoints: Each service has its own dedicated endpoint
- Extensible: Easy to add new services and customize functionality
🚀 Quick Start #
import 'package:longvan_dart_sdk/longvan_dart_sdk.dart';
void main() async {
// Tạo SDK instance với environment và context
final sdk = await LongVanSDK.create(
environment: Environment.dev,
orgId: 'your-org-id',
storeId: 'your-store-id',
);
// Sử dụng authentication service
final loginResponse = await sdk.auth.login(LoginRequest(
username: 'user@example.com',
password: 'password123',
));
// Cập nhật context với thông tin login
sdk.setContext(
orgId: loginResponse.orgId,
accessToken: loginResponse.accessToken,
);
print('Đăng nhập thành công: ${loginResponse.fullName}');
}
🔧 Authentication Service #
// Đăng nhập
final loginResponse = await sdk.auth.login(LoginRequest(
username: 'user@example.com',
password: 'password123',
));
// Gửi OTP
final otpResponse = await sdk.auth.sendOTP('+84123456789');
// Xác thực OTP
final validateResponse = await sdk.auth.validateOTP('123456', '+84123456789');
// Lấy access token bằng OTP
final tokenResponse = await sdk.auth.getAccessTokenByOTP('123456', '+84123456789');
🌍 Environment Configuration #
SDK hỗ trợ hai môi trường với endpoint riêng cho từng service:
Development (dev) #
- Auth:
https://crm.dev.longvan.vn/authorization-gateway/graphql - Product:
https://product-service.dev.longvan.vn/product-service/graphql - User:
https://user.dev.longvan.vn/user-gateway/graphql - Order:
https://storefront.dev.longvan.vn/v2
Production (live) #
- Auth:
https://crm.longvan.vn/authorization-gateway/graphql - Product:
https://product-service.longvan.vn/product-service/graphql - User:
https://user.longvan.vn/user-gateway/graphql - Order:
https://storefront.longvan.vn/v2
🎛️ Context Management #
// Cập nhật context
sdk.setContext(
orgId: 'new-org-id',
storeId: 'new-store-id',
accessToken: 'new-access-token',
);
// Kiểm tra trạng thái xác thực
if (sdk.isAuthenticated) {
print('Người dùng đã được xác thực');
}
⚠️ Error Handling #
SDK trả về lỗi trực tiếp từ BE, không wrap hay xử lý gì thêm:
try {
final loginResponse = await sdk.auth.login(loginRequest);
print('Đăng nhập thành công: ${loginResponse.fullName}');
} on GraphQLException catch (e) {
// Lỗi GraphQL trực tiếp từ BE
print('GraphQL Error: ${e.message}');
if (e.errors != null) {
for (final error in e.errors!) {
print('- ${error['message']}');
}
}
} on RestException catch (e) {
// Lỗi REST trực tiếp từ BE
print('REST Error: ${e.message} (Status: ${e.statusCode})');
} catch (e) {
// Các lỗi khác (network, parsing, etc.)
print('Lỗi: $e');
}
📝 Example #
Xem file example/auth_example.dart để biết cách sử dụng chi tiết.
🤝 Contributing #
- Fork repository
- Tạo feature branch
- Commit changes
- Push to branch
- Mở Pull Request
📄 License #
MIT License