Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

types: add overwriteMiddlewareResult and skipMiddlewareFunction to types #14328

Merged
merged 7 commits into from Mar 4, 2024
9 changes: 9 additions & 0 deletions test/types/base.test.ts
Expand Up @@ -8,6 +8,15 @@ Object.values(mongoose.models).forEach(model => {

mongoose.pluralize(null);

mongoose.overwriteMiddlewareResult('foo');
const schema = new mongoose.Schema({ name: String });
schema.pre('save', function() {
return mongoose.skipMiddlewareFunction('foobar');
});
schema.post('save', function() {
return mongoose.overwriteMiddlewareResult('foobar');
});

function gh10746() {
type A = string extends Function ? never : string;

Expand Down
6 changes: 6 additions & 0 deletions types/index.d.ts
Expand Up @@ -677,5 +677,11 @@ declare module 'mongoose' {
/* for ts-mongoose */
export class mquery { }

export class OverwriteMiddlewareResult {}
export function overwriteMiddlewareResult(val: any): OverwriteMiddlewareResult;

export class SkipWrappedFunction {}
export function skipMiddlewareFunction(val: any): SkipWrappedFunction;

export default mongoose;
}
8 changes: 4 additions & 4 deletions types/middlewares.d.ts
Expand Up @@ -37,13 +37,13 @@ declare module 'mongoose' {
this: ThisType,
next: CallbackWithoutResultAndOptionalError,
opts?: Record<string, any>
) => void | Promise<void>;
) => void | Promise<void> | SkipWrappedFunction;
type PreSaveMiddlewareFunction<ThisType = any> = (
this: ThisType,
next: CallbackWithoutResultAndOptionalError,
opts: SaveOptions
) => void | Promise<void>;
type PostMiddlewareFunction<ThisType = any, ResType = any> = (this: ThisType, res: ResType, next: CallbackWithoutResultAndOptionalError) => void | Promise<void>;
) => void | Promise<void> | SkipWrappedFunction;
type PostMiddlewareFunction<ThisType = any, ResType = any> = (this: ThisType, res: ResType, next: CallbackWithoutResultAndOptionalError) => void | Promise<void> | OverwriteMiddlewareResult;
type ErrorHandlingMiddlewareFunction<ThisType = any, ResType = any> = (this: ThisType, err: NativeError, res: ResType, next: CallbackWithoutResultAndOptionalError) => void;
type ErrorHandlingMiddlewareWithOption<ThisType = any, ResType = any> = (this: ThisType, err: NativeError, res: ResType | null, next: CallbackWithoutResultAndOptionalError) => void | Promise<void>;
type ErrorHandlingMiddlewareWithOption<ThisType = any, ResType = any> = (this: ThisType, err: NativeError, res: ResType | null, next: CallbackWithoutResultAndOptionalError) => void | Promise<void> | OverwriteMiddlewareResult;
}