@@ -2,32 +2,22 @@ import { Injectable } from '@angular/core';
2
2
3
3
import { CookiesOptions } from './cookies-options' ;
4
4
import { CookiesOptionsService } from './cookies-options.service' ;
5
- import { isBlank , isString , mergeOptions , safeDecodeURIComponent , safeJsonParse } from './utils' ;
6
-
7
- export interface ICookiesService {
8
- get ( key : string ) : string ;
9
- getObject ( key : string ) : { [ key : string ] : string } | string ;
10
- getAll ( ) : { [ key : string ] : string } ;
11
- put ( key : string , value : string , options ?: CookiesOptions ) : void ;
12
- putObject ( key : string , value : { [ key : string ] : string } , options ?: CookiesOptions ) : void ;
13
- remove ( key : string , options ?: CookiesOptions ) : void ;
14
- removeAll ( ) : void ;
15
- }
5
+ import { safeJsonParse } from './utils' ;
16
6
17
7
@Injectable ( )
18
- export class CookiesService implements ICookiesService {
8
+ export class CookiesService {
19
9
protected options : CookiesOptions ;
20
10
21
- get cookieString ( ) : string {
22
- return document . cookie || '' ;
11
+ constructor ( cookiesOptions : CookiesOptionsService ) {
12
+ this . options = cookiesOptions . options ;
23
13
}
24
14
25
- set cookieString ( val : string ) {
26
- document . cookie = val ;
15
+ put ( key : string , value : string , options ?: CookiesOptions ) : void {
16
+ this . cookiesWriter ( ) ( key , value , options ) ;
27
17
}
28
18
29
- constructor ( cookiesOptions : CookiesOptionsService ) {
30
- this . options = cookiesOptions . options ;
19
+ putObject ( key : string , value : Object , options ?: CookiesOptions ) : void {
20
+ this . put ( key , JSON . stringify ( value ) , options ) ;
31
21
}
32
22
33
23
get ( key : string ) : string {
@@ -43,14 +33,6 @@ export class CookiesService implements ICookiesService {
43
33
return < any > this . cookiesReader ( ) ;
44
34
}
45
35
46
- put ( key : string , value : string , options ?: CookiesOptions ) : void {
47
- this . cookiesWriter ( ) ( key , value , options ) ;
48
- }
49
-
50
- putObject ( key : string , value : Object , options ?: CookiesOptions ) : void {
51
- this . put ( key , JSON . stringify ( value ) , options ) ;
52
- }
53
-
54
36
remove ( key : string , options ?: CookiesOptions ) : void {
55
37
this . cookiesWriter ( ) ( key , undefined , options ) ;
56
38
}
@@ -63,54 +45,10 @@ export class CookiesService implements ICookiesService {
63
45
}
64
46
65
47
protected cookiesReader ( ) : { [ key : string ] : any } {
66
- let lastCookies = { } ;
67
- let lastCookieString = '' ;
68
- let cookiesArray : string [ ] , cookie : string , i : number , index : number , name : string ;
69
- let currentCookieString = this . cookieString ;
70
- if ( currentCookieString !== lastCookieString ) {
71
- lastCookieString = currentCookieString ;
72
- cookiesArray = lastCookieString . split ( '; ' ) ;
73
- lastCookies = { } ;
74
- for ( i = 0 ; i < cookiesArray . length ; i ++ ) {
75
- cookie = cookiesArray [ i ] ;
76
- index = cookie . indexOf ( '=' ) ;
77
- if ( index > 0 ) { // ignore nameless cookies
78
- name = safeDecodeURIComponent ( cookie . substring ( 0 , index ) ) ;
79
- if ( isBlank ( ( < any > lastCookies ) [ name ] ) ) {
80
- ( < any > lastCookies ) [ name ] = safeDecodeURIComponent ( cookie . substring ( index + 1 ) ) ;
81
- }
82
- }
83
- }
84
- }
85
- return lastCookies ;
48
+ return { } ;
86
49
}
87
50
88
51
protected cookiesWriter ( ) : ( name : string , value : string | undefined , options ?: CookiesOptions ) => void {
89
- return ( name : string , value : string | undefined , options ?: CookiesOptions ) => {
90
- this . cookieString = this . buildCookieString ( name , value , options ) ;
91
- } ;
92
- }
93
-
94
- private buildCookieString ( name : string , value : string | undefined , options ?: CookiesOptions ) : string {
95
- let opts : CookiesOptions = mergeOptions ( this . options , options ) ;
96
- let expires : any = opts . expires ;
97
- if ( isBlank ( value ) ) {
98
- expires = 'Thu, 01 Jan 1970 00:00:00 GMT' ;
99
- value = '' ;
100
- }
101
- if ( isString ( expires ) ) {
102
- expires = new Date ( expires ) ;
103
- }
104
- let str = encodeURIComponent ( name ) + '=' + encodeURIComponent ( ( value as string ) ) ;
105
- str += opts . path ? ';path=' + opts . path : '' ;
106
- str += opts . domain ? ';domain=' + opts . domain : '' ;
107
- str += expires ? ';expires=' + expires . toUTCString ( ) : '' ;
108
- str += opts . secure ? ';secure' : '' ;
109
- let cookiesLength = str . length + 1 ;
110
- if ( cookiesLength > 4096 ) {
111
- console . log ( `Cookie \'${ name } \' possibly not set or overflowed because it was too
112
- large (${ cookiesLength } > 4096 bytes)!` ) ;
113
- }
114
- return str ;
52
+ return ( ) => { } ;
115
53
}
116
54
}
0 commit comments