corrad-af-2024/composables/useScalarConfig.js

209 lines
6.3 KiB
JavaScript

// API Documentation configuration management composable
const apiDocsConfig = ref({
// Basic Information
title: 'Corrad AF 2024 API Platform',
description: 'Complete API reference for the Corrad AF 2024 API Platform project',
version: '2.0.0',
// OpenAPI Source Configuration
sourceType: 'default', // 'default', 'collections', 'custom'
openApiUrl: '/openapi.json', // Default main OpenAPI JSON file
// Theme Configuration
theme: 'light', // light, dark, auto
// Features Configuration
showSidebar: true,
defaultExpandedTags: true,
showSearch: true,
// Advanced Configuration
metaData: {
title: 'Corrad AF 2024 API Documentation',
description: 'Complete API reference for all endpoints in the Corrad AF 2024 platform',
favicon: '/favicon.ico'
},
// Display Options
groupByTags: true,
showParameters: true,
showRequestBody: true,
showResponses: true,
showExamples: true,
// Contact Information
contact: {
name: 'API Support',
email: 'support@corradaf.com',
url: 'https://corradaf.com'
}
});
// Available source types
const availableSourceTypes = [
{ value: 'default', label: 'Default OpenAPI', description: 'Main OpenAPI specification', url: '/openapi.json' },
{ value: 'collections', label: 'Generated from Collections', description: 'Auto-generated from API collections', url: '/openapi-coll.json' },
{ value: 'custom', label: 'Custom JSON', description: 'Custom edited OpenAPI specification', url: '/openapi-custom.json' }
];
// Available theme options
const availableThemes = [
{ value: 'light', label: 'Light', description: 'Light theme' },
{ value: 'dark', label: 'Dark', description: 'Dark theme' },
{ value: 'auto', label: 'Auto', description: 'Follow system preference' }
];
// Configuration loaded flag
let configLoaded = false;
// Load configuration from localStorage
const loadApiDocsConfig = () => {
if (configLoaded) return; // Avoid loading multiple times
try {
const saved = localStorage.getItem('api-docs-config');
if (saved) {
const parsed = JSON.parse(saved);
// Merge with defaults to ensure all properties exist
apiDocsConfig.value = { ...apiDocsConfig.value, ...parsed };
}
configLoaded = true;
} catch (error) {
console.error('Failed to load API docs configuration:', error);
}
};
// Save configuration to localStorage
const saveApiDocsConfig = () => {
try {
localStorage.setItem('api-docs-config', JSON.stringify(apiDocsConfig.value));
return true;
} catch (error) {
console.error('Failed to save API docs configuration:', error);
return false;
}
};
// Update specific configuration
const updateApiDocsConfig = (updates) => {
apiDocsConfig.value = { ...apiDocsConfig.value, ...updates };
// Update OpenAPI URL based on source type
if (updates.sourceType) {
const sourceConfig = availableSourceTypes.find(s => s.value === updates.sourceType);
if (sourceConfig) {
apiDocsConfig.value.openApiUrl = sourceConfig.url;
}
}
return saveApiDocsConfig();
};
// Update source type and corresponding URL
const updateSourceType = (sourceType) => {
const sourceConfig = availableSourceTypes.find(s => s.value === sourceType);
if (sourceConfig) {
apiDocsConfig.value.sourceType = sourceType;
apiDocsConfig.value.openApiUrl = sourceConfig.url;
return saveApiDocsConfig();
}
return false;
};
// Reset to defaults
const resetApiDocsConfig = () => {
apiDocsConfig.value = {
title: 'Corrad AF 2024 API Platform',
description: 'Complete API reference for the Corrad AF 2024 API Platform project',
version: '2.0.0',
sourceType: 'default',
openApiUrl: '/openapi.json',
theme: 'light',
showSidebar: true,
defaultExpandedTags: true,
showSearch: true,
metaData: {
title: 'Corrad AF 2024 API Documentation',
description: 'Complete API reference for all endpoints in the Corrad AF 2024 platform',
favicon: '/favicon.ico'
},
groupByTags: true,
showParameters: true,
showRequestBody: true,
showResponses: true,
showExamples: true,
contact: {
name: 'API Support',
email: 'support@corradaf.com',
url: 'https://corradaf.com'
}
};
configLoaded = false;
return saveApiDocsConfig();
};
// Generate API docs configuration object
const getApiDocsConfiguration = () => {
// Ensure config is loaded before generating
if (!configLoaded && process.client) {
loadApiDocsConfig();
}
const config = apiDocsConfig.value;
// Ensure the URL is valid and use default if not
let openApiUrl = config.openApiUrl;
if (!openApiUrl || typeof openApiUrl !== 'string') {
console.warn('Invalid OpenAPI URL in config, using default');
openApiUrl = '/openapi.json';
}
// Make sure URL starts with / or http(s)
if (!openApiUrl.startsWith('/') && !openApiUrl.startsWith('http')) {
openApiUrl = `/${openApiUrl}`;
}
return {
url: openApiUrl,
title: config.title || 'API Documentation',
description: config.description || '',
version: config.version || '1.0.0',
theme: config.theme || 'light',
showSidebar: Boolean(config.showSidebar),
defaultExpandedTags: Boolean(config.defaultExpandedTags),
showSearch: Boolean(config.showSearch),
groupByTags: Boolean(config.groupByTags),
showParameters: Boolean(config.showParameters),
showRequestBody: Boolean(config.showRequestBody),
showResponses: Boolean(config.showResponses),
showExamples: Boolean(config.showExamples),
metaData: {
title: config.metaData?.title || 'API Documentation',
description: config.metaData?.description || 'Complete API reference',
favicon: config.metaData?.favicon || '/favicon.ico'
},
contact: config.contact || {}
};
};
// Auto-load configuration on client side
if (process.client) {
loadApiDocsConfig();
}
export const useScalarConfig = () => {
return {
// State (keeping the same export name for compatibility)
scalarConfig: readonly(apiDocsConfig),
availableThemes,
availableSourceTypes,
// Methods (keeping the same names for compatibility)
loadScalarConfig: loadApiDocsConfig,
saveScalarConfig: saveApiDocsConfig,
updateScalarConfig: updateApiDocsConfig,
updateSourceType,
resetScalarConfig: resetApiDocsConfig,
getScalarConfiguration: getApiDocsConfiguration
};
};