Files
Cato 20d75f1b9a feat(auth): one password rule from @bitbaum/accountkit (non-empty, up to 128) (#1461)
* feat(auth): one password rule from accountkit (non-empty, up to 128); remove complexity rules, common-password list and strength meter

* fix(auth): reset-password form and settings use the one rule; drop stale 8-char docs example

* docs(utils): password example follows the one rule
2026-10-28 04:03:10 +01:00

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

  1. Function Structure

    • Pure functions where possible
    • Clear input/output types
    • Proper error handling
    • Consistent return types
  2. Documentation

    • JSDoc comments
    • Usage examples
    • Parameter descriptions
    • Return type documentation
  3. 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 > 128) {
    // The one bitbaum password rule: not empty, at most 128. Use passwordProblem()
    // from @bitbaum/accountkit/auth/password in real code; no other rules.
    errors.push('Password is too long');
  }

  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

  1. Error Handling

    • Use custom error classes
    • Provide meaningful error messages
    • Include error codes
    • Log errors appropriately
  2. Type Safety

    • Use TypeScript strictly
    • Avoid any type
    • Use proper type guards
    • Implement runtime validation
  3. Performance

    • Optimize for performance
    • Use memoization when appropriate
    • Avoid unnecessary computations
    • Handle large datasets efficiently
  4. 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'
    );
  }
}