veliovgroup / Meteor-Files

🚀 Upload files via DDP or HTTP to ☄️ Meteor server FS, AWS, GridFS, DropBox or Google Drive. Fast, secure and robust.
https://packosphere.com/ostrio/files
BSD 3-Clause "New" or "Revised" License
1.11k stars 168 forks source link

Typescript definitions for Meteor-Files #226

Closed Lavan closed 4 years ago

Lavan commented 8 years ago

I'm interested in using Meteor-Files together with ng2-files-upload which being an Angular 2 component is written in Typescript.

Here's my initial try if anyone is interested.

declare module 'meteor/ostrio:files' {

    import {Mongo} from 'meteor/mongo';
    import {ReactiveVar} from 'meteor/reactive-var';

    export interface FilesCollectionOptions {
        storagePath?: string;
        collection?: Mongo.Collection<any>;
        collectionName?: string;
        continueUploadTTL?: string;
        cacheControl?: string;
        responseHeaders?: {[x: string]: string} | ((responseCode?, fileRef?, versionRef?, version?) => {[x: string]: string});
        throttle?: string;
        schema?: Object;
        chunkSize?: number;
        namingFunction?: Function;
        permissions?: number;
        parentDirPermissions?: number;
        integrityCheck?: boolean;
        strict?: boolean;
        downloadCallback?: Function;
        protected?: boolean | Function;
        public?: boolean;
        onBeforeUpload?: (fileData) => boolean | string;
        onInitiateUpload?: (fileData) => void;
        onBeforeRemove?: (cursor: Mongo.Cursor<any>) => boolean;
        onAfterUpload?: (Object) => any;
        onAfterRemove?: (Object) => any;
        onbeforeunloadMessage?: string | (() => string);
        allowClientCode?: boolean;
        debug?: boolean;
        interceptDownload?: (http, fileRef, version) => any;
    }

    export interface SearchOptions {
        sort?: Mongo.SortSpecifier;
        skip?: number;
        limit?: number;
        fields?: Mongo.FieldSpecifier;
        reactive?: boolean;
        transform?: Function;
    }

    export interface InsertOptions {
        file: File | Object | string;
        isBase64?: boolean;
        meta?: {[x: string]: any};
        transport?: 'ddp' | 'http'
        onStart?: (error: Object, fileData: Object) => any;
        onUploaded?: (error: Object, fileData: Object) => any;
        onAbort?: (fileData: Object) => any;
        onError?: (error: Object, fileData: Object) => any;
        onProgress?: (progress: number, fileData: Object) => any;
        onBeforeUpload?: (fileData: Object) => any;
        chunkSize?: number | 'dynamic';
        allowWebWorkers?: boolean;
    }

    export interface LoadOptions {
        fileName: string;
        meta?: Object;
        type?: string;
        size?: number;
    }

    export class FileUpload {
        file: File;
        onPause: ReactiveVar<boolean>;
        progress: ReactiveVar<number>;
        estimateTime: ReactiveVar<number>;
        estimateSpeed: ReactiveVar<number>;
        state: ReactiveVar<'active' | 'paused' | 'aborted' | 'completed'>;

        pause();

        continue();

        toggle();

        pipe();
    }

    export class FileCursor {
        remove(callback: (err) => void);

        link(): string;

        get(property: string): any;

        fetch(): Object[];

        with(): FileCursor;
    }

    export class FilesCursor extends Mongo.Cursor<any> {

        cursor: Mongo.Cursor<any>;

        get(): Object[];

        hasNext(): boolean;

        next(): Object;

        hasPrevious(): boolean;

        previous(): Object;

        fetch(): Object[]

        first(): Object;

        last(): Object;

        count(): number;

        remove(callback: (err) => void);

        forEach(callback: (doc)=>void, context: Object);

        each(): FileCursor[];

        map(callback: (doc)=>Object, context: Object): Object[];

        current(): Object | undefined;

        observe(callbacks: Mongo.ObserveCallbacks): Meteor.LiveQueryHandle;

        observeChanges(callbacks: Mongo.ObserveChangesCallbacks): Meteor.LiveQueryHandle;
    }

    export class FilesCollection {
        collection: Mongo.Collection<any>;
        schema: any;

        constructor(config: FilesCollectionOptions)

        find(selector?: Mongo.Selector, options?: SearchOptions): FilesCursor;

        findOne(selector?: Mongo.Selector, options?: SearchOptions): FileCursor;

        insert(settings: InsertOptions, autoStart: boolean): FileUpload;

        remove(select: Mongo.Selector, callback: (error)=>any): FilesCollection;

        link(fileRef: Object): string;

        allow(options: Mongo.AllowDenyOptions): void;

        deny(options: Mongo.AllowDenyOptions): void;

        denyClient(): void;

        on(event: string, callback: (fileRef) => void);

        unlink(fileRef: Object, version: string): FilesCollection;

        addFile(path: string, opts: LoadOptions, callback: (err: any, fileRef: Object) => any, proceedAfterUpload: boolean);

        load(url: string, opts: LoadOptions, callback: (err: any, fileRef: Object) => any, proceedAfterUpload: boolean);

        write(buffer: Buffer, opts: LoadOptions, callback: (err: any, fileRef: Object) => any, proceedAfterUpload: boolean);
    }
}

Edit: allowWebWorkers should be optional Edit 2: I've made a few more changes. This will be my last update as while this package does have some good features it doesn't really fit into my app.

dr-dimitru commented 8 years ago

Hi @Lavan ,

Is there any chance make a docs, like "TS definition" from your snippet?

Lavan commented 8 years ago

@dr-dimitru what do you mean by docs like?

As I mentioned in my edit, I'm not using this package any more and I really don't have time to work on it anymore.

dr-dimitru commented 8 years ago

@Lavan sad to hear that. I saw some developers already young your definitions within theirs TS apps.

Okay, hope someone who uses TS will make docs from it.

Avijobo commented 7 years ago

Hi dr.dimitru,

Thanks for pointing me here. I am trying to update and further complete the definitions file

, however I still have a few questions:

Attached is a first attempt of my updated version. Would you or anyone else be able to roughly verify if this is more or less correct, and I don't make any wrong assumptions?

Thanks!

Avijobo

declare module "meteor/ostrio:files" {

    import { Mongo } from 'meteor/mongo';
    import { ReactiveVar } from 'meteor/reactive-var';

    class FileObj {
        size: number;
        name: string;
        type: string;
        path: string;
        isVideo: boolean;
        isAudio: boolean;
        isImage: boolean;
        isText: boolean;
        isJSON: boolean;
        isPDF: boolean;
        extension?: string;
        _storagePath: string;
        _downloadRoute: string;
        _collectionName: string;
        public?: boolean;
        meta?: Object;
        userid?: string;
        updatedAt?: Date;
        versions: Object;
    }

    type FileRef = any; // File record from Mongo DB... don't know the details yet

    interface FileData {
        size: number;
        type: string;
        mime: string;
        "mime-type": string;
        ext: string;
        extension: string;
        name: string;
    }

    interface FilesCollectionConfig {
        storagePath?: string;
        collection?: Mongo.Collection<any>;
        collectionName?: string;
        continueUploadTTL?: string;
        ddp?: Object;
        cacheControl?: string;
        responseHeaders?: { [x: string]: string } | ((responseCode?, fileRef?, versionRef?, version?) => { [x: string]: string });
        throttle?: number | boolean;
        downloadRoute?: string;
        schema?: Object;
        chunkSize?: number;
        namingFunction?: () => string;
        permissions?: number;
        parentDirPermissions?: number;
        integrityCheck?: boolean;
        strict?: boolean;
        downloadCallback?: (fileObj: FileObj) => boolean;
        protected?: boolean | ((fileObj: FileObj) => boolean | number);
        public?: boolean;
        onBeforeUpload?: (fileData: FileData) => boolean | string;
        onBeforeRemove?: (cursor: Mongo.Cursor<any>) => boolean;
        onInitiateUpload?: (fileData: FileData) => void;
        onAfterUpload?: (fileRef: FileRef) => any;
        onAfterRemove?: (files: Object[]) => any;
        onbeforeunloadMessage?: string | (() => string);
        allowClientCode?: boolean;
        debug?: boolean;
        interceptDownload?: (http: any, fileRef: any, version: string) => boolean;
    }

    export interface SearchOptions {
        sort?: Mongo.SortSpecifier;
        skip?: number;
        limit?: number;
        fields?: Mongo.FieldSpecifier;
        reactive?: boolean;
        transform?: Function;
    }

    export interface InsertOptions {
        file: File | Object | string;
        isBase64?: boolean;
        meta?: { [x: string]: any };
        transport?: 'ddp' | 'http'
        onStart?: (error: Object, fileData: Object) => any;
        onUploaded?: (error: Object, fileData: Object) => any;
        onAbort?: (fileData: Object) => any;
        onError?: (error: Object, fileData: Object) => any;
        onProgress?: (progress: number, fileData: Object) => any;
        onBeforeUpload?: (fileData: Object) => any;
        streams?: number | 'dynamic';
        chunkSize?: number | 'dynamic';
        allowWebWorkers?: boolean;
    }

    export interface LoadOptions {
        fileName: string;
        meta?: Object;
        type?: string;
        size?: number;
    }

    export class FileUpload {
        file: File;
        onPause: ReactiveVar<boolean>;
        progress: ReactiveVar<number>;
        estimateTime: ReactiveVar<number>;
        estimateSpeed: ReactiveVar<number>;
        state: ReactiveVar<'active' | 'paused' | 'aborted' | 'completed'>;

        pause();
        continue();
        toggle();
        pipe();
        start();
        on(event: string, callback: Function): void;
    }

    export class FileCursor extends FileObj { // Is it correct to say that it extends FileObj?
        remove(callback: (err) => void): void;
        link(): string;
        get(property: string): Object | any;
        fetch(): Object[];
        with(): ReactiveVar<FileCursor>;
    }

    export class FilesCursor extends Mongo.Cursor<FileObj> {
        cursor: Mongo.Cursor<FileObj>; // Refers to base cursor? Why is this existing?

        get(): Object[];
        hasNext(): boolean;
        next(): Object;
        hasPrevious(): boolean;
        previous(): Object;
        first(): Object;
        last(): Object;
        remove(callback: (err) => void): void;
        each(): FileCursor[];
        current(): Object | undefined;
    }

    export class FilesCollection {
        collection: Mongo.Collection<FileObj>;
        schema: any;

        constructor(config: FilesCollectionConfig)

        find(selector?: Mongo.Selector, options?: SearchOptions): FilesCursor;
        findOne(selector?: Mongo.Selector, options?: SearchOptions): FileCursor;
        insert(settings: InsertOptions, autoStart?: boolean): FileUpload;
        remove(select: Mongo.Selector, callback: (error) => any): FilesCollection;
        link(fileRef: FileRef, version?: string): string;
        allow(options: Mongo.AllowDenyOptions): void;
        deny(options: Mongo.AllowDenyOptions): void;
        denyClient(): void;
        on(event: string, callback: (fileRef: FileRef) => void): void;
        unlink(fileRef: FileRef, version?: string): FilesCollection;
        addFile(path: string, opts: LoadOptions, callback: (err: any, fileRef: FileRef) => any, proceedAfterUpload: boolean);
        load(url: string, opts: LoadOptions, callback: (err: any, fileRef: FileRef) => any, proceedAfterUpload: boolean);
        write(buffer: Buffer, opts: LoadOptions, callback: (err: any, fileRef: FileRef) => any, proceedAfterUpload: boolean);
    }
}
vjau commented 4 years ago

I have updated @Avijobo version, there was a typo on fileObj userId

declare module "meteor/ostrio:files" {

    import { Mongo } from 'meteor/mongo';
    import { ReactiveVar } from 'meteor/reactive-var';

    class FileObj {
        size: number;
        name: string;
        type: string;
        path: string;
        isVideo: boolean;
        isAudio: boolean;
        isImage: boolean;
        isText: boolean;
        isJSON: boolean;
        isPDF: boolean;
        extension?: string;
        _storagePath: string;
        _downloadRoute: string;
        _collectionName: string;
        public?: boolean;
        meta?: Object;
        userId?: string;
        updatedAt?: Date;
        versions: Object;
    }

    type FileRef = any; // File record from Mongo DB... don't know the details yet

    interface FileData {
        size: number;
        type: string;
        mime: string;
        "mime-type": string;
        ext: string;
        extension: string;
        name: string;
    }

    interface FilesCollectionConfig {
        storagePath?: string;
        collection?: Mongo.Collection<any>;
        collectionName?: string;
        continueUploadTTL?: string;
        ddp?: Object;
        cacheControl?: string;
        responseHeaders?: { [x: string]: string } | ((responseCode?, fileRef?, versionRef?, version?) => { [x: string]: string });
        throttle?: number | boolean;
        downloadRoute?: string;
        schema?: Object;
        chunkSize?: number;
        namingFunction?: () => string;
        permissions?: number;
        parentDirPermissions?: number;
        integrityCheck?: boolean;
        strict?: boolean;
        downloadCallback?: (fileObj: FileObj) => boolean;
        protected?: boolean | ((fileObj: FileObj) => boolean | number);
        public?: boolean;
        onBeforeUpload?: (fileData: FileData) => boolean | string;
        onBeforeRemove?: (cursor: Mongo.Cursor<any>) => boolean;
        onInitiateUpload?: (fileData: FileData) => void;
        onAfterUpload?: (fileRef: FileRef) => any;
        onAfterRemove?: (files: Object[]) => any;
        onbeforeunloadMessage?: string | (() => string);
        allowClientCode?: boolean;
        debug?: boolean;
        interceptDownload?: (http: any, fileRef: any, version: string) => boolean;
    }

    export interface SearchOptions {
        sort?: Mongo.SortSpecifier;
        skip?: number;
        limit?: number;
        fields?: Mongo.FieldSpecifier;
        reactive?: boolean;
        transform?: Function;
    }

    export interface InsertOptions {
        file: File | Object | string;
        isBase64?: boolean;
        meta?: { [x: string]: any };
        transport?: 'ddp' | 'http'
        onStart?: (error: Object, fileData: Object) => any;
        onUploaded?: (error: Object, fileData: Object) => any;
        onAbort?: (fileData: Object) => any;
        onError?: (error: Object, fileData: Object) => any;
        onProgress?: (progress: number, fileData: Object) => any;
        onBeforeUpload?: (fileData: Object) => any;
        streams?: number | 'dynamic';
        chunkSize?: number | 'dynamic';
        allowWebWorkers?: boolean;
    }

    export interface LoadOptions {
        fileName: string;
        meta?: Object;
        type?: string;
        size?: number;
    }

    export class FileUpload {
        file: File;
        onPause: ReactiveVar<boolean>;
        progress: ReactiveVar<number>;
        estimateTime: ReactiveVar<number>;
        estimateSpeed: ReactiveVar<number>;
        state: ReactiveVar<'active' | 'paused' | 'aborted' | 'completed'>;

        pause();
        continue();
        toggle();
        pipe();
        start();
        on(event: string, callback: Function): void;
    }

    export class FileCursor extends FileObj { // Is it correct to say that it extends FileObj?
        remove(callback: (err) => void): void;
        link(): string;
        get(property: string): Object | any;
        fetch(): Object[];
        with(): ReactiveVar<FileCursor>;
    }

    export class FilesCursor extends Mongo.Cursor<FileObj> {
        cursor: Mongo.Cursor<FileObj>; // Refers to base cursor? Why is this existing?

        get(): Object[];
        hasNext(): boolean;
        next(): Object;
        hasPrevious(): boolean;
        previous(): Object;
        first(): Object;
        last(): Object;
        remove(callback: (err) => void): void;
        each(): FileCursor[];
        current(): Object | undefined;
    }

    export class FilesCollection {
        collection: Mongo.Collection<FileObj>;
        schema: any;

        constructor(config: FilesCollectionConfig)

        find(selector?: Mongo.Selector, options?: SearchOptions): FilesCursor;
        findOne(selector?: Mongo.Selector, options?: SearchOptions): FileCursor;
        insert(settings: InsertOptions, autoStart?: boolean): FileUpload;
        remove(select: Mongo.Selector, callback: (error) => any): FilesCollection;
        link(fileRef: FileRef, version?: string): string;
        allow(options: Mongo.AllowDenyOptions): void;
        deny(options: Mongo.AllowDenyOptions): void;
        denyClient(): void;
        on(event: string, callback: (fileRef: FileRef) => void): void;
        unlink(fileRef: FileRef, version?: string): FilesCollection;
        addFile(path: string, opts: LoadOptions, callback: (err: any, fileRef: FileRef) => any, proceedAfterUpload: boolean);
        load(url: string, opts: LoadOptions, callback: (err: any, fileRef: FileRef) => any, proceedAfterUpload: boolean);
        write(buffer: Buffer, opts: LoadOptions, callback: (err: any, fileRef: FileRef) => any, proceedAfterUpload: boolean);
    }
}
OliverColeman commented 4 years ago

Here's yet more changes:

// defs for meteor/ostrio:files, adapted from https://github.com/VeliovGroup/Meteor-Files/issues/226#issuecomment-603940729

declare module "meteor/ostrio:files" {

    import { Mongo } from 'meteor/mongo';
    import { ReactiveVar } from 'meteor/reactive-var';

    class FileObj<M> {
        size: number;
        name: string;
        type: string;
        path: string;
        isVideo: boolean;
        isAudio: boolean;
        isImage: boolean;
        isText: boolean;
        isJSON: boolean;
        isPDF: boolean;
        extension?: string;
        _storagePath: string;
        _downloadRoute: string;
        _collectionName: string;
        public?: boolean;
        meta?: M;
        userId?: string;
        updatedAt?: Date;
        versions: Object;
    }

    type FileRef = any; // File record from Mongo DB... don't know the details yet

    interface FileData<M> {
        size: number;
        type: string;
        mime: string;
        "mime-type": string;
        ext: string;
        extension: string;
    name: string;
    meta: M;
    }

    interface FilesCollectionConfig<M> {
        storagePath?: string | ((fileObj: FileObj<M>) => string);
        collection?: Mongo.Collection<any>;
        collectionName?: string;
        continueUploadTTL?: string;
        ddp?: Object;
        cacheControl?: string;
        responseHeaders?: { [x: string]: string } | ((responseCode?, fileRef?, versionRef?, version?) => { [x: string]: string });
        throttle?: number | boolean;
        downloadRoute?: string;
        schema?: Object;
        chunkSize?: number;
        namingFunction?: (fileObj: FileObj<M>) => string;
        permissions?: number;
        parentDirPermissions?: number;
        integrityCheck?: boolean;
        strict?: boolean;
        downloadCallback?: (fileObj: FileObj<M>) => boolean;
        protected?: boolean | ((fileObj: FileObj<M>) => boolean | number);
        public?: boolean;
        onBeforeUpload?: (fileData: FileData<M>) => boolean | string;
        onBeforeRemove?: (cursor: Mongo.Cursor<any>) => boolean;
        onInitiateUpload?: (fileData: FileData<M>) => void;
        onAfterUpload?: (fileRef: FileRef) => any;
        onAfterRemove?: (files: Object[]) => any;
        onbeforeunloadMessage?: string | (() => string);
        allowClientCode?: boolean;
        debug?: boolean;
        interceptDownload?: (http: any, fileRef: any, version: string) => boolean;
    }

    export interface SearchOptions {
        sort?: Mongo.SortSpecifier;
        skip?: number;
        limit?: number;
        fields?: Mongo.FieldSpecifier;
        reactive?: boolean;
        transform?: Function;
    }

    export interface InsertOptions {
        file: File | Object | string;
        isBase64?: boolean;
        meta?: { [x: string]: any };
        transport?: 'ddp' | 'http'
        onStart?: (error: Object, fileData: Object) => any;
        onUploaded?: (error: Object, fileData: Object) => any;
        onAbort?: (fileData: Object) => any;
        onError?: (error: Object, fileData: Object) => any;
        onProgress?: (progress: number, fileData: Object) => any;
        onBeforeUpload?: (fileData: Object) => any;
        streams?: number | 'dynamic';
        chunkSize?: number | 'dynamic';
        allowWebWorkers?: boolean;
    }

    export interface LoadOptions<M> {
        fileName: string;
        meta?: M;
        type?: string;
        size?: number;
    }

    export class FileUpload {
        file: File;
        onPause: ReactiveVar<boolean>;
        progress: ReactiveVar<number>;
        estimateTime: ReactiveVar<number>;
        estimateSpeed: ReactiveVar<number>;
        state: ReactiveVar<'active' | 'paused' | 'aborted' | 'completed'>;

        pause();
        continue();
        toggle();
        pipe();
        start();
        on(event: string, callback: Function): void;
    }

    export class FileCursor<M> extends FileObj<M> { // Is it correct to say that it extends FileObj?
        remove(callback: (err) => void): void;
        link(): string;
        get(property: string): Object | any;
        fetch(): Object[];
        with(): ReactiveVar<FileCursor<M>>;
    }

    export class FilesCursor<M> extends Mongo.Cursor<FileObj<M>> {
        cursor: Mongo.Cursor<FileObj<M>>; // Refers to base cursor? Why is this existing?

        get(): Object[];
        hasNext(): boolean;
        next(): Object;
        hasPrevious(): boolean;
        previous(): Object;
        first(): Object;
        last(): Object;
        remove(callback: (err) => void): void;
        each(): FileCursor<M>[];
        current(): Object | undefined;
    }

    export class FilesCollection<M> {
        collection: Mongo.Collection<FileObj<M>>;
        schema: any;

        constructor(config: FilesCollectionConfig<M>)

        find(selector?: Mongo.Selector<FileObj<M>>, options?: SearchOptions): FilesCursor<M>;
        findOne(selector?: Mongo.Selector<FileObj<M>>, options?: SearchOptions): FileCursor<M>;
        insert(settings: InsertOptions, autoStart?: boolean): FileUpload;
        remove(select: Mongo.Selector<FileObj<M>>, callback: (error) => any): FilesCollection<M>;
        link(fileRef: FileRef, version?: string): string;
        allow(options: Mongo.AllowDenyOptions): void;
        deny(options: Mongo.AllowDenyOptions): void;
        denyClient(): void;
        on(event: string, callback: (fileRef: FileRef) => void): void;
        unlink(fileRef: FileRef, version?: string): FilesCollection<M>;
        addFile(path: string, opts: LoadOptions<M>, callback: (err: any, fileRef: FileRef) => any, proceedAfterUpload: boolean);
        load(url: string, opts: LoadOptions<M>, callback: (err: any, fileRef: FileRef) => any, proceedAfterUpload: boolean);
        write(buffer: Buffer, opts: LoadOptions<M>, callback: (err: any, fileRef: FileRef) => any, proceedAfterUpload: boolean);
    }
}
dr-dimitru commented 4 years ago

@OliverColeman thank you for contribution!

Feel free to send a PR to the docs, here's original file — https://github.com/VeliovGroup/Meteor-Files/blob/dev/docs/typescript-definitions.md

Please, make sure you're pushing to dev branch;

dr-dimitru commented 4 years ago

Reopening as updates submitted by @OliverColeman are pending for PR

dr-dimitru commented 4 years ago

Closed with #743, thanks to @OliverColeman

Delph commented 2 years ago

Shouldn't the type for FileUpload's on member be on(event: string, callback(error: unknown, obj: FileObj<MetadataType>) => void): void? With perhaps a proper type for error rather than unknown, I'm not sure what that should be right now.

dr-dimitru commented 2 years ago

@Delph thank you for the addition @Lavan can you check on the suggestion over your PR, please?

Delph commented 2 years ago

I now have;

  type FileUploadCallback<MetadataType> = ((error: unknown, obj: FileObj<MetadataType>) => void) |
    ((progress: number, obj: FileObj<MetadataType>) => void)

  class FileUpload<MetadataType> {
      file: File;
      onPause: ReactiveVar<boolean>;
      progress: ReactiveVar<number>;
      estimateTime: ReactiveVar<number>;
      estimateSpeed: ReactiveVar<number>;
      state: ReactiveVar<'active' | 'paused' | 'aborted' | 'completed'>;
      pause(): void;
      continue(): void;
      toggle(): void;
      pipe(): void;
      start(): void;
      on(event: string, callback: FileUploadCallback<MetadataType>): void;
  }
dr-dimitru commented 2 years ago

@Delph do you wish to PR into typescript-definitions.md ?

Lavan commented 2 years ago

I have no comment at all regarding this. I haven't used meteor at all these last 4-5 years.