Skip to content

Conversation

@SaisakthiM
Copy link

Add Safety Checks for Critical Environment Variables in fetchAPI

This PR improves the fetchAPI utility by adding explicit validation for critical environment variables. Previously, missing GRAPHCMS_PROJECT_API or auth tokens could lead to cryptic errors during runtime. With this change:

  • GRAPHCMS_PROJECT_API is validated at the start of the function.
  • GRAPHCMS_DEV_AUTH_TOKEN and GRAPHCMS_PROD_AUTH_TOKEN are validated based on the preview flag.
  • Clear and descriptive error messages are thrown if any required environment variable is missing.
  • Maintains the original API fetching logic and error handling for json.errors.

Benefits

  • Provides early, clear feedback to developers when environment variables are misconfigured.
  • Prevents runtime errors caused by undefined or missing environment variables.
  • Improves code readability and maintainability.

Example Error Messages After This PR

  • Missing GRAPHCMS_PROJECT_API environment variable
  • Missing GRAPHCMS_DEV_AUTH_TOKEN environment variable
  • Missing GRAPHCMS_PROD_AUTH_TOKEN environment variable

This enhancement increases the robustness of the API utility and follows best practices for handling critical environment dependencies.

@nextjs-bot nextjs-bot added the examples Issue was opened via the examples template. label Dec 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

examples Issue was opened via the examples template.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants