Gathering detailed insights and metrics for @tinkoff/browser-cookies
Gathering detailed insights and metrics for @tinkoff/browser-cookies
Gathering detailed insights and metrics for @tinkoff/browser-cookies
Gathering detailed insights and metrics for @tinkoff/browser-cookies
A modular framework for universal JS applications
npm install @tinkoff/browser-cookies
Typescript
Module System
Node Version
NPM Version
TypeScript (96.08%)
JavaScript (2.18%)
Rust (1.15%)
Handlebars (0.5%)
CSS (0.08%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
Apache-2.0 License
105 Stars
822 Commits
8 Forks
3 Watchers
5 Branches
23 Contributors
Updated on Jul 16, 2025
Latest Version
4.0.3
Package Id
@tinkoff/browser-cookies@4.0.3
Unpacked Size
21.90 kB
Size
4.99 kB
File Count
12
NPM Version
10.7.0
Node Version
18.20.4
Published on
Oct 17, 2024
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
1
Tiny cookies library for the browser
Fork of browser-cookies
Using npm
1npm install @tinkoff/browser-cookies
Using yarn
1npm install @tinkoff/browser-cookies
1import { Cookies } from '@tinkoff/browser-cookies'; 2 3const cookies = new Cookies({ sameSite: 'lax' }); 4 5cookies.set('firstName', 'Lisa'); 6cookies.set('firstName', 'Lisa', { expires: 365 }); // Expires after 1 year 7cookies.set('firstName', 'Lisa', { secure: true, domain: 'www.example.org' }); 8 9cookies.get('firstName'); // Returns cookie value (or null) 10 11cookies.erase('firstName'); // Removes cookie
Cookies
API:
defaults
)name
, value
[, options
])name
)name
, [, options
])Cookies.set(name
, value
[, options
])
Method to save a cookie.
argument | type | description |
---|---|---|
name | string | The name of the cookie to save. |
value | string | The value to save, [percent encoding][ref-percent-encoding] will automatically be applied. Note that only strings are allowed as value, the examples section shows how to save JSON data. |
options | object | May contain any of the properties specified in options below. If an option is not specified, the value configured in Cookies.constructor will be used. |
Cookies.get(name
)
Method that returns a cookie value, or null if the cookie is not found. [Percent encoded][ref-percent-encoding] values will automatically be decoded.
argument | type | description |
---|---|---|
name | string | The name of the cookie to retrieve. |
Cookies.erase(name
[, options
])
Method to remove a cookie.
argument | type | description |
---|---|---|
name | string | The name of the cookie to remove. |
options | object | May contain the domain and path properties specified in options below. If an option is not specified, the value configured in Cookies.constructor will be used. |
Cookies.all()
Method to get all cookies.
Returns an object containing all cookie values with the cookie names used as keys. Percent encoded names and values will automatically be decoded.
Cookies.constructor(defaults
)
defaults
argument may be used to change the default value of each option specified in options below.
The options shown in the table below may be set to instance of Cookies.constructor or passed as function argument to Cookies.set() and Cookies.erase(). Also check out the Examples further below.
Name | Type | Default | Description |
---|---|---|---|
expires | Number , Date , String | 0 | Configure when the cookie expires by using one of the following types as value:
|
domain | String | "" | The [domain][ref-cookie-domain] from where the cookie is readable.
|
path | String | "/" | The path from where the cookie is readable.
|
secure | Boolean | false | If true the cookie will only be transmitted over secure protocols like https. |
httponly | Boolean | false | If true the cookie may only be read by the web server.
|
samesite | String | "" | The samesite argument may be used to [prevent cookies from being sent along with cross-site requests][ref-samesite].
|
Count the number of a visits to a page:
1import { Cookies } from '@tinkoff/browser-cookies'; 2 3const cookies = new Cookies(); 4 5// Get cookie value 6const visits = cookies.get('count') || 0; 7console.log("You've been here " + parseInt(visits) + " times before!"); 8 9// Increment the counter and set (or update) the cookie 10cookies.set('count', parseInt(visits) + 1, {expires: 365});
JSON may be saved by converting the JSON object into a string:
1import { Cookies } from '@tinkoff/browser-cookies'; 2 3const cookies = new Cookies(); 4 5// Store JSON data 6const user = { firstName: 'Sofia', lastName: 'Dueñas' }; 7cookies.set('user', JSON.stringify(user)) 8 9// Retrieve JSON data 10const userString = cookies.get('user'); 11alert('Hi ' + JSON.parse(userString).firstName);
The default cookie options may be changed:
1import { Cookies } from '@tinkoff/browser-cookies'; 2 3// Apply defaults 4const cookies = new Cookies({ 5 secure: true, 6 expires: 7, 7}); 8 9// 'secure' option enabled and cookie expires in 7 days 10cookies.set('FirstName', 'John') 11 12// 'secure' option enabled and cookie expires in 30 days 13cookies.set('LastName', 'Smith', { expires: 30 })
The cookies.all
method can be used for more advanced functionality, for example to erase all cookies except one:
1import { Cookies } from '@tinkoff/browser-cookies'; 2 3const cookies = new Cookies(); 4 5const cookieToKeep = 'FirstName'; // Name of the cookie to keep 6 7// Get all cookies as an object 8const allCookies = cookies.all(); 9 10// Iterate over all cookie names 11for (let cookieName in allCookies) { 12 // Erase the cookie (except if it's the cookie that needs to be kept) 13 if (allCookies.hasOwnProperty(cookieName) && cookieName != cookieToKeep) { 14 cookies.erase(cookieName); 15 } 16}
No vulnerabilities found.
No security vulnerabilities found.