/**
* Invalidates the cache of specified API endpoints for development purposes.
*
* @param {string[]} endpoints An array of API endpoint URLs to invalidate.
* @param {object} options Configuration options.
* @param {boolean} options.dryRun Whether to only log the invalidation actions (default: false).
*/
function invalidateApiCache(endpoints, options = {}) {
const { dryRun = false } = options;
if (!Array.isArray(endpoints)) {
console.error("InvalidateApiCache: Endpoints must be an array.");
return;
}
if (!endpoints || endpoints.length === 0) {
console.warn("InvalidateApiCache: No endpoints provided.");
return;
}
if (typeof dryRun !== 'boolean') {
console.warn("InvalidateApiCache: dryRun must be a boolean. Defaulting to false.");
dryRun = false;
}
for (const endpoint of endpoints) {
if (typeof endpoint !== 'string' || endpoint.trim() === '') {
console.warn(`InvalidateApiCache: Invalid endpoint provided: ${endpoint}. Skipping.`);
continue;
}
try {
// Simulate API cache invalidation. Replace with your actual cache invalidation logic.
console.log(`Invalidating cache for endpoint: ${endpoint}`);
if (!dryRun) {
// Replace with your actual cache invalidation calls
// Example: cacheService.invalidate(endpoint);
// console.log(`Successfully invalidated cache for ${endpoint}`);
}
} catch (error) {
console.error(`InvalidateApiCache: Error invalidating cache for ${endpoint}:`, error);
}
}
}
//Example Usage (for testing/development):
// invalidateApiCache(["/api/users", "/api/products"]);
// invalidateApiCache(["/api/orders", "invalid-endpoint"], { dryRun: true }); //dry run example
// invalidateApiCache(null); //handles null input
// invalidateApiCache([]); //handles empty array
Add your comment