Ocriva Logo

Documents

TypeScript

Import และใช้งาน type definition ครบถ้วนที่ export จาก @ocriva/sdk เพื่อประสบการณ์การพัฒนาแบบ strongly-typed

sdktypescripttypesgenerics

Published: 4/1/2026

TypeScript

@ocriva/sdk เขียนด้วย TypeScript และมี type declaration มาให้ในตัว ไม่จำเป็นต้องติดตั้งแพ็กเกจ @types/ แยกต่างหาก type สาธารณะทั้งหมด export จาก package root เพื่อให้คุณ import ได้โดยตรงพร้อมกับ client

การ Import Type

import {
  OcrivaClient,
  // Error classes
  OcrivaError,
  ApiError,
  AuthenticationError,
  RateLimitError,
  NotFoundError,
  // Types
  type OcrivaClientConfig,
  type PaginationParams,
  type PaginatedResponse,
  type FileType,
  type UploadFileParams,
  type UploadResponse,
  type ResultFormat,
  type ExtractionMode,
  type TemplateDataFile,
  type Template,
  type CreateTemplateParams,
  type UpdateTemplateParams,
  type ListTemplatesParams,
  type Project,
  type ListProjectsParams,
  type ProcessingStatus,
  type ProcessingPriority,
  type ProcessingHistory,
  type ListProcessingHistoryParams,
  type ProcessingHistoryStats,
  type ExportResultParams,
  type BatchStatus,
  type Batch,
  type BatchUploadParams,
  type ListBatchParams,
  type ExportBatchParams,
} from '@ocriva/sdk';

Import เฉพาะที่คุณต้องการ คีย์เวิร์ด type ช่วยให้มั่นใจว่า type-only import ถูกลบออกตอน compile และไม่กระทบ bundle

อ้างอิง Type หลัก

Configuration

OcrivaClientConfig

Option ที่ส่งให้ constructor ของ OcrivaClient

interface OcrivaClientConfig {
  apiKey: string;         // required
  baseUrl?: string;       // default: 'https://api.ocriva.com/v1'
  timeout?: number;       // default: 30000 (ms)
  maxRetries?: number;    // default: 3
}

Pagination

PaginationParams

Query parameter ทั่วไปที่ list method รับได้

interface PaginationParams {
  page?: number;   // 1-based page number
  limit?: number;  // items per page
}

PaginatedResponse

Wrapper ที่ method ที่คืนรายการแบบ paginated ส่งกลับ

interface PaginatedResponse<T> {
  data: T[];
  pagination: {
    page: number;
    limit: number;
    total: number;
    pages: number;
  };
}

Upload

FileType

ประเภทหลักของไฟล์ที่อัปโหลด

type FileType = 'pdf' | 'image' | 'document';

UploadFileParams

Parameter สำหรับ client.upload.file()

interface UploadFileParams {
  fileName: string;
  fileType: FileType;
  fileSize: string;          // bytes as a string
  mimeType: string;
  projectId: string;
  templateId?: string;
  uploadType?: string;
  metadata?: Record<string, unknown>;
  batchId?: string;
}

UploadResponse

ค่าที่คืนจาก client.upload.file()

interface UploadResponse {
  id: string;
  fileName: string;
  fileUrl: string;
  publicUrl: string;
  filePath: string;
  fileSize: string;
  mimeType: string;
  status: 'uploaded' | 'processing' | 'completed' | 'failed';
  templateId?: string;
  projectId: string;
  uploadedAt: string;
  userId: string;
  organizationId?: string;
  bucketName?: string;
}

Templates

ResultFormat

รูปแบบ output ที่ template สามารถสร้างได้

type ResultFormat = 'json' | 'text' | 'xml' | 'html' | 'csv' | 'pdf' | 'docx' | 'image';

ExtractionMode

กลยุทธ์การดึงข้อมูลที่ใช้เมื่อ resultFormat เป็น 'text'

type ExtractionMode = 'structured' | 'freeText';

Template

Template record ที่ API ส่งกลับ

interface Template {
  templateId: string;
  name: string;
  description?: string;
  templateSchema?: Record<string, unknown>;
  projectId: string;
  isActive: boolean;
  createdAt: string;
  updatedAt: string;
  resultFormat?: ResultFormat;
  extractionMode?: ExtractionMode;
  instructions?: string;
}

CreateTemplateParams / UpdateTemplateParams

interface CreateTemplateParams {
  name: string;
  description: string;
  templateSchema: Record<string, unknown>;
  projectId: string;
  instructions?: string;
}
 
interface UpdateTemplateParams {
  projectId: string;  // required for scoping
  name?: string;
  description?: string;
  templateSchema?: Record<string, unknown>;
  instructions?: string;
}

Projects

Project

interface Project {
  id: string;
  name: string;
  description?: string;
  organizationId: string;
  isActive: boolean;
  createdAt: string;
  updatedAt: string;
}

Processing History

ProcessingStatus

ค่า status ที่เป็นไปได้ทั้งหมดสำหรับ processing record

type ProcessingStatus = 'pending' | 'queued' | 'in_progress' | 'completed' | 'failed';

ProcessingPriority

type ProcessingPriority = 'low' | 'medium' | 'high';

ProcessingHistory

Processing history record แบบเต็ม

interface ProcessingHistory {
  id: string;
  uploadId: string;
  projectId: string;
  userId: string;
  fileName: string;
  fileUrl: string;
  filePath: string;
  fileSize: string;
  mimeType: string;
  status: ProcessingStatus;
  templateId?: string;
  templateName?: string;
  resultFormat?: ResultFormat;
  priority: ProcessingPriority;
  processingResult?: Record<string, unknown>;
  errorMessage?: string;
  startedAt?: string;
  completedAt?: string;
  estimatedCompletionTime?: string;
  createdAt: string;
  updatedAt: string;
  metadata?: Record<string, unknown>;
  batchId?: string;
}

ProcessingHistoryStats

interface ProcessingHistoryStats {
  total: number;
  pending: number;
  processing: number;
  completed: number;
  failed: number;
}

Batch

BatchStatus

type BatchStatus = 'uploading' | 'processing' | 'completed' | 'partially_failed' | 'failed';

Batch

interface Batch {
  id: string;
  name: string;
  projectId: string;
  organizationId: string;
  userId: string;
  templateId?: string;
  templateName?: string;
  totalFiles: number;
  completedFiles: number;
  failedFiles: number;
  status: BatchStatus;
  uploadIds: string[];
  processingHistoryIds: string[];
  progress: number;   // 0–100
  metadata?: Record<string, unknown>;
  createdAt: string;
  updatedAt: string;
}

BatchUploadParams

interface BatchUploadParams {
  projectId: string;
  templateId?: string;
  name?: string;
  metadata?: Record<string, unknown>;
}

Generics

PaginatedResponse<T> เป็น generic wrapper คุณสามารถระบุ type annotation ชัดเจน หรือปล่อยให้ TypeScript infer type จาก return ของ method:

import type { PaginatedResponse, ProcessingHistory, Batch } from '@ocriva/sdk';
 
// Inferred — TypeScript knows this is PaginatedResponse<ProcessingHistory>
const historyPage = await client.processingHistory.list({
  projectId: 'proj_abc123',
});
 
// Explicit annotation (useful when storing the result for later)
let batchPage: PaginatedResponse<Batch>;
batchPage = await client.batch.list({ projectId: 'proj_abc123' });
 
// Access the generic T[] array
const batches: Batch[] = batchPage.data;
console.log(`Total: ${batchPage.pagination.total}`);

Type Narrowing

ProcessingStatus เป็น union type ซึ่งช่วยให้ TypeScript ตรวจสอบ exhaustive check ได้:

import type { ProcessingStatus } from '@ocriva/sdk';
 
function describeStatus(status: ProcessingStatus): string {
  switch (status) {
    case 'pending':
      return 'Waiting to be queued';
    case 'queued':
      return 'In queue for processing';
    case 'in_progress':
      return 'Currently being processed';
    case 'completed':
      return 'Processing finished successfully';
    case 'failed':
      return 'Processing failed';
    default: {
      // TypeScript will error here if a new status is added to the SDK
      // but not handled in this switch statement
      const _exhaustive: never = status;
      return _exhaustive;
    }
  }
}

pattern เดียวกันใช้ได้กับ BatchStatus, FileType, ResultFormat และ ProcessingPriority

การรองรับ IDE

เนื่องจาก SDK มี TypeScript declaration ครบถ้วน editor ของคุณจะมี:

  • Autocomplete สำหรับชื่อ method ทุกตัว, ฟิลด์ parameter และ property ของ return type
  • Hover documentation — JSDoc comment จาก source ของ SDK แสดงใน tooltip
  • Inline type error — TypeScript แจ้ง field ที่จำเป็นแต่ขาดหาย (เช่น ลืมใส่ projectId ใน CreateTemplateParams) ก่อนที่คุณจะรันโค้ด

ไม่จำเป็นต้องตั้งค่าเพิ่มเติม ติดตั้งแพ็กเกจแล้วเริ่มพิมพ์โค้ดได้เลย — type ทำงานได้ทันทีกับ VSCode, WebStorm, Neovim with LSP และ editor ที่รองรับ TypeScript อื่น ๆ