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 all 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 @@ -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>>
Murderlon marked this conversation as resolved.
Show resolved Hide resolved
): 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
Loading