TypeScript Types
Dropup වෙතින් export වන සියලුම types.
Types Import කිරීම
import type {
DropupFile,
DropupState,
DropupActions,
DropupError,
UseDropupOptions,
UseDropupReturn,
FileStatus,
DropupStatus,
ValidationRule,
ValidationError,
UploadConfig,
CustomUploader,
UploadOptions,
UploadResult,
} from '@samithahansaka/dropup';
Core Types
DropupFile
Upload queue එකේ ගොනුවක් නියෝජනය කරයි.
interface DropupFile {
/** අනන්ය හඳුනාගැනීම */
id: string;
/** මුල් browser File object */
file: File;
/** ගොනු නාමය */
name: string;
/** Bytes වලින් ගොනු ප්රමාණය */
size: number;
/** MIME type */
type: string;
/** වත්මන් upload status */
status: FileStatus;
/** Upload progress 0-100 */
progress: number;
/** Images සඳහා Preview URL (Object URL) */
preview?: string;
/** සාර්ථක upload පසු URL */
uploadedUrl?: string;
/** Raw server response */
response?: unknown;
/** අසාර්ථක නම් Error විස්තර */
error?: DropupError;
/** Custom metadata */
meta?: Record<string, unknown>;
}
FileStatus
ගොනු status අගයන්.
type FileStatus = 'idle' | 'uploading' | 'complete' | 'error' | 'paused';
DropupState
Dropzone හි වත්මන් state.
interface DropupState {
/** ගොනු drag වෙමින් */
isDragging: boolean;
/** Drag කළ ගොනු validation pass කරයි */
isDragAccept: boolean;
/** Drag කළ ගොනු validation fail කරයි */
isDragReject: boolean;
/** ගොනුවක් upload වෙමින් */
isUploading: boolean;
/** සියලුම ගොනුවල සාමාන්ය progress (0-100) */
progress: number;
/** සමස්ත status */
status: DropupStatus;
}
DropupStatus
සමස්ත upload status.
type DropupStatus = 'idle' | 'uploading' | 'complete' | 'error';
DropupActions
Uploader පාලනය කිරීමට ලබා ගත හැකි actions.
interface DropupActions {
/** ගොනු upload ආරම්භ කරන්න */
upload: (fileIds?: string[]) => void;
/** Uploads අවලංගු කරන්න */
cancel: (fileId?: string) => void;
/** ගොනුවක් ඉවත් කරන්න */
remove: (fileId: string) => void;
/** සියලුම ගොනු ඉවත් කර state reset කරන්න */
reset: () => void;
/** අසාර්ථක uploads නැවත උත්සාහ කරන්න */
retry: (fileIds?: string[]) => void;
/** Programmatically ගොනු එකතු කරන්න */
addFiles: (files: File[] | FileList) => void;
/** File metadata යාවත්කාලීන කරන්න */
updateFileMeta: (fileId: string, meta: Record<string, unknown>) => void;
}
DropupError
අසාර්ථක uploads සඳහා Error object.
interface DropupError {
/** Error code */
code: string;
/** මිනිසුන්ට කියවිය හැකි පණිවිඩය */
message: string;
/** ලබා ගත හැකි නම් මුල් error */
cause?: Error;
}
විකල්ප වර්ග
UseDropupOptions
Hook සඳහා configuration options.
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
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 */
fieldName?: string;
/** Credentials/cookies ඇතුළත් කරන්න */
withCredentials?: boolean;
/** Milliseconds වලින් Request timeout */
timeout?: number;
/** අතිරේක form data */
data?: Record<string, unknown>;
}
CustomUploader
Custom upload function type.
type CustomUploader = (
file: DropupFile,
options: UploadOptions
) => Promise<UploadResult>;
UploadOptions
Custom uploader වෙත pass කරන options.
interface UploadOptions {
/** අවලංගු කිරීම සඳහා Abort signal */
signal: AbortSignal;
/** Progress callback */
onProgress: (progress: number) => void;
}
UploadResult
Upload එකෙන් return වන ප්රතිඵලය.
interface UploadResult {
/** Upload වූ ගොනුවේ 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 සාර්ථකයිfalse- Validation අසාර්ථකයි (generic error)string- Custom message සමඟ validation අසාර්ථකයි
ValidationError
ගොනුවක් සඳහා validation error.
interface ValidationError {
/** Validation අසාර්ථක වූ ගොනුව */
file: File;
/** Error messages array */
errors: string[];
}
Return Types
UseDropupReturn
Hook හි සම්පූර්ණ return type.
interface UseDropupReturn {
/** ගොනු Array */
files: DropupFile[];
/** වත්මන් state */
state: DropupState;
/** ලබා ගත හැකි actions */
actions: DropupActions;
/** Drop zone සඳහා Props getter */
getDropProps: <E extends HTMLElement = HTMLDivElement>(
props?: React.HTMLAttributes<E>
) => DropZoneProps<E>;
/** Input element සඳහා Props getter */
getInputProps: (
props?: React.InputHTMLAttributes<HTMLInputElement>
) => InputProps;
/** File dialog programmatically විවෘත කරන්න */
openFileDialog: () => void;
}
DropZoneProps
getDropProps මගින් return වන props.
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
getInputProps මගින් return වන props.
interface InputProps extends React.InputHTMLAttributes<HTMLInputElement> {
type: 'file';
accept?: string;
multiple?: boolean;
onChange: React.ChangeEventHandler<HTMLInputElement>;
style: React.CSSProperties;
}
Cloud Types
S3UploaderConfig
S3 uploader සඳහා configuration.
interface S3UploaderConfig {
getPresignedUrl: (file: DropupFile) => Promise<PresignedUrlResponse>;
}
interface PresignedUrlResponse {
url: string;
fields?: Record<string, string>;
}
GCSUploaderConfig
Google Cloud Storage uploader සඳහා configuration.
interface GCSUploaderConfig {
getSignedUrl: (file: DropupFile) => Promise<SignedUrlResponse>;
}
interface SignedUrlResponse {
url: string;
headers?: Record<string, string>;
}
AzureUploaderConfig
Azure Blob Storage uploader සඳහා configuration.
interface AzureUploaderConfig {
getSasUrl: (file: DropupFile) => Promise<SasUrlResponse>;
}
interface SasUrlResponse {
url: string;
headers?: Record<string, string>;
}
Chunked Upload Types
ChunkedUploaderConfig
Chunked uploads සඳහා configuration.
interface ChunkedUploaderConfig {
/** Upload endpoint URL */
url: string;
/** Bytes වලින් Chunk size (පෙරනිමි: 5MB) */
chunkSize?: number;
/** උපරිම concurrent chunks */
concurrency?: number;
/** Request headers */
headers?: Record<string, string>;
}
Image Processing Types
CompressOptions
Image compression options.
interface CompressOptions {
/** උපරිම width */
maxWidth?: number;
/** උපරිම height */
maxHeight?: number;
/** Quality 0-1 (පෙරනිමි: 0.8) */
quality?: number;
/** Output format */
type?: 'image/jpeg' | 'image/png' | 'image/webp';
}
Type Utilities
Generic Types
// File status extract කරන්න
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();
// Type safety සමඟ filter කරන්න
const uploadingFiles = files.filter(isUploading);
// uploadingFiles යනු UploadingFile[]
const completedFiles = files.filter(isComplete);
// completedFiles යනු CompletedFile[]