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

fix(middleware): Update invoke middleware implementation #10537

Closed

Conversation

dac09
Copy link
Collaborator

@dac09 dac09 commented May 3, 2024

Found an issue while writing up the blogpost on middleware.

The invokeOptions is always supplied when invoking middleware, but may not always be used.

In order to do this, I had to change how middleware is invoked (use an object instead of parameters)

// before
const [mwRes] = await invoke(req, middleware, options)

// after
const [mwRes] = await invoke({req, middleware, options})

@dac09 dac09 added the release:fix This PR is a fix label May 3, 2024
@dac09 dac09 added this to the SSR milestone May 3, 2024
@dac09 dac09 self-assigned this May 3, 2024
@dac09 dac09 changed the title fix(mw): Make invokeOptions a non-optional type fix(middleware): Make invokeOptions a non-optional type May 3, 2024
@dac09 dac09 changed the title fix(middleware): Make invokeOptions a non-optional type fix(middleware): Update invoke middleware implementation May 3, 2024
@dac09 dac09 requested a review from cannikin May 3, 2024 13:41
@Tobbe
Copy link
Member

Tobbe commented May 4, 2024

@dac09 I don't understand why you had to change to use an object here. Can you please try to explain in a bit more detail so even I can follow along 😄

The invokeOptions is always supplied when invoking middleware, but may not always be used.

It should be fine to pass invokeOptions even if they're not used. The receiving function can just ignore the parameter I think

.changesets/10537.md Outdated Show resolved Hide resolved
Copy link
Member

@Tobbe Tobbe left a comment

Choose a reason for hiding this comment

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

See my other comments

@dac09
Copy link
Collaborator Author

dac09 commented May 7, 2024

@dac09 I don't understand why you had to change to use an object here. Can you please try to explain in a bit more detail so even I can follow along 😄

Sure. In TS you cannot supply an required third parameter after a optional parameter (the middleware function). https://stackoverflow.com/questions/46958782/why-does-typescript-require-optional-parameters-after-required-parameters

It should be fine to pass invokeOptions even if they're not used. The receiving function can just ignore the parameter I think

Exactly. My original type was that invoke options was optional, which is incorrect. Invoke options are always supplied, even if the middleware is undefined. This happens when the middleware router doesn't find a match. The logic for whether to excecute the middleware sits inside the invoke function.

@Tobbe
Copy link
Member

Tobbe commented May 9, 2024

In TS you cannot supply an required third parameter after a optional parameter

Yeah, that much I knew. What I'm trying to say is that it's fine to leave both as optional. Having them optional just means that the invoke function itself can handle them not being supplied. Which is true with the way it's writtten.

Now the case may be that the third option is in all practical cases in fact passed to the function. But that doesn't change the fact that for the function itself the third parameter is still optional 🙂

@dac09
Copy link
Collaborator Author

dac09 commented May 10, 2024

Now the case may be that the third option is in all practical cases in fact passed to the function. But that doesn't change the fact that for the function itself the third parameter is still optional 🙂

Hmm you're right in that it CAN be optional, I was focusing on the auth ones and not the cases where you might register a middleware to handle POST requests (like an API endpoint).

I'll close this for now, and come back if required.

@dac09 dac09 closed this May 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release:fix This PR is a fix
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

None yet

3 participants