تخطي إلى المحتوى الرئيسي

أنواع TypeScript

جميع الأنواع المُصدَّرة من Dropup.

استيراد الأنواع

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

الأنواع الأساسية

DropupFile

يمثل ملفاً في قائمة انتظار الرفع.

interface DropupFile {
/** معرف فريد */
id: string;

/** كائن File الأصلي من المتصفح */
file: File;

/** اسم الملف */
name: string;

/** حجم الملف بالبايتات */
size: number;

/** نوع MIME */
type: string;

/** حالة الرفع الحالية */
status: FileStatus;

/** تقدم الرفع 0-100 */
progress: number;

/** رابط المعاينة للصور (Object URL) */
preview?: string;

/** الرابط بعد نجاح الرفع */
uploadedUrl?: string;

/** استجابة الخادم الخام */
response?: unknown;

/** تفاصيل الخطأ في حالة الفشل */
error?: DropupError;

/** بيانات وصفية مخصصة */
meta?: Record<string, unknown>;
}

FileStatus

قيم حالة الملف الممكنة.

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

DropupState

الحالة الحالية لمنطقة الإفلات.

interface DropupState {
/** الملفات تُسحب فوقها */
isDragging: boolean;

/** الملفات المسحوبة تجتاز التحقق */
isDragAccept: boolean;

/** الملفات المسحوبة تفشل في التحقق */
isDragReject: boolean;

/** أي ملف قيد الرفع حالياً */
isUploading: boolean;

/** متوسط تقدم جميع الملفات (0-100) */
progress: number;

/** الحالة الإجمالية */
status: DropupStatus;
}

DropupStatus

حالة الرفع الإجمالية.

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

DropupActions

الإجراءات المتاحة للتحكم في الرافع.

interface DropupActions {
/** بدء رفع الملفات */
upload: (fileIds?: string[]) => void;

/** إلغاء عمليات الرفع */
cancel: (fileId?: string) => void;

/** إزالة ملف */
remove: (fileId: string) => void;

/** إزالة جميع الملفات وإعادة تعيين الحالة */
reset: () => void;

/** إعادة محاولة الرفع الفاشل */
retry: (fileIds?: string[]) => void;

/** إضافة ملفات برمجياً */
addFiles: (files: File[] | FileList) => void;

/** تحديث البيانات الوصفية للملف */
updateFileMeta: (fileId: string, meta: Record<string, unknown>) => void;
}

DropupError

كائن الخطأ لعمليات الرفع الفاشلة.

interface DropupError {
/** رمز الخطأ */
code: string;

/** رسالة قابلة للقراءة */
message: string;

/** الخطأ الأصلي إن وُجد */
cause?: Error;
}

أنواع الخيارات

UseDropupOptions

خيارات التكوين للخطاف.

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;

// الاستدعاءات
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: string;

/** طريقة HTTP */
method?: 'POST' | 'PUT' | 'PATCH';

/** ترويسات الطلب */
headers?: Record<string, string>;

/** اسم حقل النموذج للملف */
fieldName?: string;

/** تضمين بيانات الاعتماد/ملفات تعريف الارتباط */
withCredentials?: boolean;

/** مهلة الطلب بالمللي ثانية */
timeout?: number;

/** بيانات نموذج إضافية */
data?: Record<string, unknown>;
}

CustomUploader

نوع دالة الرفع المخصصة.

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

UploadOptions

الخيارات المُمررة للرافع المخصص.

interface UploadOptions {
/** إشارة الإلغاء */
signal: AbortSignal;

/** استدعاء التقدم */
onProgress: (progress: number) => void;
}

UploadResult

النتيجة المُرجعة من الرفع.

interface UploadResult {
/** رابط الملف المرفوع */
url?: string;

/** استجابة الخادم الخام */
response?: unknown;
}

أنواع التحقق

ValidationRule

دالة تحقق مخصصة.

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

القيم المُرجعة:

  • true - نجح التحقق
  • false - فشل التحقق (خطأ عام)
  • string - فشل التحقق مع رسالة مخصصة

ValidationError

خطأ التحقق لملف.

interface ValidationError {
/** الملف الذي فشل في التحقق */
file: File;

/** مصفوفة رسائل الخطأ */
errors: string[];
}

أنواع القيم المُرجعة

UseDropupReturn

نوع الإرجاع الكامل للخطاف.

interface UseDropupReturn {
/** مصفوفة الملفات */
files: DropupFile[];

/** الحالة الحالية */
state: DropupState;

/** الإجراءات المتاحة */
actions: DropupActions;

/** مُحصّل خصائص منطقة الإفلات */
getDropProps: <E extends HTMLElement = HTMLDivElement>(
props?: React.HTMLAttributes<E>
) => DropZoneProps<E>;

/** مُحصّل خصائص عنصر الإدخال */
getInputProps: (
props?: React.InputHTMLAttributes<HTMLInputElement>
) => InputProps;

/** فتح حوار الملفات برمجياً */
openFileDialog: () => void;
}

DropZoneProps

الخصائص المُرجعة من 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

الخصائص المُرجعة من getInputProps.

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

أنواع السحابة

S3UploaderConfig

تكوين رافع S3.

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

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

GCSUploaderConfig

تكوين رافع Google Cloud Storage.

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

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

AzureUploaderConfig

تكوين رافع Azure Blob Storage.

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

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

أنواع الرفع المجزأ

ChunkedUploaderConfig

تكوين الرفع المجزأ.

interface ChunkedUploaderConfig {
/** رابط نقطة نهاية الرفع */
url: string;

/** حجم الجزء بالبايتات (الافتراضي: 5MB) */
chunkSize?: number;

/** الحد الأقصى للأجزاء المتزامنة */
concurrency?: number;

/** ترويسات الطلب */
headers?: Record<string, string>;
}

أنواع معالجة الصور

CompressOptions

خيارات ضغط الصور.

interface CompressOptions {
/** الحد الأقصى للعرض */
maxWidth?: number;

/** الحد الأقصى للارتفاع */
maxHeight?: number;

/** الجودة 0-1 (الافتراضي: 0.8) */
quality?: number;

/** صيغة الإخراج */
type?: 'image/jpeg' | 'image/png' | 'image/webp';
}

أدوات الأنواع

الأنواع العامة

// استخراج حالة الملف
type IdleFile = DropupFile & { status: 'idle' };
type UploadingFile = DropupFile & { status: 'uploading' };
type CompletedFile = DropupFile & { status: 'complete' };
type FailedFile = DropupFile & { status: 'error' };

// دوال حراسة الأنواع
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';
}

الاستخدام مع حراس الأنواع

const { files } = useDropup();

// التصفية مع أمان الأنواع
const uploadingFiles = files.filter(isUploading);
// uploadingFiles من نوع UploadingFile[]

const completedFiles = files.filter(isComplete);
// completedFiles من نوع CompletedFile[]