diff --git a/README.md b/README.md index 640db57..905ea3c 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,7 @@ you will get type-safe mocks and you can at least be sure you will not have - `CSS` — no need to do anything, mock object is already injected as if you were using Internet Explorer - `LOCATION` — see _special cases_ below - `LOCAL_STORAGE` — add `UNIVERSAL_LOCAL_STORAGE` for a `Map` based mock for `window.localStorage` +- `SESSION_STORAGE` — add `UNIVERSAL_SESSION_STORAGE` for a `Map` based mock for `window.sessionStorage` - `PAGE_VISIBILITY` — no need to do anything You can also provide all the tokens at once with `UNIVERSAL_TOKENS` constant diff --git a/package-lock.json b/package-lock.json index 5aa758e..81eb72b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -888,9 +888,9 @@ } }, "@ng-web-apis/common": { - "version": "1.4.0", - "resolved": "https://registry.npmjs.org/@ng-web-apis/common/-/common-1.4.0.tgz", - "integrity": "sha512-ZH8OMyw4/6DJ1wuc6Egtn5Lep2R75J/NBifHJVpVJlX2MFFlTQ24qX7H2cC4l0ne7iavHjxoScqOAwv9kBI68A==", + "version": "1.6.0", + "resolved": "https://registry.npmjs.org/@ng-web-apis/common/-/common-1.6.0.tgz", + "integrity": "sha512-rayzzFOvgXe2rTfy3WrYj+ZWA9hPMsq1vplmM3uknbB/TFKMIXdkQgzNLUP8cV8kpwHWNiN0TELKGboyEDelbw==", "requires": { "tslib": "^1.9.0" } diff --git a/package.json b/package.json index b2cfd8a..a9dddbd 100644 --- a/package.json +++ b/package.json @@ -32,9 +32,7 @@ "name": "Alex Inkin", "email": "alexander@inkin.ru" }, - "contributors": [ - "Roman Sedov <79601794011@ya.ru> (http://marsibarsi.me/)" - ], + "contributors": ["Roman Sedov <79601794011@ya.ru> (http://marsibarsi.me/)"], "repository": "https://github.com/ng-web-apis/universal", "bugs": "https://github.com/ng-web-apis/universal/issues", "homepage": "https://github.com/ng-web-apis/universal#README", @@ -53,7 +51,7 @@ "@nguniversal/common": "^7.1.1", "@nguniversal/express-engine": "^7.1.1", "@nguniversal/module-map-ngfactory-loader": "^7.1.1", - "@ng-web-apis/common": "1.4.0", + "@ng-web-apis/common": "latest", "core-js": "^2.5.4", "rxjs": "^6.5.2", "tslib": "^1.10.0", @@ -104,10 +102,7 @@ } }, "lint-staged": { - "*.{js,ts,html,md,less,json}": [ - "prettier --write", - "git add" - ], + "*.{js,ts,html,md,less,json}": ["prettier --write", "git add"], "*.ts": "tslint" }, "standard-version": { diff --git a/projects/universal/package.json b/projects/universal/package.json index 8e8c336..b842c24 100644 --- a/projects/universal/package.json +++ b/projects/universal/package.json @@ -3,7 +3,7 @@ "version": "1.5.0", "peerDependencies": { "@angular/core": ">=6.0.0", - "@ng-web-apis/common": "1.4.0", + "@ng-web-apis/common": "1.6.0", "@types/node": ">=10.0.0", "rxjs": ">=6.0.0" }, diff --git a/projects/universal/src/classes/storage-mock.ts b/projects/universal/src/classes/storage-mock.ts new file mode 100644 index 0000000..481002b --- /dev/null +++ b/projects/universal/src/classes/storage-mock.ts @@ -0,0 +1,27 @@ +export class StorageMock implements Storage { + private readonly storage = new Map(); + + get length(): number { + return this.storage.size; + } + + getItem(key: string): string | null { + return this.storage.has(key) ? this.storage.get(key)! : null; + } + + setItem(key: string, value: string) { + this.storage.set(key, value); + } + + clear() { + this.storage.clear(); + } + + key(index: number): string | null { + return index < this.storage.size ? [...this.storage.keys()][index] : null; + } + + removeItem(key: string): void { + this.storage.delete(key); + } +} diff --git a/projects/universal/src/constants/universal-local-storage.ts b/projects/universal/src/constants/universal-local-storage.ts index 8c7fd82..7bc4032 100644 --- a/projects/universal/src/constants/universal-local-storage.ts +++ b/projects/universal/src/constants/universal-local-storage.ts @@ -1,33 +1,6 @@ import {ClassProvider} from '@angular/core'; import {LOCAL_STORAGE} from '@ng-web-apis/common'; - -export class StorageMock implements Storage { - private readonly storage = new Map(); - - get length(): number { - return this.storage.size; - } - - getItem(key: string): string | null { - return this.storage.has(key) ? this.storage.get(key)! : null; - } - - setItem(key: string, value: string) { - this.storage.set(key, value); - } - - clear() { - this.storage.clear(); - } - - key(index: number): string | null { - return index < this.storage.size ? [...this.storage.keys()][index] : null; - } - - removeItem(key: string): void { - this.storage.delete(key); - } -} +import {StorageMock} from '../classes/storage-mock'; export const UNIVERSAL_LOCAL_STORAGE: ClassProvider = { provide: LOCAL_STORAGE, diff --git a/projects/universal/src/constants/universal-session-storage.ts b/projects/universal/src/constants/universal-session-storage.ts new file mode 100644 index 0000000..b3201a7 --- /dev/null +++ b/projects/universal/src/constants/universal-session-storage.ts @@ -0,0 +1,8 @@ +import {ClassProvider} from '@angular/core'; +import {SESSION_STORAGE} from '@ng-web-apis/common'; +import {StorageMock} from '../classes/storage-mock'; + +export const UNIVERSAL_SESSION_STORAGE: ClassProvider = { + provide: SESSION_STORAGE, + useClass: StorageMock, +}; diff --git a/projects/universal/src/constants/universal-tokens.ts b/projects/universal/src/constants/universal-tokens.ts index 9625bf8..1a01662 100644 --- a/projects/universal/src/constants/universal-tokens.ts +++ b/projects/universal/src/constants/universal-tokens.ts @@ -4,11 +4,13 @@ import {UNIVERSAL_LOCAL_STORAGE} from './universal-local-storage'; import {UNIVERSAL_LOCATION} from './universal-location'; import {UNIVERSAL_NAVIGATOR} from './universal-navigator'; import {UNIVERSAL_PERFORMANCE} from './universal-performance'; +import {UNIVERSAL_SESSION_STORAGE} from './universal-session-storage'; import {UNIVERSAL_USER_AGENT} from './universal-user-agent'; export const UNIVERSAL_TOKENS: Provider[] = [ UNIVERSAL_ANIMATION_FRAME, UNIVERSAL_LOCAL_STORAGE, + UNIVERSAL_SESSION_STORAGE, UNIVERSAL_LOCATION, UNIVERSAL_NAVIGATOR, UNIVERSAL_PERFORMANCE, diff --git a/projects/universal/src/public-api.ts b/projects/universal/src/public-api.ts index 63f49c7..6ad6302 100644 --- a/projects/universal/src/public-api.ts +++ b/projects/universal/src/public-api.ts @@ -7,6 +7,7 @@ export * from './constants/universal-local-storage'; export * from './constants/universal-location'; export * from './constants/universal-navigator'; export * from './constants/universal-performance'; +export * from './constants/universal-session-storage'; export * from './constants/universal-tokens'; export * from './constants/universal-user-agent';