Skip to content

Commit 72a194a

Browse files
[Port dspace-9_x] fix: update js-cookie to v3 and refactor cookie services (#4833) (#4850)
Co-authored-by: Roshan Kumar <theroshansingh89@gmail.com>
1 parent 80c7b33 commit 72a194a

File tree

5 files changed

+90
-32
lines changed

5 files changed

+90
-32
lines changed

package-lock.json

Lines changed: 14 additions & 9 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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@
133133
"http-proxy-middleware": "^2.0.9",
134134
"http-terminator": "^3.2.0",
135135
"isbot": "^5.1.31",
136-
"js-cookie": "2.2.1",
136+
"js-cookie": "3.0.5",
137137
"js-yaml": "^4.1.0",
138138
"json5": "^2.2.3",
139139
"jsonschema": "1.5.0",
@@ -185,7 +185,7 @@
185185
"@types/express": "^4.17.17",
186186
"@types/grecaptcha": "^3.0.9",
187187
"@types/jasmine": "~3.6.0",
188-
"@types/js-cookie": "2.2.6",
188+
"@types/js-cookie": "3.0.6",
189189
"@types/lodash-es": "^4.17.12",
190190
"@types/node": "^18.19.124",
191191
"@typescript-eslint/eslint-plugin": "^7.18.0",
Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
import { Injectable } from '@angular/core';
2-
import {
3-
CookieAttributes,
4-
getJSON,
5-
remove,
6-
set,
7-
} from 'js-cookie';
2+
import Cookies, { CookieAttributes } from 'js-cookie';
83

94
import {
105
CookieService,
@@ -15,20 +10,40 @@ import {
1510
export class ClientCookieService extends CookieService implements ICookieService {
1611

1712
public set(name: string, value: any, options?: CookieAttributes): void {
18-
set(name, value, options);
13+
const toStore = typeof value === 'string' ? value : JSON.stringify(value);
14+
Cookies.set(name, toStore, options);
1915
this.updateSource();
2016
}
2117

2218
public remove(name: string, options?: CookieAttributes): void {
23-
remove(name, options);
19+
Cookies.remove(name, options);
2420
this.updateSource();
2521
}
2622

2723
public get(name: string): any {
28-
return getJSON(name);
24+
const raw = Cookies.get(name);
25+
if (raw === undefined) {
26+
return undefined;
27+
}
28+
try {
29+
return JSON.parse(raw);
30+
} catch {
31+
return raw;
32+
}
2933
}
3034

3135
public getAll(): any {
32-
return getJSON();
36+
const all = Cookies.get();
37+
const parsed: Record<string, any> = {};
38+
39+
Object.entries(all).forEach(([key, value]) => {
40+
try {
41+
parsed[key] = JSON.parse(value);
42+
} catch {
43+
parsed[key] = value;
44+
}
45+
});
46+
47+
return parsed;
3348
}
3449
}

src/app/core/services/cookie.service.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,22 @@ export interface ICookieService {
1919

2020
@Injectable()
2121
export abstract class CookieService implements ICookieService {
22-
protected readonly cookieSource = new Subject<{ readonly [key: string]: any }>();
22+
protected readonly cookieSource = new Subject<{
23+
readonly [key: string]: any;
24+
}>();
25+
2326
public readonly cookies$ = this.cookieSource.asObservable();
2427

25-
public abstract set(name: string, value: any, options?: CookieAttributes): void;
28+
public abstract set(
29+
name: string,
30+
value: any,
31+
options?: CookieAttributes,
32+
): void;
2633

27-
public abstract remove(name: string, options?: CookieAttributes): void;
34+
public abstract remove(
35+
name: string,
36+
options?: CookieAttributes,
37+
): void;
2838

2939
public abstract get(name: string): any;
3040

src/app/core/services/server-cookie.service.ts

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,58 @@ import {
1212

1313
@Injectable()
1414
export class ServerCookieService extends CookieService implements ICookieService {
15-
1615
constructor(@Inject(REQUEST) protected req: any) {
1716
super();
1817
}
1918

20-
public set(name: string, value: any, options?: CookieAttributes): void {
19+
public set(
20+
name: string,
21+
value: any,
22+
options?: CookieAttributes,
23+
): void {
2124
return;
2225
}
2326

24-
public remove(name: string, options?: CookieAttributes): void {
27+
public remove(
28+
name: string,
29+
options?: CookieAttributes,
30+
): void {
2531
return;
2632
}
2733

2834
public get(name: string): any {
35+
if (!this.req || !this.req.cookies) {
36+
return undefined;
37+
}
38+
39+
const raw = this.req.cookies[name];
40+
if (raw === undefined) {
41+
return undefined;
42+
}
43+
2944
try {
30-
return JSON.parse(this.req.cookies[name]);
31-
} catch (err) {
32-
return this.req ? this.req.cookies[name] : undefined;
45+
return JSON.parse(raw);
46+
} catch {
47+
return raw;
3348
}
3449
}
3550

3651
public getAll(): any {
37-
if (this.req) {
38-
return this.req.cookies;
52+
if (!this.req || !this.req.cookies) {
53+
return {};
3954
}
55+
56+
const all = this.req.cookies;
57+
const parsed: Record<string, any> = {};
58+
59+
for (const [key, value] of Object.entries(all)) {
60+
try {
61+
parsed[key] = JSON.parse(value as string);
62+
} catch {
63+
parsed[key] = value;
64+
}
65+
}
66+
67+
return parsed;
4068
}
4169
}

0 commit comments

Comments
 (0)