Skip to content

Commit efed0a4

Browse files
authored
Merge pull request #12723 from Automattic/vkarpov15/gh-12595
feat(schema+types): add `{ errorHandler: true }` option to Schema `post()` for better TypeScript support
2 parents bc750fd + 4bedc7a commit efed0a4

4 files changed

Lines changed: 44 additions & 2 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"license": "MIT",
2121
"dependencies": {
2222
"bson": "^4.7.0",
23-
"kareem": "2.4.1",
23+
"kareem": "2.5.0",
2424
"mongodb": "4.12.1",
2525
"mpath": "0.9.0",
2626
"mquery": "4.0.3",

test/types/middleware.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,23 @@ function gh11480(): void {
135135
next();
136136
});
137137
}
138+
139+
function gh12583() {
140+
interface IUser {
141+
name: string;
142+
email: string;
143+
avatar?: string;
144+
}
145+
146+
const userSchema = new Schema<IUser>({
147+
name: { type: String, required: true },
148+
email: { type: String, required: true },
149+
avatar: String
150+
});
151+
152+
userSchema.post('save', { errorHandler: true }, function(error, doc, next) {
153+
expectType<Error>(error);
154+
console.log(error.name);
155+
console.log(doc.name);
156+
});
157+
}

types/index.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,11 @@ declare module 'mongoose' {
281281
plugin<PFunc extends PluginFunction<DocType, M, any, any, any, any>, POptions extends Parameters<PFunc>[1] = Parameters<PFunc>[1]>(fn: PFunc, opts?: POptions): this;
282282

283283
/** Defines a post hook for the model. */
284+
post<T = Query<any, any>>(method: MongooseQueryMiddleware | MongooseQueryMiddleware[] | RegExp, options: SchemaPostOptions & { errorHandler: true }, fn: ErrorHandlingMiddlewareWithOption<T>): this;
285+
post<T = HydratedDocument<DocType, TInstanceMethods>>(method: MongooseDocumentMiddleware | MongooseDocumentMiddleware[] | RegExp, options: SchemaPostOptions & { errorHandler: true }, fn: ErrorHandlingMiddlewareWithOption<T>): this;
286+
post<T extends Aggregate<any>>(method: 'aggregate' | RegExp, options: SchemaPostOptions & { errorHandler: true }, fn: ErrorHandlingMiddlewareWithOption<T, Array<any>>): this;
287+
post<T = M>(method: 'insertMany' | RegExp, options: SchemaPostOptions & { errorHandler: true }, fn: ErrorHandlingMiddlewareWithOption<T>): this;
288+
284289
post<T = Query<any, any>>(method: MongooseQueryMiddleware | MongooseQueryMiddleware[] | RegExp, fn: PostMiddlewareFunction<T, QueryResultType<T>>): this;
285290
post<T = Query<any, any>>(method: MongooseQueryMiddleware | MongooseQueryMiddleware[] | RegExp, options: SchemaPostOptions, fn: PostMiddlewareFunction<T, QueryResultType<T>>): this;
286291
post<T = HydratedDocument<DocType, TInstanceMethods>>(method: MongooseDocumentMiddleware | MongooseDocumentMiddleware[] | RegExp, fn: PostMiddlewareFunction<T, T>): this;

types/middlewares.d.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,29 @@ declare module 'mongoose' {
33
type MongooseDocumentMiddleware = 'validate' | 'save' | 'remove' | 'updateOne' | 'deleteOne' | 'init';
44
type MongooseQueryMiddleware = 'count' | 'estimatedDocumentCount' | 'countDocuments' | 'deleteMany' | 'deleteOne' | 'distinct' | 'find' | 'findOne' | 'findOneAndDelete' | 'findOneAndRemove' | 'findOneAndReplace' | 'findOneAndUpdate' | 'remove' | 'replaceOne' | 'update' | 'updateOne' | 'updateMany';
55

6-
type MiddlewareOptions = { document?: boolean, query?: boolean };
6+
type MiddlewareOptions = {
7+
/**
8+
* Enable this Hook for the Document Methods
9+
* @default true
10+
*/
11+
document?: boolean,
12+
/**
13+
* Enable this Hook for the Query Methods
14+
* @default true
15+
*/
16+
query?: boolean,
17+
/**
18+
* Explicitly set this function to be a Error handler instead of based on how many arguments are used
19+
* @default false
20+
*/
21+
errorHandler?: boolean
22+
};
723
type SchemaPreOptions = MiddlewareOptions;
824
type SchemaPostOptions = MiddlewareOptions;
925

1026
type PreMiddlewareFunction<ThisType = any> = (this: ThisType, next: CallbackWithoutResultAndOptionalError) => void | Promise<void>;
1127
type PreSaveMiddlewareFunction<ThisType = any> = (this: ThisType, next: CallbackWithoutResultAndOptionalError, opts: SaveOptions) => void | Promise<void>;
1228
type PostMiddlewareFunction<ThisType = any, ResType = any> = (this: ThisType, res: ResType, next: CallbackWithoutResultAndOptionalError) => void | Promise<void>;
1329
type ErrorHandlingMiddlewareFunction<ThisType = any, ResType = any> = (this: ThisType, err: NativeError, res: ResType, next: CallbackWithoutResultAndOptionalError) => void;
30+
type ErrorHandlingMiddlewareWithOption<ThisType = any, ResType = any> = (this: ThisType, err: NativeError, res: ResType | null, next: CallbackWithoutResultAndOptionalError) => void | Promise<void>;
1431
}

0 commit comments

Comments
 (0)