Blog>
Snippets

Refactoring to TanStack Config from a Legacy Config System

Provide an example of transitioning from a legacy configuration system to TanStack Config, focusing on maintaining backward compatibility.
// Assuming we have a legacyConfig object from the old system
const legacyConfig = {
  endpoint: 'https://api.example.com',
  timeout: 5000
};

// Function to adapt legacy configuration to TanStack Config
function adaptLegacyConfig(legacyConfig) {
  return {
    queryClientConfig: {
      defaultOptions: {
        queries: {
          refetchOnWindowFocus: false,
          retry: false,
          staleTime: Infinity,
          cacheTime: legacyConfig.timeout // Adapted legacy timeout
        }
      },
      networkMode: 'offlineFirst' // Example of an additional option not related to legacy
    }
  };
}

// Using the adapted config with TanStack Query
const adaptedConfig = adaptLegacyConfig(legacyConfig);
This code demonstrates how to adapt a legacy configuration object to be compatible with TanStack Config, specifically focusing on adapting timeout settings for caching and adjusting other default behaviors. The adaptLegacyConfig function converts the old settings into a format that could be used with TanStack Query, ensuring backward compatibility while leveraging new features.