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/core: use variadic arguments for uppy.use #4888

Merged
merged 6 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
1 change: 1 addition & 0 deletions packages/@uppy/core/src/Uppy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ describe('src/Core', () => {
this.bar = this.opts.bar
}
}
// @ts-expect-error missing mandatory option foo
new Core().use(TestPlugin)
new Core().use(TestPlugin, { foo: '', bar: '' })
// @ts-expect-error boolean not allowed
Expand Down
6 changes: 4 additions & 2 deletions packages/@uppy/core/src/Uppy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ type UnknownPlugin<M extends Meta, B extends Body> = InstanceType<
typeof BasePlugin<any, M, B> | typeof UIPlugin<any, M, B>
>

type OmitFirstArg<T> = T extends [any, ...infer U] ? U : never

type UnknownProviderPlugin<M extends Meta, B extends Body> = UnknownPlugin<
M,
B
Expand Down Expand Up @@ -1653,7 +1655,7 @@ export class Uppy<M extends Meta, B extends Body> {
*/
use<T extends typeof BasePlugin<any, M, B>>(
Plugin: T,
opts?: ConstructorParameters<T>[1],
...args: OmitFirstArg<ConstructorParameters<T>>
Murderlon marked this conversation as resolved.
Show resolved Hide resolved
): this {
if (typeof Plugin !== 'function') {
const msg =
Expand All @@ -1665,7 +1667,7 @@ export class Uppy<M extends Meta, B extends Body> {
}

// Instantiate
const plugin = new Plugin(this, opts)
const plugin = new Plugin(this, ...args)
const pluginId = plugin.id

if (!pluginId) {
Expand Down
2 changes: 1 addition & 1 deletion packages/@uppy/core/src/mocks/acquirerPlugin1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default class TestSelector1 extends UIPlugin<any, any, any> {

mocks: { run: mock; update: mock; uninstall: mock }

constructor(uppy: Uppy<any, any>, opts: any) {
constructor(uppy: Uppy<any, any>, opts?: any) {
super(uppy, opts)
this.type = 'acquirer'
this.id = 'TestSelector1'
Expand Down
2 changes: 1 addition & 1 deletion packages/@uppy/core/src/mocks/acquirerPlugin2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default class TestSelector2 extends UIPlugin<any, any, any> {

mocks: { run: mock; update: mock; uninstall: mock }

constructor(uppy: Uppy<any, any>, opts: any) {
constructor(uppy: Uppy<any, any>, opts?: any) {
super(uppy, opts)
this.type = 'acquirer'
this.id = 'TestSelector2'
Expand Down
2 changes: 1 addition & 1 deletion packages/@uppy/core/src/mocks/invalidPluginWithoutId.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default class InvalidPluginWithoutName extends UIPlugin<any, any, any> {

public name: string

constructor(uppy: Uppy<any, any>, opts: any) {
constructor(uppy: Uppy<any, any>, opts?: any) {
super(uppy, opts)
this.type = 'acquirer'
this.name = this.constructor.name
Expand Down
2 changes: 1 addition & 1 deletion packages/@uppy/core/src/mocks/invalidPluginWithoutType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default class InvalidPluginWithoutType extends UIPlugin<any, any, any> {

public name: string

constructor(uppy: Uppy<any, any>, opts: any) {
constructor(uppy: Uppy<any, any>, opts?: any) {
super(uppy, opts)
this.id = 'InvalidPluginWithoutType'
this.name = this.constructor.name
Expand Down
Loading