mirror of
https://github.com/transloadit/uppy.git
synced 2026-07-27 04:05:10 +00:00
Always enable strict types and remove .run method (#2957)
* Always use strict types and remove `.run` method * Remove accidental package-lock.json files * Actually remove `.run` method
This commit is contained in:
parent
a4e2da159b
commit
acb6566ec0
18 changed files with 54 additions and 15066 deletions
11664
examples/vue/package-lock.json
generated
11664
examples/vue/package-lock.json
generated
File diff suppressed because it is too large
Load diff
3269
examples/vue3/package-lock.json
generated
3269
examples/vue3/package-lock.json
generated
File diff suppressed because it is too large
Load diff
|
|
@ -3,7 +3,7 @@ import Uppy = require('@uppy/core')
|
|||
import AwsS3Multipart = require('../')
|
||||
|
||||
{
|
||||
const uppy = Uppy<Uppy.StrictTypes>()
|
||||
const uppy = Uppy()
|
||||
uppy.use(AwsS3Multipart, {
|
||||
createMultipartUpload (file) {
|
||||
expectType<Uppy.UppyFile>(file)
|
||||
|
|
@ -39,7 +39,7 @@ import AwsS3Multipart = require('../')
|
|||
}
|
||||
|
||||
{
|
||||
const uppy = Uppy<Uppy.StrictTypes>()
|
||||
const uppy = Uppy()
|
||||
expectError(uppy.use(AwsS3Multipart, { getChunkSize: 100 }))
|
||||
expectError(uppy.use(AwsS3Multipart, { getChunkSize: () => 'not a number' }))
|
||||
uppy.use(AwsS3Multipart, { getChunkSize: () => 100 })
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ import Uppy = require('@uppy/core')
|
|||
import AwsS3 = require('../')
|
||||
|
||||
{
|
||||
const uppy = Uppy<Uppy.StrictTypes>()
|
||||
const uppy = Uppy()
|
||||
uppy.use(AwsS3, {
|
||||
getUploadParameters (file) {
|
||||
expectType<Uppy.UppyFile>(file)
|
||||
|
|
|
|||
|
|
@ -1459,14 +1459,6 @@ class Uppy {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Obsolete, event listeners are now added in the constructor.
|
||||
*/
|
||||
run () {
|
||||
this.log('Calling run() is no longer necessary.', 'warning')
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* Restore an upload by its ID.
|
||||
*/
|
||||
|
|
|
|||
41
packages/@uppy/core/types/index.d.ts
vendored
41
packages/@uppy/core/types/index.d.ts
vendored
|
|
@ -133,21 +133,16 @@ declare module Uppy {
|
|||
|
||||
type LogLevel = 'info' | 'warning' | 'error'
|
||||
|
||||
/** Enable the old, untyped `uppy.use()` signature. */
|
||||
type LooseTypes = 'loose'
|
||||
/** Disable the old, untyped `uppy.use()` signature. */
|
||||
type StrictTypes = 'strict'
|
||||
type TypeChecking = LooseTypes | StrictTypes
|
||||
|
||||
// This hack accepts _any_ string for `Event`, but also tricks VSCode and friends into providing autocompletions
|
||||
// for the names listed. https://github.com/microsoft/TypeScript/issues/29729#issuecomment-505826972
|
||||
type LiteralUnion<T extends U, U = string> = T | (U & { });
|
||||
type LiteralUnion<T extends U, U = string> = T | (U & Record<never, never>);
|
||||
|
||||
type Event = LiteralUnion<'file-added' | 'file-removed' | 'upload' | 'upload-progress' | 'upload-success' | 'complete' | 'error' | 'upload-error' |
|
||||
'upload-retry' | 'info-visible' | 'info-hidden' | 'cancel-all' | 'restriction-failed' | 'reset-progress'>;
|
||||
|
||||
type UploadHandler = (fileIDs: string[]) => Promise<void>
|
||||
|
||||
class Uppy<TUseStrictTypes extends TypeChecking = TypeChecking> {
|
||||
class Uppy {
|
||||
constructor(opts?: UppyOptions)
|
||||
on<TMeta extends IndexedObject<any> = {}>(
|
||||
event: 'upload-success',
|
||||
|
|
@ -219,21 +214,10 @@ declare module Uppy {
|
|||
retryUpload<TMeta extends IndexedObject<any> = {}>(fileID: string): Promise<UploadResult<TMeta>>
|
||||
reset(): void
|
||||
getID(): string
|
||||
/**
|
||||
* Add a plugin to this Uppy instance.
|
||||
*/
|
||||
use<TOptions, TInstance extends Plugin<TOptions>>(
|
||||
pluginClass: new (uppy: this, opts: TOptions) => TInstance,
|
||||
opts?: TOptions
|
||||
): this
|
||||
/**
|
||||
* Fallback `.use()` overload with unchecked plugin options.
|
||||
*
|
||||
* This does not validate that the options you pass in are correct.
|
||||
* We recommend disabling this overload by using the `Uppy<Uppy.StrictTypes>` type, instead of the plain `Uppy` type, to enforce strict typechecking.
|
||||
* This overload will be removed in Uppy 2.0.
|
||||
*/
|
||||
use(pluginClass: TUseStrictTypes extends StrictTypes ? never : new (uppy: this, opts: any) => Plugin<any>, opts?: object): this
|
||||
getPlugin(name: string): Plugin
|
||||
iteratePlugins(callback: (plugin: Plugin) => void): void
|
||||
removePlugin(instance: Plugin): void
|
||||
|
|
@ -246,10 +230,6 @@ declare module Uppy {
|
|||
): void
|
||||
hideInfo(): void
|
||||
log(msg: string, type?: LogLevel): void
|
||||
/**
|
||||
* Obsolete: do not use. This method does nothing and will be removed in a future release.
|
||||
*/
|
||||
run(): this
|
||||
restore<TMeta extends IndexedObject<any> = {}>(
|
||||
uploadID: string
|
||||
): Promise<UploadResult<TMeta>>
|
||||
|
|
@ -258,19 +238,6 @@ declare module Uppy {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an uppy instance.
|
||||
*
|
||||
* By default, Uppy's `.use(Plugin, options)` method uses loose type checking.
|
||||
* In Uppy 2.0, the `.use()` method will get a stricter type signature. You can enable strict type checking of plugin classes and their options today by using:
|
||||
* ```ts
|
||||
* const uppy = Uppy<Uppy.StrictTypes>()
|
||||
* ```
|
||||
* Make sure to also declare any variables and class properties with the `StrictTypes` parameter:
|
||||
* ```ts
|
||||
* private uppy: Uppy<Uppy.StrictTypes>;
|
||||
* ```
|
||||
*/
|
||||
declare function Uppy<TUseStrictTypes extends Uppy.TypeChecking = Uppy.TypeChecking>(opts?: Uppy.UppyOptions): Uppy.Uppy<TUseStrictTypes>
|
||||
declare function Uppy(opts?: Uppy.UppyOptions): Uppy.Uppy
|
||||
|
||||
export = Uppy
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ import Uppy = require('../')
|
|||
import DefaultStore = require('@uppy/store-default')
|
||||
|
||||
{
|
||||
const uppy = Uppy<Uppy.StrictTypes>()
|
||||
const uppy = Uppy()
|
||||
uppy.addFile({
|
||||
data: new Blob([new ArrayBuffer(1024)], {
|
||||
type: 'application/octet-stream'
|
||||
|
|
@ -18,11 +18,11 @@ import DefaultStore = require('@uppy/store-default')
|
|||
|
||||
{
|
||||
const store = DefaultStore()
|
||||
const uppy = Uppy<Uppy.StrictTypes>({ store })
|
||||
const uppy = Uppy({ store })
|
||||
}
|
||||
|
||||
{
|
||||
const uppy = Uppy<Uppy.StrictTypes>()
|
||||
const uppy = Uppy()
|
||||
// this doesn't exist but type checking works anyway :)
|
||||
const f = uppy.getFile('virtual')
|
||||
if (f && f.progress && f.progress.uploadStarted === null) {
|
||||
|
|
@ -40,13 +40,13 @@ import DefaultStore = require('@uppy/store-default')
|
|||
type ResponseBody = {
|
||||
averageColor: string
|
||||
}
|
||||
const uppy = Uppy<Uppy.StrictTypes>()
|
||||
const uppy = Uppy()
|
||||
const f = uppy.getFile<Meta, ResponseBody>('virtual')!
|
||||
expectType<ResponseBody>(f.response!.body)
|
||||
}
|
||||
|
||||
{
|
||||
const uppy = Uppy<Uppy.StrictTypes>()
|
||||
const uppy = Uppy()
|
||||
uppy.addFile({
|
||||
name: 'empty.json',
|
||||
data: new Blob(['null'], { type: 'application/json' }),
|
||||
|
|
@ -59,16 +59,11 @@ import DefaultStore = require('@uppy/store-default')
|
|||
types: 'are checked'
|
||||
}
|
||||
class SomePlugin extends Uppy.Plugin<SomeOptions> {}
|
||||
const untypedUppy = Uppy()
|
||||
untypedUppy.use(SomePlugin, { types: 'are unchecked' })
|
||||
const typedUppy = Uppy<Uppy.StrictTypes>()
|
||||
expectError(typedUppy.use(SomePlugin, { types: 'are unchecked' }))
|
||||
typedUppy.use(SomePlugin, { types: 'are checked' })
|
||||
const typedUppy = Uppy()
|
||||
|
||||
// strictly-typed instance can be cast to a loosely-typed instance
|
||||
const widenUppy: Uppy.Uppy = Uppy<Uppy.StrictTypes>()
|
||||
// and disables the type checking
|
||||
widenUppy.use(SomePlugin, { random: 'nonsense' })
|
||||
expectError(typedUppy.use(SomePlugin, { types: 'error' }))
|
||||
|
||||
typedUppy.use(SomePlugin, { types: 'are checked' })
|
||||
}
|
||||
|
||||
{
|
||||
|
|
@ -107,7 +102,7 @@ import DefaultStore = require('@uppy/store-default')
|
|||
}
|
||||
class TestPlugin extends Uppy.Plugin<TestOptions> {}
|
||||
|
||||
const strict = Uppy<Uppy.StrictTypes>().use(TestPlugin, { testOption: 'hello' })
|
||||
const strict = Uppy().use(TestPlugin, { testOption: 'hello' })
|
||||
;(strict.getPlugin('TestPlugin') as TestPlugin).setOptions({ testOption: 'world' })
|
||||
expectError((strict.getPlugin('TestPlugin') as TestPlugin).setOptions({ testOption: 0 }))
|
||||
expectError((strict.getPlugin('TestPlugin') as TestPlugin).setOptions({ unknownKey: false }))
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ import Uppy = require('@uppy/core')
|
|||
import Dashboard = require('../')
|
||||
|
||||
{
|
||||
const uppy = Uppy<Uppy.StrictTypes>()
|
||||
const uppy = Uppy()
|
||||
uppy.use(Dashboard, {
|
||||
target: 'body'
|
||||
})
|
||||
|
|
@ -15,7 +15,7 @@ import Dashboard = require('../')
|
|||
}
|
||||
|
||||
{
|
||||
const uppy = Uppy<Uppy.StrictTypes>()
|
||||
const uppy = Uppy()
|
||||
uppy.use(Dashboard, {
|
||||
width: '100%',
|
||||
height: 700,
|
||||
|
|
@ -51,7 +51,7 @@ import Dashboard = require('../')
|
|||
}
|
||||
|
||||
{
|
||||
const uppy = Uppy<Uppy.StrictTypes>()
|
||||
const uppy = Uppy()
|
||||
uppy.use(Dashboard, {
|
||||
locale: {
|
||||
strings: {
|
||||
|
|
@ -79,6 +79,6 @@ import Dashboard = require('../')
|
|||
}))
|
||||
}
|
||||
{
|
||||
const uppy = Uppy<Uppy.StrictTypes>()
|
||||
const uppy = Uppy()
|
||||
expectError(uppy.use(Dashboard, { height: {} }))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ import Uppy = require("@uppy/core");
|
|||
import DragDrop = require("../");
|
||||
|
||||
{
|
||||
const uppy = Uppy<Uppy.StrictTypes>();
|
||||
const uppy = Uppy();
|
||||
|
||||
uppy.use(DragDrop, {
|
||||
replaceTargetContent: true,
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ import GoogleDrive = require('../')
|
|||
|
||||
class SomePlugin extends Uppy.Plugin<{}> {}
|
||||
|
||||
const uppy = Uppy<Uppy.StrictTypes>()
|
||||
const uppy = Uppy()
|
||||
uppy.use(GoogleDrive, { companionUrl: '' })
|
||||
uppy.use(GoogleDrive, { target: SomePlugin, companionUrl: '' })
|
||||
uppy.use(GoogleDrive, { target: document.querySelector('#gdrive')!, companionUrl: '' })
|
||||
|
|
|
|||
8
packages/@uppy/react/src/useUppy.d.ts
vendored
8
packages/@uppy/react/src/useUppy.d.ts
vendored
|
|
@ -1,9 +1,5 @@
|
|||
import Uppy = require('@uppy/core')
|
||||
import Uppy = require("@uppy/core")
|
||||
|
||||
declare function useUppy<
|
||||
Types extends Uppy.TypeChecking = Uppy.LooseTypes
|
||||
>(
|
||||
factory: () => Uppy.Uppy<Types>
|
||||
): Uppy.Uppy<Types>
|
||||
declare function useUppy(factory: () => Uppy.Uppy): Uppy.Uppy
|
||||
|
||||
export = useUppy
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ import * as components from '../'
|
|||
|
||||
const { useUppy } = components
|
||||
|
||||
const uppy = Uppy<Uppy.StrictTypes>()
|
||||
const uppy = Uppy()
|
||||
|
||||
function TestComponent() {
|
||||
return (
|
||||
|
|
@ -60,7 +60,7 @@ expectError(<components.DashboardModal replaceTargetContent />)
|
|||
}
|
||||
|
||||
function TestHook () {
|
||||
expectType<Uppy.Uppy<Uppy.StrictTypes>>(useUppy(() => uppy))
|
||||
expectType<Uppy.Uppy<Uppy.LooseTypes>>(useUppy(() => Uppy()))
|
||||
expectType<Uppy.Uppy>(useUppy(() => uppy))
|
||||
expectType<Uppy.Uppy>(useUppy(() => Uppy()))
|
||||
expectError(useUppy(uppy))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,18 +2,20 @@ import { expectError } from 'tsd'
|
|||
import Uppy = require('@uppy/core')
|
||||
import ScreenCapture = require('../')
|
||||
|
||||
Uppy<Uppy.StrictTypes>().use(ScreenCapture)
|
||||
Uppy<Uppy.StrictTypes>().use(ScreenCapture, {})
|
||||
Uppy<Uppy.StrictTypes>().use(ScreenCapture, { preferredVideoMimeType: 'video/mp4' })
|
||||
expectError(Uppy<Uppy.StrictTypes>().use(ScreenCapture, { preferredVideoMimeType: 10 }))
|
||||
{
|
||||
Uppy().use(ScreenCapture)
|
||||
Uppy().use(ScreenCapture, {})
|
||||
Uppy().use(ScreenCapture, { preferredVideoMimeType: 'video/mp4' })
|
||||
expectError(Uppy().use(ScreenCapture, { preferredVideoMimeType: 10 }))
|
||||
}
|
||||
|
||||
function constraints () {
|
||||
Uppy<Uppy.StrictTypes>().use(ScreenCapture, {
|
||||
{
|
||||
Uppy().use(ScreenCapture, {
|
||||
displayMediaConstraints: {
|
||||
video: { displaySurface: 'window' }
|
||||
}
|
||||
})
|
||||
expectError(Uppy<Uppy.StrictTypes>().use(ScreenCapture, {
|
||||
expectError(Uppy().use(ScreenCapture, {
|
||||
displayMediaConstraints: {
|
||||
video: { displaySurface: 'some nonsense' }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ const validParams = {
|
|||
}
|
||||
|
||||
{
|
||||
const uppy = Uppy<Uppy.StrictTypes>()
|
||||
const uppy = Uppy()
|
||||
uppy.use(Transloadit, {
|
||||
getAssemblyOptions (file) {
|
||||
expectType<Uppy.UppyFile>(file)
|
||||
|
|
@ -27,7 +27,7 @@ const validParams = {
|
|||
}
|
||||
|
||||
{
|
||||
const uppy = Uppy<Uppy.StrictTypes>()
|
||||
const uppy = Uppy()
|
||||
// must be bools
|
||||
expectError(
|
||||
uppy.use(Transloadit, { waitForEncoding: null, params: validParams })
|
||||
|
|
@ -38,7 +38,7 @@ const validParams = {
|
|||
}
|
||||
|
||||
{
|
||||
const uppy = Uppy<Uppy.StrictTypes>()
|
||||
const uppy = Uppy()
|
||||
// params.auth.key must be string
|
||||
expectError(uppy.use(Transloadit, { params: {} }))
|
||||
expectError(uppy.use(Transloadit, { params: { auth: {} } }))
|
||||
|
|
|
|||
|
|
@ -3,13 +3,13 @@ import Uppy = require('@uppy/core')
|
|||
import Webcam = require('../')
|
||||
|
||||
{
|
||||
Uppy<Uppy.StrictTypes>().use(Webcam, {
|
||||
Uppy().use(Webcam, {
|
||||
modes: ['video-only']
|
||||
})
|
||||
}
|
||||
|
||||
{
|
||||
Uppy<Uppy.StrictTypes>().use(Webcam, {
|
||||
Uppy().use(Webcam, {
|
||||
modes: ['video-only'],
|
||||
videoConstraints: {
|
||||
width: { min: 420, ideal: 420, max: 1920 },
|
||||
|
|
@ -19,7 +19,7 @@ import Webcam = require('../')
|
|||
}
|
||||
|
||||
{
|
||||
expectError(Uppy<Uppy.StrictTypes>().use(Webcam, {
|
||||
expectError(Uppy().use(Webcam, {
|
||||
modes: ['video-only'],
|
||||
videoConstraints: {
|
||||
width: 'not a number har har'
|
||||
|
|
|
|||
|
|
@ -1,18 +1,20 @@
|
|||
import Uppy = require('@uppy/core')
|
||||
import XHRUpload = require('../')
|
||||
|
||||
Uppy<Uppy.StrictTypes>().use(XHRUpload, {
|
||||
bundle: false,
|
||||
formData: true,
|
||||
endpoint: 'xyz'
|
||||
})
|
||||
{
|
||||
Uppy().use(XHRUpload, {
|
||||
bundle: false,
|
||||
formData: true,
|
||||
endpoint: 'xyz'
|
||||
})
|
||||
}
|
||||
|
||||
function methodMayBeUpperOrLowerCase () {
|
||||
Uppy<Uppy.StrictTypes>().use(XHRUpload, {
|
||||
{
|
||||
Uppy().use(XHRUpload, {
|
||||
endpoint: '/upload',
|
||||
method: 'post'
|
||||
})
|
||||
Uppy<Uppy.StrictTypes>().use(XHRUpload, {
|
||||
Uppy().use(XHRUpload, {
|
||||
endpoint: '/upload',
|
||||
method: 'PUT'
|
||||
})
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ import {
|
|||
const isOnTravis = !!(process.env.TRAVIS && process.env.CI)
|
||||
const TUS_ENDPOINT = `http://${isOnTravis ? 'companion.test' : 'localhost'}:1080/files/`
|
||||
|
||||
const uppy = Core<Core.StrictTypes>({
|
||||
const uppy = Core({
|
||||
debug: true,
|
||||
meta: {
|
||||
username: 'John',
|
||||
|
|
|
|||
|
|
@ -30,39 +30,6 @@ In the [CDN package](/docs/#With-a-script-tag), it is available on the `Uppy` gl
|
|||
const Core = Uppy.Core
|
||||
```
|
||||
|
||||
## TypeScript
|
||||
|
||||
When using TypeScript, Uppy has weak type checking by default. That means that the options to plugins are not type-checked. For example, this is allowed:
|
||||
```ts
|
||||
import Uppy from '@uppy/core'
|
||||
import Tus from '@uppy/tus'
|
||||
const uppy = new Uppy()
|
||||
uppy.use(Tus, {
|
||||
invalidOption: null,
|
||||
endpoint: ['a', 'wrong', 'type']
|
||||
})
|
||||
```
|
||||
|
||||
As of Uppy 1.10, Uppy supports a strict typechecking mode. This mode typechecks the options passed in to plugins. This will be the only mode in Uppy 2.0, but is currently optional to preserve backwards compatibility. The strict mode can be enabled by passing a special generic type parameter to the Uppy constructor:
|
||||
|
||||
```ts
|
||||
import Uppy from '@uppy/core'
|
||||
import Tus from '@uppy/tus'
|
||||
const uppy = Uppy<Uppy.StrictTypes>()
|
||||
uppy.use(Tus, {
|
||||
invalidOption: null // this will now make the compilation fail!
|
||||
})
|
||||
```
|
||||
|
||||
If you are storing Uppy instances in your code, for example in a property on a React or Angular component class, make sure to add the `StrictTypes` flag there as well:
|
||||
```ts
|
||||
class MyComponent extends React.Component {
|
||||
private uppy: Uppy<Uppy.StrictTypes>
|
||||
}
|
||||
```
|
||||
|
||||
In Uppy 2.0, this generic parameter will be removed, and your plugin options will always be type-checked.
|
||||
|
||||
## Options
|
||||
|
||||
The Uppy core module has the following configurable options:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue