Skip to content

Save State (api/saveState.ts)

Endpoint Overview

Handles saving document state to Azure Blob Storage and updating document metadata in the database. Supports both new document creation and updates to existing documents.

Request Handlers

POST

  • Body:
    {
      uuid: string;          // Document unique identifier
      userID: string;        // User ID
      documentName: string;  // Name of the document
      status: string;        // Document status
      clientName?: string;   // Optional client name
      state: string;         // Document data (base64 string)
      alreadyExists: boolean; // Whether document exists
    }
    
  • Response (New Document):
    {
      message: "Document inserted successfully."
    }
    
  • Response (Existing Document):
    {
      message: "Document updated successfully."
    }
    
  • Status: 201 Created, 200 OK, 400 Bad Request, 405 Method Not Allowed, 500 Internal Server Error

Error Handling

try {
  await uploadDocumentToContainer(containerClient, state, documentName);
  // Database operations...
} catch (uploadError) {
  console.error('Error uploading document to blob storage:', uploadError);
  return res.status(500).json({ error: 'Failed to upload document to blob storage.' });
}

Common errors: - 400: Invalid request body - 400: Empty file buffer - 405: Non-POST request method - 500: Blob storage upload failure - 500: Database operation failure

Usage Example

const response = await fetch("/api/saveState", {
  method: "POST",
  headers: { 
    "Content-Type": "application/json"
  },
  body: JSON.stringify({
    uuid: "doc-123",
    userID: "user-456",
    documentName: "example.html",
    status: "draft",
    state: "<html>...</html>",
    alreadyExists: false
  })
});

const result = await response.json();

Implementation Details

  • Uses Azure Blob Storage for document storage
  • Implements Prisma for database operations
  • Validates request using Joi schema
  • Creates user-specific containers
  • Handles both new and existing documents
  • Sets appropriate content types
  • Includes error handling and logging
  • 10MB request size limit
  • Stores blob URLs in database

Pages/Components Referenced By

Notes

  • Requires Azure Storage configuration
  • Creates containers per user
  • Stores HTML content type
  • Updates modification dates
  • Handles large documents
  • Includes retry logic
  • Validates all inputs
  • Uses secure connection string
  • Maintains document history