Blog>
Snippets

Environment-Specific Configurations

Explain how to manage different configurations for development, testing, and production environments using TanStack Config.
import { createConfig } from '@tanstack/config';

// Define a common configuration
const commonConfig = {
  appName: 'MyApp',
  apiBaseUrl: '',
};

// Define environment-specific configurations
const envConfig = {
  development: {
    apiBaseUrl: 'https://dev.api.example.com',
  },
  test: {
    apiBaseUrl: 'https://test.api.example.com',
  },
  production: {
    apiBaseUrl: 'https://api.example.com',
  },
};

// Create and export the configuration
export const config = createConfig({
  ...commonConfig,
  ...envConfig[process.env.NODE_ENV || 'development'],
});
This code demonstrates how to use TanStack Config to manage environment-specific configurations. It defines a common configuration applicable to all environments, and then overrides or extends it with environment-specific details. The process.env.NODE_ENV variable determines the current environment, defaulting to 'development' if unspecified. Finally, it creates and exports the final configuration using createConfig.