/**
* Flattens a structure of API endpoints with manual overrides.
*
* @param {object} apiEndpoints - The API endpoint structure to flatten.
* @param {object} overrides - An object containing manual overrides for specific endpoints.
* @returns {object} - The flattened API endpoint structure.
*/
function flattenApiEndpoints(apiEndpoints, overrides) {
const flattened = {};
function traverse(current, path) {
for (const key in current) {
if (current.hasOwnProperty(key)) {
const newPath = path ? `${path}.${key}` : key;
if (typeof current[key] === 'object' && current[key] !== null) {
traverse(current[key], newPath);
} else {
// Apply override if present
if (overrides && overrides[newPath]) {
flattened[newPath] = overrides[newPath];
} else {
flattened[newPath] = current[key];
}
}
}
}
}
traverse(apiEndpoints, '');
return flattened;
}
// Example Usage (can be uncommented for testing)
/*
const apiEndpoints = {
'/users': {
'get': {
'url': '/api/users',
'method': 'GET'
},
'/create': {
'url': '/api/users',
'method': 'POST'
}
},
'/products': {
'get': {
'url': '/api/products',
'method': 'GET'
}
}
};
const overrides = {
'/users.get': { 'url': '/new/api/users' },
'/products.get': { 'method': 'HEAD' }
};
const flattenedEndpoints = flattenApiEndpoints(apiEndpoints, overrides);
console.log(JSON.stringify(flattenedEndpoints, null, 2));
*/
Add your comment