UNPKG

@uploadx/client

Version:

Resumable upload client for browser and Node.js

103 lines (102 loc) 3.71 kB
import { type AxiosRequestConfig } from 'axios'; import { type IAxiosRetryConfig } from 'axios-retry'; export type Uploadable = File | Blob | NodeJS.ReadableStream | ArrayBuffer | Uint8Array<ArrayBuffer>; /** * Represents metadata for upload operations */ export interface UploadMetadata { /** The name of the content being uploaded */ name: string; /** The MIME type of the content */ mimeType?: string; /** The size of the content in bytes */ size: number; /** Optional timestamp of when the content was last modified*/ lastModified?: number; [key: string]: unknown; } export type ProgressCallback = (progress: number) => void; export interface UploadConfig { chunkSize?: number; retryConfig?: IAxiosRetryConfig; requestConfig?: AxiosRequestConfig; } export declare class AbortError extends Error { constructor(message?: string); } /** * Client for handling resumable file uploads * * Supports chunked uploads with progress tracking, resuming interrupted uploads, * and works in both browser and Node.js environments. */ export declare class UploadxClient { private client; private chunkSize; private abortController; /** * Creates a new UploadxClient instance * @param config - Configuration options for chunk size, retry behavior, and axios settings */ constructor(config?: UploadConfig); /** * Aborts all ongoing upload operations * Cancels any in-progress uploads initiated by this client instance */ abort(): void; /** * Deletes an existing upload from the server */ deleteUpload(url: string, signal?: AbortSignal): Promise<void>; /** * * Updates the metadata of an existing upload */ updateUpload(url: string, metadata: Partial<UploadMetadata>, signal?: AbortSignal): Promise<void>; /** * Creates a new upload session on the server */ createUpload(endpoint: string, metadata: UploadMetadata, signal?: AbortSignal): Promise<{ url: string; uploadedBytes?: number; }>; /** * Creates an upload session for a file in Node.js environment */ createFileUpload(filePath: string, metadata: UploadMetadata, endpoint: string, signal: AbortSignal | undefined): Promise<{ url: string; uploadedBytes?: number; }>; /** * Uploads a file from disk (Node.js only) */ fileUpload(endpoint: string, filePath: string, metadata: UploadMetadata, onProgress?: ProgressCallback, signal?: AbortSignal): Promise<void>; /** * Uploads data from various sources (Blob, File, Stream, Buffer) */ upload(endpoint: string, data: Uploadable, metadata: UploadMetadata, onProgress?: ProgressCallback, signal?: AbortSignal): Promise<void>; /** * Resumes an upload from a previously created session */ resumeUpload(url: string, data: Uploadable, metadata: UploadMetadata, onProgress?: ProgressCallback, signal?: AbortSignal): Promise<void>; /** * Resumes a file upload from disk (Node.js only) */ resumeFileUpload(url: string, filePath: string, metadata: UploadMetadata, onProgress?: ProgressCallback, signal?: AbortSignal): Promise<void>; /** * Gets the current upload progress from the server */ getUploadStatus(url: string, metadata?: Partial<UploadMetadata>, signal?: AbortSignal): Promise<{ uploadedBytes: number; }>; private uploadChunk; private uploadDataInChunks; private uploadFileInChunks; private uploadBlobInChunks; private uploadStreamInChunks; private uploadBufferInChunks; private isBlob; private isStream; private parseRangeHeader; private handleError; }