diff --git a/package.json b/package.json index dd22ea7a5cc..cb0047cb9ce 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,7 @@ "license": "MIT", "dependencies": { "bson": "^4.7.0", - "kareem": "2.4.1", + "kareem": "2.5.0", "mongodb": "4.12.1", "mpath": "0.9.0", "mquery": "4.0.3", diff --git a/test/types/middleware.test.ts b/test/types/middleware.test.ts index 4b7b07d3437..7f1464375a4 100644 --- a/test/types/middleware.test.ts +++ b/test/types/middleware.test.ts @@ -135,3 +135,23 @@ function gh11480(): void { next(); }); } + +function gh12583() { + interface IUser { + name: string; + email: string; + avatar?: string; + } + + const userSchema = new Schema({ + name: { type: String, required: true }, + email: { type: String, required: true }, + avatar: String + }); + + userSchema.post('save', { errorHandler: true }, function(error, doc, next) { + expectType(error); + console.log(error.name); + console.log(doc.name); + }); +} diff --git a/types/index.d.ts b/types/index.d.ts index c91097e27bb..b2973fd8aad 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -281,6 +281,11 @@ declare module 'mongoose' { plugin, POptions extends Parameters[1] = Parameters[1]>(fn: PFunc, opts?: POptions): this; /** Defines a post hook for the model. */ + post>(method: MongooseQueryMiddleware | MongooseQueryMiddleware[] | RegExp, options: SchemaPostOptions & { errorHandler: true }, fn: ErrorHandlingMiddlewareWithOption): this; + post>(method: MongooseDocumentMiddleware | MongooseDocumentMiddleware[] | RegExp, options: SchemaPostOptions & { errorHandler: true }, fn: ErrorHandlingMiddlewareWithOption): this; + post>(method: 'aggregate' | RegExp, options: SchemaPostOptions & { errorHandler: true }, fn: ErrorHandlingMiddlewareWithOption>): this; + post(method: 'insertMany' | RegExp, options: SchemaPostOptions & { errorHandler: true }, fn: ErrorHandlingMiddlewareWithOption): this; + post>(method: MongooseQueryMiddleware | MongooseQueryMiddleware[] | RegExp, fn: PostMiddlewareFunction>): this; post>(method: MongooseQueryMiddleware | MongooseQueryMiddleware[] | RegExp, options: SchemaPostOptions, fn: PostMiddlewareFunction>): this; post>(method: MongooseDocumentMiddleware | MongooseDocumentMiddleware[] | RegExp, fn: PostMiddlewareFunction): this; diff --git a/types/middlewares.d.ts b/types/middlewares.d.ts index b8e604f2dad..7aa28b6753f 100644 --- a/types/middlewares.d.ts +++ b/types/middlewares.d.ts @@ -3,7 +3,23 @@ declare module 'mongoose' { type MongooseDocumentMiddleware = 'validate' | 'save' | 'remove' | 'updateOne' | 'deleteOne' | 'init'; type MongooseQueryMiddleware = 'count' | 'estimatedDocumentCount' | 'countDocuments' | 'deleteMany' | 'deleteOne' | 'distinct' | 'find' | 'findOne' | 'findOneAndDelete' | 'findOneAndRemove' | 'findOneAndReplace' | 'findOneAndUpdate' | 'remove' | 'replaceOne' | 'update' | 'updateOne' | 'updateMany'; - type MiddlewareOptions = { document?: boolean, query?: boolean }; + type MiddlewareOptions = { + /** + * Enable this Hook for the Document Methods + * @default true + */ + document?: boolean, + /** + * Enable this Hook for the Query Methods + * @default true + */ + query?: boolean, + /** + * Explicitly set this function to be a Error handler instead of based on how many arguments are used + * @default false + */ + errorHandler?: boolean + }; type SchemaPreOptions = MiddlewareOptions; type SchemaPostOptions = MiddlewareOptions; @@ -11,4 +27,5 @@ declare module 'mongoose' { type PreSaveMiddlewareFunction = (this: ThisType, next: CallbackWithoutResultAndOptionalError, opts: SaveOptions) => void | Promise; type PostMiddlewareFunction = (this: ThisType, res: ResType, next: CallbackWithoutResultAndOptionalError) => void | Promise; type ErrorHandlingMiddlewareFunction = (this: ThisType, err: NativeError, res: ResType, next: CallbackWithoutResultAndOptionalError) => void; + type ErrorHandlingMiddlewareWithOption = (this: ThisType, err: NativeError, res: ResType | null, next: CallbackWithoutResultAndOptionalError) => void | Promise; }