Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

more cleanup #9

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

hassancodess
Copy link
Contributor

@hassancodess hassancodess commented Sep 13, 2024

This PR includes the following updates:

  • Improved formatting for environment variables validation errors.
  • Replaced dotenv with dotenvx.

Summary by Sourcery

Enhance environment variable management by replacing dotenv with dotenvx and improve error formatting for validation errors. Update import paths in the upload controller for better module organization.

Enhancements:

  • Improve formatting for environment variables validation errors by implementing a pretty print function.
  • Replace dotenv with dotenvx for environment variable management.

Copy link

sourcery-ai bot commented Sep 13, 2024

Reviewer's Guide by Sourcery

This PR focuses on improving environment variable handling and code organization. It replaces the 'dotenv' package with 'dotenvx' for environment variable management, enhances error reporting for environment variable validation, and updates import paths for better modularity.

File-Level Changes

Change Details Files
Replaced dotenv with dotenvx and improved environment variable validation error handling
  • Imported dotenvx instead of dotenv
  • Added a prettyPrintErrors function to format ZodErrors
  • Changed config parsing from parse to safeParse
  • Added error handling and pretty printing for environment variable validation errors
  • Removed optional DATABASE_URL from the config schema
src/config/config.service.ts
Updated import paths for better code organization
  • Changed import path for updateUser from '../user/user.services' to '../modules/user/user.services'
  • Changed import path for UserType from '../types' to '../modules/user/user.dto'
src/upload/upload.controller.ts

Tips
  • Trigger a new Sourcery review by commenting @sourcery-ai review on the pull request.
  • Continue your discussion with Sourcery by replying directly to review comments.
  • You can change your review settings at any time by accessing your dashboard:
    • Enable or disable the Sourcery-generated pull request summary or reviewer's guide;
    • Change the review language;
  • You can always contact us if you have any questions or feedback.

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @hassancodess - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Can you provide more context on why the switch from dotenv to dotenvx was made? Are there specific features or benefits we're aiming to leverage?
  • The DATABASE_URL has been removed from the config schema. Was this intentional, or should it be kept?
Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 1 issue found
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment to tell me if it was helpful.

}
}
});
};

// Remove .optional() from requried schema properties
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick (typo): Update or remove the outdated comment

This comment contains a typo ('requried') and doesn't seem to reflect the actual changes made. Consider updating it to accurately describe the current schema or remove it if it's no longer relevant.

dotenv.config();
dotenvx.config();

const prettyPrintErrors = (errObj: ZodError) => {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (complexity): Consider simplifying error handling by using Zod's built-in error formatting.

While the intention to provide more detailed error information is good, the current implementation adds unnecessary complexity. Consider simplifying the error handling by leveraging Zod's built-in error formatting capabilities. Here's a suggested approach:

import dotenvx from '@dotenvx/dotenvx';
import { z } from 'zod';

dotenvx.config();

const configSchema = z.object({
  // ... (keep the existing schema definition)
});

export type Config = z.infer<typeof configSchema>;

const config = configSchema.safeParse(process.env);

if (!config.success) {
  console.error('Error in environment variables:');
  console.error(config.error.format());
  process.exit(1);
}

export default config.data;

This approach:

  1. Uses Zod's built-in format() method to display errors, eliminating the need for a custom prettyPrintErrors function.
  2. Simplifies the error logging while still providing detailed information about which environment variables are incorrect.
  3. Maintains the improved debugging capabilities with less code.

This change reduces complexity while keeping the functionality intact, making the code more maintainable and easier to understand.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants