メインコンテンツまでスキップ

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;

/** 画像のプレビューURL(Object URL) */
preview?: string;

/** アップロード成功後のURL */
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 */
url: string;

/** HTTPメソッド */
method?: 'POST' | 'PUT' | 'PATCH';

/** リクエストヘッダー */
headers?: Record<string, string>;

/** ファイルのフォームフィールド名 */
fieldName?: string;

/** クレデンシャル/Cookieを含める */
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 */
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;

/** ドロップゾーン用のpropsゲッター */
getDropProps: <E extends HTMLElement = HTMLDivElement>(
props?: React.HTMLAttributes<E>
) => DropZoneProps<E>;

/** input要素用のpropsゲッター */
getInputProps: (
props?: React.InputHTMLAttributes<HTMLInputElement>
) => InputProps;

/** プログラムでファイルダイアログを開く */
openFileDialog: () => void;
}

DropZoneProps

getDropPropsによって返される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によって返されるprops。

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 */
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[]