ng-cookie-manager
TypeScript icon, indicating that this package has built-in type declarations

0.1.2 • Public • Published

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.

Package Sidebar

Install

npm i ng-cookie-manager

Weekly Downloads

6

Version

0.1.2

License

MIT

Unpacked Size

40.4 kB

Total Files

12

Last publish

Collaborators

  • ndiaye_d.