Skip to content
This repository has been archived by the owner on Jul 29, 2023. It is now read-only.

Commit

Permalink
docs: update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
chernodub committed Nov 4, 2022
1 parent b35f320 commit 176ed74
Showing 1 changed file with 140 additions and 2 deletions.
142 changes: 140 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,141 @@
# ngx-lss
<p align="center"><img height=200 src="https://user-images.githubusercontent.com/37013688/199909362-c2fd546d-e84d-46a8-b023-6a634bbdb483.png" /></p>

`ngx-lss` is the library that provides the `LocalStorageService` service for your Angular apps

<h1 align="center">ngx-lss</h1>

<p align="center">
<a href="https://www.npmjs.com/package/ngx-lss"><img src="https://img.shields.io/npm/v/ngx-lss?style=flat-square" alt="NPM Version" /></a>
<a href="https://github.com/chernodub/ngx-lss/blob/main/LICENSE"><img src="https://img.shields.io/github/license/chernodub/ngx-lss?style=flat-square" alt="GitHub license" /></a>
<a href="https://github.com/chernodub/ngx-lss/releases/latest"><img alt="Last release date" src="https://img.shields.io/github/release-date/chernodub/ngx-lss?style=flat-square"/></a>
</p>

> `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

```bash
npm i ngx-lss
```

## Setting up

1. Import `LocalStorageModule` to root module of your app

```typescript
import { LocalStorageModule } from 'ngx-lss';

@NgModule({
// ...
imports: [
// ...
LocalStorageModule,
]
})
export class AppModule { }
```

2. Inject the service in your injectable class

```typescript
import { Component } from '@angular/core';
import { LocalStorageService } from 'ngx-lss';

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

## 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.

```ts
import { Injectable } from '@angular/core';
import { LocalStorageService } 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: LocalStorageService,
) {
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

```typescript
import { Component } from '@angular/core';
import { LocalStorageService } 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: LocalStorageService,
) {
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();
}
}
```

0 comments on commit 176ed74

Please sign in to comment.