Dynamic Configuration Based on Runtime Conditions
Provide an example of how to dynamically adjust application configurations using TanStack Config based on certain runtime conditions, such as user roles or deployment environment.
import { createConfig } from 'tanstack-config';
// Define a base configuration
const baseConfig = {
apiUrl: 'https://api.example.com',
featureFlags: {
enableFeatureX: false,
enableFeatureY: false
}
};
const config = createConfig(async () => {
// Runtime condition: Check user role
const userHasAdminRole = await checkUserRole('admin');
// Runtime condition: Check environment
const isProduction = process.env.NODE_ENV === 'production';
// Adjusting configuration based on runtime conditions
return {
...baseConfig,
apiUrl: isProduction ? 'https://api.example.com' : 'https://dev-api.example.com',
featureFlags: {
...baseConfig.featureFlags,
enableFeatureX: userHasAdminRole,
enableFeatureY: !isProduction
}
};
});
// Function to check user role (mocked for demonstration purposes)
async function checkUserRole(role) {
// Logic to determine if the current user has the specified role
return Promise.resolve(role === 'admin');
}
This code dynamically adjusts the application's configuration using TanStack Config based on runtime conditions such as user roles and deployment environment. It defines a base configuration and modifies it by checking if the user has an admin role and if the environment is production, altering API URLs and feature flags accordingly.