@@ -8,22 +8,19 @@ Example in [@ngx-utils/universal-starter](https://github.com/ngx-utils/universal
8
8
9
9
## Table of contents:
10
10
11
- * [ Prerequisites] ( #prerequisites )
12
- * [ Getting started] ( #getting-started )
13
- * [ Installation] ( #installation )
14
- * [ browser.module.ts] ( #browsermodulets )
15
- * [ server.module.ts] ( #servermodulets )
16
- * [ Cookies options] ( #cookies-options )
17
- * [ API] ( #api )
18
- * [ Example of usage] ( #example-of-usage )
19
- * [ License] ( #license )
11
+ - [ Prerequisites] ( #prerequisites )
12
+ - [ Getting started] ( #getting-started )
13
+ - [ Installation] ( #installation )
14
+ - [ browser.module.ts] ( #browsermodulets )
15
+ - [ server.module.ts] ( #servermodulets )
16
+ - [ Cookies options] ( #cookies-options )
17
+ - [ API] ( #api )
18
+ - [ Example of usage] ( #example-of-usage )
19
+ - [ License] ( #license )
20
20
21
21
## Prerequisites
22
22
23
- This package depends on ` @angular v5.0.0 ` .
24
-
25
- And if you want to manage cookies on server side and you're using express as server you need install:
26
- ` npm i -S cookie-parser @nguniversal/module-map-ngfactory-loader `
23
+ This package depends on ` @angular v9.0.0 ` .
27
24
28
25
## Getting started
29
26
@@ -111,37 +108,37 @@ ServerCookiesModule.forRoot({
111
108
112
109
` CookieService ` has following methods:
113
110
114
- * ` put(key: string, value: string, options?: CookiesOptions): void ` put some value to cookies;
115
- * ` putObject(key: string, value: Object, options?: CookiesOptions): void ` put object value to cookies;
116
- * ` get(key: string): string ` get some value from cookies by ` key ` ;
117
- * ` getObject(key: string): { [key: string]: string } | string ` get object value from cookies by ` key ` ;
118
- * ` getAll(): { [key: string]: string } ` get all cookies ;
119
- * ` remove(key: string, options?: CookiesOptions): void ` remove cookie by ` key ` ;
120
- * ` removeAll(): void ` remove all cookies;
111
+ - ` put(key: string, value: string, options?: CookiesOptions): void ` put some value to cookies;
112
+ - ` putObject(key: string, value: Object, options?: CookiesOptions): void ` put object value to cookies;
113
+ - ` get(key: string): string ` get some value from cookies by ` key ` ;
114
+ - ` getObject(key: string): { [key: string]: string } | string ` get object value from cookies by ` key ` ;
115
+ - ` getAll(): { [key: string]: string } ` get all cookies ;
116
+ - ` remove(key: string, options?: CookiesOptions): void ` remove cookie by ` key ` ;
117
+ - ` removeAll(): void ` remove all cookies;
121
118
122
119
## Example of usage
123
120
124
121
If you're using ` express ` as server then add following code to your ` server.ts ` :
125
122
126
123
``` ts
127
- import { renderModuleFactory } from ' @angular/platform-server' ;
128
- import { provideModuleMap } from ' @nguniversal/module-map-ngfactory-loader' ;
129
- import * as cookieParser from ' cookie-parser' ;
124
+ import { renderModuleFactory } from " @angular/platform-server" ;
125
+ import { provideModuleMap } from " @nguniversal/module-map-ngfactory-loader" ;
126
+ import * as cookieParser from " cookie-parser" ;
130
127
131
- app .use (cookieParser (' Your private token' ));
128
+ app .use (cookieParser (" Your private token" ));
132
129
133
- app .engine (' html' , (_ , options , callback ) => {
130
+ app .engine (" html" , (_ , options , callback ) => {
134
131
renderModuleFactory (AppServerModuleNgFactory , {
135
132
document: template ,
136
133
url: options .req .url ,
137
134
extraProviders: [
138
135
provideModuleMap (LAZY_MODULE_MAP ),
139
136
{
140
- provide: ' REQUEST' ,
137
+ provide: " REQUEST" ,
141
138
useValue: options .req
142
139
},
143
140
{
144
- provide: ' RESPONSE' ,
141
+ provide: " RESPONSE" ,
145
142
useValue: options .req .res
146
143
}
147
144
]
@@ -154,24 +151,24 @@ app.engine('html', (_, options, callback) => {
154
151
Then just import ` CookiesService ` from ` @ngx-utils/cookies ` and use it:
155
152
156
153
``` ts
157
- import { Component , OnInit } from ' @angular/core' ;
158
- import { CookiesService } from ' @ngx-utils/cookies' ;
154
+ import { Component , OnInit } from " @angular/core" ;
155
+ import { CookiesService } from " @ngx-utils/cookies" ;
159
156
160
157
@Component ({
161
- selector: ' app-root' ,
162
- templateUrl: ' ./app.component.html' ,
163
- styleUrls: [' ./app.component.scss' ]
158
+ selector: " app-root" ,
159
+ templateUrl: " ./app.component.html" ,
160
+ styleUrls: [" ./app.component.scss" ]
164
161
})
165
162
export class AppComponent implements OnInit {
166
163
constructor (private cookies : CookiesService ) {}
167
164
168
165
ngOnInit() {
169
- this .cookies .put (' some_cookie' , ' some_cookie' );
170
- this .cookies .put (' http_only_cookie' , ' http_only_cookie' , {
166
+ this .cookies .put (" some_cookie" , " some_cookie" );
167
+ this .cookies .put (" http_only_cookie" , " http_only_cookie" , {
171
168
httpOnly: true
172
169
});
173
- console .log (this .cookies .get (' some_cookie' ), ' => some_cookie' );
174
- console .log (this .cookies .get (' http_only_cookie' ), ' => undefined' );
170
+ console .log (this .cookies .get (" some_cookie" ), " => some_cookie" );
171
+ console .log (this .cookies .get (" http_only_cookie" ), " => undefined" );
175
172
console .log (this .cookies .getAll ());
176
173
}
177
174
}
@@ -189,7 +186,7 @@ app.use(async (ctx: Context) => {
189
186
extraProviders: [
190
187
provideModuleMap (LAZY_MODULE_MAP ),
191
188
{
192
- provide: ' KOA_CONTEXT' ,
189
+ provide: " KOA_CONTEXT" ,
193
190
useValue: ctx
194
191
}
195
192
]
@@ -200,21 +197,21 @@ app.use(async (ctx: Context) => {
200
197
Then create ` server-cookies.service.ts ` :
201
198
202
199
``` ts
203
- import { Context } from ' koa' ;
204
- import { Inject , Injectable } from ' @angular/core' ;
200
+ import { Context } from " koa" ;
201
+ import { Inject , Injectable } from " @angular/core" ;
205
202
import {
206
203
CookiesService ,
207
204
CookiesOptionsService ,
208
205
CookiesOptions
209
- } from ' @ngx-utils/cookies' ;
206
+ } from " @ngx-utils/cookies" ;
210
207
211
208
@Injectable ()
212
209
export class ServerCookiesService extends CookiesService {
213
210
private newCookies: { [name : string ]: string | undefined } = {};
214
211
215
212
constructor (
216
213
cookiesOptions : CookiesOptionsService ,
217
- @Inject (' KOA_CONTEXT' ) private ctx : Context
214
+ @Inject (" KOA_CONTEXT" ) private ctx : Context
218
215
) {
219
216
super (cookiesOptions );
220
217
}
0 commit comments