TypeScript Types
All exported types from Dropup.
Import Types
import type {
DropupFile,
DropupState,
DropupActions,
DropupError,
UseDropupOptions,
UseDropupReturn,
FileStatus,
DropupStatus,
ValidationRule,
ValidationError,
UploadConfig,
CustomUploader,
UploadOptions,
UploadResult,
} from '@samithahansaka/dropup';
Core Types
DropupFile
Represents a file in the upload queue.
interface DropupFile {
/** Unique identifier */
id: string;
/** Original browser File object */
file: File;
/** File name */
name: string;
/** File size in bytes */
size: number;
/** MIME type */
type: string;
/** Current upload status */
status: FileStatus;
/** Upload progress 0-100 */
progress: number;
/** Preview URL for images (Object URL) */
preview?: string;
/** URL after successful upload */
uploadedUrl?: string;
/** Raw server response */
response?: unknown;
/** Error details if failed */
error?: DropupError;
/** Custom metadata */
meta?: Record<string, unknown>;
}
FileStatus
Possible file status values.
type FileStatus = 'idle' | 'uploading' | 'complete' | 'error' | 'paused';
DropupState
Current state of the dropzone.
interface DropupState {
/** Files being dragged over */
isDragging: boolean;
/** Dragged files pass validation */
isDragAccept: boolean;
/** Dragged files fail validation */
isDragReject: boolean;
/** Any file currently uploading */
isUploading: boolean;
/** Average progress of all files (0-100) */
progress: number;
/** Overall status */
status: DropupStatus;
}
DropupStatus
Overall upload status.
type DropupStatus = 'idle' | 'uploading' | 'complete' | 'error';
DropupActions
Available actions to control the uploader.
interface DropupActions {
/** Start uploading files */
upload: (fileIds?: string[]) => void;
/** Cancel uploads */
cancel: (fileId?: string) => void;
/** Remove a file */
remove: (fileId: string) => void;
/** Remove all files and reset state */
reset: () => void;
/** Retry failed uploads */
retry: (fileIds?: string[]) => void;
/** Add files programmatically */
addFiles: (files: File[] | FileList) => void;
/** Update file metadata */
updateFileMeta: (fileId: string, meta: Record<string, unknown>) => void;
}
DropupError
Error object for failed uploads.
interface DropupError {
/** Error code */
code: string;
/** Human-readable message */
message: string;
/** Original error if available */
cause?: Error;
}
選項類型
UseDropupOptions
Configuration options for the hook.
interface UseDropupOptions {
// Validation
accept?: string | string[];
maxSize?: number;
minSize?: number;
maxFiles?: number;
maxWidth?: number;
maxHeight?: number;
minWidth?: number;
minHeight?: number;
customRules?: ValidationRule[];
// Behavior
multiple?: boolean;
disabled?: boolean;
autoUpload?: boolean;
generatePreviews?: boolean;
// Upload
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-based upload configuration.
interface UploadConfig {
/** Upload endpoint URL */
url: string;
/** HTTP method */
method?: 'POST' | 'PUT' | 'PATCH';
/** Request headers */
headers?: Record<string, string>;
/** Form field name for the file */
fieldName?: string;
/** Include credentials/cookies */
withCredentials?: boolean;
/** Request timeout in milliseconds */
timeout?: number;
/** Additional form data */
data?: Record<string, unknown>;
}
CustomUploader
Custom upload function type.
type CustomUploader = (
file: DropupFile,
options: UploadOptions
) => Promise<UploadResult>;
UploadOptions
Options passed to custom uploader.
interface UploadOptions {
/** Abort signal for cancellation */
signal: AbortSignal;
/** Progress callback */
onProgress: (progress: number) => void;
}
UploadResult
Result returned from upload.
interface UploadResult {
/** Uploaded file URL */
url?: string;
/** Raw server response */
response?: unknown;
}
Validation Types
ValidationRule
Custom validation function.
type ValidationRule = (file: File) =>
| boolean
| string
| Promise<boolean | string>;
Return values:
true- Validation passedfalse- Validation failed (generic error)string- Validation failed with custom message
ValidationError
Validation error for a file.
interface ValidationError {
/** The file that failed validation */
file: File;
/** Array of error messages */
errors: string[];
}
Return Types
UseDropupReturn
Full return type of the hook.
interface UseDropupReturn {
/** Array of files */
files: DropupFile[];
/** Current state */
state: DropupState;
/** Available actions */
actions: DropupActions;
/** Props getter for drop zone */
getDropProps: <E extends HTMLElement = HTMLDivElement>(
props?: React.HTMLAttributes<E>
) => DropZoneProps<E>;
/** Props getter for input element */
getInputProps: (
props?: React.InputHTMLAttributes<HTMLInputElement>
) => InputProps;
/** Open file dialog programmatically */
openFileDialog: () => void;
}
DropZoneProps
Props returned by 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 returned by getInputProps.
interface InputProps extends React.InputHTMLAttributes<HTMLInputElement> {
type: 'file';
accept?: string;
multiple?: boolean;
onChange: React.ChangeEventHandler<HTMLInputElement>;
style: React.CSSProperties;
}
Cloud Types
S3UploaderConfig
Configuration for S3 uploader.
interface S3UploaderConfig {
getPresignedUrl: (file: DropupFile) => Promise<PresignedUrlResponse>;
}
interface PresignedUrlResponse {
url: string;
fields?: Record<string, string>;
}
GCSUploaderConfig
Configuration for Google Cloud Storage uploader.
interface GCSUploaderConfig {
getSignedUrl: (file: DropupFile) => Promise<SignedUrlResponse>;
}
interface SignedUrlResponse {
url: string;
headers?: Record<string, string>;
}
AzureUploaderConfig
Configuration for Azure Blob Storage uploader.
interface AzureUploaderConfig {
getSasUrl: (file: DropupFile) => Promise<SasUrlResponse>;
}
interface SasUrlResponse {
url: string;
headers?: Record<string, string>;
}
Chunked Upload Types
ChunkedUploaderConfig
Configuration for chunked uploads.
interface ChunkedUploaderConfig {
/** Upload endpoint URL */
url: string;
/** Chunk size in bytes (default: 5MB) */
chunkSize?: number;
/** Maximum concurrent chunks */
concurrency?: number;
/** Request headers */
headers?: Record<string, string>;
}
Image Processing Types
CompressOptions
Image compression options.
interface CompressOptions {
/** Maximum width */
maxWidth?: number;
/** Maximum height */
maxHeight?: number;
/** Quality 0-1 (default: 0.8) */
quality?: number;
/** Output format */
type?: 'image/jpeg' | 'image/png' | 'image/webp';
}
Type Utilities
Generic Types
// Extract file status
type IdleFile = DropupFile & { status: 'idle' };
type UploadingFile = DropupFile & { status: 'uploading' };
type CompletedFile = DropupFile & { status: 'complete' };
type FailedFile = DropupFile & { status: 'error' };
// Type guard functions
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();
// Filter with type safety
const uploadingFiles = files.filter(isUploading);
// uploadingFiles is UploadingFile[]
const completedFiles = files.filter(isComplete);
// completedFiles is CompletedFile[]