Skip to content

Commit

Permalink
Fix build:ts
Browse files Browse the repository at this point in the history
  • Loading branch information
Murderlon committed Dec 6, 2023
1 parent 8f79ed2 commit 71a98b2
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 23 deletions.
7 changes: 5 additions & 2 deletions packages/@uppy/core/src/Restricter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
/* eslint-disable max-classes-per-file, class-methods-use-this */
// @ts-ignore untyped
import prettierBytes from '@transloadit/prettier-bytes'
// @ts-ignore untyped
import match from 'mime-match'
import Translator from '@uppy/utils/lib/Translator'
import type { Body, Meta, UppyFile } from '@uppy/utils/lib/UppyFile'
Expand Down Expand Up @@ -36,7 +39,7 @@ class RestrictionError<M extends Meta, B extends Body> extends Error {
opts?: { isUserFacing?: boolean; file?: UppyFile<M, B> },
) {
super(message)
this.isUserFacing = opts?.isUserFacing ?? true
this.isUserFacing = opts?.isUserFacing ?? false
if (opts?.file) {
this.file = opts.file // only some restriction errors are related to a particular file
}
Expand Down Expand Up @@ -186,7 +189,7 @@ class Restricter<M extends Meta, B extends Body> {
this.i18n('missingRequiredMetaFieldOnFile', { fileName: file.name }),
)
const { requiredMetaFields } = this.getOpts().restrictions
const missingFields = []
const missingFields: string[] = []

for (const field of requiredMetaFields) {
if (!Object.hasOwn(file.meta, field) || file.meta[field] === '') {
Expand Down
2 changes: 2 additions & 0 deletions packages/@uppy/core/src/Uppy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ describe('src/Core', () => {
const core = new Core()
core.use(AcquirerPlugin1)
expect(
// @ts-expect-error untyped
Object.keys(core[Symbol.for('uppy test: getPlugins')]('acquirer'))
.length,
).toEqual(1)
Expand All @@ -68,6 +69,7 @@ describe('src/Core', () => {
const core = new Core()

expect(() => {
// @ts-expect-error expected
core.use(InvalidPlugin)
}).toThrowErrorMatchingSnapshot()
})
Expand Down
28 changes: 17 additions & 11 deletions packages/@uppy/core/src/Uppy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
/* global AggregateError */

import Translator from '@uppy/utils/lib/Translator'
// @ts-expect-error untyped
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore untyped
import ee from 'namespace-emitter'
import { nanoid } from 'nanoid/non-secure'
import throttle from 'lodash/throttle.js'
Expand Down Expand Up @@ -43,6 +44,11 @@ type UnknownPlugin<M extends Meta, B extends Body> = InstanceType<
typeof BasePlugin<any, M, B> | typeof UIPlugin<any, M, B>
>

type MinimalRequiredUppyFile<M extends Meta, B extends Body> = Required<
Pick<UppyFile<M, B>, 'name' | 'data' | 'type' | 'source'>
> &
Partial<Omit<UppyFile<M, B>, 'name' | 'data' | 'type' | 'source'>>

interface UploadResult<M extends Meta, B extends Body> {
successful?: UppyFile<M, B>[]
failed?: UppyFile<M, B>[]
Expand Down Expand Up @@ -327,7 +333,8 @@ export class Uppy<M extends Meta, B extends Body> {

// Exposing uppy object on window for debugging and testing
if (this.opts.debug && typeof window !== 'undefined') {
// @ts-expect-error fine
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
window[this.opts.id] = this
}

Expand Down Expand Up @@ -798,8 +805,8 @@ export class Uppy<M extends Meta, B extends Body> {

// create a copy of the files object only once
const nextFilesState = { ...existingFiles }
const validFilesToAdd = []
const errors = []
const validFilesToAdd: UppyFile<M, B>[] = []
const errors: RestrictionError<M, B>[] = []

for (const fileToAdd of filesToAdd) {
try {
Expand Down Expand Up @@ -891,11 +898,11 @@ export class Uppy<M extends Meta, B extends Body> {
* try to guess file type in a clever way, check file against restrictions,
* and start an upload if `autoProceed === true`.
*/
addFile(file: UppyFile<M, B>): UppyFile<M, B>['id'] {
this.#assertNewUploadAllowed(file)
addFile(file: MinimalRequiredUppyFile<M, B>): UppyFile<M, B>['id'] {
this.#assertNewUploadAllowed(file as UppyFile<M, B>)

const { nextFilesState, validFilesToAdd, errors } =
this.#checkAndUpdateFileState([file])
this.#checkAndUpdateFileState([file as UppyFile<M, B>])

const restrictionErrors = errors.filter((error) => error.isRestriction)
this.#informAndEmit(restrictionErrors)
Expand Down Expand Up @@ -924,11 +931,11 @@ export class Uppy<M extends Meta, B extends Body> {
* This is good for UI plugins, but not for programmatic use.
* Programmatic users should usually still use `addFile()` on individual files.
*/
addFiles(fileDescriptors: UppyFile<M, B>[]): void {
addFiles(fileDescriptors: MinimalRequiredUppyFile<M, B>[]): void {
this.#assertNewUploadAllowed()

const { nextFilesState, validFilesToAdd, errors } =
this.#checkAndUpdateFileState(fileDescriptors)
this.#checkAndUpdateFileState(fileDescriptors as UppyFile<M, B>[])

const restrictionErrors = errors.filter((error) => error.isRestriction)
this.#informAndEmit(restrictionErrors)
Expand Down Expand Up @@ -1800,8 +1807,7 @@ export class Uppy<M extends Meta, B extends Body> {
return uploadID
}

// @ts-expect-error same as createUpload
private [Symbol.for('uppy test: createUpload')](...args): string {
private [Symbol.for('uppy test: createUpload')](...args: any[]): string {
// @ts-expect-error https://github.com/microsoft/TypeScript/issues/47595
return this.#createUpload(...args)
}
Expand Down
13 changes: 10 additions & 3 deletions packages/@uppy/core/src/mocks/acquirerPlugin1.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import { vi } from 'vitest' // eslint-disable-line import/no-extraneous-dependencies
import UIPlugin from '../UIPlugin.ts'
import type Uppy from '../Uppy.ts'

type mock = ReturnType<typeof vi.fn>

export default class TestSelector1 extends UIPlugin<any, any, any> {
constructor(uppy, opts) {
name: string

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

constructor(uppy: Uppy<any, any>, opts: any) {
super(uppy, opts)
this.type = 'acquirer'
this.id = 'TestSelector1'
Expand All @@ -15,7 +22,7 @@ export default class TestSelector1 extends UIPlugin<any, any, any> {
}
}

run(results) {
run(results: any) {
this.uppy.log({
class: this.constructor.name,
method: 'run',
Expand All @@ -25,7 +32,7 @@ export default class TestSelector1 extends UIPlugin<any, any, any> {
return Promise.resolve('success')
}

update(state) {
update(state: any) {
this.mocks.update(state)
}

Expand Down
13 changes: 10 additions & 3 deletions packages/@uppy/core/src/mocks/acquirerPlugin2.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import { vi } from 'vitest' // eslint-disable-line import/no-extraneous-dependencies
import UIPlugin from '../UIPlugin.ts'
import type Uppy from '../Uppy.ts'

type mock = ReturnType<typeof vi.fn>

export default class TestSelector2 extends UIPlugin<any, any, any> {
constructor(uppy, opts) {
name: string

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

constructor(uppy: Uppy<any, any>, opts: any) {
super(uppy, opts)
this.type = 'acquirer'
this.id = 'TestSelector2'
Expand All @@ -15,7 +22,7 @@ export default class TestSelector2 extends UIPlugin<any, any, any> {
}
}

run(results) {
run(results: any) {
this.uppy.log({
class: this.constructor.name,
method: 'run',
Expand All @@ -25,7 +32,7 @@ export default class TestSelector2 extends UIPlugin<any, any, any> {
return Promise.resolve('success')
}

update(state) {
update(state: any) {
this.mocks.update(state)
}

Expand Down
5 changes: 3 additions & 2 deletions packages/@uppy/core/src/mocks/invalidPluginWithoutId.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import UIPlugin from '../UIPlugin.ts'
import type Uppy from '../Uppy.ts'

export default class InvalidPluginWithoutName extends UIPlugin<any, any, any> {
public type: string

public name: string

constructor(uppy, opts) {
constructor(uppy: Uppy<any, any>, opts: any) {
super(uppy, opts)
this.type = 'acquirer'
this.name = this.constructor.name
}

run(results) {
run(results: any) {
this.uppy.log({
class: this.constructor.name,
method: 'run',
Expand Down
5 changes: 3 additions & 2 deletions packages/@uppy/core/src/mocks/invalidPluginWithoutType.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import UIPlugin from '../UIPlugin.ts'
import type Uppy from '../Uppy.ts'

export default class InvalidPluginWithoutType extends UIPlugin<any, any, any> {
public id: string

public name: string

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

run(results) {
run(results: any) {
this.uppy.log({
class: this.constructor.name,
method: 'run',
Expand Down
1 change: 1 addition & 0 deletions packages/@uppy/core/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"noEmit": true
},
"include": ["./package.json", "./src/**/*.*"],
"exclude": ["./src/Uppy.test.ts"],
"references": [
{
"path": "../store-default/tsconfig.build.json"
Expand Down

0 comments on commit 71a98b2

Please sign in to comment.