UNPKG

8.87 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 maxTotalFileSize?: number | null
80 maxNumberOfFiles?: number | null
81 minNumberOfFiles?: number | null
82 allowedFileTypes?: string[] | null
83 }
84
85 interface UppyOptions<TMeta extends IndexedObject<any> = {}> {
86 id?: string
87 autoProceed?: boolean
88 allowMultipleUploads?: boolean
89 debug?: boolean
90 restrictions?: Restrictions
91 meta?: TMeta
92 onBeforeFileAdded?: (
93 currentFile: UppyFile<TMeta>,
94 files: { [key: string]: UppyFile<TMeta> }
95 ) => UppyFile<TMeta> | boolean | undefined
96 onBeforeUpload?: (files: {
97 [key: string]: UppyFile<TMeta>
98 }) => { [key: string]: UppyFile<TMeta> } | boolean
99 locale?: Locale
100 store?: Store
101 infoTimeout?: number
102 }
103
104 interface UploadResult<
105 TMeta extends IndexedObject<any> = {},
106 TBody extends IndexedObject<any> = {}
107 > {
108 successful: UploadedUppyFile<TMeta, TBody>[]
109 failed: FailedUppyFile<TMeta, TBody>[]
110 }
111
112 interface State<
113 TMeta extends IndexedObject<any> = {},
114 TBody extends IndexedObject<any> = {}
115 > extends IndexedObject<any> {
116 capabilities?: { resumableUploads?: boolean }
117 currentUploads: {}
118 error?: string
119 files: {
120 [key: string]:
121 | UploadedUppyFile<TMeta, TBody>
122 | FailedUppyFile<TMeta, TBody>
123 }
124 info?: {
125 isHidden: boolean
126 type: string
127 message: string
128 details: string
129 }
130 plugins?: IndexedObject<any>
131 totalProgress: number
132 }
133
134 type LogLevel = 'info' | 'warning' | 'error'
135
136 /** Enable the old, untyped `uppy.use()` signature. */
137 type LooseTypes = 'loose'
138 /** Disable the old, untyped `uppy.use()` signature. */
139 type StrictTypes = 'strict'
140 type TypeChecking = LooseTypes | StrictTypes
141
142 // This hack accepts _any_ string for `Event`, but also tricks VSCode and friends into providing autocompletions
143 // for the names listed. https://github.com/microsoft/TypeScript/issues/29729#issuecomment-505826972
144 type LiteralUnion<T extends U, U = string> = T | (U & { });
145 type Event = LiteralUnion<'file-added' | 'file-removed' | 'upload' | 'upload-progress' | 'upload-success' | 'complete' | 'error' | 'upload-error' |
146 'upload-retry' | 'info-visible' | 'info-hidden' | 'cancel-all' | 'restriction-failed' | 'reset-progress'>;
147
148 type UploadHandler = (fileIDs: string[]) => Promise<void>
149
150 class Uppy<TUseStrictTypes extends TypeChecking = TypeChecking> {
151 constructor(opts?: UppyOptions)
152 on<TMeta extends IndexedObject<any> = {}>(
153 event: 'upload-success',
154 callback: (file: UppyFile<TMeta>, body: any, uploadURL: string) => void
155 ): this
156 on<TMeta extends IndexedObject<any> = {}>(
157 event: 'complete',
158 callback: (result: UploadResult<TMeta>) => void
159 ): this
160 on(event: Event, callback: (...args: any[]) => void): this
161 off(event: Event, callback: (...args: any[]) => void): this
162 /**
163 * For use by plugins only.
164 */
165 emit(event: Event, ...args: any[]): void
166 updateAll(state: object): void
167 setOptions(update: Partial<UppyOptions>): void
168 setState(patch: object): void
169 getState<TMeta extends IndexedObject<any> = {}>(): State<TMeta>
170 readonly state: State
171 setFileState(fileID: string, state: object): void
172 resetProgress(): void
173 addPreProcessor(fn: UploadHandler): void
174 removePreProcessor(fn: UploadHandler): void
175 addPostProcessor(fn: UploadHandler): void
176 removePostProcessor(fn: UploadHandler): void
177 addUploader(fn: UploadHandler): void
178 removeUploader(fn: UploadHandler): void
179 setMeta<TMeta extends IndexedObject<any> = {}>(data: TMeta): void
180 setFileMeta<TMeta extends IndexedObject<any> = {}>(
181 fileID: string,
182 data: TMeta
183 ): void
184 getFile<
185 TMeta extends IndexedObject<any> = {},
186 TBody extends IndexedObject<any> = {}
187 >(fileID: string): UppyFile<TMeta, TBody>
188 getFiles<
189 TMeta extends IndexedObject<any> = {},
190 TBody extends IndexedObject<any> = {}
191 >(): Array<UppyFile<TMeta, TBody>>
192 addFile<TMeta extends IndexedObject<any> = {}>(
193 file: AddFileOptions<TMeta>
194 ): void
195 removeFile(fileID: string): void
196 pauseResume(fileID: string): boolean
197 pauseAll(): void
198 resumeAll(): void
199 retryAll<TMeta extends IndexedObject<any> = {}>(): Promise<UploadResult<TMeta>>
200 cancelAll(): void
201 retryUpload<TMeta extends IndexedObject<any> = {}>(fileID: string): Promise<UploadResult<TMeta>>
202 reset(): void
203 getID(): string
204 /**
205 * Add a plugin to this Uppy instance.
206 */
207 use<TOptions, TInstance extends Plugin<TOptions>>(
208 pluginClass: new (uppy: this, opts: TOptions) => TInstance,
209 opts?: TOptions
210 ): this
211 /**
212 * Fallback `.use()` overload with unchecked plugin options.
213 *
214 * This does not validate that the options you pass in are correct.
215 * We recommend disabling this overload by using the `Uppy<Uppy.StrictTypes>` type, instead of the plain `Uppy` type, to enforce strict typechecking.
216 * This overload will be removed in Uppy 2.0.
217 */
218 use(pluginClass: TUseStrictTypes extends StrictTypes ? never : new (uppy: this, opts: any) => Plugin<any>, opts?: object): this
219 getPlugin(name: string): Plugin
220 iteratePlugins(callback: (plugin: Plugin) => void): void
221 removePlugin(instance: Plugin): void
222 close(): void
223 info(
224 message: string | { message: string; details: string },
225 type?: LogLevel,
226 duration?: number
227 ): void
228 hideInfo(): void
229 log(msg: string, type?: LogLevel): void
230 /**
231 * Obsolete: do not use. This method does nothing and will be removed in a future release.
232 */
233 run(): this
234 restore<TMeta extends IndexedObject<any> = {}>(
235 uploadID: string
236 ): Promise<UploadResult<TMeta>>
237 addResultData(uploadID: string, data: object): void
238 upload<TMeta extends IndexedObject<any> = {}>(): Promise<UploadResult<TMeta>>
239 }
240}
241
242/**
243 * Create an uppy instance.
244 *
245 * By default, Uppy's `.use(Plugin, options)` method uses loose type checking.
246 * 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:
247 * ```ts
248 * const uppy = Uppy<Uppy.StrictTypes>()
249 * ```
250 * Make sure to also declare any variables and class properties with the `StrictTypes` parameter:
251 * ```ts
252 * private uppy: Uppy<Uppy.StrictTypes>;
253 * ```
254 */
255declare function Uppy<TUseStrictTypes extends Uppy.TypeChecking = Uppy.TypeChecking>(opts?: Uppy.UppyOptions): Uppy.Uppy<TUseStrictTypes>
256
257export = Uppy
258
\No newline at end of file