diff --git a/packages/@uppy/core/src/Uppy.ts b/packages/@uppy/core/src/Uppy.ts index 6aaec44f8b..2e7d8f052d 100644 --- a/packages/@uppy/core/src/Uppy.ts +++ b/packages/@uppy/core/src/Uppy.ts @@ -280,8 +280,8 @@ export class Uppy { this.opts = { ...merged, restrictions: { - ...defaultOptions.restrictions, - ...(opts && (opts.restrictions as Restrictions)), + ...(defaultOptions.restrictions as Restrictions), + ...(opts && opts.restrictions), }, } diff --git a/packages/@uppy/utils/src/EventManager.ts b/packages/@uppy/utils/src/EventManager.ts index 395448ac53..72071fea29 100644 --- a/packages/@uppy/utils/src/EventManager.ts +++ b/packages/@uppy/utils/src/EventManager.ts @@ -6,17 +6,17 @@ import type { GenericEventCallback, // @ts-expect-error @uppy/core has not been typed yet } from '@uppy/core' -import type { UppyFile } from './UppyFile' +import type { Meta, Body, UppyFile } from './UppyFile' /** * Create a wrapper around an event emitter with a `remove` method to remove * all events that were added using the wrapped emitter. */ -export default class EventManager { - #uppy: Uppy +export default class EventManager { + #uppy: Uppy #events: Array> = [] - constructor(uppy: Uppy) { + constructor(uppy: Uppy) { this.#uppy = uppy } @@ -34,7 +34,10 @@ export default class EventManager { } } - onFilePause(fileID: UppyFile['id'], cb: (isPaused: boolean) => void): void { + onFilePause( + fileID: UppyFile['id'], + cb: (isPaused: boolean) => void, + ): void { this.on( 'upload-pause', ( @@ -50,8 +53,8 @@ export default class EventManager { } onFileRemove( - fileID: UppyFile['id'], - cb: (isPaused: UppyFile['id']) => void, + fileID: UppyFile['id'], + cb: (isPaused: UppyFile['id']) => void, ): void { this.on('file-removed', (file: Parameters>[0]) => { // @ts-expect-error @uppy/core has not been typed yet @@ -59,7 +62,7 @@ export default class EventManager { }) } - onPause(fileID: UppyFile['id'], cb: (isPaused: boolean) => void): void { + onPause(fileID: UppyFile['id'], cb: (isPaused: boolean) => void): void { this.on( 'upload-pause', ( @@ -75,7 +78,7 @@ export default class EventManager { ) } - onRetry(fileID: UppyFile['id'], cb: () => void): void { + onRetry(fileID: UppyFile['id'], cb: () => void): void { this.on( 'upload-retry', (targetFileID: Parameters[0]) => { @@ -86,14 +89,14 @@ export default class EventManager { ) } - onRetryAll(fileID: UppyFile['id'], cb: () => void): void { + onRetryAll(fileID: UppyFile['id'], cb: () => void): void { this.on('retry-all', () => { if (!this.#uppy.getFile(fileID)) return cb() }) } - onPauseAll(fileID: UppyFile['id'], cb: () => void): void { + onPauseAll(fileID: UppyFile['id'], cb: () => void): void { this.on('pause-all', () => { if (!this.#uppy.getFile(fileID)) return cb() @@ -101,7 +104,7 @@ export default class EventManager { } onCancelAll( - fileID: UppyFile['id'], + fileID: UppyFile['id'], eventHandler: GenericEventCallback, ): void { this.on('cancel-all', (...args: Parameters) => { @@ -110,7 +113,7 @@ export default class EventManager { }) } - onResumeAll(fileID: UppyFile['id'], cb: () => void): void { + onResumeAll(fileID: UppyFile['id'], cb: () => void): void { this.on('resume-all', () => { if (!this.#uppy.getFile(fileID)) return cb() diff --git a/packages/@uppy/utils/src/emitSocketProgress.ts b/packages/@uppy/utils/src/emitSocketProgress.ts index 7443729229..fa3c0a242e 100644 --- a/packages/@uppy/utils/src/emitSocketProgress.ts +++ b/packages/@uppy/utils/src/emitSocketProgress.ts @@ -5,7 +5,7 @@ import type { FileProgress } from './FileProgress' function emitSocketProgress( uploader: any, progressData: FileProgress, - file: UppyFile, + file: UppyFile, ): void { const { progress, bytesUploaded, bytesTotal } = progressData if (progress) { diff --git a/packages/@uppy/utils/src/fileFilters.ts b/packages/@uppy/utils/src/fileFilters.ts index 255e23b8a6..4b12936aba 100644 --- a/packages/@uppy/utils/src/fileFilters.ts +++ b/packages/@uppy/utils/src/fileFilters.ts @@ -1,13 +1,13 @@ import type { UppyFile } from './UppyFile' -export function filterNonFailedFiles(files: UppyFile[]): UppyFile[] { - const hasError = (file: UppyFile): boolean => 'error' in file && !!file.error +export function filterNonFailedFiles(files: UppyFile[]): UppyFile[] { + const hasError = (file: UppyFile): boolean => 'error' in file && !!file.error return files.filter((file) => !hasError(file)) } // Don't double-emit upload-started for Golden Retriever-restored files that were already started -export function filterFilesToEmitUploadStarted(files: UppyFile[]): UppyFile[] { +export function filterFilesToEmitUploadStarted(files: UppyFile[]): UppyFile[] { return files.filter( (file) => !file.progress?.uploadStarted || !file.isRestored, ) diff --git a/packages/@uppy/utils/src/generateFileID.ts b/packages/@uppy/utils/src/generateFileID.ts index 74c53a75da..17f3867fd9 100644 --- a/packages/@uppy/utils/src/generateFileID.ts +++ b/packages/@uppy/utils/src/generateFileID.ts @@ -19,7 +19,7 @@ function encodeFilename(name: string): string { * Takes a file object and turns it into fileID, by converting file.name to lowercase, * removing extra characters and adding type, size and lastModified */ -export default function generateFileID(file: UppyFile): string { +export default function generateFileID(file: UppyFile): string { // It's tempting to do `[items].filter(Boolean).join('-')` here, but that // is slower! simple string concatenation is fast @@ -48,7 +48,7 @@ export default function generateFileID(file: UppyFile): string { // If the provider has a stable, unique ID, then we can use that to identify the file. // Then we don't have to generate our own ID, and we can add the same file many times if needed (different path) -function hasFileStableId(file: UppyFile): boolean { +function hasFileStableId(file: UppyFile): boolean { if (!file.isRemote || !file.remote) return false // These are the providers that it seems like have stable IDs for their files. The other's I haven't checked yet. const stableIdProviders = new Set([ @@ -61,7 +61,7 @@ function hasFileStableId(file: UppyFile): boolean { return stableIdProviders.has(file.remote.provider as any) } -export function getSafeFileId(file: UppyFile): string { +export function getSafeFileId(file: UppyFile): string { if (hasFileStableId(file)) return file.id const fileType = getFileType(file) diff --git a/packages/@uppy/utils/src/getBytesRemaining.ts b/packages/@uppy/utils/src/getBytesRemaining.ts index 94675ae4c0..023bdc8498 100644 --- a/packages/@uppy/utils/src/getBytesRemaining.ts +++ b/packages/@uppy/utils/src/getBytesRemaining.ts @@ -1,5 +1,6 @@ -import type { FileProgress } from './FileProgress' - -export default function getBytesRemaining(fileProgress: FileProgress): number { - return fileProgress.bytesTotal - (fileProgress.bytesUploaded as number) +export default function getBytesRemaining(fileProgress: { + bytesTotal: number + bytesUploaded: number +}): number { + return fileProgress.bytesTotal - fileProgress.bytesUploaded } diff --git a/packages/@uppy/utils/src/getETA.ts b/packages/@uppy/utils/src/getETA.ts index e85416aeec..f5aa53f34c 100644 --- a/packages/@uppy/utils/src/getETA.ts +++ b/packages/@uppy/utils/src/getETA.ts @@ -2,7 +2,20 @@ import getSpeed from './getSpeed.ts' import getBytesRemaining from './getBytesRemaining.ts' import type { FileProgress } from './FileProgress.ts' -export default function getETA(fileProgress: FileProgress): number { +type NonNullableFileProgress = Omit< + FileProgress, + | 'preprocess' + | 'postprocess' + | 'bytesTotal' + | 'bytesUploaded' + | 'uploadStarted' +> & { + bytesTotal: number + bytesUploaded: number + uploadStarted: number +} + +export default function getETA(fileProgress: NonNullableFileProgress): number { if (!fileProgress.bytesUploaded) return 0 const uploadSpeed = getSpeed(fileProgress) diff --git a/packages/@uppy/utils/src/getFileType.test.ts b/packages/@uppy/utils/src/getFileType.test.ts index e120ef073c..aa0175bccd 100644 --- a/packages/@uppy/utils/src/getFileType.test.ts +++ b/packages/@uppy/utils/src/getFileType.test.ts @@ -8,7 +8,7 @@ describe('getFileType', () => { isRemote: true, type: 'audio/webm', name: 'foo.webm', - } as any as UppyFile + } as any as UppyFile expect(getFileType(file)).toEqual('audio/webm') }) @@ -17,7 +17,7 @@ describe('getFileType', () => { type: 'audio/webm', name: 'foo.webm', data: 'sdfsdfhq9efbicw', - } as any as UppyFile + } as any as UppyFile expect(getFileType(file)).toEqual('audio/webm') }) @@ -25,24 +25,24 @@ describe('getFileType', () => { const fileMP3 = { name: 'foo.mp3', data: 'sdfsfhfh329fhwihs', - } as any as UppyFile + } as any as UppyFile const fileYAML = { name: 'bar.yaml', data: 'sdfsfhfh329fhwihs', - } as any as UppyFile + } as any as UppyFile const fileMKV = { name: 'bar.mkv', data: 'sdfsfhfh329fhwihs', - } as any as UppyFile + } as any as UppyFile const fileDicom = { name: 'bar.dicom', data: 'sdfsfhfh329fhwihs', - } as any as UppyFile + } as any as UppyFile const fileWebp = { name: 'bar.webp', data: 'sdfsfhfh329fhwihs', - } as any as UppyFile - const toUpper = (file: UppyFile) => ({ + } as any as UppyFile + const toUpper = (file: UppyFile) => ({ ...file, name: file.name.toUpperCase(), }) @@ -62,7 +62,7 @@ describe('getFileType', () => { const file = { name: 'foobar', data: 'sdfsfhfh329fhwihs', - } as any as UppyFile + } as any as UppyFile expect(getFileType(file)).toEqual('application/octet-stream') }) }) diff --git a/packages/@uppy/utils/src/getFileType.ts b/packages/@uppy/utils/src/getFileType.ts index 83920e93e1..a407124721 100644 --- a/packages/@uppy/utils/src/getFileType.ts +++ b/packages/@uppy/utils/src/getFileType.ts @@ -2,7 +2,7 @@ import type { UppyFile } from './UppyFile' import getFileNameAndExtension from './getFileNameAndExtension.ts' import mimeTypes from './mimeTypes.ts' -export default function getFileType(file: Partial): string { +export default function getFileType(file: Partial>): string { if (file.type) return file.type const fileExtension = file.name diff --git a/packages/@uppy/utils/src/getSpeed.ts b/packages/@uppy/utils/src/getSpeed.ts index 1d12c27b39..ea48cac400 100644 --- a/packages/@uppy/utils/src/getSpeed.ts +++ b/packages/@uppy/utils/src/getSpeed.ts @@ -1,6 +1,7 @@ -import type { FileProgress } from './FileProgress' - -export default function getSpeed(fileProgress: FileProgress): number { +export default function getSpeed(fileProgress: { + bytesUploaded?: number + uploadStarted: number +}): number { if (!fileProgress.bytesUploaded) return 0 const timeElapsed = Date.now() - fileProgress.uploadStarted