diff --git a/types/multer-gridfs-storage/index.d.ts b/types/multer-gridfs-storage/index.d.ts index 0a827bedcb..42f02c024b 100644 --- a/types/multer-gridfs-storage/index.d.ts +++ b/types/multer-gridfs-storage/index.d.ts @@ -1,4 +1,4 @@ -// Type definitions for multer-gridfs-storage 2.0 +// Type definitions for multer-gridfs-storage 3.1 // Project: https://github.com/devconcept/multer-gridfs-storage // Definitions by: devconcept // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped @@ -7,37 +7,69 @@ import { EventEmitter } from 'events'; import { Express } from 'express'; import * as Multer from 'multer'; -import { Db } from 'mongodb'; +import { Db, MongoClient } from 'mongodb'; +import { Connection, Mongoose } from 'mongoose'; + +declare class Cache { + initialize(opts: object): object; + findUri(cacheName: string, url: string): string; + has(cacheIndex: object): boolean; + get(cacheIndex: object): object; + set(cacheIndex: object, value: object): void; + isPending(cacheIndex: object): boolean; + isOpening(cacheIndex: object): boolean; + resolve(cacheIndex: object, db: Db, client: MongoClient): void; + reject(cacheIndex: object, err: Error): void; + waitFor(cacheIndex: object): Promise; + connections(): number; + remove(cacheIndex: object): void; + clear(): void; +} interface MulterGfsOptions { file?(req: Express.Request, file: Express.Multer.File): any; } declare class MulterGridfsStorage extends EventEmitter implements Multer.StorageEngine { - constructor(settings: MulterGridfsStorage.UrlStorageOptions | MulterGridfsStorage.DbStorageOptions); + db: Db; + client: MongoClient; + connected: boolean; + connecting: boolean; + configuration: MulterGridfsStorage.UrlStorageOptions | MulterGridfsStorage.DbStorageOptions; + error: Error; + caching: boolean; + cacheName: string; + cacheIndex: object; + + constructor(configuration: MulterGridfsStorage.UrlStorageOptions | MulterGridfsStorage.DbStorageOptions); _handleFile(req: Express.Request, file: Express.Multer.File, callback: (error?: any, info?: Express.Multer.File) => void): void; _removeFile(req: Express.Request, file: Express.Multer.File, callback: (error: Error) => void): void; + + static cache: Cache; } declare namespace MulterGridfsStorage { interface UrlStorageOptions extends MulterGfsOptions { url: string; - connectionOpts?: any; + options?: any; + cache?: boolean | string; } interface DbStorageOptions extends MulterGfsOptions { - db: Promise | Db; + db: Mongoose | Connection | Db | Promise; } interface FileConfig { filename?: string; id?: any; - metadata?: any; + metadata?: object; chunkSize?: number; bucketName?: string; contentType?: string; + aliases?: string[]; + disableMD5?: boolean; } } diff --git a/types/multer-gridfs-storage/multer-gridfs-storage-tests.ts b/types/multer-gridfs-storage/multer-gridfs-storage-tests.ts index d34eaeeae6..95c466ae36 100644 --- a/types/multer-gridfs-storage/multer-gridfs-storage-tests.ts +++ b/types/multer-gridfs-storage/multer-gridfs-storage-tests.ts @@ -1,5 +1,6 @@ import MulterGridfsStorage = require('multer-gridfs-storage'); -import { Db, MongoClient, Server } from "mongodb"; +import { Db, MongoClient, Server } from 'mongodb'; +import * as mongoose from 'mongoose'; // Exported interfaces const conf: MulterGridfsStorage.FileConfig = { @@ -9,10 +10,16 @@ const conf: MulterGridfsStorage.FileConfig = { // Connection promise const dbPromise = MongoClient.connect('mongodb://yourhost:27017/database'); +// Mongoose promise +const mongoosePromise = mongoose.connect('mongodb://yourhost:27017/database'); +const mgConnectionPromise = mongoose + .connect('mongodb://yourhost:27017/database') + .then(instance => instance.connection); const server = new Server('localhost', 27017); const db = new Db('database', server); +// Database instance const opt1: MulterGridfsStorage.DbStorageOptions = { db, file: (req, file) => { @@ -24,9 +31,31 @@ const opt1: MulterGridfsStorage.DbStorageOptions = { } }; -const opt2: MulterGridfsStorage.UrlStorageOptions = { +const dbFileStorage = new MulterGridfsStorage(opt1); + +const opt2: MulterGridfsStorage.DbStorageOptions = { + db: mongoosePromise, + file: (req, file) => { + return { + disableMd5: true + }; + } +}; + +const opt3: MulterGridfsStorage.DbStorageOptions = { + db: mgConnectionPromise, + file: (req, file) => { + return 5; + } +}; + +const mongooseStorage = new MulterGridfsStorage(opt2); +const mgConnectionStorage = new MulterGridfsStorage(opt3); + +// Url based instance +const opt4: MulterGridfsStorage.UrlStorageOptions = { url: 'mongodb://yourhost:27017/database', - connectionOpts: {}, + options: {}, file: (req, file) => { return { metadata: file.mimetype @@ -34,25 +63,30 @@ const opt2: MulterGridfsStorage.UrlStorageOptions = { } }; -// All options -const dbFileStorage = new MulterGridfsStorage(opt1); +const urlFileStorage = new MulterGridfsStorage(opt4); -const urlFileStorage = new MulterGridfsStorage(opt2); +// Cache +const opt5: MulterGridfsStorage.UrlStorageOptions = { + url: 'mongodb://yourhost:27017/database', + cache: 'cache' +}; + +const cachedStorage = new MulterGridfsStorage(opt5); // Other properties are optional -const promiseStorage = new MulterGridfsStorage({ - db: dbPromise -}); +const promiseStorage = new MulterGridfsStorage({db: dbPromise}); -const dbStorage = new MulterGridfsStorage({ - db -}); +const dbStorage = new MulterGridfsStorage({db}); const urlStorage = new MulterGridfsStorage({ url: 'mongodb://yourhost:27017/database' }); // Extends event emitter -promiseStorage.on('connection', () => {}); -urlStorage.addListener('conection', () => {}); +promiseStorage.on('connection', () => { +}); +urlStorage.addListener('conection', () => { +}); dbStorage.removeAllListeners('conection'); + +MulterGridfsStorage.cache.connections(); diff --git a/types/multer-gridfs-storage/v2/index.d.ts b/types/multer-gridfs-storage/v2/index.d.ts new file mode 100644 index 0000000000..0a827bedcb --- /dev/null +++ b/types/multer-gridfs-storage/v2/index.d.ts @@ -0,0 +1,63 @@ +// Type definitions for multer-gridfs-storage 2.0 +// Project: https://github.com/devconcept/multer-gridfs-storage +// Definitions by: devconcept +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.3 + +import { EventEmitter } from 'events'; +import { Express } from 'express'; +import * as Multer from 'multer'; +import { Db } from 'mongodb'; + +interface MulterGfsOptions { + file?(req: Express.Request, file: Express.Multer.File): any; +} + +declare class MulterGridfsStorage extends EventEmitter implements Multer.StorageEngine { + constructor(settings: MulterGridfsStorage.UrlStorageOptions | MulterGridfsStorage.DbStorageOptions); + + _handleFile(req: Express.Request, file: Express.Multer.File, callback: (error?: any, info?: Express.Multer.File) => void): void; + + _removeFile(req: Express.Request, file: Express.Multer.File, callback: (error: Error) => void): void; +} + +declare namespace MulterGridfsStorage { + interface UrlStorageOptions extends MulterGfsOptions { + url: string; + connectionOpts?: any; + } + + interface DbStorageOptions extends MulterGfsOptions { + db: Promise | Db; + } + + interface FileConfig { + filename?: string; + id?: any; + metadata?: any; + chunkSize?: number; + bucketName?: string; + contentType?: string; + } +} + +// Merge multer's file declaration with ours +declare global { + namespace Express { + namespace Multer { + interface File { + id: any; + filename: string; + metadata: any; + contentType: string; + chunkSize: number; + bucketName: string; + uploadDate: Date; + md5: string; + size: number; + } + } + } +} + +export = MulterGridfsStorage; diff --git a/types/multer-gridfs-storage/v2/multer-gridfs-storage-tests.ts b/types/multer-gridfs-storage/v2/multer-gridfs-storage-tests.ts new file mode 100644 index 0000000000..f904ca053e --- /dev/null +++ b/types/multer-gridfs-storage/v2/multer-gridfs-storage-tests.ts @@ -0,0 +1,58 @@ +import MulterGridfsStorage = require('multer-gridfs-storage'); +import { Db, MongoClient, Server } from "mongodb"; + +// Exported interfaces +const conf: MulterGridfsStorage.FileConfig = { + filename: 'name', + bucketName: 'plants' +}; + +// Connection promise +const dbPromise = MongoClient.connect('mongodb://yourhost:27017').then((client) => client.db('database')); + +const server = new Server('localhost', 27017); +const db = new Db('database', server); + +const opt1: MulterGridfsStorage.DbStorageOptions = { + db, + file: (req, file) => { + return new Promise((resolve) => { + resolve({ + filename: file.originalname + }); + }); + } +}; + +const opt2: MulterGridfsStorage.UrlStorageOptions = { + url: 'mongodb://yourhost:27017/database', + connectionOpts: {}, + file: (req, file) => { + return { + metadata: file.mimetype + }; + } +}; + +// All options +const dbFileStorage = new MulterGridfsStorage(opt1); + +const urlFileStorage = new MulterGridfsStorage(opt2); + +// Other properties are optional +const promiseStorage = new MulterGridfsStorage({ + db: dbPromise +}); + +const dbStorage = new MulterGridfsStorage({ + db +}); + +const urlStorage = new MulterGridfsStorage({ + url: 'mongodb://yourhost:27017/database' +}); + +// Extends event emitter +promiseStorage.on('connection', () => {}); +urlStorage.addListener('conection', () => {}); +dbStorage.removeAllListeners('conection'); diff --git a/types/multer-gridfs-storage/v2/tsconfig.json b/types/multer-gridfs-storage/v2/tsconfig.json new file mode 100644 index 0000000000..81dfdd435a --- /dev/null +++ b/types/multer-gridfs-storage/v2/tsconfig.json @@ -0,0 +1,28 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../../", + "typeRoots": [ + "../../" + ], + "paths": { + "multer-gridfs-storage": [ + "multer-gridfs-storage/v2" + ] + }, + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "multer-gridfs-storage-tests.ts" + ] +} diff --git a/types/multer-gridfs-storage/v2/tslint.json b/types/multer-gridfs-storage/v2/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/multer-gridfs-storage/v2/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }