saas-platform-core-client
v1.2.0
Published
Core HTTP client with authentication management for SaaS platform
Maintainers
Readme
@saas-platform/core-client
Core HTTP client with authentication management for SaaS platform APIs.
Features
- 🔐 Automatic JWT Management - Handles token refresh automatically
- 🚀 HTTP Client with Interceptors - Built on Axios with smart retry logic
- 📦 TypeScript Support - Full type safety
- 🎯 SaaS Isolation - Automatic SaasId extraction from JWT tokens
- 🔄 Auto Retry - Automatic retry on authentication failures
- 💾 Token Persistence - Stores tokens in localStorage
Installation
npm install @saas-platform/core-clientBasic Usage
import { HttpClient, AuthManager } from '@saas-platform/core-client';
const config = {
saasId: 'your-saas-id',
authServiceUrl: 'http://localhost:8000',
timeout: 30000
};
const authManager = new AuthManager(config.authServiceUrl, config.saasId);
const httpClient = new HttpClient(config, authManager);
// Set base URL for your service
httpClient.setBaseURL('http://localhost:8001');
// Make authenticated requests
const response = await httpClient.get('/api/data');AuthManager
Manages JWT tokens with automatic refresh:
import { AuthManager } from '@saas-platform/core-client';
const authManager = new AuthManager('http://localhost:8000', 'your-saas-id');
// Set tokens after login
authManager.setTokens({
accessToken: 'jwt-token',
refreshToken: 'refresh-token',
expiresIn: 900,
tokenType: 'Bearer'
});
// Check token validity
if (authManager.hasValidToken()) {
const token = authManager.getAccessToken();
}
// Get user info from token
const userId = authManager.getUserIdFromToken();
const saasId = authManager.getSaasIdFromToken();
// Clear tokens (logout)
authManager.clearTokens();HttpClient
HTTP client with automatic authentication:
import { HttpClient } from '@saas-platform/core-client';
const httpClient = new HttpClient(config, authManager);
// All methods return ApiResponse<T>
const users = await httpClient.get<User[]>('/api/users');
const newUser = await httpClient.post<User>('/api/users', userData);
const updatedUser = await httpClient.put<User>('/api/users/123', userData);
await httpClient.delete('/api/users/123');
// Handle responses
if (users.success) {
console.log('Users:', users.data);
} else {
console.error('Error:', users.error?.message);
}Configuration
interface SaasConfig {
saasId: string; // Required: Your SaaS identifier
authServiceUrl: string; // Required: Auth service URL
billingServiceUrl?: string; // Optional: Billing service URL
subscriptionServiceUrl?: string; // Optional: Subscription service URL
timeout?: number; // Optional: Request timeout (default: 30000ms)
retryAttempts?: number; // Optional: Retry attempts (default: 3)
}API Response Format
All HTTP methods return a standardized response:
interface ApiResponse<T> {
success: boolean;
data?: T;
error?: {
code: string;
message: string;
details?: any;
};
timestamp: string;
}Error Handling
The client automatically handles:
- 401 Unauthorized: Attempts token refresh and retries
- Network Errors: Returns structured error response
- Timeout Errors: Configurable timeout with proper error messages
const response = await httpClient.get('/api/data');
if (!response.success) {
switch (response.error?.code) {
case 'NETWORK_ERROR':
// Handle network issues
break;
case 'UNAUTHORIZED':
// Handle auth issues
break;
default:
// Handle other errors
}
}License
MIT
