//#region src/client/types/public.d.ts type ClientUploadError = { type: 'unknown' | 'invalid_request' | 'no_files' | 's3_upload' | 'file_too_large' | 'invalid_file_type' | 'rejected' | 'too_many_files' | 'aborted'; message: string; }; type UploadStatus = 'pending' | 'uploading' | 'complete' | 'failed'; type FileUploadInfo = { /** * The status of the file being uploaded. * * - `pending` - The file is waiting to be uploaded. The signed URL has already been generated. * - `uploading` - The file is currently being uploaded. * - `complete` - The file has been uploaded successfully. * - `error` - The file failed to upload. */ status: T; /** * The progress of the upload, from 0 to 1. * * @example 0.5 // 50% */ progress: number; /** * The key of the S3 object. */ objectKey: string; /** * The metadata of the S3 object. */ objectMetadata: ObjectMetadata; /** * The original file that was uploaded. */ raw: File; /** * The name of the file. */ name: string; /** * The size of the file in bytes. */ size: number; /** * The type of the file. */ type: string; } & (T extends 'failed' ? { error: ClientUploadError; } : {}); type UploadHookControl = { /** * Metadata sent back from the server. */ metadata: ServerMetadata; /** * If a critical error occurred during the upload, and no files were able to be uploaded. For example, if your server is unreachable. * * Is also `true` if some input is invalid. For example, if no files were selected. */ isError: boolean; /** * The error critical that occurred during the upload. * * @see `isError` for more information. */ error: ClientUploadError | null; /** * If the upload is in progress. */ isPending: boolean; /** * If the upload progress is complete. Regardless of if all files succeeded or failed to upload. */ isSettled: boolean; /** * If the upload was aborted. */ isAborted: boolean; /** * Reset the state of the upload. */ reset: () => void; /** * Upload files to S3. * * Will throw if critical errors occur. */ uploadAsync: (input: T extends true ? File[] | FileList : File, options?: { metadata?: ServerMetadata; }) => Promise>; /** * Upload files to S3. * * Will never throw an error. */ upload: (input: T extends true ? File[] | FileList : File, options?: { metadata?: ServerMetadata; }) => Promise>; } & (T extends true ? { /** * The progress of all files during the upload process. * * `uploadedFiles` and `failedFiles` derive from this array, use this to get information about **all** files. */ progresses: FileUploadInfo[]; /** * If all files succeeded to upload. */ allSucceeded: boolean; /** * If some files failed to upload. */ hasFailedFiles: boolean; /** * Files that succeeded to upload. */ uploadedFiles: FileUploadInfo<'complete'>[]; /** * Files that failed to upload. */ failedFiles: FileUploadInfo<'failed'>[]; /** * The progress of **all** files during the upload. Goes from 0 to 1. * * If one file is 100% complete, and another is 0% complete, this will be 0.5. * * @example 0.5 // 50% */ averageProgress: number; } : { /** * The progress of the file during the upload process. */ progress: number; /** * The file that was successfully uploaded. */ uploadedFile: FileUploadInfo<'complete'> | null; /** * If the file was successfully uploaded. */ isSuccess: boolean; }); //#endregion //#region src/client/types/internal.d.ts type ObjectMetadata = Record; type ServerMetadata = Record; type SignedUrlsSuccessResponse = { metadata: ServerMetadata; } & ({ multipart: { files: { file: { objectKey: string; objectMetadata: ObjectMetadata; name: string; size: number; type: string; }; parts: { signedUrl: string; partNumber: number; size: number; }[]; uploadId: string; completeSignedUrl: string; abortSignedUrl: string; }[]; partSize: number; }; } | { files: { signedUrl: string; file: { objectKey: string; objectMetadata: ObjectMetadata; objectCacheControl?: string; name: string; size: number; type: string; }; }[]; }); type UploadHookProps = { /** * The API endpoint to use for uploading files. * * @default '/api/upload' */ api?: string; /** * The route to use to upload the files. Should match the upload route name defined in the server. */ route: string; /** * The number of parts that will be uploaded in parallel when uploading a file. * * **Only used in multipart uploads.** * * @default All parts at once. */ multipartBatchSize?: number; /** * Callback that is called before requesting the pre-signed URLs. Use this to modify files before uploading them, like resizing or compressing. * * You can also throw an error to reject the file upload. */ onBeforeUpload?: (data: T extends true ? { files: File[]; } : { file: File; }) => void | (T extends true ? File[] | Promise : File | Promise); /** * Event that is called before the files start being uploaded to S3. This happens after the server responds with the pre-signed URL. */ onUploadBegin?: (data: { /** * Metadata sent from the server. */ metadata: ServerMetadata; } & (T extends true ? { files: FileUploadInfo<'pending'>[]; } : { file: FileUploadInfo<'pending'>; })) => void; /** * Event that is called when a file upload progress changes. */ onUploadProgress?: (data: { file: FileUploadInfo; }) => void; /** * Event that is called after files are successfully uploaded. * * This event is called even if some files fail to upload, but some succeed. This event is not called if all files fail to upload. */ onUploadComplete?: (data: { /** * Metadata sent back from the server. */ metadata: ServerMetadata; } & (T extends true ? { files: FileUploadInfo<'complete'>[]; failedFiles: FileUploadInfo<'failed'>[]; } : { file: FileUploadInfo<'complete'>; })) => void | Promise; /** * Event that is called after the upload settles (either successfully completed or an error occurs). */ onUploadSettle?: (data: { /** * Metadata sent back from the server. */ metadata: ServerMetadata; } & (T extends true ? { files: FileUploadInfo<'complete'>[]; failedFiles: FileUploadInfo<'failed'>[]; } : { file: FileUploadInfo<'complete'>; })) => void | Promise; /** * Abort signal to cancel the upload. */ signal?: AbortSignal; /** * Headers to send to your server when requesting the pre-signed URLs. */ headers?: HeadersInit; /** * Credentials mode when requesting pre-signed URLs from your server. * * Use `include` to send cookies if your server is on a different origin. */ credentials?: RequestCredentials; /** * Number of times to retry network requests that fail. * * @default 0 */ retry?: number; /** * Delay between retries in milliseconds. * * @default 0 */ retryDelay?: number; } & (T extends true ? { /** * The size of the batch to upload files in parallel. Use `1` to upload files sequentially. * * By default, all files are uploaded in parallel. */ uploadBatchSize?: number; /** * Event that is called after the entire upload if a file fails to upload. * * This event is called even if some files succeed to upload, but some fail. This event is not called if all files succeed. */ onUploadFail?: (data: { /** * Metadata sent back from the server. */ metadata: ServerMetadata; succeededFiles: FileUploadInfo<'complete'>[]; failedFiles: FileUploadInfo<'failed'>[]; }) => void | Promise; /** * Event that is called if a critical error occurs before the upload to S3, and no files were able to be uploaded. For example, if your server is unreachable. * * Is also called some input is invalid. For example, if no files were selected. */ onError?: (error: ClientUploadError) => void; } : { /** * Event that is called if the upload fails. * * Also called if some input is invalid. For example, if no files were selected. */ onError?: (error: ClientUploadError) => void; }); type UploadHookReturn = UploadHookControl & { control: UploadHookControl; }; type DirectUploadResult = { /** * Metadata sent back from the server. */ metadata: ServerMetadata; } & (T extends true ? { /** * Files that were successfully uploaded. */ files: FileUploadInfo<'complete'>[]; /** * Files that failed to upload. */ failedFiles: FileUploadInfo<'failed'>[]; } : { /** * The file that was successfully uploaded. */ file: FileUploadInfo<'complete'>; }); //#endregion export { ClientUploadError, DirectUploadResult, FileUploadInfo, ObjectMetadata, ServerMetadata, SignedUrlsSuccessResponse, UploadHookControl, UploadHookProps, UploadHookReturn, UploadStatus };