-
Notifications
You must be signed in to change notification settings - Fork 5
/
UniversalCookie.ts
53 lines (46 loc) · 1.61 KB
/
UniversalCookie.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { useRequestEvent } from '#imports';
import { parse, serialize, type CookieSerializeOptions } from 'cookie-es';
import { setCookie as _setCookie, deleteCookie } from 'h3';
export default class UniversalCookie {
private requestEvent;
/**
* Since the cookies in the request are not updated until the request is finished.
* We need to cache the cookies in the request to get the latest cookies.
* This is only used in the server side.
*/
private cache: Record<string, string> | null = null;
constructor() {
this.requestEvent = useRequestEvent();
if (import.meta.server && this.requestEvent) {
this.cache = parse(this.requestEvent?.headers?.get('cookie') || '');
}
}
getCookie(name: string): string | undefined {
if (import.meta.server) {
return this.requestEvent ? this.cache![name] : undefined;
}
return parse(document.cookie)[name];
}
setCookie(name: string, value: string | null, options: CookieSerializeOptions = {}) {
if (value == null) {
this.deleteCookie(name, options);
return;
}
if (import.meta.server) {
if (!this.requestEvent) return;
_setCookie(this.requestEvent, name, value, options);
this.cache![name] = value;
} else {
document.cookie = serialize(name, value, options);
}
}
deleteCookie(name: string, options: CookieSerializeOptions = {}) {
if (import.meta.server) {
if (!this.requestEvent) return;
deleteCookie(this.requestEvent, name, options);
delete this.cache![name];
} else {
document.cookie = serialize(name, '', { ...options, maxAge: -1 });
}
}
}