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

feat(cache): allow setting custom getKey for defineCachedEventHandler #744

Merged
merged 6 commits into from
Dec 23, 2022

Conversation

yassilah
Copy link
Contributor

πŸ”— Linked issue

resolves #691

❓ Type of change

  • πŸ“– Documentation (updates to the documentation or readme)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • πŸ‘Œ Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

πŸ“š Description

πŸ“ Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

getKey:
opts.getKey ||
((event) => {
const url = event.req.originalUrl || event.req.url;
Copy link
Member

Choose a reason for hiding this comment

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

Maybe we can call getKey inside this function? this way if it returns null, we can fallback to the default key behavior.

We also need to escape invalid chars also in all conditions (including ..) as they can lead to security issues.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I think it makes sense to call it inside and fallback to the default; as for the escaping of invalid chars, do you mean escaping them from the returned key?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, to only allow ascii charachters like later regex we have.

@@ -142,11 +146,9 @@ export function defineCachedEventHandler<T = any>(
...opts,
getKey: (event) => {
const key = opts.getKey?.(event)
if (key) { return key }
if (key) { return escapeKey(key) }
Copy link
Contributor Author

Choose a reason for hiding this comment

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

like this? @pi0

@pi0 pi0 changed the title feat: allow setting custom getKey in cached event handlers feat(cache): allow setting custom getKey for defineCachedEventHandler Dec 23, 2022
@pi0 pi0 merged commit 907d432 into unjs:main Dec 23, 2022
@pi0
Copy link
Member

pi0 commented Dec 23, 2022

Thanks <3

@MiniDigger
Copy link
Contributor

thank you, now I can't wait to get back to work next year and remove all my disgusting hacks, lmao

@yassilah yassilah deleted the fix/set-custom-get-key branch January 17, 2023 21:21
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.

Allow passing getKey method to route rules
3 participants