@craftworks/ngx-common
TypeScript icon, indicating that this package has built-in type declarations

17.0.2 • Public • Published

@craftworks/ngx-common

NPM version Downloads

An Angular library that provides useful general-purpose functionalities.

Installation

npm install --save @craftworks/ngx-common

Check for necessary peer dependencies.

Peer Dependencies

  • @angular/common: ^17.0.0
  • @angular/core: ^17.0.0

API

Interfaces

ComponentChanges

The ComponentChanges interface extends the SimpleChange interface to allow a more strongly typed ngOnChanges implementation.

@Component({ ... })
class MyComponent implements OnChanges {
  @Input() public aProperty: string | undefined;

  public ngOnChanges(changes: ComponentChanges<MyComponent, 'aProperty'>): void {
    console.log(changes['aProperty']);

    // Error: Property 'notAProperty' does not exist on type 'ComponentChanges<MyComponent, "aProperty">
    console.log(changes['notAProperty']);
  }
}

ℹ️ Note that this interface does not distinguish @Input and non @Input properties.

Store

LocalStoreService / SessionStoreService

Store a value to the localStorage / sessionStorage. Note that the data to be stored must be seralizable.

import { LocalStoreService } from '@craftworks/ngx-common';

interface SomeData {
  someProperty: string;
}

@Component({ ... })
class MyComponent {
  private readonly SOME_KEY = 'SOME_KEY_1';
  private readonly SOME_DATA: SomeData = {
    someProperty: string,
  };

  constructor(private readonly localStoreService: LocalStoreService<SomeData>) {}

  public ngOnInit() {
    // Set data from storage
    this.localStoreService.set(this.SOME_KEY, this.SOME_DATA);

    // Load data from storage
    this.localStoreService.load(this.SOME_KEY);

    // Remove data from storage
    this.localStoreService.remove(this.SOME_KEY);
  }
}

STORE_CONFIG

Inject STORE_CONFIG to set the available config parameters.

@NgModule({
  ...
  providers: [{ provide: STORE_CONFIG, useValue: { encodeValue: true } }],
})
export class SomeModule {}

StoreConfig has the following interface

interface StoreConfig {
  // Whether the stored data should be encrypted using Base64 encoding.
  // Note: This is not an actual security feature, but only prevents the stored data in the memory from being displayed as plain text.
  // Note2: Migration is not handled, make sure you either change the key or clear the memory for all users beforehand
  // Default: true
  encodeStorage: boolean;
}

Change Log

See CHANGELOG.md

License

MIT

Package Sidebar

Install

npm i @craftworks/ngx-common

Weekly Downloads

56

Version

17.0.2

License

MIT

Unpacked Size

30.7 kB

Total Files

19

Last publish

Collaborators

  • zualexander
  • phi_spindler