unjs / cookie-es

🍪 Cookie and Set-Cookie parser and serializer
Other
151 stars 10 forks source link

parsing single cookie #31

Closed danielroe closed 5 months ago

danielroe commented 6 months ago

original issue: https://github.com/nuxt/nuxt/issues/27246

It might be worth considering an option or separate function to return/decode only a single cookie.

What do you think @pi0?

pi0 commented 6 months ago

This library is for parsing Cookie header (incoming) which is multi value. Not sure to understand are you proposing to support Set-Cookie possible values too?

danielroe commented 6 months ago

Just like serialize serialises only a single cookie, we could optionally decode only a single value (that is, skip calling the decode function for the other ones).

Like the difference between getRequestHeaders and getRequestHeader.

Eazash commented 6 months ago

@danielroe As an alternate solution, what if the decode function had access to the cookie key? Something like decode?(value: string, key: string). That way existing behaviour can be retained while allowing upstream to handle filtering by key to decode only a single value.

pi0 commented 5 months ago

https://github.com/unjs/cookie-es/pull/35 is a little bit more verbose but also gives more flexibility.

@danielroe one thing i would suggest for Nuxt use btw if perf of not parsing everytime matters, we could cache the value of parses Cookie value (string) and only reparse when changed but i guess filter would work too.

I delay next release to finish work on #33 but please let me know if need it earlier or have an opinion about new api.

danielroe commented 5 months ago

No worries at all; there's no rush. I like the perf suggestion too!