@sudowealth/schwab-api
Version:
TypeScript client for Charles Schwab API with OAuth support, market data, trading functionality, and complete type safety
49 lines (48 loc) • 1.69 kB
JavaScript
import { API_URLS, TIMEOUTS, } from '../constants';
import { createLogger } from '../utils/secure-logger';
const logger = createLogger('Config');
/**
* Default console logger implementation
*/
const DEFAULT_CONSOLE_LOGGER = {
debug: (message, ...args) => console.debug(message, ...args),
info: (message, ...args) => console.info(message, ...args),
warn: (message, ...args) => logger.warn(message, ...args),
error: (message, ...args) => logger.error(message, ...args),
};
// Default API configuration
const DEFAULT_API_CONFIG = {
baseUrl: API_URLS.PRODUCTION,
environment: 'PRODUCTION',
enableLogging: false,
logLevel: 'info',
apiVersion: 'v1',
timeout: TIMEOUTS.DEFAULT_REQUEST,
// Default logger not set here, will be added in getSchwabApiConfigDefaults
};
/**
* Get a copy of the default API configuration
*/
export function getSchwabApiConfigDefaults() {
return {
...DEFAULT_API_CONFIG,
// Add default logger if enableLogging is true
logger: DEFAULT_CONSOLE_LOGGER,
};
}
/**
* Resolves the environment configuration to determine the appropriate base URL
* This is the central function for environment/URL resolution
*
* @param config The configuration to resolve
* @returns The resolved base URL
*/
export function resolveBaseUrl(config = {}) {
// If a custom baseUrl is provided, use it regardless of environment
if (config.baseUrl) {
return config.baseUrl;
}
// Otherwise, derive the baseUrl from the environment
const environment = config.environment || DEFAULT_API_CONFIG.environment;
return environment === 'PRODUCTION' ? API_URLS.PRODUCTION : API_URLS.SANDBOX;
}