Skip to content

Get Document Metadata (api/documents/getDocumentMetadata.ts)

Endpoint Overview

Retrieves specific metadata fields from a document stored in Azure Blob Storage. Validates user access and retrieves blob properties including content type and custom metadata.

Request Handlers

POST

  • Body:
    {
      userID: string;       // User identifier
      companyID: string;    // Company identifier
      documentID: string;   // Document identifier
      metadataField: string; // Metadata field to retrieve
    }
    
  • Response:
    {
      // Document metadata properties
      contentType: string;
      metadata: {
        [key: string]: string;
      }
    }
    
  • Status: 200 OK, 400 Bad Request, 500 Internal Server Error

Error Handling

try {
  const { error, value } = schema.validate(req.body);
  if (error) {
    res.status(400).send(error.details[0].message);
    return;
  }

  const blobClient = await getBlobClient(userID, companyID, documentID);
  const result = await getDocumentMetadata(blobClient, metadataField);
} catch (error) {
  console.error(`Error retrieving metadata for documentID ${documentID}:`, error);
  res.status(500).send("Error retrieving document metadata");
}

Common errors: - 400: Invalid request body - 400: Missing required fields - 500: Blob storage error - 500: Metadata retrieval error

Usage Example

const response = await fetch("/api/documents/getDocumentMetadata", {
  method: "POST",
  headers: { 
    "Content-Type": "application/json"
  },
  body: JSON.stringify({
    userID: "123",
    companyID: "comp-456",
    documentID: "doc-789",
    metadataField: "status"
  })
});

const metadata = await response.json();

Implementation Details

  • Uses Azure Blob Storage
  • Implements Joi for validation
  • Constructs blob paths using IDs
  • Retrieves blob properties
  • Accesses custom metadata
  • Uses company-specific containers
  • Organizes by user folders
  • Includes error logging
  • Returns typed metadata

Pages/Components Referenced By

Notes

  • Requires Azure Storage configuration
  • Uses hierarchical storage
  • Validates all inputs
  • Supports custom metadata
  • Returns content type
  • Handles missing metadata
  • Logs operation errors
  • Uses secure connection string
  • Maintains company isolation