Skip to content
This repository was archived by the owner on Nov 28, 2025. It is now read-only.

Commit 989c78d

Browse files
committed
feat: improve config
1 parent afa0f0f commit 989c78d

File tree

7 files changed

+38
-9
lines changed

7 files changed

+38
-9
lines changed

src/config/config.interface.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export interface WebStorageConfigInterface {
2+
prefix?: string;
3+
clearType?: ClearType;
4+
mutateObjects?: boolean;
5+
}
6+
export type ClearType = 'decorators' | 'prefix' | 'all';

src/config/config.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { WebStorageConfigInterface } from './config.interface';
2+
3+
const DefaultConfig: WebStorageConfigInterface = {
4+
prefix: 'angular2_ws', // TODO: change default to 'ngx_'
5+
clearType: 'decorators', // TODO: change default to 'prefix'
6+
mutateObjects: true
7+
};
8+
9+
// TODO allow to set configuration in node-config (`config` on npm)
10+
// take configuration provided as a global variable
11+
declare const NGXSTORE_CONFIG: WebStorageConfigInterface;
12+
13+
/**
14+
* @deprecated define global variable `NGXSTORE_CONFIG` instead
15+
*/
16+
export const WEBSTORAGE_CONFIG = DefaultConfig;
17+
18+
// merge default config, deprecated config and global config all together
19+
export const Config = Object.assign({}, DefaultConfig, WEBSTORAGE_CONFIG, NGXSTORE_CONFIG);

src/config/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './config';
2+
export * from './config.interface';

src/decorator/webstorage.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { WebStorageUtility } from '../utility/webstorage.utility';
22
import { LocalStorageService, SessionStorageService, WebStorageServiceInterface } from '../service/webstorage.service';
33
import * as isEmpty from 'is-empty';
4+
import { Config } from '../config';
45

56
export function LocalStorage(key?: string) {
67
return WebStorage(localStorage, LocalStorageService, key);
@@ -45,6 +46,9 @@ function WebStorage(webStorage: Storage, service: WebStorageServiceInterface, ke
4546
proxy = WebStorageUtility.set(webStorage, key, value);
4647
}
4748

49+
// Object mutations below
50+
if (!Config.mutateObjects) return;
51+
4852
// manual method for force save
4953
if (proxy instanceof Object) {
5054
proxy.save = function () {

src/index.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,10 @@ import { LocalStorageService, SessionStorageService } from './service/webstorage
44
export { LocalStorage, SessionStorage } from './decorator/webstorage'
55
export { WebStorageService, LocalStorageService, SessionStorageService } from './service/webstorage.service';
66
export { WebStorageUtility } from './utility/webstorage.utility';
7+
export { WebStorageConfigInterface, WEBSTORAGE_CONFIG } from './config';
78
export declare class Webstorable {
89
save(): void;
910
}
10-
export type ClearType = 'decorators' | 'prefix' | 'all';
11-
export let WEBSTORAGE_CONFIG = {
12-
prefix: 'angular2ws_'
13-
};
1411

1512
@NgModule({
1613
providers: [

src/service/webstorage.service.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Injectable } from '@angular/core';
2+
import { ClearType, Config } from '../config';
23
import { WebStorageUtility } from '../utility/webstorage.utility';
3-
import { ClearType, WEBSTORAGE_CONFIG } from '../index';
44

55
export interface WebStorageServiceInterface {
66
keys: Array<string>;
@@ -29,15 +29,16 @@ export abstract class WebStorageService {
2929
WebStorageUtility.remove(this.storage, key);
3030
}
3131

32-
public clear(clearType: ClearType = 'decorators'): void {
32+
public clear(clearType?: ClearType): void {
33+
clearType = clearType || Config.clearType;
3334
let keys = (<WebStorageServiceInterface>this.constructor).keys; // get keys from child class
3435
if (clearType === 'decorators') {
3536
for (let key of keys) {
3637
this.storage.removeItem(key);
3738
}
3839
} else if (clearType === 'prefix') {
3940
for (let key in this.storage) {
40-
if (this.storage.getItem(key).startsWith(WEBSTORAGE_CONFIG.prefix)) {
41+
if (this.storage.getItem(key).startsWith(Config.prefix)) {
4142
this.storage.removeItem(key);
4243
}
4344
}

src/utility/webstorage.utility.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import {WEBSTORAGE_CONFIG} from '../index';
1+
import { Config } from '../config';
22

33
export class WebStorageUtility {
44
public static generateStorageKey(key: string): string {
5-
return `${WEBSTORAGE_CONFIG.prefix}${key}`
5+
return `${Config.prefix}${key}`;
66
}
77

88
public static get(storage: Storage, key: string): any {

0 commit comments

Comments
 (0)