From e671fbc00a6959644db0e596f6e75d0464ffaf0f Mon Sep 17 00:00:00 2001 From: Rafael <10549329+devconcept@users.noreply.github.com> Date: Mon, 4 Nov 2019 11:32:26 -0500 Subject: [PATCH] Updated multer-gridfs-storage to v4 (#40122) * Updated to v4 * Fixed tslint path for old package --- types/multer-gridfs-storage/index.d.ts | 14 ++- .../multer-gridfs-storage-tests.ts | 27 +++++- types/multer-gridfs-storage/tsconfig.json | 2 +- types/multer-gridfs-storage/v3/index.d.ts | 97 +++++++++++++++++++ .../v3/multer-gridfs-storage-tests.ts | 94 ++++++++++++++++++ types/multer-gridfs-storage/v3/tsconfig.json | 28 ++++++ types/multer-gridfs-storage/v3/tslint.json | 1 + 7 files changed, 258 insertions(+), 5 deletions(-) create mode 100644 types/multer-gridfs-storage/v3/index.d.ts create mode 100644 types/multer-gridfs-storage/v3/multer-gridfs-storage-tests.ts create mode 100644 types/multer-gridfs-storage/v3/tsconfig.json create mode 100644 types/multer-gridfs-storage/v3/tslint.json diff --git a/types/multer-gridfs-storage/index.d.ts b/types/multer-gridfs-storage/index.d.ts index 364f0d748d..4e05f0c809 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 3.1 +// Type definitions for multer-gridfs-storage 4.0 // Project: https://github.com/devconcept/multer-gridfs-storage // Definitions by: devconcept // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped @@ -47,10 +47,19 @@ declare class MulterGridfsStorage extends EventEmitter implements Multer.Storage _removeFile(req: Express.Request, file: Express.Multer.File, callback: (error: Error) => void): void; + ready(): Promise; + static cache: Cache; + + static generateBytes(): Promise<{filename: string}>; } declare namespace MulterGridfsStorage { + interface ConnectionResult { + db: Db; + client?: MongoClient; + } + interface UrlStorageOptions extends MulterGfsOptions { url: string; options?: any; @@ -58,7 +67,8 @@ declare namespace MulterGridfsStorage { } interface DbStorageOptions extends MulterGfsOptions { - db: Mongoose | Connection | Db | MongoClient | Promise; + db: Mongoose | Connection | Db | Promise; + client?: MongoClient | Promise; } interface FileConfig { diff --git a/types/multer-gridfs-storage/multer-gridfs-storage-tests.ts b/types/multer-gridfs-storage/multer-gridfs-storage-tests.ts index 95c466ae36..7aa074cad6 100644 --- a/types/multer-gridfs-storage/multer-gridfs-storage-tests.ts +++ b/types/multer-gridfs-storage/multer-gridfs-storage-tests.ts @@ -8,8 +8,6 @@ const conf: MulterGridfsStorage.FileConfig = { bucketName: 'plants' }; -// Connection promise -const dbPromise = MongoClient.connect('mongodb://yourhost:27017/database'); // Mongoose promise const mongoosePromise = mongoose.connect('mongodb://yourhost:27017/database'); const mgConnectionPromise = mongoose @@ -71,10 +69,24 @@ const opt5: MulterGridfsStorage.UrlStorageOptions = { cache: 'cache' }; +// Generators +const opt6: MulterGridfsStorage.UrlStorageOptions = { + url: 'mongodb://yourhost:27017/database', + *file(req, file) { + yield { + metadata: file.mimetype + }; + } +}; + const cachedStorage = new MulterGridfsStorage(opt5); +// Connection promise +const clientPromise = MongoClient.connect('mongodb://yourhost:27017/database'); +const dbPromise = clientPromise.then(client => client.db('database')); // Other properties are optional const promiseStorage = new MulterGridfsStorage({db: dbPromise}); +const clientPromiseStorage = new MulterGridfsStorage({db: dbPromise, client: clientPromise}); const dbStorage = new MulterGridfsStorage({db}); @@ -82,6 +94,17 @@ const urlStorage = new MulterGridfsStorage({ url: 'mongodb://yourhost:27017/database' }); +// Ready method +clientPromiseStorage.ready().then(result => { + const db = result.db; + const client = result.client; +}); + +MulterGridfsStorage.generateBytes().then(result => { + const filename = result.filename; + console.log(result); +}); + // Extends event emitter promiseStorage.on('connection', () => { }); diff --git a/types/multer-gridfs-storage/tsconfig.json b/types/multer-gridfs-storage/tsconfig.json index 4729416ff1..1c67f399c4 100644 --- a/types/multer-gridfs-storage/tsconfig.json +++ b/types/multer-gridfs-storage/tsconfig.json @@ -20,4 +20,4 @@ "index.d.ts", "multer-gridfs-storage-tests.ts" ] -} \ No newline at end of file +} diff --git a/types/multer-gridfs-storage/v3/index.d.ts b/types/multer-gridfs-storage/v3/index.d.ts new file mode 100644 index 0000000000..6be14edb0e --- /dev/null +++ b/types/multer-gridfs-storage/v3/index.d.ts @@ -0,0 +1,97 @@ +// 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 +// TypeScript Version: 3.0 + +import { EventEmitter } from 'events'; +import { Express } from 'express'; +import * as Multer from 'multer'; +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 { + 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; + + ready: Promise; + + static cache: Cache; +} + +declare namespace MulterGridfsStorage { + interface UrlStorageOptions extends MulterGfsOptions { + url: string; + options?: any; + cache?: boolean | string; + } + + interface DbStorageOptions extends MulterGfsOptions { + db: Mongoose | Connection | Db | Promise; + } + + interface FileConfig { + filename?: string; + id?: any; + metadata?: object; + chunkSize?: number; + bucketName?: string; + contentType?: string; + aliases?: string[]; + disableMD5?: boolean; + } +} + +// 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/v3/multer-gridfs-storage-tests.ts b/types/multer-gridfs-storage/v3/multer-gridfs-storage-tests.ts new file mode 100644 index 0000000000..51ae6d4601 --- /dev/null +++ b/types/multer-gridfs-storage/v3/multer-gridfs-storage-tests.ts @@ -0,0 +1,94 @@ +import MulterGridfsStorage = require('multer-gridfs-storage'); +import { Db, MongoClient, Server } from 'mongodb'; +import * as mongoose from 'mongoose'; + +// Exported interfaces +const conf: MulterGridfsStorage.FileConfig = { + filename: 'name', + bucketName: 'plants' +}; + +// 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) => { + return new Promise((resolve) => { + resolve({ + filename: file.originalname + }); + }); + } +}; + +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', + options: {}, + file: (req, file) => { + return { + metadata: file.mimetype + }; + } +}; + +const urlFileStorage = new MulterGridfsStorage(opt4); + +// Cache +const opt5: MulterGridfsStorage.UrlStorageOptions = { + url: 'mongodb://yourhost:27017/database', + cache: 'cache' +}; + +const cachedStorage = new MulterGridfsStorage(opt5); + +// Connection promise +const dbPromise = MongoClient.connect('mongodb://yourhost:27017/database') + .then(client => client.db('database')); + +// 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'); + +MulterGridfsStorage.cache.connections(); diff --git a/types/multer-gridfs-storage/v3/tsconfig.json b/types/multer-gridfs-storage/v3/tsconfig.json new file mode 100644 index 0000000000..5e9259243f --- /dev/null +++ b/types/multer-gridfs-storage/v3/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/v3" + ] + }, + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "multer-gridfs-storage-tests.ts" + ] +} diff --git a/types/multer-gridfs-storage/v3/tslint.json b/types/multer-gridfs-storage/v3/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/multer-gridfs-storage/v3/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }