Zum Hauptinhalt springen

TypeScript-Typen

Alle exportierten Typen von Dropup.

Typen importieren

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

Kerntypen

DropupFile

Repräsentiert eine Datei in der Upload-Warteschlange.

interface DropupFile {
/** Eindeutige Kennung */
id: string;

/** Ursprüngliches Browser-File-Objekt */
file: File;

/** Dateiname */
name: string;

/** Dateigröße in Bytes */
size: number;

/** MIME-Typ */
type: string;

/** Aktueller Upload-Status */
status: FileStatus;

/** Upload-Fortschritt 0-100 */
progress: number;

/** Vorschau-URL für Bilder (Object URL) */
preview?: string;

/** URL nach erfolgreichem Upload */
uploadedUrl?: string;

/** Rohe Server-Antwort */
response?: unknown;

/** Fehlerdetails bei Fehlschlag */
error?: DropupError;

/** Benutzerdefinierte Metadaten */
meta?: Record<string, unknown>;
}

FileStatus

Mögliche Dateistatuswerte.

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

DropupState

Aktueller Zustand der Ablagezone.

interface DropupState {
/** Dateien werden darüber gezogen */
isDragging: boolean;

/** Gezogene Dateien bestehen Validierung */
isDragAccept: boolean;

/** Gezogene Dateien bestehen Validierung nicht */
isDragReject: boolean;

/** Eine Datei wird gerade hochgeladen */
isUploading: boolean;

/** Durchschnittlicher Fortschritt aller Dateien (0-100) */
progress: number;

/** Gesamtstatus */
status: DropupStatus;
}

DropupStatus

Gesamter Upload-Status.

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

DropupActions

Verfügbare Aktionen zur Steuerung des Uploaders.

interface DropupActions {
/** Uploads starten */
upload: (fileIds?: string[]) => void;

/** Uploads abbrechen */
cancel: (fileId?: string) => void;

/** Eine Datei entfernen */
remove: (fileId: string) => void;

/** Alle Dateien entfernen und Zustand zurücksetzen */
reset: () => void;

/** Fehlgeschlagene Uploads wiederholen */
retry: (fileIds?: string[]) => void;

/** Dateien programmatisch hinzufügen */
addFiles: (files: File[] | FileList) => void;

/** Datei-Metadaten aktualisieren */
updateFileMeta: (fileId: string, meta: Record<string, unknown>) => void;
}

DropupError

Fehlerobjekt für fehlgeschlagene Uploads.

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

/** Lesbare Nachricht */
message: string;

/** Ursprünglicher Fehler falls verfügbar */
cause?: Error;
}

Optionstypen

UseDropupOptions

Konfigurationsoptionen für den Hook.

interface UseDropupOptions {
// Validierung
accept?: string | string[];
maxSize?: number;
minSize?: number;
maxFiles?: number;
maxWidth?: number;
maxHeight?: number;
minWidth?: number;
minHeight?: number;
customRules?: ValidationRule[];

// Verhalten
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-basierte Upload-Konfiguration.

interface UploadConfig {
/** Upload-Endpunkt-URL */
url: string;

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

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

/** Formularfeldname für die Datei */
fieldName?: string;

/** Anmeldedaten/Cookies einschließen */
withCredentials?: boolean;

/** Request-Timeout in Millisekunden */
timeout?: number;

/** Zusätzliche Formulardaten */
data?: Record<string, unknown>;
}

CustomUploader

Typ für benutzerdefinierte Upload-Funktion.

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

UploadOptions

Optionen, die an benutzerdefinierten Uploader übergeben werden.

interface UploadOptions {
/** Abbruchsignal für Stornierung */
signal: AbortSignal;

/** Fortschritts-Callback */
onProgress: (progress: number) => void;
}

UploadResult

Vom Upload zurückgegebenes Ergebnis.

interface UploadResult {
/** URL der hochgeladenen Datei */
url?: string;

/** Rohe Server-Antwort */
response?: unknown;
}

Validierungstypen

ValidationRule

Benutzerdefinierte Validierungsfunktion.

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

Rückgabewerte:

  • true - Validierung bestanden
  • false - Validierung fehlgeschlagen (generischer Fehler)
  • string - Validierung fehlgeschlagen mit benutzerdefinierter Nachricht

ValidationError

Validierungsfehler für eine Datei.

interface ValidationError {
/** Die Datei, die die Validierung nicht bestanden hat */
file: File;

/** Array von Fehlermeldungen */
errors: string[];
}

Rückgabetypen

UseDropupReturn

Vollständiger Rückgabetyp des Hooks.

interface UseDropupReturn {
/** Array von Dateien */
files: DropupFile[];

/** Aktueller Zustand */
state: DropupState;

/** Verfügbare Aktionen */
actions: DropupActions;

/** Props-Getter für Ablagezone */
getDropProps: <E extends HTMLElement = HTMLDivElement>(
props?: React.HTMLAttributes<E>
) => DropZoneProps<E>;

/** Props-Getter für Input-Element */
getInputProps: (
props?: React.InputHTMLAttributes<HTMLInputElement>
) => InputProps;

/** Dateiauswahldialog programmatisch öffnen */
openFileDialog: () => void;
}

DropZoneProps

Von getDropProps zurückgegebene 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

Von getInputProps zurückgegebene Props.

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

Cloud-Typen

S3UploaderConfig

Konfiguration für S3-Uploader.

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

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

GCSUploaderConfig

Konfiguration für Google Cloud Storage Uploader.

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

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

AzureUploaderConfig

Konfiguration für Azure Blob Storage Uploader.

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

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

Chunked Upload Typen

ChunkedUploaderConfig

Konfiguration für Chunked Uploads.

interface ChunkedUploaderConfig {
/** Upload-Endpunkt-URL */
url: string;

/** Chunk-Größe in Bytes (Standard: 5MB) */
chunkSize?: number;

/** Maximale gleichzeitige Chunks */
concurrency?: number;

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

Bildverarbeitungstypen

CompressOptions

Bildkompressionsoptionen.

interface CompressOptions {
/** Maximale Breite */
maxWidth?: number;

/** Maximale Höhe */
maxHeight?: number;

/** Qualität 0-1 (Standard: 0.8) */
quality?: number;

/** Ausgabeformat */
type?: 'image/jpeg' | 'image/png' | 'image/webp';
}

Typ-Hilfsfunktionen

Generische Typen

// Dateistatus extrahieren
type IdleFile = DropupFile & { status: 'idle' };
type UploadingFile = DropupFile & { status: 'uploading' };
type CompletedFile = DropupFile & { status: 'complete' };
type FailedFile = DropupFile & { status: 'error' };

// Type Guard Funktionen
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';
}

Verwendung mit Type Guards

const { files } = useDropup();

// Mit Typsicherheit filtern
const uploadingFiles = files.filter(isUploading);
// uploadingFiles ist UploadingFile[]

const completedFiles = files.filter(isComplete);
// completedFiles ist CompletedFile[]