@dbs-portal/tool-mock
Version:
API mocking toolkit using MSW for DBS Portal development workflows
232 lines • 6.55 kB
JavaScript
/**
* Mock configuration management
*/
import { getEnvironmentDefaults, getMockingEnvironmentVariables } from './environment';
/**
* Global mock configuration
*/
let globalMockConfig = null;
/**
* Default mock configuration
*/
const DEFAULT_CONFIG = {
enabled: false,
mode: 'disabled',
baseUrl: '',
delay: 0,
logging: false,
handlers: [],
errorSimulation: {
networkErrorRate: 0,
serverErrorRate: 0,
timeoutErrorRate: 0,
customErrors: [],
},
serviceWorker: {
url: '/mockServiceWorker.js',
scope: '/',
updateViaCache: 'none',
},
};
/**
* Initialize mock configuration with environment-aware defaults
*/
export function initializeMockConfig(overrides = {}) {
const envDefaults = getEnvironmentDefaults();
const envVars = getMockingEnvironmentVariables();
// Parse environment variables
const envConfig = {};
if (envVars.enabled !== undefined) {
envConfig.enabled = envVars.enabled === 'true';
}
if (envVars.mode) {
envConfig.mode = envVars.mode;
}
if (envVars.baseUrl) {
envConfig.baseUrl = envVars.baseUrl;
}
if (envVars.delay) {
const delay = parseInt(envVars.delay, 10);
if (!isNaN(delay)) {
envConfig.delay = delay;
}
}
if (envVars.logging !== undefined) {
envConfig.logging = envVars.logging === 'true';
}
// Merge configurations in order of precedence:
// 1. Default config
// 2. Environment-specific defaults
// 3. Environment variables
// 4. User overrides
globalMockConfig = {
...DEFAULT_CONFIG,
...envDefaults,
...envConfig,
...overrides,
errorSimulation: {
...DEFAULT_CONFIG.errorSimulation,
...envDefaults.errorSimulation,
...envConfig.errorSimulation,
...overrides.errorSimulation,
},
serviceWorker: {
...DEFAULT_CONFIG.serviceWorker,
...envConfig.serviceWorker,
...overrides.serviceWorker,
},
};
return globalMockConfig;
}
/**
* Get the current mock configuration
*/
export function getMockConfig() {
if (!globalMockConfig) {
return initializeMockConfig();
}
return globalMockConfig;
}
/**
* Update the global mock configuration
*/
export function updateMockConfig(updates) {
if (!globalMockConfig) {
globalMockConfig = initializeMockConfig();
}
globalMockConfig = {
...globalMockConfig,
...updates,
errorSimulation: {
...globalMockConfig.errorSimulation,
...updates.errorSimulation,
},
serviceWorker: {
...globalMockConfig.serviceWorker,
...updates.serviceWorker,
},
};
return globalMockConfig;
}
/**
* Reset mock configuration to defaults
*/
export function resetMockConfig() {
globalMockConfig = null;
return initializeMockConfig();
}
/**
* Validate mock configuration
*/
export function validateMockConfig(config) {
const errors = [];
if (config.delay !== undefined) {
if (Array.isArray(config.delay)) {
const [min, max] = config.delay;
if (min < 0 || max < 0) {
errors.push('Delay values must be non-negative');
}
if (min > max) {
errors.push('Minimum delay must be less than or equal to maximum delay');
}
}
else if (typeof config.delay === 'number' && config.delay < 0) {
errors.push('Delay must be non-negative');
}
}
if (config.errorSimulation) {
const { networkErrorRate, serverErrorRate, timeoutErrorRate } = config.errorSimulation;
if (networkErrorRate !== undefined && (networkErrorRate < 0 || networkErrorRate > 1)) {
errors.push('Network error rate must be between 0 and 1');
}
if (serverErrorRate !== undefined && (serverErrorRate < 0 || serverErrorRate > 1)) {
errors.push('Server error rate must be between 0 and 1');
}
if (timeoutErrorRate !== undefined && (timeoutErrorRate < 0 || timeoutErrorRate > 1)) {
errors.push('Timeout error rate must be between 0 and 1');
}
}
if (config.baseUrl && !isValidUrl(config.baseUrl)) {
errors.push('Base URL must be a valid URL');
}
return errors;
}
/**
* Create configuration from setup options
*/
export function createConfigFromSetupOptions(options) {
const baseConfig = options.config || {};
// Add handlers from options to config
if (options.handlers && options.handlers.length > 0) {
baseConfig.handlers = [
...(baseConfig.handlers || []),
...options.handlers,
];
}
return initializeMockConfig(baseConfig);
}
/**
* Get configuration for specific environment
*/
export function getConfigForEnvironment(environment) {
switch (environment) {
case 'development':
return {
enabled: true,
mode: 'development',
logging: true,
delay: [100, 300],
errorSimulation: {
networkErrorRate: 0.01,
serverErrorRate: 0.005,
},
};
case 'testing':
return {
enabled: true,
mode: 'testing',
logging: false,
delay: 0,
errorSimulation: {
networkErrorRate: 0,
serverErrorRate: 0,
},
};
case 'storybook':
return {
enabled: true,
mode: 'storybook',
logging: false,
delay: [50, 150],
errorSimulation: {
networkErrorRate: 0,
serverErrorRate: 0,
},
};
case 'production':
default:
return {
enabled: false,
mode: 'disabled',
logging: false,
delay: 0,
errorSimulation: {
networkErrorRate: 0,
serverErrorRate: 0,
},
};
}
}
/**
* Helper function to validate URLs
*/
function isValidUrl(url) {
try {
new URL(url);
return true;
}
catch {
return false;
}
}
//# sourceMappingURL=config.js.map