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: allow accessing options from pre middleware #13708

Merged
merged 1 commit into from Aug 9, 2023

Conversation

vkarpov15
Copy link
Collaborator

Fix #13633

Summary

TypeScript types don't support options param for document and model middleware currently. This PR adds that.

Examples

type PreSaveMiddlewareFunction<ThisType = any> = (
this: ThisType,
next: CallbackWithoutResultAndOptionalError,
opts: SaveOptions
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any particular reason opts is not optional here, and is optional in PreMiddlewareFunction?
Does it make any difference when defining a last parameter for a function?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For pre('save'), Mongoose defaults to passing empty options, so for schema.pre('save', function(next, opts) { opts; }) opts will always be defined. Even if you call save() with no args.

Not true for all middleware functions.

Copy link
Collaborator

@AbdelrahmanHafez AbdelrahmanHafez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks. 👍

@vkarpov15 vkarpov15 added this to the 7.4.3 milestone Aug 9, 2023
@vkarpov15 vkarpov15 merged commit 35d0978 into master Aug 9, 2023
3 checks passed
@vkarpov15 vkarpov15 deleted the vkarpov15/gh-13633 branch August 9, 2023 20:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Some middleware does not have access to operation options
2 participants