New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

ngx-lss

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ngx-lss

Reactive adapter for browser's `localStoarge` API

latest
Source
npmnpm
Version
2.0.2
Version published
Weekly downloads
5
150%
Maintainers
1
Weekly downloads
 
Created
Source

ngx-lss

NPM Version GitHub license Last release date

ngx-lss is an angular library that provides a reactive adapter for localStorage API

Features

  • 🎆 Provides Observable API
  • 👀 Allows watching changes made in other tabs
  • 🐣 Easily adaptable and replaceable (3 simple methods: save, get, remove)
  • 🔬 Fully tested with jasmine

Installation

npm i ngx-lss

Setting up

  • Import NgxLocalStorageModule to root module of your app
import { NgxLocalStorageModule } from 'ngx-lss';

@NgModule({
  // ...
  imports: [
    // ...
    NgxLocalStorageModule.forRoot(),
  ]
})
export class AppModule { }
  • Inject the service in your injectable class
import { Component } from '@angular/core';
import { NgxLocalStorageService } from 'ngx-lss';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent {
  public constructor(
    private readonly NgxLocalStorageService: NgxLocalStorageService,
 ) {
    // ...
  }
}

Usage examples

Abstracting the service in a domain specific storage

💡 This is a recommended way to use the ngx-lss in your applications. It allows you to clearly separate the logic of working with the local storage from the business logic of your application. Also, this way, you can easily replace the library with another storage or your own implementation.

import { Injectable } from '@angular/core';
import { NgxLocalStorageService } from 'ngx-lss';
import { map, Observable } from 'rxjs';

import { UserSecret } from '../models/user-secret';


const USER_SECRET_STORAGE_KEY = 'user';

@Injectable({ providedIn: 'root' })
export class UserSecretStorageService {
  /** Credential secret of the current user. */
  public readonly currentSecret$: Observable<UserSecret | null>;

  public constructor(
    private readonly storageService: NgxLocalStorageService,
  ) {
    this.currentSecret$ = this.storageService.get<UserSecret>(USER_SECRET_STORAGE_KEY);
  }

  /**
   * Saves secret to the current storage.
   * @param secret Secret to save.
   */
  public saveSecret(
    secret: UserSecret,
  ): Observable<UserSecret> {
    return this.storageService.save(USER_SECRET_STORAGE_KEY, secret).pipe(map(() => secret));
  }

  /** Removes current secret. */
  public removeSecret(): Observable<void> {
    return this.storageService.remove(USER_SECRET_STORAGE_KEY);
  }
}

Accessing the storage API directly from a component

import { Component } from '@angular/core';
import { NgxLocalStorageService } from 'ngx-lss';

const COUNTER_KEY = 'counter';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {

  /** Clicks counter. */
  public readonly counter$: Observable<number | null>;

  public constructor(
    private readonly storage: NgxLocalStorageService,
  ) {
    this.counter$ = this.storage.get<number>(COUNTER_KEY);
  }

  public onUserClick(): void {
    this.counter$.pipe(
      first(),
      map(counter => counter ?? 0),
      switchMap(counter => this.storage.save(COUNTER_KEY, counter + 1)),
    ).subscribe();
  }
}

Keywords

typescript

FAQs

Package last updated on 06 Dec 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts