Skip to content

Commit

Permalink
@uppy/core: use variadic arguments for uppy.use (#4888)
Browse files Browse the repository at this point in the history
  • Loading branch information
aduh95 authored Apr 11, 2024
1 parent 04839fd commit 09e6a0f
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 6 deletions.
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 @@ -88,6 +88,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
9 changes: 7 additions & 2 deletions packages/@uppy/core/src/Uppy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ export type UnknownPlugin<
PluginState extends Record<string, unknown> = Record<string, unknown>,
> = BasePlugin<any, M, B, PluginState>

// `OmitFirstArg<typeof someArray>` is the type of the returned value of `someArray.slice(1)`.
type OmitFirstArg<T> = T extends [any, ...infer U] ? U : never

export type UnknownProviderPluginState = {
authenticated: boolean | undefined
breadcrumbs: {
Expand Down Expand Up @@ -1718,7 +1721,9 @@ export class Uppy<M extends Meta, B extends Body> {
*/
use<T extends typeof BasePlugin<any, M, B>>(
Plugin: T,
opts?: ConstructorParameters<T>[1],
// We want to let the plugin decide whether `opts` is optional or not
// so we spread the argument rather than defining `opts:` ourselves.
...args: OmitFirstArg<ConstructorParameters<T>>
): this {
if (typeof Plugin !== 'function') {
const msg =
Expand All @@ -1730,7 +1735,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

0 comments on commit 09e6a0f

Please sign in to comment.