UNPKG

8.8 kBTypeScriptView Raw
1import UppyUtils = require('@uppy/utils')
2
3declare module Uppy {
4 // Utility types
5 type OmitKey<T, Key> = Pick<T, Exclude<keyof T, Key>>
6
7 // These are defined in @uppy/utils instead of core so it can be used there without creating import cycles
8 export type UppyFile<
9 TMeta extends IndexedObject<any> = {},
10 TBody extends IndexedObject<any> = {}
11 > = UppyUtils.UppyFile<TMeta, TBody>
12 export type Store = UppyUtils.Store
13 export type InternalMetadata = UppyUtils.InternalMetadata
14
15 interface IndexedObject<T> {
16 [key: string]: T
17 [key: number]: T
18 }
19
20 interface UploadedUppyFile<TMeta, TBody> extends UppyFile<TMeta, TBody> {
21 uploadURL: string
22 }
23
24 interface FailedUppyFile<TMeta, TBody> extends UppyFile<TMeta, TBody> {
25 error: string
26 }
27
28 // Replace the `meta` property type with one that allows omitting internal metadata; addFile() will add that
29 type UppyFileWithoutMeta<TMeta, TBody> = OmitKey<
30 UppyFile<TMeta, TBody>,
31 'meta'
32 >
33 interface AddFileOptions<
34 TMeta = IndexedObject<any>,
35 TBody = IndexedObject<any>
36 > extends Partial<UppyFileWithoutMeta<TMeta, TBody>> {
37 // `.data` is the only required property here.
38 data: Blob | File
39 meta?: Partial<InternalMetadata> & TMeta
40 }
41
42 interface PluginOptions {
43 id?: string
44 }
45 interface DefaultPluginOptions extends PluginOptions {
46 [prop: string]: any
47 }
48
49 type PluginTarget = string | Element | typeof Plugin
50
51 class Plugin<TOptions extends PluginOptions = DefaultPluginOptions> {
52 id: string
53 uppy: Uppy
54 type: string
55 constructor(uppy: Uppy, opts?: TOptions)
56 setOptions(update: Partial<TOptions>): void
57 getPluginState(): object
58 setPluginState(update: IndexedObject<any>): object
59 update(state?: object): void
60 mount(target: PluginTarget, plugin: typeof Plugin): void
61 render(state: object): void
62 addTarget<TPlugin extends Plugin>(plugin: TPlugin): void
63 unmount(): void
64 install(): void
65 uninstall(): void
66 }
67
68 type LocaleStrings<TNames extends string> = {
69 [K in TNames]?: string | { [n: number]: string }
70 }
71 interface Locale<TNames extends string = string> {
72 strings: LocaleStrings<TNames>
73 pluralize?: (n: number) => number
74 }
75
76 interface Restrictions {
77 maxFileSize?: number | null
78 minFileSize?: number | null
79 maxNumberOfFiles?: number | null
80 minNumberOfFiles?: number | null
81 allowedFileTypes?: string[] | null
82 }
83
84 interface UppyOptions<TMeta extends IndexedObject<any> = {}> {
85 id?: string
86 autoProceed?: boolean
87 allowMultipleUploads?: boolean
88 debug?: boolean
89 restrictions?: Restrictions
90 meta?: TMeta
91 onBeforeFileAdded?: (
92 currentFile: UppyFile<TMeta>,
93 files: { [key: string]: UppyFile<TMeta> }
94 ) => UppyFile<TMeta> | boolean | undefined
95 onBeforeUpload?: (files: {
96 [key: string]: UppyFile<TMeta>
97 }) => { [key: string]: UppyFile<TMeta> } | boolean
98 locale?: Locale
99 store?: Store
100 }
101
102 interface UploadResult<
103 TMeta extends IndexedObject<any> = {},
104 TBody extends IndexedObject<any> = {}
105 > {
106 successful: UploadedUppyFile<TMeta, TBody>[]
107 failed: FailedUppyFile<TMeta, TBody>[]
108 }
109
110 interface State<
111 TMeta extends IndexedObject<any> = {},
112 TBody extends IndexedObject<any> = {}
113 > extends IndexedObject<any> {
114 capabilities?: { resumableUploads?: boolean }
115 currentUploads: {}
116 error?: string
117 files: {
118 [key: string]:
119 | UploadedUppyFile<TMeta, TBody>
120 | FailedUppyFile<TMeta, TBody>
121 }
122 info?: {
123 isHidden: boolean
124 type: string
125 message: string
126 details: string
127 }
128 plugins?: IndexedObject<any>
129 totalProgress: number
130 }
131
132 type LogLevel = 'info' | 'warning' | 'error'
133
134 /** Enable the old, untyped `uppy.use()` signature. */
135 type LooseTypes = 'loose'
136 /** Disable the old, untyped `uppy.use()` signature. */
137 type StrictTypes = 'strict'
138 type TypeChecking = LooseTypes | StrictTypes
139
140 // This hack accepts _any_ string for `Event`, but also tricks VSCode and friends into providing autocompletions
141 // for the names listed. https://github.com/microsoft/TypeScript/issues/29729#issuecomment-505826972
142 type LiteralUnion<T extends U, U = string> = T | (U & { });
143 type Event = LiteralUnion<'file-added' | 'file-removed' | 'upload' | 'upload-progress' | 'upload-success' | 'complete' | 'error' | 'upload-error' |
144 'upload-retry' | 'info-visible' | 'info-hidden' | 'cancel-all' | 'restriction-failed' | 'reset-progress'>;
145
146 type UploadHandler = (fileIDs: string[]) => Promise<void>
147
148 class Uppy<TUseStrictTypes extends TypeChecking = TypeChecking> {
149 constructor(opts?: UppyOptions)
150 on<TMeta extends IndexedObject<any> = {}>(
151 event: 'upload-success',
152 callback: (file: UppyFile<TMeta>, body: any, uploadURL: string) => void
153 ): this
154 on<TMeta extends IndexedObject<any> = {}>(
155 event: 'complete',
156 callback: (result: UploadResult<TMeta>) => void
157 ): this
158 on(event: Event, callback: (...args: any[]) => void): this
159 off(event: Event, callback: (...args: any[]) => void): this
160 /**
161 * For use by plugins only.
162 */
163 emit(event: Event, ...args: any[]): void
164 updateAll(state: object): void
165 setOptions(update: Partial<UppyOptions>): void
166 setState(patch: object): void
167 getState<TMeta extends IndexedObject<any> = {}>(): State<TMeta>
168 readonly state: State
169 setFileState(fileID: string, state: object): void
170 resetProgress(): void
171 addPreProcessor(fn: UploadHandler): void
172 removePreProcessor(fn: UploadHandler): void
173 addPostProcessor(fn: UploadHandler): void
174 removePostProcessor(fn: UploadHandler): void
175 addUploader(fn: UploadHandler): void
176 removeUploader(fn: UploadHandler): void
177 setMeta<TMeta extends IndexedObject<any> = {}>(data: TMeta): void
178 setFileMeta<TMeta extends IndexedObject<any> = {}>(
179 fileID: string,
180 data: TMeta
181 ): void
182 getFile<
183 TMeta extends IndexedObject<any> = {},
184 TBody extends IndexedObject<any> = {}
185 >(fileID: string): UppyFile<TMeta, TBody>
186 getFiles<
187 TMeta extends IndexedObject<any> = {},
188 TBody extends IndexedObject<any> = {}
189 >(): Array<UppyFile<TMeta, TBody>>
190 addFile<TMeta extends IndexedObject<any> = {}>(
191 file: AddFileOptions<TMeta>
192 ): void
193 removeFile(fileID: string): void
194 pauseResume(fileID: string): boolean
195 pauseAll(): void
196 resumeAll(): void
197 retryAll<TMeta extends IndexedObject<any> = {}>(): Promise<UploadResult<TMeta>>
198 cancelAll(): void
199 retryUpload<TMeta extends IndexedObject<any> = {}>(fileID: string): Promise<UploadResult<TMeta>>
200 reset(): void
201 getID(): string
202 /**
203 * Add a plugin to this Uppy instance.
204 */
205 use<TOptions, TInstance extends Plugin<TOptions>>(
206 pluginClass: new (uppy: this, opts: TOptions) => TInstance,
207 opts?: TOptions
208 ): this
209 /**
210 * Fallback `.use()` overload with unchecked plugin options.
211 *
212 * This does not validate that the options you pass in are correct.
213 * We recommend disabling this overload by using the `Uppy<Uppy.StrictTypes>` type, instead of the plain `Uppy` type, to enforce strict typechecking.
214 * This overload will be removed in Uppy 2.0.
215 */
216 use(pluginClass: TUseStrictTypes extends StrictTypes ? never : new (uppy: this, opts: any) => Plugin<any>, opts?: object): this
217 getPlugin(name: string): Plugin
218 iteratePlugins(callback: (plugin: Plugin) => void): void
219 removePlugin(instance: Plugin): void
220 close(): void
221 info(
222 message: string | { message: string; details: string },
223 type?: LogLevel,
224 duration?: number
225 ): void
226 hideInfo(): void
227 log(msg: string, type?: LogLevel): void
228 /**
229 * Obsolete: do not use. This method does nothing and will be removed in a future release.
230 */
231 run(): this
232 restore<TMeta extends IndexedObject<any> = {}>(
233 uploadID: string
234 ): Promise<UploadResult<TMeta>>
235 addResultData(uploadID: string, data: object): void
236 upload<TMeta extends IndexedObject<any> = {}>(): Promise<UploadResult<TMeta>>
237 }
238}
239
240/**
241 * Create an uppy instance.
242 *
243 * By default, Uppy's `.use(Plugin, options)` method uses loose type checking.
244 * In Uppy 2.0, the `.use()` method will get a stricter type signature. You can enable strict type checking of plugin classes and their options today by using:
245 * ```ts
246 * const uppy = Uppy<Uppy.StrictTypes>()
247 * ```
248 * Make sure to also declare any variables and class properties with the `StrictTypes` parameter:
249 * ```ts
250 * private uppy: Uppy<Uppy.StrictTypes>;
251 * ```
252 */
253declare function Uppy<TUseStrictTypes extends Uppy.TypeChecking = Uppy.TypeChecking>(opts?: Uppy.UppyOptions): Uppy.Uppy<TUseStrictTypes>
254
255export = Uppy
256
\No newline at end of file