npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ptdev-cookie-jar

v1.0.0

Published

Persistent Cookie Management

Downloads

4

Readme

CJ Cookie Manager

CJ Cookie Manager is a simple and flexible utility for managing cookies in web applications. It provides an easy-to-use API for setting, getting, and deleting cookies, with support for common cookie attributes such as expires, path, secure, sameSite, and domain. It also includes methods to handle cookies with JSON values.

Features

  • Set, get, and delete cookies with ease.
  • Support for advanced cookie options like Secure, SameSite, and Domain.
  • JSON encoding/decoding for cookie values.
  • Built using TypeScript for type safety.

Installation

Install the package via npm:

npm install cj-cookie-manager

Usage

Basic Usage

import CJ from 'cj-cookie-manager';

// Get the singleton instance
const cookieManager = CJ.getInstance();

// Set a cookie
cookieManager.setCookie('username', 'JohnDoe', { expires: 7 }); // Expires in 7 days

// Get a cookie
const username = cookieManager.getCookie('username');
console.log(username); // Output: 'JohnDoe'

// Delete a cookie
cookieManager.deleteCookie('username');

Setting a Cookie with Options

You can set cookies with additional options such as expiration time, secure flag, sameSite, and path.

cookieManager.setCookie('sessionId', 'abc123', {
  expires: 3,              // Expires in 3 days
  path: '/',                // Available throughout the site
  secure: true,             // Only sent over HTTPS
  sameSite: 'Strict',       // SameSite policy
  domain: '.example.com'    // Cookie available across subdomains
});

Handling JSON Data in Cookies

You can easily store and retrieve JSON objects as cookies using the setJSONCookie and getJSONCookie methods.

// Set a cookie with a JSON object
cookieManager.setJSONCookie('preferences', { theme: 'dark', language: 'en' });

// Get the JSON object from the cookie
const preferences = cookieManager.getJSONCookie('preferences');
console.log(preferences); // Output: { theme: 'dark', language: 'en' }

API

setCookie(name: string, value: string, options?: CookieOptions) Sets a cookie with the given name and value, with optional settings for expires, path, secure, sameSite, and domain.

Parameters:

  • name: The name of the cookie.
  • value: The value of the cookie.
  • options (optional): Additional cookie settings.
    • expires: Number of days until the cookie expires or a specific Date.
    • path: The path on which the cookie is available (defaults to /).
    • secure: Set to true to only send the cookie over HTTPS.
    • sameSite: SameSite attribute ("Strict", "Lax", "None").
    • domain: The domain where the cookie is available.

getCookie(name: string): string | null Retrieves the value of the cookie with the specified name. Returns null if the cookie does not exist.

Parameters:

name: The name of the cookie. deleteCookie(name: string, path?: string, domain?: string) Deletes the cookie with the given name. Optionally specify the path and domain if they were set when the cookie was created.

Parameters:

name: The name of the cookie to delete. path: The path of the cookie (defaults to /). domain: The domain of the cookie.

setJSONCookie(name: string, value: object, options?: CookieOptions) Sets a cookie with a JSON object as the value. The object will be automatically stringified.

Parameters:

name: The name of the cookie. value: The JSON object to store. options (optional): Additional cookie settings (same as setCookie).

getJSONCookie(name: string): object | null Retrieves the JSON object stored in the cookie with the specified name. Returns null if the cookie does not exist or if it can't be parsed.

Parameters:

name: The name of the cookie.

Example

import CJ from 'cj-cookie-manager';

const cookieManager = CJ.getInstance();

// Set a simple cookie
cookieManager.setCookie('userToken', '123456', { secure: true, sameSite: 'Lax' });

// Set a cookie with a JSON object
cookieManager.setJSONCookie('settings', { darkMode: true, notifications: false });

// Retrieve a simple cookie
const token = cookieManager.getCookie('userToken');
console.log(token); // Output: '123456'

// Retrieve a JSON cookie
const settings = cookieManager.getJSONCookie('settings');
console.log(settings); // Output: { darkMode: true, notifications: false }

// Delete a cookie
cookieManager.deleteCookie('userToken');

License

This project is licensed under the MIT License. See the LICENSE file for details.