NgxStorageUtils is an npm package designed to simplify cookie and storage management in Angular applications, especially in Server-Side Rendering (SSR) environments.
- Manage cookies and storage in Angular SSR applications effortlessly.
- Compatible with Angular Universal for SSR setups.
- Provides a simple API for setting, getting, and removing cookies and storage items.
- Supports both client-side and server-side execution.
- Encrypts data stored in local storage for enhanced security.
Install the package via npm:
npm install ngx-storage-utils
Inject and use CookiesService
or StorageService
in your Angular components or services:
import { Component } from '@angular/core';
import { CookiesService, StorageService } from 'ngx-storage-utils';
@Component({
selector: 'app-root',
template: `<h1>ngx-storage-utils</h1>`,
})
export class AppComponent {
cookiesService = inject(CookiesService);
storageService = inject(StorageService);
}
-
clear(): void
: Clears all items from local storage. -
getTypedItem<T = unknown>(key: string, encryptionKey: string = '', validate: boolean = true): T | null
: Retrieves and parses an item by key. -
getStorageItem(key: string, validate: boolean): StorageObject | null
: Retrieves the storage item asStorageObject
associated with the key. -
getItem(key: string, validate: boolean = true): string | null
: Retrieves the string value associated with the key. -
key(index: number): string | null
: Retrieves the key of the storage item at the specified index. -
removeItem(key: string): void
: Removes the storage item associated with the key. -
setItem(key: string, value: string, options?: StorageSetOptions): void
: Sets the value of the storage item associated with the key. -
validate(): void
: Validates and removes expired storage items.
Represents an object stored in local storage.
-
value: string
: The value stored in the local storage. -
isEncrypted?: boolean
: (Optional) Indicates whether the stored item is encrypted. -
isExpirable?: boolean
: (Optional) Indicates whether the stored item is expirable. -
expires?: Date
: (Optional) The expiration date of the stored item.
-
check(name: string): boolean
: Check if a cookie exists. -
get(name: string): string
: Get the value of a cookie. -
getAll(): { [key: string]: string }
: Get all cookies. -
set(name: string, value: string, options?: CookieOptions): void
: Set a cookie. -
delete(name: string, path?: string, domain?: string, secure?: boolean, sameSite: 'Lax' | 'Strict' | 'None' = 'Lax'): void
: Delete a cookie. -
deleteAll(path?: string, domain?: string, secure?: boolean, sameSite: 'Lax' | 'None' | 'Strict' = 'Lax'): void
: Delete all cookies.
-
expires?: number | Date
: Expiration date of the cookie. If not set, the cookie is a session cookie. -
path?: string
: Path for which the cookie is valid. -
domain?: string
: Domain for which the cookie is valid. -
secure?: boolean
: If true, the cookie will only be sent over HTTPS. -
sameSite?: 'Lax' | 'Strict' | 'None'
: SameSite attribute of the cookie.
This project is licensed under the MIT License - see the LICENSE file for details.