Skip to content

Commit 51373f9

Browse files
fix: add cookies module
1 parent e826ce5 commit 51373f9

10 files changed

+125
-0
lines changed

package-lock.json

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
"@types/express": "^4.16.0",
7777
"@types/fs-extra": "^5.0.4",
7878
"@types/jest": "^23.1.6",
79+
"@types/js-cookie": "^2.1.0",
7980
"@types/lru-cache": "^4.1.1",
8081
"@types/node": "^10.5.2",
8182
"@types/object-hash": "^1.2.0",
@@ -99,6 +100,7 @@
99100
"jest-junit-reporter": "^1.1.0",
100101
"jest-preset-angular": "^5.2.3",
101102
"jest-zone-patch": "0.0.8",
103+
"js-cookie": "^2.2.0",
102104
"lru-cache": "^4.1.3",
103105
"ng2-fused": "^0.5.1",
104106
"node-sass": "^4.9.2",

src/modules/cookies/common.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { Observable } from 'rxjs'
2+
import { CookieAttributes } from 'js-cookie'
3+
4+
export interface ICookieService {
5+
readonly valueChanges: Observable<StringDict>
6+
readonly getAll: () => any
7+
readonly get: (name: string) => any
8+
readonly set: (name: string, value: any, options?: CookieAttributes) => void
9+
readonly remove: (name: string, options?: CookieAttributes) => void
10+
}
11+
12+
export interface StringDict {
13+
readonly [key: string]: any
14+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { NgModule } from '@angular/core'
2+
import { CookieService } from './cookies.browser.service'
3+
4+
// tslint:disable-next-line:no-class
5+
@NgModule({
6+
providers: [CookieService]
7+
})
8+
export class CookiesBrowserModule {}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { Injectable } from '@angular/core'
2+
import { Subject } from 'rxjs'
3+
import { CookieAttributes, getJSON, remove, set } from 'js-cookie'
4+
import { ICookieService, StringDict } from './common'
5+
6+
// tslint:disable:no-this
7+
// tslint:disable-next-line:no-class
8+
@Injectable()
9+
export class CookieService implements ICookieService {
10+
private readonly cookieSource = new Subject<StringDict>()
11+
public readonly valueChanges = this.cookieSource.asObservable()
12+
13+
public set(name: string, value: any, opts?: CookieAttributes): void {
14+
set(name, value, opts)
15+
this.updateSource()
16+
}
17+
18+
public remove(name: string, opts?: CookieAttributes): void {
19+
remove(name, opts)
20+
this.updateSource()
21+
}
22+
23+
public get(name: string): any {
24+
return getJSON(name)
25+
}
26+
27+
public getAll(): any {
28+
return getJSON()
29+
}
30+
31+
private updateSource() {
32+
this.cookieSource.next(this.getAll())
33+
}
34+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { NgModule } from '@angular/core'
2+
import { CookieService } from './cookies.browser.service'
3+
import { ServerCookieService } from './cookies.server.service'
4+
5+
// tslint:disable-next-line:no-class
6+
@NgModule({
7+
providers: [{ provide: CookieService, useClass: ServerCookieService }]
8+
})
9+
export class CookiesServerModule {}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { empty } from 'rxjs'
2+
import { REQUEST } from '@nguniversal/express-engine/tokens'
3+
import { Inject, Injectable } from '@angular/core'
4+
import { CookieAttributes } from 'js-cookie'
5+
import { ICookieService } from './common'
6+
import * as express from 'express'
7+
8+
// tslint:disable:no-this
9+
// tslint:disable-next-line:no-class
10+
@Injectable()
11+
export class ServerCookieService implements ICookieService {
12+
public readonly valueChanges = empty()
13+
14+
constructor(@Inject(REQUEST) private req: express.Request) {}
15+
16+
public get(name: string): any {
17+
try {
18+
return JSON.parse(this.req.cookies[name])
19+
} catch (err) {
20+
return this.req ? this.req.cookies[name] : undefined
21+
}
22+
}
23+
24+
public getAll(): any {
25+
return this.req && this.req.cookies
26+
}
27+
28+
public set(name: string, value: any, opts?: CookieAttributes): void {
29+
// noop
30+
}
31+
32+
public remove(name: string, opts?: CookieAttributes): void {
33+
// noop
34+
}
35+
36+
updateSource() {
37+
// noop
38+
}
39+
}

src/modules/cookies/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export { CookieService } from './cookies.browser.service'
2+
export { ServerCookieService } from './cookies.server.service'
3+
export { CookiesServerModule } from './cookies.server.module'
4+
export { CookiesBrowserModule } from './cookies.browser.module'
5+
export { ICookieService } from './common'

src/templates/core/browser/app.browser.module.ts.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ import { BrowserTransferStateModule, BrowserModule } from '@angular/platform-bro
44
import { AppComponent } from '../app/app.component'
55
import { EnvironmentBrowserModule } from 'fusing-angular-cli/.build/modules/src/modules/environment'
66
import { WindowBrowserModule } from 'fusing-angular-cli/.build/modules/src/modules'
7+
import { CookiesBrowserModule } from 'fusing-angular-cli/.build/modules/src/modules/cookies'
78

89
@NgModule({
910
imports: [
1011
BrowserModule.withServerTransition({ appId: 'app-root' }),
1112
BrowserTransferStateModule,
1213
WindowBrowserModule.forRoot(),
14+
CookiesBrowserModule,
1315
EnvironmentBrowserModule,
1416
AppModule
1517
],

src/templates/core/server/server.angular.module.ts.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ import { AppModule } from '../app/app.module'
44
import { AppComponent } from '../app/app.component'
55
import { WindowServerModule } from 'fusing-angular-cli/.build/modules/src/modules'
66
import { EnvironmentServerModule } from 'fusing-angular-cli/.build/modules/src/modules/environment'
7+
import { CookiesServerModule } from 'fusing-angular-cli/.build/modules/src/modules/cookies'
78

89
@NgModule({
910
imports: [
1011
ServerTransferStateModule,
1112
WindowServerModule.forRoot({}),
13+
CookiesServerModule,
1214
EnvironmentServerModule,
1315
ServerModule,
1416
AppModule

0 commit comments

Comments
 (0)