Skip to content

Commit 9d2121e

Browse files
committed
types: add overwriteMiddlewareResult and skipMiddlewareFunction to types
Fix #14289
1 parent 8ef0d9a commit 9d2121e

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

test/types/base.test.ts

+9
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@ Object.values(mongoose.models).forEach(model => {
88

99
mongoose.pluralize(null);
1010

11+
mongoose.overwriteMiddlewareResult('foo');
12+
const schema = new mongoose.Schema({ name: String });
13+
schema.pre('save', function() {
14+
return mongoose.skipMiddlewareFunction('foobar');
15+
});
16+
schema.post('save', function() {
17+
return mongoose.overwriteMiddlewareResult('foobar');
18+
});
19+
1120
function gh10746() {
1221
type A = string extends Function ? never : string;
1322

types/index.d.ts

+6
Original file line numberDiff line numberDiff line change
@@ -677,5 +677,11 @@ declare module 'mongoose' {
677677
/* for ts-mongoose */
678678
export class mquery { }
679679

680+
export class OverwriteMiddlewareResult {}
681+
export function overwriteMiddlewareResult(val: any): OverwriteMiddlewareResult;
682+
683+
export class SkipWrappedFunction {}
684+
export function skipMiddlewareFunction(val: any): SkipWrappedFunction;
685+
680686
export default mongoose;
681687
}

types/middlewares.d.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ declare module 'mongoose' {
3737
this: ThisType,
3838
next: CallbackWithoutResultAndOptionalError,
3939
opts?: Record<string, any>
40-
) => void | Promise<void>;
40+
) => void | Promise<void> | SkipWrappedFunction;
4141
type PreSaveMiddlewareFunction<ThisType = any> = (
4242
this: ThisType,
4343
next: CallbackWithoutResultAndOptionalError,
4444
opts: SaveOptions
45-
) => void | Promise<void>;
46-
type PostMiddlewareFunction<ThisType = any, ResType = any> = (this: ThisType, res: ResType, next: CallbackWithoutResultAndOptionalError) => void | Promise<void>;
45+
) => void | Promise<void> | SkipWrappedFunction;
46+
type PostMiddlewareFunction<ThisType = any, ResType = any> = (this: ThisType, res: ResType, next: CallbackWithoutResultAndOptionalError) => void | Promise<void> | OverwriteMiddlewareResult;
4747
type ErrorHandlingMiddlewareFunction<ThisType = any, ResType = any> = (this: ThisType, err: NativeError, res: ResType, next: CallbackWithoutResultAndOptionalError) => void;
48-
type ErrorHandlingMiddlewareWithOption<ThisType = any, ResType = any> = (this: ThisType, err: NativeError, res: ResType | null, next: CallbackWithoutResultAndOptionalError) => void | Promise<void>;
48+
type ErrorHandlingMiddlewareWithOption<ThisType = any, ResType = any> = (this: ThisType, err: NativeError, res: ResType | null, next: CallbackWithoutResultAndOptionalError) => void | Promise<void> | OverwriteMiddlewareResult;
4949
}

0 commit comments

Comments
 (0)