ข้ามไปยังเนื้อหาหลัก

TypeScript Types

Types ทั้งหมดที่ส่งออกจาก Dropup

การนำเข้า Types

import type {
DropupFile,
DropupState,
DropupActions,
DropupError,
UseDropupOptions,
UseDropupReturn,
FileStatus,
DropupStatus,
ValidationRule,
ValidationError,
UploadConfig,
CustomUploader,
UploadOptions,
UploadResult,
} from '@samithahansaka/dropup';

Core Types

DropupFile

แทนไฟล์ในคิวอัปโหลด

interface DropupFile {
/** ตัวระบุเฉพาะ */
id: string;

/** File object ดั้งเดิมของเบราว์เซอร์ */
file: File;

/** ชื่อไฟล์ */
name: string;

/** ขนาดไฟล์เป็นไบต์ */
size: number;

/** MIME type */
type: string;

/** สถานะการอัปโหลดปัจจุบัน */
status: FileStatus;

/** ความคืบหน้าการอัปโหลด 0-100 */
progress: number;

/** Preview URL สำหรับรูปภาพ (Object URL) */
preview?: string;

/** URL หลังอัปโหลดสำเร็จ */
uploadedUrl?: string;

/** การตอบกลับดิบจากเซิร์ฟเวอร์ */
response?: unknown;

/** รายละเอียดข้อผิดพลาดถ้าล้มเหลว */
error?: DropupError;

/** Metadata ที่กำหนดเอง */
meta?: Record<string, unknown>;
}

FileStatus

ค่าสถานะไฟล์ที่เป็นไปได้

type FileStatus = 'idle' | 'uploading' | 'complete' | 'error' | 'paused';

DropupState

สถานะปัจจุบันของ dropzone

interface DropupState {
/** กำลังลากไฟล์อยู่เหนือ */
isDragging: boolean;

/** ไฟล์ที่ลากผ่านการตรวจสอบ */
isDragAccept: boolean;

/** ไฟล์ที่ลากไม่ผ่านการตรวจสอบ */
isDragReject: boolean;

/** มีไฟล์กำลังอัปโหลดอยู่ */
isUploading: boolean;

/** ความคืบหน้าเฉลี่ยของไฟล์ทั้งหมด (0-100) */
progress: number;

/** สถานะโดยรวม */
status: DropupStatus;
}

DropupStatus

สถานะการอัปโหลดโดยรวม

type DropupStatus = 'idle' | 'uploading' | 'complete' | 'error';

DropupActions

Actions ที่พร้อมใช้สำหรับควบคุม uploader

interface DropupActions {
/** เริ่มอัปโหลดไฟล์ */
upload: (fileIds?: string[]) => void;

/** ยกเลิกการอัปโหลด */
cancel: (fileId?: string) => void;

/** ลบไฟล์ */
remove: (fileId: string) => void;

/** ลบไฟล์ทั้งหมดและรีเซ็ต state */
reset: () => void;

/** ลองใหม่การอัปโหลดที่ล้มเหลว */
retry: (fileIds?: string[]) => void;

/** เพิ่มไฟล์แบบ programmatic */
addFiles: (files: File[] | FileList) => void;

/** อัปเดต metadata ของไฟล์ */
updateFileMeta: (fileId: string, meta: Record<string, unknown>) => void;
}

DropupError

Error object สำหรับการอัปโหลดที่ล้มเหลว

interface DropupError {
/** Error code */
code: string;

/** ข้อความที่อ่านได้ */
message: string;

/** Error ดั้งเดิมถ้ามี */
cause?: Error;
}

ประเภทตัวเลือก

UseDropupOptions

ตัวเลือกการกำหนดค่าสำหรับ hook

interface UseDropupOptions {
// การตรวจสอบ
accept?: string | string[];
maxSize?: number;
minSize?: number;
maxFiles?: number;
maxWidth?: number;
maxHeight?: number;
minWidth?: number;
minHeight?: number;
customRules?: ValidationRule[];

// พฤติกรรม
multiple?: boolean;
disabled?: boolean;
autoUpload?: boolean;
generatePreviews?: boolean;

// การอัปโหลด
upload?: UploadConfig | CustomUploader;

// Callbacks
onFilesAdded?: (files: DropupFile[]) => void;
onFileRemoved?: (file: DropupFile) => void;
onValidationError?: (errors: ValidationError[]) => void;
onUploadStart?: (file: DropupFile) => void;
onUploadProgress?: (file: DropupFile, progress: number) => void;
onUploadComplete?: (file: DropupFile, response: unknown) => void;
onUploadError?: (file: DropupFile, error: DropupError) => void;
onAllComplete?: (files: DropupFile[]) => void;
}

UploadConfig

การกำหนดค่าการอัปโหลดตาม URL

interface UploadConfig {
/** URL endpoint สำหรับอัปโหลด */
url: string;

/** HTTP method */
method?: 'POST' | 'PUT' | 'PATCH';

/** Request headers */
headers?: Record<string, string>;

/** ชื่อ form field สำหรับไฟล์ */
fieldName?: string;

/** รวม credentials/cookies */
withCredentials?: boolean;

/** Request timeout เป็นมิลลิวินาที */
timeout?: number;

/** ข้อมูล form เพิ่มเติม */
data?: Record<string, unknown>;
}

CustomUploader

ประเภทฟังก์ชันอัปโหลดที่กำหนดเอง

type CustomUploader = (
file: DropupFile,
options: UploadOptions
) => Promise<UploadResult>;

UploadOptions

Options ที่ส่งไปยัง custom uploader

interface UploadOptions {
/** Abort signal สำหรับการยกเลิก */
signal: AbortSignal;

/** Progress callback */
onProgress: (progress: number) => void;
}

UploadResult

ผลลัพธ์ที่ส่งคืนจากการอัปโหลด

interface UploadResult {
/** URL ไฟล์ที่อัปโหลด */
url?: string;

/** การตอบกลับดิบจากเซิร์ฟเวอร์ */
response?: unknown;
}

Validation Types

ValidationRule

ฟังก์ชันการตรวจสอบที่กำหนดเอง

type ValidationRule = (file: File) =>
| boolean
| string
| Promise<boolean | string>;

ค่าที่ส่งคืน:

  • true - ผ่านการตรวจสอบ
  • false - ไม่ผ่านการตรวจสอบ (ข้อผิดพลาดทั่วไป)
  • string - ไม่ผ่านการตรวจสอบพร้อมข้อความที่กำหนดเอง

ValidationError

ข้อผิดพลาดการตรวจสอบสำหรับไฟล์

interface ValidationError {
/** ไฟล์ที่ไม่ผ่านการตรวจสอบ */
file: File;

/** อาร์เรย์ของข้อความผิดพลาด */
errors: string[];
}

Return Types

UseDropupReturn

ประเภทการส่งคืนเต็มของ hook

interface UseDropupReturn {
/** อาร์เรย์ของไฟล์ */
files: DropupFile[];

/** สถานะปัจจุบัน */
state: DropupState;

/** Actions ที่พร้อมใช้ */
actions: DropupActions;

/** Props getter สำหรับ drop zone */
getDropProps: <E extends HTMLElement = HTMLDivElement>(
props?: React.HTMLAttributes<E>
) => DropZoneProps<E>;

/** Props getter สำหรับ input element */
getInputProps: (
props?: React.InputHTMLAttributes<HTMLInputElement>
) => InputProps;

/** เปิด file dialog แบบ programmatic */
openFileDialog: () => void;
}

DropZoneProps

Props ที่ส่งคืนโดย getDropProps

interface DropZoneProps<E extends HTMLElement>
extends React.HTMLAttributes<E> {
onDragEnter: React.DragEventHandler<E>;
onDragOver: React.DragEventHandler<E>;
onDragLeave: React.DragEventHandler<E>;
onDrop: React.DragEventHandler<E>;
onClick: React.MouseEventHandler<E>;
role: string;
tabIndex: number;
}

InputProps

Props ที่ส่งคืนโดย getInputProps

interface InputProps extends React.InputHTMLAttributes<HTMLInputElement> {
type: 'file';
accept?: string;
multiple?: boolean;
onChange: React.ChangeEventHandler<HTMLInputElement>;
style: React.CSSProperties;
}

Cloud Types

S3UploaderConfig

การกำหนดค่าสำหรับ S3 uploader

interface S3UploaderConfig {
getPresignedUrl: (file: DropupFile) => Promise<PresignedUrlResponse>;
}

interface PresignedUrlResponse {
url: string;
fields?: Record<string, string>;
}

GCSUploaderConfig

การกำหนดค่าสำหรับ Google Cloud Storage uploader

interface GCSUploaderConfig {
getSignedUrl: (file: DropupFile) => Promise<SignedUrlResponse>;
}

interface SignedUrlResponse {
url: string;
headers?: Record<string, string>;
}

AzureUploaderConfig

การกำหนดค่าสำหรับ Azure Blob Storage uploader

interface AzureUploaderConfig {
getSasUrl: (file: DropupFile) => Promise<SasUrlResponse>;
}

interface SasUrlResponse {
url: string;
headers?: Record<string, string>;
}

Chunked Upload Types

ChunkedUploaderConfig

การกำหนดค่าสำหรับการอัปโหลดแบบ chunked

interface ChunkedUploaderConfig {
/** URL endpoint สำหรับอัปโหลด */
url: string;

/** ขนาด chunk เป็นไบต์ (ค่าเริ่มต้น: 5MB) */
chunkSize?: number;

/** จำนวน chunks พร้อมกันสูงสุด */
concurrency?: number;

/** Request headers */
headers?: Record<string, string>;
}

Image Processing Types

CompressOptions

ตัวเลือกการบีบอัดรูปภาพ

interface CompressOptions {
/** ความกว้างสูงสุด */
maxWidth?: number;

/** ความสูงสูงสุด */
maxHeight?: number;

/** คุณภาพ 0-1 (ค่าเริ่มต้น: 0.8) */
quality?: number;

/** รูปแบบ output */
type?: 'image/jpeg' | 'image/png' | 'image/webp';
}

Type Utilities

Generic Types

// ดึงสถานะไฟล์
type IdleFile = DropupFile & { status: 'idle' };
type UploadingFile = DropupFile & { status: 'uploading' };
type CompletedFile = DropupFile & { status: 'complete' };
type FailedFile = DropupFile & { status: 'error' };

// ฟังก์ชัน type guard
function isIdle(file: DropupFile): file is IdleFile {
return file.status === 'idle';
}

function isUploading(file: DropupFile): file is UploadingFile {
return file.status === 'uploading';
}

function isComplete(file: DropupFile): file is CompletedFile {
return file.status === 'complete';
}

function isFailed(file: DropupFile): file is FailedFile {
return file.status === 'error';
}

การใช้งานกับ Type Guards

const { files } = useDropup();

// กรองด้วย type safety
const uploadingFiles = files.filter(isUploading);
// uploadingFiles คือ UploadingFile[]

const completedFiles = files.filter(isComplete);
// completedFiles คือ CompletedFile[]