API Reference - Connections

Complete API reference for managing connection profiles

Functions

initializeSGERP

Initialize the library with connection configuration.

function initializeSGERP(config: SGERPConfig): void

Parameters:

  • config.activeConnection (string) - ID of the initially active connection
  • config.connections (Record) - Map of connection IDs to connection objects
  • config.timeout (number, optional) - Global timeout in milliseconds (default: 30000)

Throws: Error if activeConnection doesn't exist in connections

Example:

initializeSGERP({
  activeConnection: 'admin@localhost',
  timeout: 30000,
  connections: {
    'admin@localhost': {
      id: 'admin@localhost',
      environment: 'localhost',
      user: 'admin',
      password: 'admin123',
      baseUrl: 'http://localhost:3000',
    },
  },
});

getActiveConnection

Get the currently active connection profile.

function getActiveConnection(): Connection

Returns: The active Connection object

Throws: Error if library not initialized or connection not found

getAllConnections

Get all available connection profiles.

function getAllConnections(): Connection[]

Returns: Array of all Connection objects

getConnection

Get a specific connection by ID.

function getConnection(connectionId: string): Connection | undefined

Parameters:

  • connectionId (string) - The connection ID to retrieve

Returns: Connection object or undefined if not found

switchConnection

Switch to a different connection profile.

function switchConnection(connectionId: string): void

Parameters:

  • connectionId (string) - The connection ID to switch to

Throws: Error if connection ID not found

addConnection

Add a new connection profile.

function addConnection(connection: Connection): void

Parameters:

  • connection (Connection) - The connection object to add

removeConnection

Remove a connection profile.

function removeConnection(connectionId: string): void

Parameters:

  • connectionId (string) - The connection ID to remove

Throws: Error if trying to remove the active connection

updateConnectionPassword

Update the password for a connection.

function updateConnectionPassword(
  connectionId: string,
  password: string
): void

Parameters:

  • connectionId (string) - The connection ID to update
  • password (string) - The new password

Throws: Error if connection not found

getBaseUrl

Get the base URL for the active connection.

function getBaseUrl(): string

Returns: The base URL string

getTimeout

Get the global timeout value.

function getTimeout(): number

Returns: Timeout in milliseconds

Types

Connection

interface Connection {
  id: string;
  name?: string;
  environment: Environment;
  user: string;
  password: string;
  baseUrl: string;
  headers?: Record<string, string>;
}

Environment

type Environment = 'localhost' | 'staging' | 'production';

SGERPConfig

interface SGERPConfig {
  connections: Record<string, Connection>;
  activeConnection: string;
  timeout?: number;
}

Examples

Complete Setup Example

import {
  initializeSGERP,
  switchConnection,
  getActiveConnection,
  getAllConnections,
} from 'sgerp-frontend-lib';

// Initialize
initializeSGERP({
  activeConnection: 'admin@localhost',
  timeout: 30000,
  connections: {
    'admin@localhost': {
      id: 'admin@localhost',
      environment: 'localhost',
      user: 'admin',
      password: 'admin123',
      baseUrl: 'http://localhost:3000',
    },
    'user@production': {
      id: 'user@production',
      environment: 'production',
      user: 'user',
      password: 'prod_pass',
      baseUrl: 'https://api.sgerp.com',
    },
  },
});

// List all connections
const connections = getAllConnections();
console.log('Available connections:', connections.map(c => c.id));

// Get current
const current = getActiveConnection();
console.log('Current:', current.id);

// Switch
switchConnection('user@production');
console.log('Switched to production');