@craftworks/ngx-common
v18.0.0
Published
An Angular library that provides useful general-purpose functionalities.
Downloads
931
Readme
@craftworks/ngx-common
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: ^18.0.0
@angular/core: ^18.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