Blog>
Snippets

Debugging Common Issues When Using TanStack Config

Offer practical code examples for debugging common issues encountered when using TanStack Config, such as configuration values not loading as expected.
import { createConfig } from 'tanstack-config';

// Define the schema for your config
const config = createConfig({
  schema: {
    apiUrl: '',
    apiKey: '',
  },
});

// Attempt to load the configuration
config.load({
  apiUrl: process.env.API_URL,
  apiKey: process.env.API_KEY,
});
This code snippet demonstrates how to define and load configuration using TanStack Config. A schema is defined to specify the expected configuration fields, and the `load` method is used to populate the config with values, typically from environment variables.
if (!config.get('apiUrl') || !config.get('apiKey')) {
  console.error('Configuration for apiUrl or apiKey is missing.');
}
This snippet checks if the `apiUrl` or `apiKey` is missing from the configuration after an attempt to load them. If either is missing, it logs an error message to the console, which helps in identifying issues with loading the configuration values.
config.onMissingKey(({ key }) => {
  console.warn(`Missing config key: ${key}`);
});
Here, an event listener is set up to warn about any missing configuration keys. This is useful for catching cases where some expected configuration keys have not been provided, aiding in the debugging of missing configuration values.