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

ng-cookie-manager

v0.1.2

Published

Unlike many other cookie management libraries, **NG Cookie Manager** offers extended functionalities that go beyond basic cookie creation and deletion. These include encryption support, prefix-based deletion, size calculation, and total cookie size manage

Downloads

105

Readme

Why NG Cookie Manager?

Unlike many other cookie management libraries, NG Cookie Manager offers extended functionalities that go beyond basic cookie creation and deletion. These include encryption support, prefix-based deletion, size calculation, and total cookie size management. This makes it ideal for developers who need more advanced cookie management options in their Angular applications.

Features

  • Set Cookies: Create new cookies with various options (expiration, path, domain, security).
  • Get Cookies: Retrieve the value of cookies by name, with optional decryption.
  • Delete Cookies: Remove cookies safely.
  • Delete All Cookies: Remove all cookies with one method.
  • Check Cookie Existence: Verify if a cookie exists.
  • Delete Cookies by Prefix: Remove cookies whose names start with a specified prefix.
  • Cookie Size Calculation: Determine the size of individual cookies or the total size of all cookies.
  • Secure Handling: Options for securing cookies with SameSite, Secure, and encryption options.
  • Type Safety: Full TypeScript support for a better development experience.

Installation

Install the package via npm:

npm install ng-cookie-manager

Usage

Importing the Service

Import the CookieManagerService in your Angular module or component:

import { CookieManagerService } from 'ng-cookie-manager';

Example Component

Here’s an example of how to use the CookieManagerService in a component:

import { Component } from '@angular/core';
import { NgCookieManagerService } from 'ng-cookie-manager';

@Component({
  selector: 'app-demo',
  templateUrl: './demo.component.html',
})
export class DemoComponent {
  constructor(private cookieManager: NgCookieManagerService) {}

  // Example usage of setting a cookie
  setCookie() {
    this.cookieManager.setCookie('username', 'ndiayeD.', 7);
  }

  // Example usage of getting a cookie
  getCookie() {
    const userName = this.cookieManager.getCookie('username');
    console.log('Username:', userName);
  }

  // Example usage of deleting a cookie
  deleteCookie() {
    this.cookieManager.deleteCookie('username');
  }

  // Check if a cookie exists
  checkCookieExists() {
    const exists = this.cookieManager.hasCookie('username');
    console.log('Cookie exists:', exists);
  }

  // Delete all cookies
  deleteAllCookies() {
    this.cookieManager.deleteAllCookies();
  }

  // Delete cookies by prefix
  deleteCookiesByPrefix() {
    this.cookieManager.deleteCookiesByPrefix('user');
  }

  // Get total size of all cookies
  getTotalCookieSize() {
    const totalSize = this.cookieManager.getTotalCookieSize();
    console.log('Total size of all cookies:', totalSize, 'bytes');
  }
}

API Documentation

setCookie

Sets a new cookie with optional parameters for expiration, path, domain, and security.

setCookie(
  name: string,
  value: string,
  expireDaysOrDate?: number | Date,
  path?: string,
  domain?: string,
  secure?: boolean,
  sameSite?: string,
  encrypted?: boolean
): void;

Parameters:

  • name: string - The name of the cookie.
  • value: string - The value to be stored in the cookie.
  • expireDaysOrDate: number | Date - Optional. The number of days until the cookie expires or a specific expiration date.
  • path: string - Optional. The path within the site for which the cookie is valid.
  • domain: string - Optional. The domain for which the cookie is valid.
  • secure: boolean - Optional. If true, the cookie will only be transmitted over secure protocols (HTTPS).
  • sameSite: string - Optional. Specifies the SameSite attribute for the cookie (Strict, Lax, or None).
  • encrypted: boolean - Optional. If true, the cookie value will be encrypted before storage.

getCookie

Retrieves the value of a cookie by its name, with support for decryption.

getCookie(name: string, encrypted?: boolean): string | null;

Parameters:

  • name: string - The name of the cookie to retrieve.
  • encrypted: boolean - Optional. If true, attempts to decrypt the cookie value.

Returns:

  • string | null - The value of the cookie if found, or null if the cookie does not exist.

deleteCookie

Removes a cookie by its name.

deleteCookie(name: string, path?: string, domain?: string): void;

Parameters:

  • name: string - The name of the cookie to delete.
  • path: string - Optional. The path within the site for which the cookie is valid.
  • domain: string - Optional. The domain for which the cookie is valid.

hasCookie

Checks if a cookie with the specified name exists.

hasCookie(name: string): boolean;

Parameters:

  • name: The name of the cookie to check.

Returns:

  • true if the cookie exists, otherwise false.

deleteAllCookies

Deletes all cookies.

deleteAllCookies(path?: string, domain?: string): void;

Parameters:

  • path: Optional. The path within the site for which the cookie is valid.
  • domain: Optional. The domain for which the cookies are valid.

deleteCookiesByPrefix

Deletes cookies whose names start with a specific prefix.

deleteCookiesByPrefix(prefix: string, path?: string, domain?: string): void;

Parameters:

  • prefix: The prefix to match cookies by their name.
  • path: Optional. The path within the site for which the cookies are valid.
  • domain: Optional. The domain for which the cookies are valid.

getCookieSize

Gets the size of a specific cookie.

getCookieSize(name: string): number;

Parameters:

  • name: The name of the cookie to get the size of.

Returns:

  • The size of the cookie in bytes.

getTotalCookieSize

Calculates the total size of all cookies.

getTotalCookieSize(): number;

Returns:

  • The total size of all cookies in bytes.

Contribution

Feel free to contribute to the project by submitting pull requests or issues. All contributions are welcome!

  • Fork the repository.
  • Create a new branch for your feature or bug fix.
  • Submit a pull request with a clear description of your changes.

License

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