Skip to content

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:

Method: setItem

To set desired item to cookie storage

Example

// to set item to cookie storage
const cookieStorage: CookieStorage = new CookieStorage();
console.log(cookieStorage.setItem("cookieKey", "cookieValue")); // true
console.log(document.cookie); // "cookieKey=cookieValue"

Properties

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

Footnotes

  1. key of item should not be reserved keywords of cookie which are expires | max-age | path | domain | secure.
  2. 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;
}

Method: getItem

To get item with specific key from cookie storage. If key is invalid or not found in cookie, null will be returned.

Example

// to get item from cookie storage
const cookieStorage: CookieStorage = new CookieStorage();
console.log(cookieStorage.getItem("cookieKey")); // "cookieValue"

Properties

Param Type Description
key string key of item to be retrieved

Method: removeItem

To remove item with specific key from cookie storage. If key is invalid or not found in cookie, false will be returned.

Example

// 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"

Properties

Param Type Description
key string key of item to be retrieved

Method: hasItem

To check if cookie storage consists of key passed. If key passed is one of the reserved words, false will be returned.

Example

// to remove item from cookie storage
const cookieStorage: CookieStorage = new CookieStorage();
console.log(cookieStorage.hasItem("cookieKey")); // true

Properties

Param Type Description
key string key of item to be retrieved

Method: clear

To clear cookie storage.

Example

// to clear cookie storage
const cookieStorage: CookieStorage = new CookieStorage();
cookieStorage.clear();
console.log(document.cookie); // "myCookie=; max-age=-1"

Method: keys

To retrieve list of keys in cookie storage.

Example

// to retrieve list of keys from cookie storage
const cookieStorage: CookieStorage = new CookieStorage();
console.log(cookieStorage.keys()); // ["myCookie"]

Method: key

To retrieve key at certain index from cookie storage. Method will return undefined if invalid index passed.

Example

// to retrieve specific key from cookie storage
const cookieStorage: CookieStorage = new CookieStorage();
console.log(cookieStorage.key(0)); // "myCookie"

Properties

Param Type Description
index number index of key in cookie storage

Getter Method: length

To length of list of keys in cookie storage.

Example

// to retrieve list of keys from cookie storage
const cookieStorage: CookieStorage = new CookieStorage();
console.log(cookieStorage.length); // 1