Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@uppy/utils: refactor to TS #4699

Merged
merged 134 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from 128 commits
Commits
Show all changes
134 commits
Select commit Hold shift + click to select a range
0124e35
meta: add support for TypeScript plugins
aduh95 Aug 22, 2023
d8a518a
fixup! meta: add support for TypeScript plugins
aduh95 Aug 30, 2023
4e8f732
fixup! meta: add support for TypeScript plugins
aduh95 Aug 30, 2023
f15b3e3
fixup! meta: add support for TypeScript plugins
aduh95 Aug 30, 2023
3985734
fix `yarn dev`
aduh95 Aug 30, 2023
f00dec9
resolve e2e build failure
aduh95 Aug 31, 2023
926effa
fixup! resolve e2e build failure
aduh95 Aug 31, 2023
ef5a87c
fixup! resolve e2e build failure
aduh95 Aug 31, 2023
7f8cda3
add `js2ts` script
aduh95 Sep 1, 2023
c1f69af
add `build:ts`
aduh95 Sep 1, 2023
da629a9
Merge branch 'main' of https://github.com/transloadit/uppy
aduh95 Sep 6, 2023
1ccdcfa
update tsconfig
aduh95 Sep 13, 2023
8ff4725
Merge branch 'main' of https://github.com/transloadit/uppy
aduh95 Sep 22, 2023
5d50cbb
@uppy/utils: refactor to TS
aduh95 Sep 25, 2023
676708b
rename `fetchWithNetworkError` and `getBytesRemaining` to TS
aduh95 Sep 25, 2023
4980818
Update private/js2ts/index.mjs
mifi Sep 26, 2023
57341b6
Update private/js2ts/index.mjs
aduh95 Sep 26, 2023
f7c2e57
Update private/js2ts/index.mjs
aduh95 Sep 26, 2023
2a80377
Merge branch 'main' of https://github.com/transloadit/uppy
aduh95 Sep 26, 2023
6c9586e
Update private/js2ts/index.mjs
aduh95 Sep 26, 2023
ea1eb9e
Merge branch 'ts-packages-support' into utils-to-ts
aduh95 Sep 26, 2023
8e59ec3
`getETA.ts`
aduh95 Sep 26, 2023
5a99859
@uppy/utils: convert `isDomElement` to TS (#4703)
nickrttn Sep 26, 2023
6cf96de
fixup! `getETA.ts`
aduh95 Sep 26, 2023
123c98d
update p-q patch
aduh95 Sep 26, 2023
1ee4651
Merge branch 'ts-packages-support' into utils-to-ts
aduh95 Sep 26, 2023
5c5d13a
convert datauritoblob to ts 🎉 (#4705)
mifi Sep 26, 2023
3dd995d
fix types
mifi Sep 27, 2023
8926f00
`delay.ts`
Murderlon Sep 27, 2023
df6da9f
Apply suggestions from code review
mifi Sep 27, 2023
c976ee2
Update packages/@uppy/utils/src/dataURItoBlob.ts
aduh95 Sep 27, 2023
0616a49
Apply suggestions from code review
aduh95 Sep 27, 2023
02e7718
Update packages/@uppy/utils/src/dataURItoBlob.ts
aduh95 Sep 27, 2023
5cd4e9b
enforce use of explicit return types in library code
aduh95 Sep 27, 2023
fb1c7e7
Merge branch 'ts-packages-support' into utils-to-ts
aduh95 Sep 27, 2023
9e3acf6
Merge branch 'main' into ts-packages-support
aduh95 Sep 27, 2023
996c2af
add comment
aduh95 Sep 27, 2023
7de1f24
add missing return types
aduh95 Sep 27, 2023
f95df25
canvasToBlob to ts
arturi Sep 27, 2023
3996dc7
dataURItoFile to ts
arturi Sep 27, 2023
2c51ed5
findAllDOMElements to ts
arturi Sep 27, 2023
0bd4abd
findDOMElement to ts
arturi Sep 27, 2023
0909fea
Merge branch 'main' into ts-packages-support
aduh95 Sep 28, 2023
d382fc6
Merge branch 'ts-packages-support' of https://github.com/transloadit/…
aduh95 Sep 28, 2023
6659c37
prettier
aduh95 Sep 28, 2023
3b19a37
prettier
aduh95 Sep 28, 2023
b685754
Merge branch 'main' of https://github.com/transloadit/uppy
aduh95 Sep 29, 2023
621ebd6
Merge branch 'ts-packages-support' into utils-to-ts
aduh95 Sep 29, 2023
d10929b
@uppy/svelte: fix TS build command
aduh95 Oct 2, 2023
1f70266
`allowSyntheticDefaultImports`
aduh95 Oct 2, 2023
7e59eb2
Merge branch 'ts-packages-support' into utils-to-ts
aduh95 Oct 2, 2023
28274d0
`hasProperty.ts`
aduh95 Oct 2, 2023
5646fcd
`AbortController.ts`
aduh95 Oct 2, 2023
18fbc49
`emaFilter.ts`
aduh95 Oct 2, 2023
e3be5b6
Move useful types to their own module
aduh95 Oct 2, 2023
30796ed
`emitSocketProgress.ts`
aduh95 Oct 2, 2023
1849561
`ErrorWithCause.ts`
aduh95 Oct 2, 2023
b6f1619
`EventManager.ts`
aduh95 Oct 2, 2023
c7f50be
Merge branch 'main' into ts-packages-support
aduh95 Oct 2, 2023
e338be3
`ErrorWithCause.test.ts`
aduh95 Oct 2, 2023
ba73746
add support for TS in test files
aduh95 Oct 2, 2023
228a5ab
revert public type changes
aduh95 Oct 2, 2023
e4eb8ef
Merge branch 'main' into ts-packages-support
aduh95 Oct 3, 2023
717a0ec
Merge branch 'ts-packages-support' into utils-to-ts
aduh95 Oct 3, 2023
d1c289a
extend config, and match more files
aduh95 Oct 3, 2023
d398bcf
fix imports
aduh95 Oct 3, 2023
40ebb91
fixup! fix imports
aduh95 Oct 3, 2023
887b173
fixup! fixup! fix imports
aduh95 Oct 3, 2023
6ccc518
uppy: fix types
aduh95 Oct 3, 2023
eb1949f
use two TSConfig files per package
aduh95 Oct 3, 2023
e71ce8e
fixup! use two TSConfig files per package
aduh95 Oct 3, 2023
f5940c0
prettier
aduh95 Oct 3, 2023
e167e22
remove outdated ignore entries
aduh95 Oct 3, 2023
9863186
`skipLibCheck`
aduh95 Oct 3, 2023
dd9cea2
Merge branch 'ts-packages-support' of https://github.com/transloadit/…
aduh95 Oct 3, 2023
14f4127
use 2 TSConfig files
aduh95 Oct 3, 2023
5bdbda7
remove debug line
aduh95 Oct 3, 2023
75e4bc0
Merge branch 'main' into ts-packages-support
aduh95 Oct 3, 2023
8e12281
`truncateString.ts`
aduh95 Oct 10, 2023
e907289
`toArray.ts`
aduh95 Oct 10, 2023
922088a
`secondsToTime.ts`
aduh95 Oct 10, 2023
1746488
fixup! `toArray.ts`
aduh95 Oct 10, 2023
9055ffc
`getFileNameAndExtension.ts`
aduh95 Oct 10, 2023
4d21d3f
`remoteFileObjToLocal.ts`
aduh95 Oct 10, 2023
adee8d1
`ProgressTimeout.ts`
aduh95 Oct 10, 2023
35cac3a
`prettyETA.ts`
aduh95 Oct 10, 2023
ef48801
`NetworkError.ts`
aduh95 Oct 10, 2023
a7ef051
`mimeType.ts`
aduh95 Oct 10, 2023
d47b015
`isTouchDevice.ts`
aduh95 Oct 10, 2023
d27066d
fixup! `ProgressTimeout.ts`
aduh95 Oct 10, 2023
3125876
fixup! `ErrorWithCause.ts`
aduh95 Oct 10, 2023
65794f4
`isPreviewSupported.ts`
aduh95 Oct 10, 2023
e37069a
`Translator.ts`
aduh95 Oct 10, 2023
2906df5
`isObjectURL.ts`
aduh95 Oct 13, 2023
9803630
`isDragDropSupported.ts`
aduh95 Oct 13, 2023
11980cc
`isMobileDevice.ts`
aduh95 Oct 13, 2023
967da7e
`isNetworkError.ts`
aduh95 Oct 13, 2023
4ca75a1
`getTimeStamp.ts`
aduh95 Oct 13, 2023
e84bfac
`getTextDirection.ts`
aduh95 Oct 13, 2023
76b9be1
`getSpeed.ts`
aduh95 Oct 13, 2023
be4d7d2
`getSocketHost.ts`
aduh95 Oct 13, 2023
7ca4e74
`getFileTypeExtension.ts`
aduh95 Oct 13, 2023
0eabe4c
`getFileType.ts`
aduh95 Oct 13, 2023
2b7fdf1
`generateFileID.ts`
aduh95 Oct 13, 2023
8c389cf
`fileFilters.ts`
aduh95 Oct 13, 2023
1a4e6e8
`fallbackApi.ts`
aduh95 Oct 13, 2023
e640840
`getFilesAndDirectoriesFromDirectory.ts`
aduh95 Oct 13, 2023
656f014
`getDroppedFile.ts`
aduh95 Oct 15, 2023
74e3dc0
Merge branch 'main' of https://github.com/transloadit/uppy
aduh95 Oct 15, 2023
0bfdf16
disable a rule that conflicts with Prettier
aduh95 Oct 15, 2023
cff7910
Merge branch 'ts-packages-support' of https://github.com/transloadit/…
aduh95 Oct 15, 2023
40a101c
fixup! `getFilesAndDirectoriesFromDirectory.ts`
aduh95 Oct 15, 2023
ab2368e
fixup! `getDroppedFile.ts`
aduh95 Oct 15, 2023
8befa3d
convert tests to TS
aduh95 Oct 17, 2023
759ecbb
exclude test files from mandatory function return type
aduh95 Oct 17, 2023
6fe32fb
Merge branch 'ts-packages-support' into utils-to-ts
aduh95 Oct 17, 2023
cfc2ae4
Merge branch 'main' into utils-to-ts
aduh95 Oct 17, 2023
e63a2d3
fix `UppyFile` definition
aduh95 Oct 17, 2023
cbc0728
Merge branch 'main' into utils-to-ts
Murderlon Nov 1, 2023
8a28aae
Update tsconfig
Murderlon Nov 1, 2023
c9a6e29
Fix format error
Murderlon Nov 1, 2023
1397054
Merge branch 'main' into utils-to-ts
Murderlon Nov 1, 2023
1fe5401
fix Translator test
aduh95 Nov 2, 2023
6cde95a
fu]ix ts errors
aduh95 Nov 2, 2023
42cfaf5
Fis Translator types
Murderlon Nov 2, 2023
4fe37f0
Merge branch 'main' into utils-to-ts
Murderlon Nov 2, 2023
b01fa13
Fix translator bug
Murderlon Nov 3, 2023
dca3777
fix types
aduh95 Nov 3, 2023
6c99442
Fix ErrorWithCause test
Murderlon Nov 3, 2023
dad85bd
Merge branch 'utils-to-ts' of https://github.com/transloadit/uppy int…
Murderlon Nov 3, 2023
1163689
fix utils ref
aduh95 Nov 3, 2023
6c8242b
fix fixes
aduh95 Nov 3, 2023
282da83
Update packages/@uppy/utils/src/Translator.ts
aduh95 Nov 6, 2023
6b1edc2
Update packages/@uppy/utils/src/FileProgress.ts
aduh95 Nov 6, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/@uppy/core/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as UppyUtils from '@uppy/utils'
// This references the old types on purpose, to make sure to not create breaking changes for TS consumers.
import * as UppyUtils from '@uppy/utils/types/index'

// Utility types
type OmitKey<T, Key> = Pick<T, Exclude<keyof T, Key>>
Expand Down
1 change: 1 addition & 0 deletions packages/@uppy/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
"preact": "^10.5.13"
},
"devDependencies": {
"@types/lodash": "^4.14.199",
"vitest": "^0.34.5"
}
}
13 changes: 0 additions & 13 deletions packages/@uppy/utils/src/AbortController.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { describe, expect, it, vi } from 'vitest'
import { AbortController, AbortSignal } from './AbortController.js'
import { AbortController, AbortSignal } from './AbortController.ts'

function flushInstantTimeouts () {
return new Promise(resolve => setTimeout(resolve, 0))
function flushInstantTimeouts() {
return new Promise((resolve) => setTimeout(resolve, 0))
}

describe('AbortController', () => {
Expand Down
22 changes: 22 additions & 0 deletions packages/@uppy/utils/src/AbortController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import hasOwnProperty from './hasProperty.ts'
/**
* Little AbortController proxy module so we can swap out the implementation easily later.
*/
export const { AbortController } = globalThis
export const { AbortSignal } = globalThis
export const createAbortError = (
message = 'Aborted',
options?: Parameters<typeof Error>[1],
): DOMException => {
const err = new DOMException(message, 'AbortError')
if (options != null && hasOwnProperty(options, 'cause')) {
Object.defineProperty(err, 'cause', {
// @ts-expect-error TS is drunk
__proto__: null,
configurable: true,
writable: true,
value: options.cause,
})
}
return err
}
13 changes: 0 additions & 13 deletions packages/@uppy/utils/src/ErrorWithCause.js

This file was deleted.

21 changes: 0 additions & 21 deletions packages/@uppy/utils/src/ErrorWithCause.test.js

This file was deleted.

24 changes: 24 additions & 0 deletions packages/@uppy/utils/src/ErrorWithCause.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { describe, expect, it } from 'vitest'
import ErrorWithCause from './ErrorWithCause.ts'
Murderlon marked this conversation as resolved.
Show resolved Hide resolved
import NetworkError from './NetworkError.ts'

describe('ErrorWithCause', () => {
it('should support a `{ cause }` option', () => {
const cause = new Error('cause')
expect(new ErrorWithCause('message').cause).toEqual(undefined)
expect(new ErrorWithCause('message', {}).cause).toEqual(undefined)
expect(new ErrorWithCause('message', { cause }).cause).toEqual(cause)
})
it('should propagate isNetworkError', () => {
const regularError = new Error('cause')
const networkError = new NetworkError('cause')
expect(
new ErrorWithCause('message', { cause: networkError }).isNetworkError,
).toEqual(true)
expect(
new ErrorWithCause('message', { cause: regularError }).isNetworkError,
).toEqual(false)
expect(new ErrorWithCause('message', {}).isNetworkError).toEqual(false)
expect(new ErrorWithCause('message').isNetworkError).toEqual(false)
})
})
21 changes: 21 additions & 0 deletions packages/@uppy/utils/src/ErrorWithCause.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import type NetworkError from './NetworkError.ts'
import hasProperty from './hasProperty.ts'

class ErrorWithCause extends Error {
public isNetworkError?: boolean

public cause: Error['cause']

constructor(
message?: ConstructorParameters<ErrorConstructor>[0],
options?: ConstructorParameters<ErrorConstructor>[1],
) {
super(message)
this.cause = options?.cause
if (this.cause && hasProperty(this.cause, 'isNetworkError')) {
this.isNetworkError = (this.cause as NetworkError).isNetworkError
}
}
}

export default ErrorWithCause
83 changes: 0 additions & 83 deletions packages/@uppy/utils/src/EventManager.js

This file was deleted.

115 changes: 115 additions & 0 deletions packages/@uppy/utils/src/EventManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import type {
Uppy,
UploadPauseCallback,
FileRemovedCallback,
UploadRetryCallback,
GenericEventCallback,
} from '@uppy/core'
import type { 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

#events: Array<Parameters<typeof Uppy.prototype.on>> = []

constructor(uppy: Uppy) {
this.#uppy = uppy
}

on(
event: Parameters<typeof Uppy.prototype.on>[0],
fn: Parameters<typeof Uppy.prototype.on>[1],
): Uppy {
this.#events.push([event, fn])
return this.#uppy.on(event, fn)
}

remove(): void {
for (const [event, fn] of this.#events.splice(0)) {
this.#uppy.off(event, fn)
}
}

onFilePause(fileID: UppyFile['id'], cb: (isPaused: boolean) => void): void {
this.on(
'upload-pause',
(
targetFileID: Parameters<UploadPauseCallback>[0],
isPaused: Parameters<UploadPauseCallback>[1],
) => {
if (fileID === targetFileID) {
cb(isPaused)
}
},
)
}

onFileRemove(
fileID: UppyFile['id'],
cb: (isPaused: UppyFile['id']) => void,
): void {
this.on('file-removed', (file: Parameters<FileRemovedCallback<any>>[0]) => {
if (fileID === file.id) cb(file.id)
})
}

onPause(fileID: UppyFile['id'], cb: (isPaused: boolean) => void): void {
this.on(
'upload-pause',
(
targetFileID: Parameters<UploadPauseCallback>[0],
isPaused: Parameters<UploadPauseCallback>[1],
) => {
if (fileID === targetFileID) {
// const isPaused = this.#uppy.pauseResume(fileID)
cb(isPaused)
}
},
)
}

onRetry(fileID: UppyFile['id'], cb: () => void): void {
this.on(
'upload-retry',
(targetFileID: Parameters<UploadRetryCallback>[0]) => {
if (fileID === targetFileID) {
cb()
}
},
)
}

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 {
this.on('pause-all', () => {
if (!this.#uppy.getFile(fileID)) return
cb()
})
}

onCancelAll(
fileID: UppyFile['id'],
eventHandler: GenericEventCallback,
): void {
this.on('cancel-all', (...args: Parameters<GenericEventCallback>) => {
if (!this.#uppy.getFile(fileID)) return
eventHandler(...args)
})
}

onResumeAll(fileID: UppyFile['id'], cb: () => void): void {
this.on('resume-all', () => {
if (!this.#uppy.getFile(fileID)) return
cb()
})
}
}
16 changes: 16 additions & 0 deletions packages/@uppy/utils/src/FileProgress.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
interface FileProgressBase {
progress: number
uploadComplete: boolean
percentage: number
bytesTotal: number
}

export type FileProgressStarted = FileProgressBase & {
uploadStarted: number
bytesUploaded: number
}
export type FileProgressNotStarted = FileProgressBase & {
uploadStarted: null
bytesUploaded: false
}
aduh95 marked this conversation as resolved.
Show resolved Hide resolved
export type FileProgress = FileProgressStarted | FileProgressNotStarted
11 changes: 0 additions & 11 deletions packages/@uppy/utils/src/NetworkError.js

This file was deleted.

19 changes: 19 additions & 0 deletions packages/@uppy/utils/src/NetworkError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class NetworkError extends Error {
public cause: unknown

public isNetworkError: true

public request: null | XMLHttpRequest

constructor(error: unknown, xhr: null | XMLHttpRequest = null) {
super(
`This looks like a network error, the endpoint might be blocked by an internet provider or a firewall.`,
)

this.cause = error
this.isNetworkError = true
this.request = xhr
}
}

export default NetworkError
Loading
Loading