mirror of
https://github.com/bitbaum/orangecat.git
synced 2026-12-05 00:28:27 +01:00
- Bump version to 0.1.1 for tracking - Add webpack-bundle-optimizer script for Next.js config - Simplify ESLint configuration to resolve missing dependencies - Prepare for production deployment with proper version control This deployment includes: - Profile update fixes with banner_url support - LN address validation - Auth cookie fixes for profile updates - Various documentation and testing improvements 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
4.9 KiB
4.9 KiB
Utils Directory
This directory contains utility functions and helper modules used throughout the application. Utilities are organized by functionality and follow a consistent pattern for error handling and type safety.
Directory Structure
utils/
├── api.ts # API utilities
├── validation.ts # Validation utilities
└── formatting.ts # Formatting utilities
Utility Guidelines
-
Function Structure
- Pure functions where possible
- Clear input/output types
- Proper error handling
- Consistent return types
-
Documentation
- JSDoc comments
- Usage examples
- Parameter descriptions
- Return type documentation
-
Testing
- Unit tests for all utilities
- Edge case coverage
- Error scenario testing
- Performance testing
Utility Examples
API Utilities
interface ApiResponse<T> {
data: T;
error: Error | null;
status: number;
}
async function fetchApi<T>(
endpoint: string,
options: RequestInit = {}
): Promise<ApiResponse<T>> {
try {
const response = await fetch(endpoint, {
...options,
headers: {
'Content-Type': 'application/json',
...options.headers,
},
});
const data = await response.json();
return {
data: data as T,
error: null,
status: response.status,
};
} catch (error) {
return {
data: null as T,
error: error as Error,
status: 500,
};
}
}
Validation Utilities
interface ValidationResult {
isValid: boolean;
errors: string[];
}
function validateEmail(email: string): ValidationResult {
const errors: string[] = [];
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!email) {
errors.push('Email is required');
} else if (!emailRegex.test(email)) {
errors.push('Invalid email format');
}
return {
isValid: errors.length === 0,
errors,
};
}
function validatePassword(password: string): ValidationResult {
const errors: string[] = [];
if (!password) {
errors.push('Password is required');
} else if (password.length < 8) {
errors.push('Password must be at least 8 characters');
} else if (!/[A-Z]/.test(password)) {
errors.push('Password must contain at least one uppercase letter');
} else if (!/[0-9]/.test(password)) {
errors.push('Password must contain at least one number');
}
return {
isValid: errors.length === 0,
errors,
};
}
Formatting Utilities
function formatCurrency(amount: number, currency: string = 'USD'): string {
return new Intl.NumberFormat('en-US', {
style: 'currency',
currency,
}).format(amount);
}
function formatDate(date: Date): string {
return new Intl.DateTimeFormat('en-US', {
year: 'numeric',
month: 'long',
day: 'numeric',
}).format(date);
}
function formatBitcoinAmount(sats: number): string {
return `${(sats / 100000000).toFixed(8)} BTC`;
}
Best Practices
-
Error Handling
- Use custom error classes
- Provide meaningful error messages
- Include error codes
- Log errors appropriately
-
Type Safety
- Use TypeScript strictly
- Avoid
anytype - Use proper type guards
- Implement runtime validation
-
Performance
- Optimize for performance
- Use memoization when appropriate
- Avoid unnecessary computations
- Handle large datasets efficiently
-
Testing
- Write comprehensive tests
- Test edge cases
- Verify error handling
- Test performance
Utility Composition
Utilities can be composed to create more complex functionality:
function validateUserInput(input: UserInput): ValidationResult {
const emailResult = validateEmail(input.email);
const passwordResult = validatePassword(input.password);
const nameResult = validateName(input.fullName);
return {
isValid: emailResult.isValid && passwordResult.isValid && nameResult.isValid,
errors: [
...emailResult.errors,
...passwordResult.errors,
...nameResult.errors,
],
};
}
Documentation
Each utility should include:
/**
* Formats a number as currency
*
* @param amount - The amount to format
* @param currency - The currency code (default: 'USD')
* @returns Formatted currency string
*
* @example
* formatCurrency(1000) // '$1,000.00'
* formatCurrency(1000, 'EUR') // '€1,000.00'
*/
function formatCurrency(amount: number, currency: string = 'USD'): string {
// Implementation
}
Error Handling
Utilities should implement proper error handling:
class ValidationError extends Error {
constructor(
public code: string,
message: string,
public field?: string
) {
super(message);
this.name = 'ValidationError';
}
}
function validateInput(input: unknown): asserts input is ValidInput {
if (!isValidInput(input)) {
throw new ValidationError(
'INVALID_INPUT',
'Input does not match expected format',
'input'
);
}
}