1. /**
  2. * Invalidates the cache of specified API endpoints for development purposes.
  3. *
  4. * @param {string[]} endpoints An array of API endpoint URLs to invalidate.
  5. * @param {object} options Configuration options.
  6. * @param {boolean} options.dryRun Whether to only log the invalidation actions (default: false).
  7. */
  8. function invalidateApiCache(endpoints, options = {}) {
  9. const { dryRun = false } = options;
  10. if (!Array.isArray(endpoints)) {
  11. console.error("InvalidateApiCache: Endpoints must be an array.");
  12. return;
  13. }
  14. if (!endpoints || endpoints.length === 0) {
  15. console.warn("InvalidateApiCache: No endpoints provided.");
  16. return;
  17. }
  18. if (typeof dryRun !== 'boolean') {
  19. console.warn("InvalidateApiCache: dryRun must be a boolean. Defaulting to false.");
  20. dryRun = false;
  21. }
  22. for (const endpoint of endpoints) {
  23. if (typeof endpoint !== 'string' || endpoint.trim() === '') {
  24. console.warn(`InvalidateApiCache: Invalid endpoint provided: ${endpoint}. Skipping.`);
  25. continue;
  26. }
  27. try {
  28. // Simulate API cache invalidation. Replace with your actual cache invalidation logic.
  29. console.log(`Invalidating cache for endpoint: ${endpoint}`);
  30. if (!dryRun) {
  31. // Replace with your actual cache invalidation calls
  32. // Example: cacheService.invalidate(endpoint);
  33. // console.log(`Successfully invalidated cache for ${endpoint}`);
  34. }
  35. } catch (error) {
  36. console.error(`InvalidateApiCache: Error invalidating cache for ${endpoint}:`, error);
  37. }
  38. }
  39. }
  40. //Example Usage (for testing/development):
  41. // invalidateApiCache(["/api/users", "/api/products"]);
  42. // invalidateApiCache(["/api/orders", "invalid-endpoint"], { dryRun: true }); //dry run example
  43. // invalidateApiCache(null); //handles null input
  44. // invalidateApiCache([]); //handles empty array

Add your comment