From 965e9507973771220ba78b3f9eb4eeb5509a5ccc Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Tue, 8 Aug 2023 15:28:20 -0400 Subject: [PATCH] types: allow accessing `options` from pre middleware Fix #13633 --- test/types/schema.test.ts | 22 +++++++++++++++++++++- types/index.d.ts | 21 +++++++++++++++++++-- types/middlewares.d.ts | 12 ++++++++++-- 3 files changed, 50 insertions(+), 5 deletions(-) diff --git a/test/types/schema.test.ts b/test/types/schema.test.ts index 45a3f34726d..52ac2229e3a 100644 --- a/test/types/schema.test.ts +++ b/test/types/schema.test.ts @@ -12,7 +12,8 @@ import { HydratedDocument, ResolveSchemaOptions, ObtainDocumentType, - ObtainSchemaGeneric + ObtainSchemaGeneric, + InsertManyOptions } from 'mongoose'; import { expectType, expectError, expectAssignable } from 'tsd'; import { ObtainDocumentPathType, ResolvePathType } from '../../types/inferschematype'; @@ -1150,3 +1151,22 @@ function gh13514() { const doc = new Test({ email: 'bar' }); const str: string = doc.email; } + +function gh13633() { + const schema = new Schema({ name: String }); + + schema.pre('updateOne', { document: true, query: false }, function(next) { + }); + + schema.pre('updateOne', { document: true, query: false }, function(next, options) { + expectType | undefined>(options); + }); + + schema.post('save', function(res, next) { + }); + schema.pre('insertMany', function(next, docs) { + }); + schema.pre('insertMany', function(next, docs, options) { + expectType<(InsertManyOptions & { lean?: boolean }) | undefined>(options); + }); +} diff --git a/types/index.d.ts b/types/index.d.ts index 56f018af999..d828815d0af 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -406,8 +406,25 @@ declare module 'mongoose' { pre>(method: 'aggregate' | RegExp, fn: PreMiddlewareFunction): this; pre>(method: 'aggregate' | RegExp, options: SchemaPreOptions, fn: PreMiddlewareFunction): this; /* method insertMany */ - pre(method: 'insertMany' | RegExp, fn: (this: T, next: (err?: CallbackError) => void, docs: any | Array) => void | Promise): this; - pre(method: 'insertMany' | RegExp, options: SchemaPreOptions, fn: (this: T, next: (err?: CallbackError) => void, docs: any | Array) => void | Promise): this; + pre( + method: 'insertMany' | RegExp, + fn: ( + this: T, + next: (err?: CallbackError) => void, + docs: any | Array, + options?: InsertManyOptions & { lean?: boolean } + ) => void | Promise + ): this; + pre( + method: 'insertMany' | RegExp, + options: SchemaPreOptions, + fn: ( + this: T, + next: (err?: CallbackError) => void, + docs: any | Array, + options?: InsertManyOptions & { lean?: boolean } + ) => void | Promise + ): this; /** Object of currently defined query helpers on this schema. */ query: TQueryHelpers; diff --git a/types/middlewares.d.ts b/types/middlewares.d.ts index 9288d6e3df5..43ca1974b81 100644 --- a/types/middlewares.d.ts +++ b/types/middlewares.d.ts @@ -31,8 +31,16 @@ declare module 'mongoose' { type SchemaPreOptions = MiddlewareOptions; type SchemaPostOptions = MiddlewareOptions; - type PreMiddlewareFunction = (this: ThisType, next: CallbackWithoutResultAndOptionalError) => void | Promise; - type PreSaveMiddlewareFunction = (this: ThisType, next: CallbackWithoutResultAndOptionalError, opts: SaveOptions) => void | Promise; + type PreMiddlewareFunction = ( + this: ThisType, + next: CallbackWithoutResultAndOptionalError, + opts?: Record + ) => void | Promise; + 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;