Skip to content

feat: implement Create Gist API with improved error handling #115

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

Merged
merged 1 commit into from
Jun 7, 2025

Conversation

nullcoder
Copy link
Owner

Summary

This PR implements the Create Gist API endpoint (POST /api/gists) with comprehensive error handling improvements. This addresses Issue #105 and establishes a pattern for consistent API error handling across the application.

Changes

🚀 New Features

  • Create Gist API (POST /api/gists)
    • Multipart/form-data support for uploading encrypted blobs
    • PBKDF2-SHA256 password hashing for edit protection
    • Integration with storage operations for R2 persistence
    • Comprehensive input validation using Zod
    • Support for expiry dates and one-time view options

🛠️ Error Handling Improvements

  • Centralized Error Handling (lib/api-errors.ts)

    • Consistent error response format across all APIs
    • Type-safe error creation and conversion utilities
    • Helper functions for common error scenarios
    • Automatic error logging for unexpected failures
  • API Error Response Format

    {
      error: ErrorCode,      // Standardized error code
      message: string,       // Human-readable message
      details?: Record<string, unknown>  // Optional context
    }

🧪 Testing

  • Full test coverage for Create Gist API (12 tests)
  • Type-safe testing with proper TypeScript type guards
  • No more any type casting in tests
  • Comprehensive error scenario testing

📚 Documentation

  • Created docs/API_ERROR_HANDLING.md with:
    • Best practices for API error handling
    • Common error patterns and examples
    • Migration guide for existing routes
    • Testing strategies

Test Results

All tests passing:

  • ✅ Create Gist API: 12/12 tests
  • ✅ API Error Utilities: 11/11 tests
  • ✅ Type checking: No errors
  • ✅ Linting: Clean

Example Usage

// Create a gist
const formData = new FormData();
formData.append('metadata', new Blob([JSON.stringify({
  expires_at: "2024-12-31T23:59:59Z",
  one_time_view: false
})], { type: 'application/json' }));
formData.append('blob', encryptedData);
formData.append('password', 'optional-edit-password');

const response = await fetch('/api/gists', {
  method: 'POST',
  body: formData
});

Breaking Changes

None. This is a new API endpoint.

Related Issues

Next Steps

🤖 Generated with Claude Code

Co-Authored-By: Claude claude@ghostpaste.dev

- Created POST /api/gists endpoint with multipart/form-data support
- Implemented centralized API error handling utilities
- Added comprehensive type-safe error responses
- Integrated with storage operations and PBKDF2 password hashing
- Added full test coverage with proper TypeScript typing
- Created API error handling documentation

🤖 Generated with Claude Code

Co-Authored-By: Claude <claude@ghostpaste.dev>
@nullcoder nullcoder added api API endpoints and backend logic priority: critical Must be done immediately needs review Ready for code review labels Jun 7, 2025
@nullcoder nullcoder merged commit a930715 into main Jun 7, 2025
1 check was pending
@nullcoder nullcoder deleted the feat/api-error-handling branch June 7, 2025 21:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api API endpoints and backend logic needs review Ready for code review priority: critical Must be done immediately
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feat: implement POST /api/gists endpoint
1 participant