- Notifications
You must be signed in to change notification settings - Fork 1
CookieStorage
Yousif Al-Raheem edited this page Jan 20, 2020 · 1 revision
This utility helps to manage cookie storage. To use it, construct a new CookieStorage.
This utility consists of 8 methods:
To set desired item to cookie storage
// to set item to cookie storage const cookieStorage: CookieStorage = new CookieStorage(); console.log(cookieStorage.setItem("cookieKey", "cookieValue")); // true console.log(document.cookie); // "cookieKey=cookieValue"| Param | Type | Description |
|---|---|---|
| key | string | key of item to be set 1 |
| value | any | value to be set to cookie storage |
| options | SetItemOptions 2 | optional params. Available options are expires, maxAge and secure |
- key of item should not be reserved keywords of cookie which are
expires | max-age | path | domain | secure. - interface of SetItemOptions is defined as below:
interface SetItemOptions { // Expiration Date expires?: Date; // Maximum age in seconds maxAge?: number; // Secure cookie. Set the security to `true` if you want it to be encrypted in **HTTPS** connections secure?: boolean; }To get item with specific key from cookie storage. If key is invalid or not found in cookie, null will be returned.
// to get item from cookie storage const cookieStorage: CookieStorage = new CookieStorage(); console.log(cookieStorage.getItem("cookieKey")); // "cookieValue"| Param | Type | Description |
|---|---|---|
| key | string | key of item to be retrieved |
To remove item with specific key from cookie storage. If key is invalid or not found in cookie, false will be returned.
// to remove item from cookie storage const cookieStorage: CookieStorage = new CookieStorage(); console.log(cookieStorage.removeItem("cookieKey")); // true console.log(document.cookie); // "cookieKey=; max-age=-1"| Param | Type | Description |
|---|---|---|
| key | string | key of item to be retrieved |
To check if cookie storage consists of key passed. If key passed is one of the reserved words, false will be returned.
// to remove item from cookie storage const cookieStorage: CookieStorage = new CookieStorage(); console.log(cookieStorage.hasItem("cookieKey")); // true| Param | Type | Description |
|---|---|---|
| key | string | key of item to be retrieved |
To clear cookie storage.
// to clear cookie storage const cookieStorage: CookieStorage = new CookieStorage(); cookieStorage.clear(); console.log(document.cookie); // "myCookie=; max-age=-1"To retrieve list of keys in cookie storage.
// to retrieve list of keys from cookie storage const cookieStorage: CookieStorage = new CookieStorage(); console.log(cookieStorage.keys()); // ["myCookie"]To retrieve key at certain index from cookie storage. Method will return undefined if invalid index passed.
// to retrieve specific key from cookie storage const cookieStorage: CookieStorage = new CookieStorage(); console.log(cookieStorage.key(0)); // "myCookie"| Param | Type | Description |
|---|---|---|
| index | number | index of key in cookie storage |
To length of list of keys in cookie storage.
// to retrieve list of keys from cookie storage const cookieStorage: CookieStorage = new CookieStorage(); console.log(cookieStorage.length); // 1