TypeScript storage module(memory, sessionStorage, localStorage) and @Cacheable decorator
npm install rebirth-storage --save
import { RebirthStorageModule } from 'rebirth-storage';
@NgModule({
imports: [
BrowserModule,
CommonModule,
SharedModule.forRoot(),
RebirthStorageModule
],
declarations: [
AppComponent
],
providers: [
],
bootstrap: [
AppComponent
]
})
export class AppModule {
}
platformBrowserDynamic().bootstrapModule(AppModule)
export declare class StorageService {
sessionStorage: Storage;
localStorage: Storage;
private defaultStorageType;
private storages;
constructor();
setDefaultStorageType(storageType: StorageType): void;
get({pool, key, storageType}: {
pool?: string;
key: string;
storageType?: StorageType;
}): Object;
put({pool, key, storageType}: {
pool?: string;
key: string;
storageType?: StorageType;
}, value: Object): any;
remove({pool, key, storageType}: {
pool?: string;
key?: string;
storageType?: StorageType;
}): any;
removeAll({storageType}: {
storageType?: StorageType;
}): any;
}
StorageType can be memory, sessionStorage, localStorage. Also can storage Rx, promise and normal object.
import { Jsonp } from '@angular/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { Cacheable, StorageType } from 'rebirth-common';
import config from 'config';
@Injectable()
export class QuestionService {
constructor(private jsonp: Jsonp) {
}
@Cacheable({ pool: 'question' })
getQuestions(): Observable<any> {
return this.jsonp
.get(config.question.url)
.map(res => res.json().data);
}
}
API:
export declare function Cacheable({pool, key, storageType}?: {
pool?: string;
key?: string;
storageType?: StorageType;
}): (target: any, name: string, methodInfo: any) => {
value: (...args: any[]) => any;
};
npm install -g typescript karma
npm install
npm run build
npm test
npm run docs