Skip to content

TypeScript storage module(memory, sessionStorage, localStorage) and @Cacheable decorator

License

Notifications You must be signed in to change notification settings

greengerong/rebirth-storage

Repository files navigation

@rebirth/rebirth-storage

Build Status dependcy dev dependcy npm version

TypeScript storage module(memory, sessionStorage, localStorage) and @Cacheable decorator

Install

npm install rebirth-storage --save

How to use?

StorageService for storage data

Register StorageService

    import { RebirthStorageModule } from 'rebirth-storage';
    
    @NgModule({
      imports: [
        BrowserModule,
        CommonModule,
        SharedModule.forRoot(),
        RebirthStorageModule
      ],
      declarations: [
        AppComponent
      ],
      providers: [
      ],
      bootstrap: [
        AppComponent
      ]
    })
    export class AppModule {
    }
    
    platformBrowserDynamic().bootstrapModule(AppModule)

rebirth-storage service

    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.

@Cacheable

    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;
};

Build project

run project

npm install -g typescript karma
npm install

build

npm run build

test(tslint, karma)

npm test

API Doc

npm run docs

About

TypeScript storage module(memory, sessionStorage, localStorage) and @Cacheable decorator

Resources

License

Stars

Watchers

Forks

Packages

No packages published