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(ts): improve events handlers' types #1853

Merged
merged 19 commits into from May 6, 2021
Merged

fix(ts): improve events handlers' types #1853

merged 19 commits into from May 6, 2021

Conversation

zackdotcomputer
Copy link
Contributor

@zackdotcomputer zackdotcomputer commented Apr 25, 2021

What:

This change introduces typing for the events object so that developers using Typescript with next-auth will receiving hinting as to the type of the incoming arguments to events callback functions.

Resolves #1639

Why:

Because previously developers didn't have any idea what arguments they'd receive when listening to events even though it was relatively well-defined in the source code.

How:

By analyzing the source code for the possible paths to trigger events and then editing the typing files to match the discovered pathways.

Checklist:

  • Documentation (I think N/A since this basically is documentation?)
  • Tests
  • Ready to be merged

@vercel
Copy link

vercel bot commented Apr 25, 2021

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/nextauthjs/next-auth/Bro6i1RYYHGXpXhNwjeir57EwAt5
✅ Preview: https://next-auth-git-fork-zackdotcomputer-main-nextauthjs.vercel.app

@vercel vercel bot temporarily deployed to Preview April 25, 2021 16:59 Inactive
@github-actions github-actions bot added test Related to testing TypeScript Issues relating to TypeScript labels Apr 25, 2021
Copy link
Member

@balazsorban44 balazsorban44 left a comment

Choose a reason for hiding this comment

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

Copy link
Collaborator

@ubbe-xyz ubbe-xyz left a comment

Choose a reason for hiding this comment

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

Thanks 💯 , brilliant work; I leave a couple of suggestions 👍🏽

types/index.d.ts Outdated Show resolved Hide resolved
types/index.d.ts Outdated Show resolved Hide resolved
types/index.d.ts Outdated Show resolved Hide resolved
Copy link
Member

@balazsorban44 balazsorban44 left a comment

Choose a reason for hiding this comment

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

Could you please fix the merge conflicts?

types/index.d.ts Outdated Show resolved Hide resolved
types/index.d.ts Outdated Show resolved Hide resolved
@balazsorban44
Copy link
Member

Also my previous comment still stands: #1853 (review)

@zackdotcomputer
Copy link
Contributor Author

Sure thing @balazsorban44 - I was just starting on the code comments before tackling the documentation so I was sure to be documenting my final draft. Will tackle the merge and your code comments and then wrap up the docs changes.

@vercel vercel bot temporarily deployed to Preview April 29, 2021 00:19 Inactive
@github-actions github-actions bot added the documentation Relates to documentation label Apr 29, 2021
@zackdotcomputer
Copy link
Contributor Author

All comments responded to and documentation updated.

Copy link
Collaborator

@ubbe-xyz ubbe-xyz left a comment

Choose a reason for hiding this comment

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

Just one small thing to prevent conflicts on the documentation 🙏🏽 , thanks a lot for this massive work 💯

@@ -5,7 +5,7 @@ title: Options

## Environment Variables

### NEXTAUTH_URL
### NEXTAUTH_URL
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could we revert this file?

I have a PR open updating it extensively and I think it's gonna end a bit in conflict hell 😬 due to the prettier changes. I'll add the bit you updated pointing to the events documentation on my PR 👍🏽

Copy link
Collaborator

Choose a reason for hiding this comment

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

In fact, NVM I confused the file name 🤦🏽‍♂️ ☕️ , ha ha we're all good 💚

Copy link
Collaborator

@ubbe-xyz ubbe-xyz left a comment

Choose a reason for hiding this comment

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

💪🏽

@@ -5,7 +5,7 @@ title: Options

## Environment Variables

### NEXTAUTH_URL
### NEXTAUTH_URL
Copy link
Collaborator

Choose a reason for hiding this comment

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

In fact, NVM I confused the file name 🤦🏽‍♂️ ☕️ , ha ha we're all good 💚

@vercel vercel bot temporarily deployed to Preview May 1, 2021 09:16 Inactive
types/index.d.ts Outdated Show resolved Hide resolved
Copy link
Member

@balazsorban44 balazsorban44 left a comment

Choose a reason for hiding this comment

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

Some feedback from me! Please remove the extraneous generics, as we discussed Module Augmentation. Otherwise, it is looking good and will be a nice improvement, so thank you! 🙏

I am also thinking that most of these should be moved to internals/events.d.ts maybe to keep down the index.d.ts file size as well. What do you think @lluia?

types/index.d.ts Outdated Show resolved Hide resolved
types/index.d.ts Outdated Show resolved Hide resolved
types/index.d.ts Outdated Show resolved Hide resolved
types/index.d.ts Show resolved Hide resolved
types/index.d.ts Outdated Show resolved Hide resolved
types/index.d.ts Outdated Show resolved Hide resolved
@github-actions github-actions bot added core Refers to `@auth/core` providers labels May 5, 2021
@zackdotcomputer
Copy link
Contributor Author

Resolved merge conflict, updated to main repo's main branch, applied all suggested changes except for keeping the createUser, updateUser, and linkAccount in the common events interface since I think those are sent even if you are using JWTs.

@balazsorban44
Copy link
Member

balazsorban44 commented May 5, 2021

Thabk you!

About the events, they aren't, you can see it in the source code:

https://github.com/nextauthjs/next-auth/blob/17b789822de64eb845e1e8e49ea83dbff56344f4/src/server/lib/callback-handler.js

See the following lines: 95, 100, 154, 199, 210

those events will only be triggered, if an adapter is used. there is no point in triggering them otherwise.

EDIT: Sorry for my quick judgment on this, I'll have a proper look later on from the desktop. Feel free to leave your changes for now, you might be right, I'll get back on this.

Thank you for your patience on this! 🙏

@zackdotcomputer
Copy link
Contributor Author

Ah no worries, it sounds like we're actually reading this the same way @balazsorban44 that those events are sent based on whether or not you have an adapter connected, which as far as I can tell is separate from whether you are using JWTs (since you could configure an adapter and also set session: { jwt: true } I think). If we want to get precise then, I think we wind up needing interface HasAdapterEventCallbacks with these three methods and then the final type is type EventCallbacks = JWTEventCallbacks | ((JWTEventCallbacks | SessionEventCallbacks) & HasAdapterEventCallbacks)

But are we now splitting hairs? Let me know if you think that's excessive or not - if not I can restructure the type like that pretty easily.

@balazsorban44
Copy link
Member

balazsorban44 commented May 5, 2021

Right! I think it's a big improvement anyway, so I'll approve this in its current form, and if for some reason it won't be good enough, we can open a new PR. Thank you for your contribution! 💚

@balazsorban44 balazsorban44 changed the title Add typing for events handlers' message arguments (Resolves #1639) fix(ts): improve events handlers' types May 5, 2021
@vercel vercel bot temporarily deployed to Preview May 5, 2021 15:40 Inactive
@github-actions github-actions bot removed client Client related code core Refers to `@auth/core` providers labels May 5, 2021
@balazsorban44
Copy link
Member

balazsorban44 commented May 5, 2021

@zackdotcomputer sorry, this is the last thing I'll ask, but before merging, could you please fix the failing tests?

@balazsorban44 balazsorban44 self-requested a review May 5, 2021 17:33
Copy link
Member

@balazsorban44 balazsorban44 left a comment

Choose a reason for hiding this comment

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

There are some failing tests

@vercel vercel bot temporarily deployed to Preview May 5, 2021 21:19 Inactive
@zackdotcomputer
Copy link
Contributor Author

Okay - we'll see how the build goes but that should have fixed it.

@balazsorban44
Copy link
Member

@zackdotcomputer Unfortunately it still fails. I recommend running npm run test locally before you commit and push.

@vercel vercel bot temporarily deployed to Preview May 6, 2021 09:16 Inactive
@zackdotcomputer
Copy link
Contributor Author

Ah yup my bad @balazsorban44 - I was running the tests but not the lint. Fixed the lint now too - run test is green locally.

@vercel vercel bot temporarily deployed to Preview May 6, 2021 09:16 Inactive
@balazsorban44
Copy link
Member

balazsorban44 commented May 6, 2021

Yeah, we don't really have actual tests at the moment. 🥲. Working on it though! #1937

@balazsorban44 balazsorban44 merged commit ffd0601 into nextauthjs:main May 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Relates to documentation test Related to testing TypeScript Issues relating to TypeScript
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Proper Typing for Events
7 participants