Feature request: Provide a way to read _all_ values of a cookie by name
Is your feature request related to a problem? Please describe.
Whenever the same cookie name is set with differing domain or path semantics, multiple instances of that cookie are added to document.cookie. For example, try running the following code at https://example.com/somepath

The current API of js-cookie will only return the first value that matches the cookie name (Cookies.get('foo') === 'c').
As an application developer, this means it is impossible to access certain cookie values, or even realize that you've gotten yourself into this situation, without looking at document.cookie yourself directly.
I recognize that this is not the default case for most situations, but it is still a scenario that developers can find themselves in. For example, suppose an application migrates its cookies from being stored on a root domain, to being shared across all subdomains (or vice versa!) - being able to read two instances of the cookie name is a useful feature. Developers can use that information to attempt to unset cookies, console.log out debug info, etc.
Describe the solution you'd like
I'd love a new method - Cookies.list() - with similar semantics to Cookies.get(). Pass in the name of the cookie to retrieve and it will return all values stored for that name. Pass in nothing and it will return an object containing all lists of cookies.
namespace Cookies {
list(name: string): string[]
list(): Record<string, string[]>
}Given a document.cookie of foo=a; foo=b; foo=c, I'd expect the following output:
Cookies.list('foo')
// => ['a', 'b', 'c']
Cookies.list()
// => { foo: ['a', 'b', 'c'] }
Cookies.list('bar')
// => []a rough implementation might look very similar to the existing implementation for get:
function list (name) {
...
var cookies = document.cookie ? document.cookie.split('; ') : []
var jar = {}
for (var i = 0; i < cookies.length; i++) {
var parts = cookies[i].split('=')
var value = parts.slice(1).join('=')
try {
var found = decodeURIComponent(parts[0])
jar[found] = jar[found] || [];
jar[found].push(converter.read(value, found))
} catch (e) {}
}
return name ? jar[name] || [] : jar
}Describe alternatives you've considered
Obviously, a developer can access document.cookie directly, but they'll also need to reimplement the logic in converter.read to ensure that they're getting the same values. At that point, they've reimplemented half of this library themselves.
Unlike issues like https://github.com/js-cookie/js-cookie/pull/800, it is impossible to build this functionality from the existing API.
Source: js-cookie/js-cookie