@sophat/cookies
TypeScript icon, indicating that this package has built-in type declarations

0.2.6 • Public • Published

Cookie Utility

This project provides a set of utility functions for interacting with browser cookies. The Cookies class offers methods to set, get, update, remove, and clear cookies, as well as retrieve all cookie keys.

Installation

You can install the package via npm:

npm install @sophat/cookies

Or using yarn:

yarn add @sophat/cookies

Usage

Importing the Library

To use the Cookies class and useCookie hook, import them as follows:

import { Cookies, useCookie } from '@sophat/cookies';

example: demo

Using the Cookies Class

Setting a Cookie

Cookies.setItem('userToken', 'abc123', { expires: 7 });

Getting a Cookie

const userToken = Cookies.getItem('userToken');
console.log(userToken); // 'abc123'

Removing a Cookie

Cookies.removeItem('userToken');

Getting All Cookie Names

const cookieNames = Cookies.getKeys();
console.log(cookieNames); // ['userToken']

Using the useCookie Hook

Setting a Cookie

const { setCookie } = useCookie();
setCookie('user', 'john', { expires: 7 });

Getting a Cookie

const { getCookie } = useCookie();
const value = getCookie('user');
console.log(value); // 'john'

Removing a Cookie

const { removeCookie } = useCookie();
removeCookie('user');

Getting All Cookie Names

const { getKeys } = useCookie();
const cookieNames = getKeys();
console.log(cookieNames); // ['user']

Package Sidebar

Install

npm i @sophat/cookies

Weekly Downloads

2

Version

0.2.6

License

MIT

Unpacked Size

20.1 kB

Total Files

18

Last publish

Collaborators

  • sophat