@ptkl/sdk - v0.9.9
    Preparing search index...

    Class DMS

    Document Management System (DMS) API client

    Provides comprehensive document and media management capabilities including:

    • File upload, download, and management
    • PDF generation and form filling
    • Data conversion between JSON, CSV, and Excel formats
    • Media processing and EXIF data extraction

    The DMS class includes powerful data conversion capabilities that allow you to:

    • Convert between JSON, CSV, and Excel (.xlsx) formats
    • Handle structured data with header, items, and footer sections
    • Auto-detect structured patterns in JSON arrays
    • Validate data format integrity
    • Analyze data structure and metadata
    • Handle large datasets with memory-efficient processing
    • JSON: Array of objects (recommended for tabular data) or structured objects
    • CSV: Comma-separated values with headers and optional comments
    • Excel: .xlsx format with optional sheet specification

    When converting from JSON, the API supports:

    Explicit Structure: JSON with dedicated sections

    {
    "header": { "content": { "title": "Report" } },
    "items": [{ "name": "Data" }],
    "footer": { "content": { "total": 100 } }
    }

    Auto-Detection: Mixed arrays with metadata and summary objects

    [
    { "metadata": "Header info" },
    { "name": "John", "age": 30 },
    { "summary": "Footer info" }
    ]

    All conversion methods may throw errors with code 3003 for conversion failures. Always wrap calls in try-catch blocks for production use.

    import { DMS } from 'protokol-sdk';

    const dms = new DMS({
    token: 'your-bearer-token',
    host: 'http://localhost:8086'
    });

    const libraryRef = 'your-library-uuid';

    // Convert structured JSON to CSV with comments
    const structuredData = {
    header: { content: { title: "Sales Report" } },
    items: [{ product: "Widget", sales: 100 }],
    footer: { content: { total: 100 } }
    };

    try {
    const csvResult = await dms.convertData(libraryRef, structuredData, {
    from: 'json',
    to: 'csv',
    header_as_comment: true
    });
    console.log(csvResult.data); // CSV with header as comments
    } catch (error) {
    console.error('Conversion failed:', error.message);
    }

    Hierarchy

    • default
      • DMS
    Index

    Constructors

    • Parameters

      • Optionaloptions: { env?: string; host?: string; token?: string }

      Returns DMS

    Properties

    client: AxiosInstance

    Methods

    • Convert data between different formats (JSON, CSV, Excel)

      Supports structured data when converting from JSON format with:

      • Explicit structure: JSON with header, items, and footer properties
      • Auto-detection: Mixed JSON arrays with metadata objects and summary rows

      Parameters

      • lib: string

        Library reference UUID

      • data: any

        Raw data to convert

      • params: DataConversionParams

        Conversion parameters including structured data options

      Returns Promise<AxiosResponse<any, any>>

      Promise resolving to converted data

      // Convert JSON to CSV
      const jsonData = [
      { name: "John Doe", age: 30, email: "john@example.com" },
      { name: "Jane Smith", age: 25, email: "jane@example.com" }
      ];

      const csvResult = await dms.convertData(libraryRef, jsonData, {
      from: 'json',
      to: 'csv'
      });
      console.log(csvResult.data); // CSV string

      // Convert structured JSON with header as comments
      const structuredData = {
      header: {
      content: {
      report_title: "Monthly Sales Report",
      generated_by: "Sales System"
      }
      },
      items: [
      { product: "Widget A", sales: 100 },
      { product: "Widget B", sales: 150 }
      ],
      footer: {
      content: {
      total_sales: 250
      }
      }
      };

      const csvWithComments = await dms.convertData(libraryRef, structuredData, {
      from: 'json',
      to: 'csv',
      header_as_comment: true,
      separator_rows: 2
      });

      // Convert JSON to Excel with custom sheet name
      const excelResult = await dms.convertData(libraryRef, jsonData, {
      from: 'json',
      to: 'excel',
      sheet_name: 'Customer Data'
      });
      // excelResult.data is a Blob
    • Parameters

      • lib: string
      • path: string

      Returns Promise<AxiosResponse<any, any>>

    • Convert CSV data to Excel (.xlsx) format

      Parameters

      • lib: string

        Library reference UUID

      • csvData: string

        CSV data string (with headers in first row)

      • Optionaloptions: ConversionOptions

        Optional conversion options

      Returns Promise<AxiosResponse<Blob, any>>

      Promise resolving to Excel file as Blob

      const csvString = `name,age,email
      John Doe,30,john@example.com
      Jane Smith,25,jane@example.com`;

      const excelResponse = await dms.csvToExcel(libraryRef, csvString, {
      sheet_name: 'Imported Data'
      });

      // Handle the Excel blob
      const blob = excelResponse.data;
      const url = URL.createObjectURL(blob);
      // Use url for download or further processing
    • Convert CSV data to JSON format

      Parameters

      • lib: string

        Library reference UUID

      • csvData: string

        CSV data string (with headers in first row)

      Returns Promise<AxiosResponse<any[], any>>

      Promise resolving to JSON array

      const csvString = `name,age,email
      John Doe,30,john@example.com
      Jane Smith,25,jane@example.com`;

      const jsonResponse = await dms.csvToJson(libraryRef, csvString);
      console.log(jsonResponse.data);
      // Output:
      // [
      // { name: "John Doe", age: "30", email: "john@example.com" },
      // { name: "Jane Smith", age: "25", email: "jane@example.com" }
      // ]
    • Parameters

      • data: any

      Returns Promise<AxiosResponse<any, any>>

    • Parameters

      • lib: string
      • path: string

      Returns Promise<AxiosResponse<any, any>>

    • Parameters

      • lib: string
      • data: string

      Returns Promise<AxiosResponse<any, any>>

    • Parameters

      • lib: string
      • key: string

      Returns Promise<AxiosResponse<any, any>>

    • Convert Excel (.xlsx) data to CSV format

      Parameters

      • lib: string

        Library reference UUID

      • excelData: ArrayBuffer | Blob

        Excel file data as Blob or ArrayBuffer

      • Optionaloptions: ConversionOptions

        Optional conversion options

      Returns Promise<AxiosResponse<string, any>>

      Promise resolving to CSV string

      // From file input
      const fileInput = document.querySelector('input[type="file"]');
      const file = fileInput.files[0]; // Excel file

      const csvResponse = await dms.excelToCsv(libraryRef, file, {
      sheet_name: 'Data' // optional, defaults to first sheet
      });

      console.log(csvResponse.data);
      // Output: CSV string with data from Excel sheet

      // Save as CSV file
      const csvBlob = new Blob([csvResponse.data], { type: 'text/csv' });
      const url = URL.createObjectURL(csvBlob);
      const link = document.createElement('a');
      link.href = url;
      link.download = 'converted.csv';
      link.click();
    • Convert Excel (.xlsx) data to JSON format

      Parameters

      • lib: string

        Library reference UUID

      • excelData: ArrayBuffer | Blob

        Excel file data as Blob or ArrayBuffer

      • Optionaloptions: ConversionOptions

        Optional conversion options

      Returns Promise<AxiosResponse<any[], any>>

      Promise resolving to JSON array

      // From file input
      const fileInput = document.querySelector('input[type="file"]');
      const file = fileInput.files[0]; // Excel file

      const jsonResponse = await dms.excelToJson(libraryRef, file, {
      sheet_name: 'Sheet1' // optional, defaults to first sheet
      });

      console.log(jsonResponse.data);
      // Output: JSON array with data from Excel sheet

      // From ArrayBuffer
      const arrayBuffer = await file.arrayBuffer();
      const jsonFromBuffer = await dms.excelToJson(libraryRef, arrayBuffer);
    • Parameters

      Returns Promise<AxiosResponse<any, any>>

    • Get information about data format and structure

      Parameters

      • lib: string

        Library reference UUID

      • data: any

        Raw data to analyze

      • params: DataInfoParams

        Analysis parameters

      Returns Promise<AxiosResponse<DataInfo, any>>

      Promise resolving to data information

      const jsonData = [
      { name: "John", age: 30, email: "john@example.com" },
      { name: "Jane", age: 25, email: "jane@example.com" }
      ];

      const dataInfo = await dms.getDataInfo(libraryRef, jsonData, {
      format: 'json'
      });

      console.log(dataInfo.data);
      // Output:
      // {
      // format: "json",
      // size_bytes: 245,
      // record_count: 2,
      // field_count: 3,
      // fields: ["name", "age", "email"],
      // library_ref: "98bee1cb-0f21-4582-a832-7c32b4b61831"
      // }
    • Parameters

      • lib: string
      • key: string

      Returns Promise<AxiosResponse<any, any>>

    • Parameters

      • lib: string
      • key: string
      • encoding: string

      Returns Promise<AxiosResponse<any, any>>

    • Parameters

      Returns Promise<AxiosResponse<any, any>>

    • Convert JSON data to CSV format

      This method supports both regular JSON arrays and structured data with auto-detection:

      • Regular arrays are converted directly to CSV
      • Structured data (with metadata objects) is automatically detected and formatted

      Parameters

      • lib: string

        Library reference UUID

      • jsonData: any

        JSON data (array of objects or structured data)

      Returns Promise<AxiosResponse<string, any>>

      Promise resolving to CSV string

      // Regular JSON to CSV
      const jsonData = [
      { name: "John Doe", age: 30, email: "john@example.com" },
      { name: "Jane Smith", age: 25, email: "jane@example.com" }
      ];

      const csvResponse = await dms.jsonToCsv(libraryRef, jsonData);
      console.log(csvResponse.data);
      // Output:
      // name,age,email
      // John Doe,30,john@example.com
      // Jane Smith,25,jane@example.com

      // Structured JSON with auto-detection
      const structuredData = [
      { metadata: "EMPLOYEE REPORT\nGenerated: 2025-10-08" },
      { name: "John Doe", age: 30, position: "Developer" },
      { name: "Jane Smith", age: 25, position: "Designer" },
      { name: "Total Employees:", age: null, position: "2 people" }
      ];

      const structuredCsv = await dms.jsonToCsv(libraryRef, structuredData);
      // Auto-detects header, items, and footer sections
    • Convert JSON data to Excel (.xlsx) format

      Supports both regular JSON arrays and structured data patterns. Excel files are always generated with .xlsx extension.

      Parameters

      • lib: string

        Library reference UUID

      • jsonData: any

        JSON data (array of objects or structured data)

      • Optionaloptions: ConversionOptions

        Optional conversion options

      Returns Promise<AxiosResponse<Blob, any>>

      Promise resolving to Excel file as Blob

      // Regular JSON to Excel
      const jsonData = [
      { name: "John Doe", age: 30, email: "john@example.com" },
      { name: "Jane Smith", age: 25, email: "jane@example.com" }
      ];

      // Basic conversion
      const excelResponse = await dms.jsonToExcel(libraryRef, jsonData);
      const blob = excelResponse.data; // Blob for download

      // With custom sheet name
      const excelWithOptions = await dms.jsonToExcel(libraryRef, jsonData, {
      sheet_name: 'Customer Data'
      });

      // Structured data with explicit sections
      const structuredData = {
      header: { content: { title: "Monthly Report" } },
      items: [{ product: "Widget A", sales: 100 }],
      footer: { content: { total_sales: 100 } }
      };

      const structuredExcel = await dms.jsonToExcel(libraryRef, structuredData);

      // Create download link
      const url = URL.createObjectURL(structuredExcel.data);
      const link = document.createElement('a');
      link.href = url;
      link.download = 'report.xlsx'; // Always .xlsx extension
      link.click();
    • Returns Promise<AxiosResponse<any, any>>

    • Parameters

      • data: any

      Returns Promise<AxiosResponse<any, any>>

    • Parameters

      • method: string
      • endpoint: string
      • Optionalparams: any

      Returns Promise<AxiosResponse<any, any>>

    • Parameters

      • method: string
      • endpoint: string
      • Optionalparams: any

      Returns Promise<AxiosResponse<any, any>>

    • Parameters

      • client: AxiosInstance

      Returns DMS

    • Parameters

      • payload: any

      Returns Promise<undefined | AxiosResponse<any, any>>

    • Parameters

      • data: any

      Returns Promise<AxiosResponse<any, any>>

    • Validate data format without performing conversion

      Parameters

      • lib: string

        Library reference UUID

      • data: any

        Raw data to validate

      • params: DataValidationParams

        Validation parameters

      Returns Promise<AxiosResponse<DataValidationResult, any>>

      Promise resolving to validation result

      const jsonData = [{ name: "John", age: 30 }];

      const validation = await dms.validateData(libraryRef, jsonData, {
      format: 'json'
      });

      console.log(validation.data);
      // Output:
      // {
      // valid: true,
      // message: "Data is valid JSON format",
      // library_ref: "98bee1cb-0f21-4582-a832-7c32b4b61831"
      // }

      // Handle invalid data
      try {
      const invalidValidation = await dms.validateData(libraryRef, "invalid json", {
      format: 'json'
      });
      } catch (error) {
      console.error('Validation failed:', error.response?.data?.message);
      }