Configuration
Learn how to configure connections, environments, and customize library behavior
Connection Profiles
Connection profiles allow you to store multiple user/environment combinations. Each connection contains credentials and backend information.
Connection Structure
interface Connection {
id: string; // Unique identifier (e.g., "admin@localhost")
name?: string; // Optional display name
environment: Environment; // 'localhost' | 'staging' | 'production'
user: string; // Username for basic auth
password: string; // Password for basic auth
baseUrl: string; // Backend URL
headers?: Record<string, string>; // Optional custom headers
}
Example: Multiple Environments
import { initializeSGERP } from 'sgerp-frontend-lib';
initializeSGERP({
activeConnection: 'admin@staging',
connections: {
'admin@staging': {
id: 'admin@staging',
name: 'Staging Admin',
environment: 'staging',
user: 'admin',
password: 'staging_password',
baseUrl: 'https://sgerp-stage.d.gcdev.swatrider.com',
},
'admin@production': {
id: 'admin@production',
name: 'Production Admin',
environment: 'production',
user: 'admin',
password: 'production_password',
baseUrl: 'https://sgerp.d.gcprod.swatrider.com',
},
'developer@localhost': {
id: 'developer@localhost',
name: 'Local Development',
environment: 'localhost',
user: 'developer',
password: 'dev_password',
baseUrl: 'http://localhost:3000',
},
},
});
Switching Connections
Easily switch between different connection profiles at runtime:
import { switchConnection, getActiveConnection } from 'sgerp-frontend-lib';
// Switch to staging environment
switchConnection('admin@staging');
// Check current connection
const current = getActiveConnection();
console.log(current.id); // "admin@staging"
// Now all API calls use staging credentials
await APIClient.get('/users');
Managing Connections
Add a Connection
import { addConnection } from 'sgerp-frontend-lib';
addConnection({
id: 'test@localhost',
environment: 'localhost',
user: 'test',
password: 'test123',
baseUrl: 'http://localhost:4000',
});
Remove a Connection
import { removeConnection } from 'sgerp-frontend-lib';
// Note: Cannot remove the currently active connection
removeConnection('test@localhost');
Update Password
import { updateConnectionPassword } from 'sgerp-frontend-lib';
updateConnectionPassword('admin@localhost', 'new_password_123');
Get All Connections
import { getAllConnections } from 'sgerp-frontend-lib';
const connections = getAllConnections();
connections.forEach(conn => {
console.log(conn.id, conn.environment);
});
Global Timeout
Set a global timeout for all requests (default: 30 seconds):
initializeSGERP({
timeout: 60000, // 60 seconds
activeConnection: 'admin@localhost',
connections: { /* ... */ },
});
Custom Headers
Add custom headers to specific connections:
initializeSGERP({
activeConnection: 'admin@localhost',
connections: {
'admin@localhost': {
id: 'admin@localhost',
environment: 'localhost',
user: 'admin',
password: 'admin123',
baseUrl: 'http://localhost:3000',
headers: {
'X-Custom-Header': 'custom-value',
'X-API-Version': 'v2',
},
},
},
});