Skip to content

Update Document API (api/documents/updateDocument.ts)

Endpoint Overview

Updates the content and properties of an existing document. Supports version control, content format validation, and concurrent editing protection.

Request Handlers

PUT

  • Body:
    {
      documentId: string;  // ID of the document to update
      content: string;     // Updated document content
      version: number;     // Current document version
      format?: string;     // Content format (html, markdown, text)
      saveAsNew?: boolean; // Whether to create new version (default: true)
    }
    
  • Response:
    {
      success: boolean;
      document: {
        id: string;           // Document identifier
        version: number;      // New version number
        lastModified: string; // Modification timestamp
        size: number;         // Content size in bytes
        format: string;       // Content format
        status: string;       // Document status
        previousVersion: number; // Previous version number
      }
    }
    
  • Status: 200 OK, 400 Bad Request, 401 Unauthorized, 409 Conflict

Error Handling

try {
  // Validate document existence and access
  const document = await prisma.documents.findUnique({
    where: { id: documentId }
  });

  if (!document) {
    throw new Error('DOCUMENT_NOT_FOUND');
  }

  // Version check
  if (document.version !== version) {
    throw new Error('VERSION_CONFLICT');
  }

  // Content validation and update
} catch (error) {
  return {
    error: error.message,
    code: error.code
  };
}

Common errors: - DOCUMENT_NOT_FOUND: Document does not exist - UNAUTHORIZED: User not authorized - INVALID_ID: Document ID is invalid - VERSION_CONFLICT: Document version mismatch - INVALID_FORMAT: Content format not supported - CONTENT_TOO_LARGE: Content exceeds size limit

Usage Example

const response = await fetch("/api/documents/updateDocument", {
  method: "PUT",
  headers: { 
    "Content-Type": "application/json"
  },
  body: JSON.stringify({
    documentId: "doc_123",
    content: "<p>Updated document content...</p>",
    version: 2,
    format: "html",
    saveAsNew: true
  })
});

const { success, document } = await response.json();
if (success) {
  console.log(`Updated to version ${document.version}`);
}

Implementation Details

  • Implements version control
  • Validates content format
  • Supports concurrent editing
  • Maintains edit history
  • Handles large content
  • Triggers update events
  • Size limit: 10MB
  • Format validation
  • Version tracking

Pages/Components Referenced By

Notes

  • Requires authentication
  • Version control enabled
  • History maintained
  • Format validation
  • 10MB size limit
  • Event triggers
  • Concurrency protection
  • Atomic updates
  • Supports versioning