Skip to content

Commit

Permalink
getting there
Browse files Browse the repository at this point in the history
  • Loading branch information
Murderlon committed Dec 5, 2023
1 parent e438e3d commit be25992
Show file tree
Hide file tree
Showing 8 changed files with 470 additions and 244 deletions.
4 changes: 4 additions & 0 deletions packages/@uppy/core/src/BasePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ export default class BasePlugin<

i18nArray: Translator['translateArray']

type: string

VERSION: string

constructor(uppy: Uppy<M, B>, opts: Opts) {
this.uppy = uppy
this.opts = opts ?? {}
Expand Down
33 changes: 18 additions & 15 deletions packages/@uppy/core/src/Restricter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import prettierBytes from '@transloadit/prettier-bytes'
// @ts-expect-error untyped
import match from 'mime-match'
import Translator from '@uppy/utils/lib/Translator'
import type { UppyFile } from '@uppy/utils/lib/UppyFile'
import type { Body, Meta, UppyFile } from '@uppy/utils/lib/UppyFile'
import type { I18n } from '@uppy/utils/lib/Translator'
import type { NonNullableUppyOptions } from './Uppy'
import type { State, NonNullableUppyOptions } from './Uppy'

export type Restrictions = {
maxFileSize: number | null
Expand All @@ -28,14 +28,14 @@ const defaultOptions = {
requiredMetaFields: [],
}

class RestrictionError extends Error {
class RestrictionError<M extends Meta, B extends Body> extends Error {
isUserFacing: boolean

file: UppyFile
file: UppyFile<M, B>

constructor(
message: string,
opts?: { isUserFacing?: boolean; file: UppyFile },
opts?: { isUserFacing?: boolean; file?: UppyFile<M, B> },
) {
super(message)
this.isUserFacing = opts?.isUserFacing ?? false
Expand All @@ -47,14 +47,14 @@ class RestrictionError extends Error {
isRestriction = true
}

class Restricter {
class Restricter<M extends Meta, B extends Body> {
i18n: Translator['translate']

getOpts: () => NonNullableUppyOptions<any, any>
getOpts: () => NonNullableUppyOptions<M, B>

constructor(getOpts: () => NonNullableUppyOptions<any, any>, i18n: I18n) {
constructor(getOpts: () => NonNullableUppyOptions<M, B>, i18n: I18n) {
this.i18n = i18n
this.getOpts = (): NonNullableUppyOptions => {
this.getOpts = (): NonNullableUppyOptions<M, B> => {
const opts = getOpts()

if (
Expand All @@ -69,8 +69,8 @@ class Restricter {

// Because these operations are slow, we cannot run them for every file (if we are adding multiple files)
validateAggregateRestrictions(
existingFiles: UppyFile[],
addingFiles: UppyFile[],
existingFiles: UppyFile<M, B>[],
addingFiles: UppyFile<M, B>[],
): void {
const { maxTotalFileSize, maxNumberOfFiles } = this.getOpts().restrictions

Expand Down Expand Up @@ -109,7 +109,7 @@ class Restricter {
}
}

validateSingleFile(file: UppyFile): void {
validateSingleFile(file: UppyFile<M, B>): void {
const { maxFileSize, minFileSize, allowedFileTypes } =
this.getOpts().restrictions

Expand Down Expand Up @@ -161,14 +161,17 @@ class Restricter {
}
}

validate(existingFiles: UppyFile[], addingFiles: UppyFile[]): void {
validate(
existingFiles: UppyFile<M, B>[],
addingFiles: UppyFile<M, B>[],
): void {
addingFiles.forEach((addingFile) => {
this.validateSingleFile(addingFile)
})
this.validateAggregateRestrictions(existingFiles, addingFiles)
}

validateMinNumberOfFiles(files: UppyFile[]): void {
validateMinNumberOfFiles(files: State<M, B>['files']): void {
const { minNumberOfFiles } = this.getOpts().restrictions
if (minNumberOfFiles && Object.keys(files).length < minNumberOfFiles) {
throw new RestrictionError(
Expand All @@ -177,7 +180,7 @@ class Restricter {
}
}

getMissingRequiredMetaFields(file: UppyFile): {
getMissingRequiredMetaFields(file: UppyFile<M, B>): {
missingFields: string[]
error: InstanceType<typeof RestrictionError>
} {
Expand Down
6 changes: 3 additions & 3 deletions packages/@uppy/core/src/UIPlugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ import Core from './index.ts'
describe('UIPlugin', () => {
describe('getPluginState', () => {
it('returns an empty object if no state is available', () => {
class Example extends UIPlugin {}
const inst = new Example(new Core(), {})
class Example extends UIPlugin<any, any, any> {}
const inst = new Example(new Core<any, any>(), {})

expect(inst.getPluginState()).toEqual({})
})
})

describe('setPluginState', () => {
it('applies patches', () => {
class Example extends UIPlugin {}
class Example extends UIPlugin<any, any, any> {}
const inst = new Example(new Core(), {})

inst.setPluginState({ a: 1 })
Expand Down
Loading

0 comments on commit be25992

Please sign in to comment.