Skip to content

Document By ID API (api/documents/[id].ts)

Endpoint Overview

Handles operations for individual documents including retrieval, updates, and deletion. Supports document metadata management and content operations.

Request Handlers

GET

  • Query Parameters:
    {
      id: string;  // Document identifier from route
    }
    
  • Response:
    {
      document: {
        id: string;           // Document identifier
        title: string;        // Document title
        content: string;      // Document content
        status: 'processing' | 'completed' | 'failed';  // Processing status
        themes: Theme[];      // Applied themes
        metadata: {
          createdAt: string;  // Creation timestamp
          updatedAt: string;  // Last update timestamp
          author: string;     // Document author
        };
      };
    }
    
  • Status: 200 OK, 401 Unauthorized, 404 Not Found

PUT

  • Body:
    {
      title?: string;              // Optional new title
      content?: string;            // Optional new content
      themes?: Theme[];            // Optional theme updates
      metadata?: Record<string, any>; // Optional metadata updates
    }
    
  • Response: Updated document object
  • Status: 200 OK, 400 Bad Request, 401 Unauthorized

DELETE

  • No body required
  • Response: Success message
  • Status: 200 OK, 401 Unauthorized, 404 Not Found

Error Handling

export default async function handler(
  req: NextApiRequest,
  res: NextApiResponse
) {
  const { id } = req.query;
  const { method } = req;

  try {
    switch (method) {
      case 'GET':
        // Handle GET
        break;
      case 'PUT':
        // Handle PUT
        break;
      case 'DELETE':
        // Handle DELETE
        break;
      default:
        res.setHeader('Allow', ['GET', 'PUT', 'DELETE']);
        res.status(405).end(`Method ${method} Not Allowed`);
    }
  } catch (error) {
    handleError(error, res);
  }
}

Common errors: - 400: Bad Request - Invalid input - 401: Unauthorized - No valid session - 403: Forbidden - No access rights - 404: Not Found - Document doesn't exist - 500: Internal Server Error

Usage Example

// Fetch document
const getResponse = await fetch(`/api/documents/${id}`);
const { document } = await getResponse.json();

// Update document
const putResponse = await fetch(`/api/documents/${id}`, {
  method: 'PUT',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({ 
    title: 'Updated Title',
    themes: [{ id: 'theme1', color: '#FF0000' }]
  })
});

// Delete document
const deleteResponse = await fetch(`/api/documents/${id}`, {
  method: 'DELETE'
});

Implementation Details

  • Supports CRUD operations
  • Implements soft deletion
  • Handles file cleanup
  • Manages permissions
  • Tracks version history
  • Supports partial updates
  • Validates input data
  • Maintains metadata
  • Uses route parameters

Pages/Components Referenced By

Notes

  • Requires authentication
  • Soft deletion enabled
  • History tracking
  • Permission checks
  • Partial updates
  • Route parameters
  • Metadata support
  • File management
  • Version control