const FilerobotUtils = require("@scaleflex/widget-utils"); declare module Filerobot { // Utility types type OmitKey = Pick>; // These are defined in @scaleflex/widget-utils instead of core so it can be used there without creating import cycles export type FilerobotFile< TMeta extends IndexedObject = {}, TBody extends IndexedObject = {} > = FilerobotUtils.FilerobotFile; export type Store = FilerobotUtils.Store; export type InternalMetadata = FilerobotUtils.InternalMetadata; interface IndexedObject { [key: string]: T; [key: number]: T; } interface UploadedFilerobotFile extends FilerobotFile { uploadURL: string; } interface FailedFilerobotFile extends FilerobotFile { error: string; } // Replace the `meta` property type with one that allows omitting internal metadata; addFile() will add that type FilerobotFileWithoutMeta = OmitKey< FilerobotFile, "meta" >; interface AddFileOptions< TMeta = IndexedObject, TBody = IndexedObject > extends Partial> { // `.data` is the only required property here. data: Blob | File; meta?: Partial & TMeta; } interface PluginOptions { id?: string; } interface DefaultPluginOptions extends PluginOptions { [prop: string]: any; } type PluginTarget = string | Element | typeof Plugin; class Plugin { id: string; filerobot: Filerobot; type: string; constructor(filerobot: Filerobot, opts?: TOptions); setOptions(update: Partial): void; getPluginState(): object; setPluginState(update: IndexedObject): object; update(state?: object): void; mount(target: PluginTarget, plugin: typeof Plugin): void; render(state: object): void; addTarget(plugin: TPlugin): void; unmount(): void; install(): void; uninstall(): void; } type LocaleStrings = { [K in TNames]?: string | { [n: number]: string }; }; interface Locale { strings: LocaleStrings; pluralize?: (n: number) => number; } interface Restrictions { maxFileSize?: number | null; maxNumberOfFiles?: number | null; minNumberOfFiles?: number | null; allowedFileTypes?: string[] | null; maxItemsSizeForCompression: number | null; } interface FilerobotOptions = {}> { container: string; securityTemplateId?: string; sassKey?: string; dev?: boolean; userInfo?: { email: string; name: string; photo_uri: string; uuid: string; }; autoProceed?: boolean; allowMultipleUploads?: boolean; debug?: boolean; restrictions?: Restrictions; meta?: TMeta; onBeforeFileAdded?: ( currentFile: FilerobotFile, files: { [key: string]: FilerobotFile } ) => FilerobotFile | boolean | undefined; onBeforeUpload?: (files: { [key: string]: FilerobotFile; }) => { [key: string]: FilerobotFile } | boolean; locale?: Locale; store?: Store; userPermissions?: string[]; userFoldersScope?: string[]; apiEndpoint?: string; skipApiEndpointToken?: boolean; adminApiEndpoint?: string; shareApiEndpoint?: string; } interface UploadResult< TMeta extends IndexedObject = {}, TBody extends IndexedObject = {} > { successful: UploadedFilerobotFile[]; failed: FailedFilerobotFile[]; } interface State< TMeta extends IndexedObject = {}, TBody extends IndexedObject = {} > extends IndexedObject { capabilities?: { resumableUploads?: boolean }; currentUploads: {}; error?: string; files: { [key: string]: | UploadedFilerobotFile | FailedFilerobotFile; }; info?: { isHidden: boolean; type: string; message: string; details: string; }; plugins?: IndexedObject; totalProgress: number; } type LogLevel = "info" | "warning" | "error"; /** Enable the old, untyped `filerobot.use()` signature. */ type LooseTypes = "loose"; /** Disable the old, untyped `filerobot.use()` signature. */ type StrictTypes = "strict"; type TypeChecking = LooseTypes | StrictTypes; // This hack accepts _any_ string for `Event`, but also tricks VSCode and friends into providing autocompletions // for the names listed. https://github.com/microsoft/TypeScript/issues/29729#issuecomment-505826972 type LiteralUnion = T | (U & {}); type Event = LiteralUnion< | "file-added" | "file-removed" | "upload" | "upload-progress" | "upload-success" | "complete" | "error" | "upload-error" | "upload-retry" | "info-visible" | "info-hidden" | "cancel-uploads" | "restriction-failed" | "reset-progress" >; type UploadHandler = (fileIDs: string[]) => Promise; class Filerobot { constructor(opts?: FilerobotOptions); on = {}>( event: "upload-success", callback: ( file: FilerobotFile, body: any, uploadURL: string ) => void ): this; on = {}>( event: "complete", callback: (result: UploadResult) => void ): this; on(event: Event, callback: (...args: any[]) => void): this; off(event: Event, callback: (...args: any[]) => void): this; /** * For use by plugins only. */ emit(event: Event, ...args: any[]): void; updateAll(state: object): void; setOptions(update: Partial): void; setState(patch: object): void; getState = {}>(): State; readonly state: State; addPreProcessor(fn: UploadHandler): void; removePreProcessor(fn: UploadHandler): void; addPostProcessor(fn: UploadHandler): void; removePostProcessor(fn: UploadHandler): void; addFilerobot(fn: UploadHandler): void; removeFilerobot(fn: UploadHandler): void; setMeta = {}>(data: TMeta): void; setFileMeta = {}>( fileID: string, data: TMeta ): void; getFile< TMeta extends IndexedObject = {}, TBody extends IndexedObject = {} >(fileID: string): FilerobotFile; getFiles< TMeta extends IndexedObject = {}, TBody extends IndexedObject = {} >(): Array>; addFile = {}>( file: AddFileOptions ): void; removeFile(fileID: string): void; pauseResume(fileID: string): void; cancelUploads(): void; retryUpload = {}>( fileID: string ): Promise>; pauseAll(): void; resumeAll(): void; retryAll = {}>(): Promise< UploadResult >; cancelAll(): void; getId(): string; /** * Add a plugin to this Filerobot instance. */ use>( pluginClass: new (filerobot: this, opts: TOptions) => TInstance, opts?: TOptions ): this; /** * Fallback `.use()` overload with unchecked plugin options. * * This does not validate that the options you pass in are correct. * We recommend disabling this overload by using the `Filerobot` type, instead of the plain `Filerobot` type, to enforce strict typechecking. * This overload will be removed in Filerobot 2.0. */ use( pluginClass: TUseStrictTypes extends StrictTypes ? never : new (filerobot: this, opts: any) => Plugin, opts?: object ): this; getPlugin(name: string): Plugin; iteratePlugins(callback: (plugin: Plugin) => void): void; removePlugin(instance: Plugin): void; close(): void; info( message: string | { message: string; details: string }, type?: LogLevel, duration?: number ): void; hideInfo(): void; log(msg: string, type?: LogLevel): void; /** * Obsolete: do not use. This method does nothing and will be removed in a future release. */ run(): this; restore = {}>( uploadID: string ): Promise>; addResultData(uploadID: string, data: object): void; upload = {}>(): Promise< UploadResult >; } } /** * Create a filerobot instance. * * By default, Filerobot's `.use(Plugin, options)` method uses loose type checking. * In Filerobot 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: * ```ts * const filerobot = Filerobot() * ``` * Make sure to also declare any variables and class properties with the `StrictTypes` parameter: * ```ts * private filerobot: Filerobot; * ``` */ declare function Filerobot< TUseStrictTypes extends Filerobot.TypeChecking = Filerobot.TypeChecking >(opts?: Filerobot.FilerobotOptions): Filerobot.Filerobot; export = Filerobot;