pushduck
Version:
The fastest way to add file uploads to any web application. Enterprise security, edge-ready. Works with 16+ frameworks and 5+ storage providers. No heavy AWS SDK required.
138 lines (136 loc) • 4.63 kB
text/typescript
import { S as S3Router, o as S3RouteUploadResult, r as RouterRouteNames, u as UploadRouteConfig } from "./index-DwDwUJqB.mjs";
//#region src/hooks/use-upload-route.d.ts
/**
* Formats estimated time remaining into a human-readable string.
*
* @param seconds - Time remaining in seconds
* @returns Formatted time string (e.g., "30s", "2m", "1h")
*
* @example
* ```typescript
* formatETA(45); // "45s"
* formatETA(120); // "2m"
* formatETA(3600); // "1h"
* formatETA(7200); // "2h"
* ```
*/
declare function formatETA(seconds: number): string;
/**
* Formats upload speed into a human-readable string with appropriate units.
*
* @param bytesPerSecond - Upload speed in bytes per second
* @returns Formatted speed string (e.g., "1.5 MB/s", "500 KB/s")
*
* @example
* ```typescript
* formatUploadSpeed(1024); // "1.0 KB/s"
* formatUploadSpeed(1048576); // "1.0 MB/s"
* formatUploadSpeed(1073741824); // "1.0 GB/s"
* ```
*/
declare function formatUploadSpeed(bytesPerSecond: number): string;
/**
* React hook for managing file uploads with type-safe route configuration.
* Provides comprehensive upload state management, progress tracking, and error handling.
*
* @template TRouter - The router type for type-safe route names
* @param routeName - Name of the upload route (type-safe when TRouter is provided)
* @param config - Optional configuration for upload behavior and callbacks
* @returns Upload state and control functions
*
* @overload
* @template TRouter - Router type extending S3Router
* @param routeName - Type-safe route name from the router
* @param config - Optional upload configuration
* @returns Upload result object with type-safe route handling
*
* @overload
* @param routeName - Route name as string (for dynamic usage)
* @param config - Optional upload configuration
* @returns Upload result object with standard route handling
*
* @example Type-Safe Usage
* ```typescript
* import type { AppRouter } from '@/lib/upload';
*
* function TypedUploader() {
* // TypeScript will validate 'imageUpload' exists in AppRouter
* const { uploadFiles, files, isUploading, progress, cancel } =
* useUploadRoute<AppRouter>('imageUpload', {
* onProgress: (progress) => {
* console.log(`Upload progress: ${progress.percentage}%`);
* console.log(`Speed: ${formatUploadSpeed(progress.speed)}`);
* console.log(`ETA: ${formatETA(progress.eta)}`);
* },
* onSuccess: (results) => {
* console.log('All uploads completed:', results);
* // Handle successful uploads
* },
* onError: (error) => {
* console.error('Upload error:', error);
* // Handle upload errors
* },
* });
*
* const handleFileSelect = (e: React.ChangeEvent<HTMLInputElement>) => {
* const files = Array.from(e.target.files || []);
* uploadFiles(files);
* };
*
* return (
* <div>
* <input
* type="file"
* multiple
* accept="image/*"
* onChange={handleFileSelect}
* disabled={isUploading}
* />
*
* <div>Overall Progress: {progress}%</div>
*
* {files.map((file) => (
* <div key={file.id}>
* <span>{file.name}</span>
* <progress value={file.progress} max={100} />
* <span>{file.status}</span>
* {file.status === 'uploading' && (
* <button onClick={() => cancel(file.id)}>Cancel</button>
* )}
* </div>
* ))}
* </div>
* );
* }
* ```
*
* @example Dynamic Route Usage
* ```typescript
* function DynamicUploader({ routeName }: { routeName: string }) {
* const { uploadFiles, files, retry, reset } = useUploadRoute(routeName, {
* onError: (error) => {
* console.error(`Upload to ${routeName} failed:`, error);
* },
* });
*
* return (
* <div>
* <input
* type="file"
* onChange={(e) => uploadFiles(Array.from(e.target.files || []))}
* />
*
* {files.filter(f => f.status === 'error').length > 0 && (
* <button onClick={retry}>Retry Failed Uploads</button>
* )}
*
* <button onClick={reset}>Clear All</button>
* </div>
* );
* }
* ```
*/
declare function useUploadRoute<TRouter extends S3Router<any>>(routeName: RouterRouteNames<TRouter>, config?: UploadRouteConfig): S3RouteUploadResult;
declare function useUploadRoute(routeName: string, config?: UploadRouteConfig): S3RouteUploadResult;
//#endregion
export { formatUploadSpeed as n, useUploadRoute as r, formatETA as t };