Tipe TypeScript
Semua tipe yang diekspor dari Dropup.
Import Tipe
import type {
DropupFile,
DropupState,
DropupActions,
DropupError,
UseDropupOptions,
UseDropupReturn,
FileStatus,
DropupStatus,
ValidationRule,
ValidationError,
UploadConfig,
CustomUploader,
UploadOptions,
UploadResult,
} from '@samithahansaka/dropup';
Tipe Inti
DropupFile
Merepresentasikan file dalam antrian upload.
interface DropupFile {
/** Identifier unik */
id: string;
/** Objek File browser asli */
file: File;
/** Nama file */
name: string;
/** Ukuran file dalam bytes */
size: number;
/** Tipe MIME */
type: string;
/** Status upload saat ini */
status: FileStatus;
/** Progres upload 0-100 */
progress: number;
/** URL preview untuk gambar (Object URL) */
preview?: string;
/** URL setelah upload berhasil */
uploadedUrl?: string;
/** Response server mentah */
response?: unknown;
/** Detail error jika gagal */
error?: DropupError;
/** Metadata kustom */
meta?: Record<string, unknown>;
}
FileStatus
Nilai status file yang mungkin.
type FileStatus = 'idle' | 'uploading' | 'complete' | 'error' | 'paused';
DropupState
State saat ini dari dropzone.
interface DropupState {
/** File sedang diseret */
isDragging: boolean;
/** File yang diseret lulus validasi */
isDragAccept: boolean;
/** File yang diseret gagal validasi */
isDragReject: boolean;
/** Ada file yang sedang diupload */
isUploading: boolean;
/** Rata-rata progres semua file (0-100) */
progress: number;
/** Status keseluruhan */
status: DropupStatus;
}
DropupStatus
Status upload keseluruhan.
type DropupStatus = 'idle' | 'uploading' | 'complete' | 'error';
DropupActions
Actions yang tersedia untuk mengontrol uploader.
interface DropupActions {
/** Mulai mengupload file */
upload: (fileIds?: string[]) => void;
/** Batalkan upload */
cancel: (fileId?: string) => void;
/** Hapus file */
remove: (fileId: string) => void;
/** Hapus semua file dan reset state */
reset: () => void;
/** Coba ulang upload yang gagal */
retry: (fileIds?: string[]) => void;
/** Tambahkan file secara programatik */
addFiles: (files: File[] | FileList) => void;
/** Perbarui metadata file */
updateFileMeta: (fileId: string, meta: Record<string, unknown>) => void;
}
DropupError
Objek error untuk upload yang gagal.
interface DropupError {
/** Kode error */
code: string;
/** Pesan yang dapat dibaca manusia */
message: string;
/** Error asli jika tersedia */
cause?: Error;
}
Tipe Opsi
UseDropupOptions
Opsi konfigurasi untuk hook.
interface UseDropupOptions {
// Validasi
accept?: string | string[];
maxSize?: number;
minSize?: number;
maxFiles?: number;
maxWidth?: number;
maxHeight?: number;
minWidth?: number;
minHeight?: number;
customRules?: ValidationRule[];
// Perilaku
multiple?: boolean;
disabled?: boolean;
autoUpload?: boolean;
generatePreviews?: boolean;
// Upload
upload?: UploadConfig | CustomUploader;
// Callback
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
Konfigurasi upload berbasis URL.
interface UploadConfig {
/** URL endpoint upload */
url: string;
/** Method HTTP */
method?: 'POST' | 'PUT' | 'PATCH';
/** Header request */
headers?: Record<string, string>;
/** Nama field form untuk file */
fieldName?: string;
/** Sertakan credentials/cookies */
withCredentials?: boolean;
/** Timeout request dalam milidetik */
timeout?: number;
/** Data form tambahan */
data?: Record<string, unknown>;
}
CustomUploader
Tipe fungsi upload kustom.
type CustomUploader = (
file: DropupFile,
options: UploadOptions
) => Promise<UploadResult>;
UploadOptions
Opsi yang diteruskan ke uploader kustom.
interface UploadOptions {
/** Sinyal abort untuk pembatalan */
signal: AbortSignal;
/** Callback progres */
onProgress: (progress: number) => void;
}
UploadResult
Hasil yang dikembalikan dari upload.
interface UploadResult {
/** URL file yang diupload */
url?: string;
/** Response server mentah */
response?: unknown;
}
Tipe Validasi
ValidationRule
Fungsi validasi kustom.
type ValidationRule = (file: File) =>
| boolean
| string
| Promise<boolean | string>;
Nilai return:
true- Validasi berhasilfalse- Validasi gagal (error generik)string- Validasi gagal dengan pesan kustom
ValidationError
Error validasi untuk file.
interface ValidationError {
/** File yang gagal validasi */
file: File;
/** Array pesan error */
errors: string[];
}
Tipe Return
UseDropupReturn
Tipe return lengkap dari hook.
interface UseDropupReturn {
/** Array file */
files: DropupFile[];
/** State saat ini */
state: DropupState;
/** Actions yang tersedia */
actions: DropupActions;
/** Props getter untuk zona drop */
getDropProps: <E extends HTMLElement = HTMLDivElement>(
props?: React.HTMLAttributes<E>
) => DropZoneProps<E>;
/** Props getter untuk elemen input */
getInputProps: (
props?: React.InputHTMLAttributes<HTMLInputElement>
) => InputProps;
/** Buka dialog file secara programatik */
openFileDialog: () => void;
}
DropZoneProps
Props yang dikembalikan oleh 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 yang dikembalikan oleh getInputProps.
interface InputProps extends React.InputHTMLAttributes<HTMLInputElement> {
type: 'file';
accept?: string;
multiple?: boolean;
onChange: React.ChangeEventHandler<HTMLInputElement>;
style: React.CSSProperties;
}
Tipe Cloud
S3UploaderConfig
Konfigurasi untuk uploader S3.
interface S3UploaderConfig {
getPresignedUrl: (file: DropupFile) => Promise<PresignedUrlResponse>;
}
interface PresignedUrlResponse {
url: string;
fields?: Record<string, string>;
}
GCSUploaderConfig
Konfigurasi untuk uploader Google Cloud Storage.
interface GCSUploaderConfig {
getSignedUrl: (file: DropupFile) => Promise<SignedUrlResponse>;
}
interface SignedUrlResponse {
url: string;
headers?: Record<string, string>;
}
AzureUploaderConfig
Konfigurasi untuk uploader Azure Blob Storage.
interface AzureUploaderConfig {
getSasUrl: (file: DropupFile) => Promise<SasUrlResponse>;
}
interface SasUrlResponse {
url: string;
headers?: Record<string, string>;
}
Tipe Upload Chunked
ChunkedUploaderConfig
Konfigurasi untuk upload chunked.
interface ChunkedUploaderConfig {
/** URL endpoint upload */
url: string;
/** Ukuran chunk dalam bytes (default: 5MB) */
chunkSize?: number;
/** Maksimum chunk konkuren */
concurrency?: number;
/** Header request */
headers?: Record<string, string>;
}
Tipe Pemrosesan Gambar
CompressOptions
Opsi kompresi gambar.
interface CompressOptions {
/** Lebar maksimum */
maxWidth?: number;
/** Tinggi maksimum */
maxHeight?: number;
/** Kualitas 0-1 (default: 0.8) */
quality?: number;
/** Format output */
type?: 'image/jpeg' | 'image/png' | 'image/webp';
}
Utilitas Tipe
Tipe Generik
// Ekstrak status file
type IdleFile = DropupFile & { status: 'idle' };
type UploadingFile = DropupFile & { status: 'uploading' };
type CompletedFile = DropupFile & { status: 'complete' };
type FailedFile = DropupFile & { status: 'error' };
// Fungsi 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';
}
Penggunaan dengan Type Guards
const { files } = useDropup();
// Filter dengan keamanan tipe
const uploadingFiles = files.filter(isUploading);
// uploadingFiles adalah UploadingFile[]
const completedFiles = files.filter(isComplete);
// completedFiles adalah CompletedFile[]