@health-ecosystem/file-upload
Version:
File upload library for Health Ecosystem applications with improved Next.js 15+ compatibility
654 lines (632 loc) • 20.2 kB
TypeScript
import React$1 from 'react';
import * as react_jsx_runtime from 'react/jsx-runtime';
declare const REACT_COMPAT_INFO: {
version: string;
isReact19Plus: boolean;
isReact18: boolean;
supportsNewJSXTransform: boolean;
supportsConcurrentFeatures: boolean;
};
/**
* File Upload Library Types
* Production-ready types for Health Ecosystem file uploads
*/
interface FileUploadConfig {
/** Base URL of the file upload service */
baseUrl: string;
/** Authentication token */
authToken: string;
/** Maximum file size in bytes (default: 50MB) */
maxFileSize?: number;
/** Allowed file types */
allowedTypes?: string[];
/** Request timeout in milliseconds (default: 30000) */
timeout?: number;
/** Enable retry on failure (default: true) */
enableRetry?: boolean;
/** Number of retry attempts (default: 3) */
maxRetries?: number;
}
interface UploadOptions {
/** File category (medical_image, document, report, etc.) */
category?: string;
/** Associated entity type (patient, doctor, appointment, etc.) */
entityType?: string;
/** Associated entity ID */
entityId?: string;
/** Whether the file should be public (default: false) */
isPublic?: boolean;
/** Additional metadata as JSON string */
metadata?: string;
/** Progress callback function */
onProgress?: (progress: number) => void;
/** Upload start callback */
onStart?: () => void;
/** Upload success callback */
onSuccess?: (result: UploadResult) => void;
/** Upload error callback */
onError?: (error: UploadError) => void;
}
interface UploadResult {
/** Unique file ID */
id: string;
/** Legacy file ID (for backward compatibility) */
fileId: string;
/** Original filename */
originalFilename: string;
/** Stored filename on server */
storedFilename: string;
/** File size in bytes */
fileSize: number;
/** MIME type */
mimeType: string;
/** File category */
category: string;
/** Download URL */
downloadUrl: string;
/** Thumbnail URL (for images) */
thumbnailUrl?: string;
/** Preview URL with transformation options */
previewUrl?: string;
/** Upload timestamp */
uploadedAt: string;
/** Legacy stored filename (for backward compatibility) */
filename?: string;
/** Whether the file is public */
isPublic?: boolean;
}
interface UploadError {
/** Error code */
code: string;
/** Error message */
message: string;
/** HTTP status code */
status?: number;
/** Additional error details */
details?: any;
}
interface FileMetadata {
/** File ID */
id: string;
/** Original filename */
originalFilename: string;
/** File size in bytes */
fileSize: number;
/** MIME type */
mimeType: string;
/** File category */
category: string;
/** Uploader user ID */
uploadedBy: string;
/** Associated entity type */
entityType?: string;
/** Associated entity ID */
entityId?: string;
/** Whether file is public */
isPublic: boolean;
/** Creation timestamp */
createdAt: string;
/** Download URL */
downloadUrl: string;
/** Thumbnail URL */
thumbnailUrl?: string;
/** Preview URL with transformation options */
previewUrl?: string;
}
interface UploadProgress {
/** Upload progress percentage (0-100) */
progress: number;
/** Bytes uploaded */
loaded: number;
/** Total bytes */
total: number;
/** Upload speed in bytes/second */
speed?: number;
/** Estimated time remaining in seconds */
timeRemaining?: number;
}
interface FileUploadState {
/** Whether upload is in progress */
isUploading: boolean;
/** Upload progress */
progress: UploadProgress;
/** Upload result (when successful) */
result?: UploadResult;
/** Upload error (when failed) */
error?: UploadError;
/** Whether upload was cancelled */
isCancelled: boolean;
}
type FileCategory = 'medical_image' | 'document' | 'report' | 'prescription' | 'lab_result' | 'profile_picture' | 'identification' | 'insurance' | 'other';
type EntityType = 'patient' | 'doctor' | 'appointment' | 'consultation' | 'prescription' | 'business' | 'user' | 'other';
/**
* Preview transformation options
*/
interface PreviewOptions {
/** Return thumbnail instead of original file */
thumbnail?: boolean;
/** Resize image to specified width */
width?: number;
/** Resize image to specified height */
height?: number;
/** Image quality (1-100) */
quality?: number;
/** Convert image to specified format */
format?: 'jpg' | 'png' | 'webp';
}
/**
* File Upload Client
* Production-ready client for Health Ecosystem file uploads
*/
declare class FileUploadClient {
private client;
private config;
private cancelTokens;
constructor(config: FileUploadConfig);
/**
* Upload a single file
*/
uploadFile(file: File, options?: UploadOptions, callbacks?: {
onProgress?: (progress: number) => void;
}): Promise<UploadResult>;
/**
* Upload multiple files
*/
uploadFiles(files: File[], options: UploadOptions): Promise<UploadResult[]>;
/**
* Upload file from URL
* Downloads the file from the provided URL and uploads it to the server
*/
uploadFromUrl(url: string, options: UploadOptions & {
filename?: string;
}): Promise<UploadResult>;
/**
* Get file metadata
*/
getFileMetadata(fileId: string): Promise<FileMetadata>;
/**
* Get download URL for a file
*/
getDownloadUrl(fileId: string): string;
/**
* Get thumbnail URL for an image
*/
getThumbnailUrl(fileId: string): string;
/**
* Get preview URL for a file with optional transformations
* @param previewUrl The preview URL from the file response
* @param options Optional transformation parameters
* @returns Full URL to the preview
*/
getPreviewUrl(previewUrl: string, options?: PreviewOptions): string;
/**
* Delete a file
*/
deleteFile(fileId: string): Promise<void>;
/**
* List files with optional filters - Updated to match backend API structure
*/
listFiles(options?: {
category?: string;
entityType?: string;
entityId?: string;
isPublic?: boolean;
skip?: number;
limit?: number;
}): Promise<FileMetadata[]>;
/**
* Cancel an ongoing upload
*/
cancelUpload(uploadId?: string): void;
/**
* Update authentication token
*/
updateAuthToken(token: string): void;
/**
* Validate file before upload
*/
private validateFile;
/**
* Parse upload response
*/
private parseUploadResponse;
/**
* Parse file metadata response - Updated to match backend structure
*/
private parseFileMetadata;
/**
* Create standardized upload error
*/
private createUploadError;
/**
* Handle axios errors with retry logic
*/
private handleError;
/**
* Determine if error should be retried
*/
private shouldRetry;
/**
* Generate unique upload ID
*/
private generateUploadId;
/**
* Format file size for display
*/
private formatFileSize;
/**
* Validate URL format
*/
private isValidUrl;
/**
* Extract filename from URL
*/
private extractFilenameFromUrl;
/**
* Get file extension from URL or content type
*/
private getFileExtensionFromUrl;
/**
* Check if file type is allowed
*/
private isAllowedFileType;
}
/**
* File Upload Library Component
* Main component that handles file library display, upload, and management
*/
interface FileUploadLibraryProps {
/** File upload configuration with server endpoint and token */
config: FileUploadConfig;
/** Upload options */
options?: UploadOptions;
/** Callback when file is selected */
onFileSelect?: (file: FileMetadata) => void;
/** Allow multiple file selection */
multiple?: boolean;
/** Accepted file types */
accept?: string;
/** Custom CSS class */
className?: string;
/** Custom styles */
style?: React$1.CSSProperties;
}
declare const FileUploadLibrary: React$1.FC<FileUploadLibraryProps>;
/**
* FileUpload Widget - Filestack-like File Upload Component
* Version 3.0 - Complete redesign with multi-source picker
*/
interface FileUploadWidgetProps {
/** Widget configuration */
config: FileUploadConfig;
/** Upload options */
options?: Partial<UploadOptions>;
/** Whether widget is open */
isOpen: boolean;
/** Close widget callback */
onClose: () => void;
/** File selection callback */
onFileSelect?: (files: UploadResult[]) => void;
/** Upload success callback */
onSuccess?: (results: UploadResult[]) => void;
/** Upload error callback */
onError?: (error: Error) => void;
/** Multiple file selection */
multiple?: boolean;
/** Accept file types */
accept?: string;
/** Maximum file size in bytes */
maxFileSize?: number;
/** Widget title */
title?: string;
/** Show file library */
showLibrary?: boolean;
/** Custom CSS classes */
className?: string;
}
declare const FileUploadWidget: React$1.FC<FileUploadWidgetProps>;
/**
* FileUpload Trigger - Button to open the FileUpload Widget
* Simple trigger component that opens the Filestack-like widget
*/
interface FileUploadTriggerProps {
/** Upload configuration */
config: FileUploadConfig;
/** Upload options */
options?: Partial<UploadOptions>;
/** File selection callback */
onFileSelect?: (files: UploadResult[]) => void;
/** Upload success callback */
onSuccess?: (results: UploadResult[]) => void;
/** Upload error callback */
onError?: (error: Error) => void;
/** Button content */
children?: React$1.ReactNode;
/** Button className */
className?: string;
/** Button styles */
style?: React$1.CSSProperties;
/** Multiple file selection */
multiple?: boolean;
/** Accept file types */
accept?: string;
/** Maximum file size */
maxFileSize?: number;
/** Widget title */
title?: string;
/** Show file library */
showLibrary?: boolean;
/** Button variant */
variant?: 'primary' | 'secondary' | 'outline';
/** Button size */
size?: 'small' | 'medium' | 'large';
/** Disabled state */
disabled?: boolean;
}
declare const FileUploadTrigger: React$1.FC<FileUploadTriggerProps>;
interface FileUploadModalProps {
isOpen: boolean;
onClose: () => void;
onFileSelect: (fileUrl: string, fileData: UploadResult) => void;
config: FileUploadConfig;
options?: UploadOptions;
multiple?: boolean;
accept?: string;
title?: string;
}
declare function FileUploadModal({ isOpen, onClose, onFileSelect, config, options, multiple, accept, title }: FileUploadModalProps): react_jsx_runtime.JSX.Element;
interface FileUploadButtonProps {
config: FileUploadConfig;
onFileSelect: (fileUrl: string, fileData: UploadResult) => void;
options?: UploadOptions;
multiple?: boolean;
accept?: string;
title?: string;
variant?: 'default' | 'destructive' | 'outline' | 'secondary' | 'ghost' | 'link';
size?: 'default' | 'sm' | 'lg' | 'icon';
className?: string;
children?: React$1.ReactNode;
disabled?: boolean;
}
declare function FileUploadButton({ config, onFileSelect, options, multiple, accept, title, variant, size, className, children, disabled }: FileUploadButtonProps): react_jsx_runtime.JSX.Element;
/**
* Modern File Uploader Component
* Production-ready React component for Health Ecosystem file uploads
* Version 2.0 - Modern UI with manual upload control
*/
interface FileUploaderProps {
/** File upload configuration */
config: FileUploadConfig;
/** Upload options */
options: UploadOptions;
/** Multiple file selection */
multiple?: boolean;
/** Accept file types */
accept?: string;
/** Enable drag and drop */
enableDropzone?: boolean;
/** Auto upload files when selected */
autoUpload?: boolean;
/** Show file previews */
showPreview?: boolean;
/** Custom styling */
className?: string;
/** Custom styles */
style?: React$1.CSSProperties;
/** Upload success callback */
onSuccess?: (result: UploadResult | UploadResult[]) => void;
/** Upload error callback */
onError?: (error: UploadError) => void;
/** Upload progress callback */
onProgress?: (progress: number) => void;
/** File selection callback */
onFilesSelected?: (files: File[]) => void;
/** Children render function */
children?: (props: FileUploaderRenderProps) => React$1.ReactNode;
}
interface FileUploaderRenderProps {
/** Whether upload is in progress */
isUploading: boolean;
/** Upload progress (0-100) */
progress: number;
/** Upload result */
result?: UploadResult;
/** Upload error */
error?: UploadError;
/** Whether drag is active (if dropzone enabled) */
isDragActive?: boolean;
/** Selected files */
selectedFiles: File[];
/** Open file picker */
openFilePicker: () => void;
/** Upload files manually */
upload: (files?: File[]) => Promise<UploadResult | UploadResult[]>;
/** Cancel upload */
cancel: () => void;
/** Reset state */
reset: () => void;
/** Clear selected files */
clearFiles: () => void;
/** Remove specific file */
removeFile: (index: number) => void;
}
declare const FileUploader: React$1.FC<FileUploaderProps>;
/**
* Modern Image Uploader Component
* Specialized component for image uploads with preview and thumbnail support
* Version 2.0 - Modern UI with manual upload control and click-to-select fix
*/
interface ImageUploaderProps {
/** File upload configuration */
config: FileUploadConfig;
/** Upload options */
options: Omit<UploadOptions, 'category'> & {
category?: string;
};
/** Show image preview */
showPreview?: boolean;
/** Preview image size */
previewSize?: {
width: number;
height: number;
};
/** Multiple image selection */
multiple?: boolean;
/** Enable drag and drop */
enableDropzone?: boolean;
/** Auto upload files when selected */
autoUpload?: boolean;
/** Custom styling */
className?: string;
/** Custom styles */
style?: React$1.CSSProperties;
/** Upload success callback */
onSuccess?: (result: UploadResult | UploadResult[]) => void;
/** Upload error callback */
onError?: (error: UploadError) => void;
/** Upload progress callback */
onProgress?: (progress: number) => void;
/** Image selection callback */
onImagesSelected?: (files: File[]) => void;
}
declare const ImageUploader: React$1.FC<ImageUploaderProps>;
/**
* React Hook for File Upload
* Production-ready React hook for Health Ecosystem file uploads
*/
interface UseFileUploadOptions extends Partial<UploadOptions> {
/** Auto-upload when files are selected */
autoUpload?: boolean;
/** Multiple file selection */
multiple?: boolean;
/** Accept file types (HTML input accept attribute) */
accept?: string;
}
interface UseFileUploadReturn {
/** Current upload state */
state: FileUploadState;
/** Upload files */
upload: (files: File[] | File, options?: Partial<UploadOptions>) => Promise<UploadResult | UploadResult[]>;
/** Cancel current upload */
cancel: () => void;
/** Reset upload state */
reset: () => void;
/** Open file picker */
openFilePicker: () => void;
/** File input props for manual integration */
getInputProps: () => {
type: 'file';
accept?: string;
multiple?: boolean;
onChange: (event: React.ChangeEvent<HTMLInputElement>) => void;
style: {
display: 'none';
};
};
/** Upload client instance */
client: FileUploadClient;
}
declare function useFileUpload(config: FileUploadConfig, options?: UseFileUploadOptions): UseFileUploadReturn;
/**
* Hook for drag and drop file upload
*/
declare function useDropzone(config: FileUploadConfig, options?: UseFileUploadOptions): {
isDragActive: boolean;
getDropzoneProps: () => {
onDragEnter: (event: React.DragEvent) => void;
onDragLeave: (event: React.DragEvent) => void;
onDragOver: (event: React.DragEvent) => void;
onDrop: (event: React.DragEvent) => void;
};
/** Current upload state */
state: FileUploadState;
/** Upload files */
upload: (files: File[] | File, options?: Partial<UploadOptions>) => Promise<UploadResult | UploadResult[]>;
/** Cancel current upload */
cancel: () => void;
/** Reset upload state */
reset: () => void;
/** Open file picker */
openFilePicker: () => void;
/** File input props for manual integration */
getInputProps: () => {
type: "file";
accept?: string;
multiple?: boolean;
onChange: (event: React.ChangeEvent<HTMLInputElement>) => void;
style: {
display: "none";
};
};
/** Upload client instance */
client: FileUploadClient;
};
/**
* Health Ecosystem File Upload Library
* Version 3.0 - Filestack-like Widget with Multi-source Picker
*/
declare const createFileUploadConfig: (baseUrl: string, authToken?: string, options?: Partial<FileUploadConfig>) => FileUploadConfig;
declare const createUploadOptions: (category: string, options?: Partial<UploadOptions>) => UploadOptions;
declare const initializeFileUploadLibrary: (serverEndpoint: string, accessToken: string, options?: {
maxFileSize?: number;
allowedTypes?: string[];
category?: string;
isPublic?: boolean;
}) => {
config: FileUploadConfig;
uploadOptions: UploadOptions;
};
declare const initializeFileUploadWidget: (baseUrl: string, authToken?: string, options?: {
maxFileSize?: number;
allowedTypes?: string[];
multiple?: boolean;
showLibrary?: boolean;
}) => {
config: FileUploadConfig;
defaultOptions: {
multiple: boolean;
showLibrary: boolean;
};
};
declare const FILE_CATEGORIES: {
readonly MEDICAL_IMAGE: "medical_image";
readonly DOCUMENT: "document";
readonly REPORT: "report";
readonly PRESCRIPTION: "prescription";
readonly LAB_RESULT: "lab_result";
readonly PROFILE_PICTURE: "profile_picture";
readonly IDENTIFICATION: "identification";
readonly INSURANCE: "insurance";
readonly VIDEO: "video";
readonly AUDIO: "audio";
readonly OTHER: "other";
};
declare const ENTITY_TYPES: {
readonly PATIENT: "patient";
readonly DOCTOR: "doctor";
readonly APPOINTMENT: "appointment";
readonly CONSULTATION: "consultation";
readonly PRESCRIPTION: "prescription";
readonly BUSINESS: "business";
readonly USER: "user";
readonly OTHER: "other";
};
declare const MAX_FILE_SIZES: {
readonly IMAGE: number;
readonly DOCUMENT: number;
readonly VIDEO: number;
readonly AUDIO: number;
};
declare const UPLOAD_SOURCES: {
readonly DEVICE: "device";
readonly CAMERA: "camera";
readonly GOOGLE_DRIVE: "googledrive";
readonly DROPBOX: "dropbox";
readonly ONEDRIVE: "onedrive";
readonly URL: "url";
readonly INSTAGRAM: "instagram";
readonly FACEBOOK: "facebook";
};
declare const VERSION = "1.0.4";
export { ENTITY_TYPES, FILE_CATEGORIES, FileUploadButton, FileUploadClient, FileUploadLibrary, FileUploadModal, FileUploadTrigger, FileUploadWidget, FileUploader, ImageUploader, MAX_FILE_SIZES, REACT_COMPAT_INFO, UPLOAD_SOURCES, VERSION, createFileUploadConfig, createUploadOptions, initializeFileUploadLibrary, initializeFileUploadWidget, useDropzone, useFileUpload };
export type { EntityType, FileCategory, FileMetadata, FileUploadConfig, FileUploadState, UploadError, UploadOptions, UploadProgress, UploadResult };