- From: Jake Archibald <notifications@github.com>
- Date: Tue, 09 Jun 2015 01:50:57 -0700
- To: slightlyoff/ServiceWorker <ServiceWorker@noreply.github.com>
- Message-ID: <slightlyoff/ServiceWorker/issues/707/110280181@github.com>
I think this is an opportunity to introduce a decent cookie API. Async, so it can be accessed from worker contexts. I imagine this would be part of the fetch spec, @annevk?
```
partial interface Navigator {
[SameObject] readonly attribute CookiesContainer cookies;
};
partial interface WorkerNavigator {
[SameObject] readonly attribute CookiesContainer cookies;
};
[Exposed=(Window,Worker)]
interface CookiesContainer {
[NewObject] Promise<Cookie> get(USVString name);
[NewObject] Promise<sequence<Cookie>> getAll();
[NewObject] Promise<void> set(Cookie cookie);
};
[Constructor(USVString name, USVString value, optional CookieOptions options),
Exposed=(Window,Worker)]
interface Cookie {
attribute USVString name;
attribute USVString value;
attribute USVString domain;
attribute USVString path;
attribute Date expires;
attribute Boolean secure;
}
dictionary CookieOptions {
USVString domain; // defaults to location.host
USVString path = "/";
Date expires;
Boolean secure = false;
}
```
Usage:
```js
// read cookies value
navigator.cookies.get('hello').then(c => c && c.value).then(value => {
if (value) {
// …
}
});
// create/overwrite cookie value
navigator.cookies.set(new Cookie('hello', 'world', {
path: '/foo/'
}));
// change cookie value
navigator.cookies.get('viewCount').then(c => c || new Cookie('viewCount', '0')).then(viewCookie => {
viewCookie.value = Number(viewCookie.value) + 1;
return navigator.cookies.set(viewCookie);
});
// delete cookie
navigator.cookies.delete('viewCount').then(cookieWasDeleted => {
// …
});
// output all cookie name/vals as urlencoded string
navigator.cookies.getAll().then(cookies => {
var params = new URLSearchParams();
for (var cookie of cookies) {
params.append(cookie.name, cookies.value);
}
return params.toString();
});
```
---
Reply to this email directly or view it on GitHub:
https://github.com/slightlyoff/ServiceWorker/issues/707#issuecomment-110280181
Received on Tuesday, 9 June 2015 08:51:33 UTC