Skip to content

Commit

Permalink
Merge pull request #13708 from Automattic/vkarpov15/gh-13633
Browse files Browse the repository at this point in the history
types: allow accessing `options` from pre middleware
  • Loading branch information
vkarpov15 committed Aug 9, 2023
2 parents 7287420 + 965e950 commit 35d0978
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 5 deletions.
22 changes: 21 additions & 1 deletion test/types/schema.test.ts
Expand Up @@ -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';
Expand Down Expand Up @@ -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<Record<string, any> | 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);
});
}
21 changes: 19 additions & 2 deletions types/index.d.ts
Expand Up @@ -406,8 +406,25 @@ declare module 'mongoose' {
pre<T extends Aggregate<any>>(method: 'aggregate' | RegExp, fn: PreMiddlewareFunction<T>): this;
pre<T extends Aggregate<any>>(method: 'aggregate' | RegExp, options: SchemaPreOptions, fn: PreMiddlewareFunction<T>): this;
/* method insertMany */
pre<T = TModelType>(method: 'insertMany' | RegExp, fn: (this: T, next: (err?: CallbackError) => void, docs: any | Array<any>) => void | Promise<void>): this;
pre<T = TModelType>(method: 'insertMany' | RegExp, options: SchemaPreOptions, fn: (this: T, next: (err?: CallbackError) => void, docs: any | Array<any>) => void | Promise<void>): this;
pre<T = TModelType>(
method: 'insertMany' | RegExp,
fn: (
this: T,
next: (err?: CallbackError) => void,
docs: any | Array<any>,
options?: InsertManyOptions & { lean?: boolean }
) => void | Promise<void>
): this;
pre<T = TModelType>(
method: 'insertMany' | RegExp,
options: SchemaPreOptions,
fn: (
this: T,
next: (err?: CallbackError) => void,
docs: any | Array<any>,
options?: InsertManyOptions & { lean?: boolean }
) => void | Promise<void>
): this;

/** Object of currently defined query helpers on this schema. */
query: TQueryHelpers;
Expand Down
12 changes: 10 additions & 2 deletions types/middlewares.d.ts
Expand Up @@ -31,8 +31,16 @@ declare module 'mongoose' {
type SchemaPreOptions = MiddlewareOptions;
type SchemaPostOptions = MiddlewareOptions;

type PreMiddlewareFunction<ThisType = any> = (this: ThisType, next: CallbackWithoutResultAndOptionalError) => void | Promise<void>;
type PreSaveMiddlewareFunction<ThisType = any> = (this: ThisType, next: CallbackWithoutResultAndOptionalError, opts: SaveOptions) => void | Promise<void>;
type PreMiddlewareFunction<ThisType = any> = (
this: ThisType,
next: CallbackWithoutResultAndOptionalError,
opts?: Record<string, any>
) => void | Promise<void>;
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>;
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>;
Expand Down

0 comments on commit 35d0978

Please sign in to comment.