forked from solidjs-community/solid-primitives
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
101 lines (91 loc) · 2.87 KB
/
index.ts
File metadata and controls
101 lines (91 loc) · 2.87 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import { createSignal, createEffect, Signal } from "solid-js";
import { isServer } from "solid-js/web";
import { parseCookie } from "solid-start";
import { useRequest } from "solid-start/server";
export type MaxAgeOptions = {
/**
* The maximum age of the cookie in seconds. Defaults to 1 year.
*/
cookieMaxAge?: number;
};
export type ServerCookieOptions<T = string> = MaxAgeOptions & {
/**
* A function to deserialize the cookie value to be used as signal value
*/
deserialize?: (str: string | undefined) => T;
/**
* A function to serialize the signal value to be used as cookie value
*/
serialize?: (value: T) => string;
};
const YEAR = 365 * 24 * 60 * 60;
/**
* A primitive for creating a cookie that can be accessed isomorphically on the client, or the server
*
* @param name The name of the cookie to be set
* @param options Options for the cookie {@see ServerCookieOptions}
* @return Returns an accessor and setter to manage the user's current theme
*/
export function createServerCookie<T>(
name: string,
options: ServerCookieOptions<T> & {
deserialize: (str: string | undefined) => T;
serialize: (value: T) => string;
},
): Signal<T>;
export function createServerCookie(
name: string,
options?: ServerCookieOptions,
): Signal<string | undefined>;
export function createServerCookie<T>(
name: string,
options?: ServerCookieOptions<T | undefined>,
): Signal<T | undefined> {
const {
deserialize = (v: any) => v as T,
serialize = String,
cookieMaxAge = YEAR,
} = options ?? {};
const [cookie, setCookie] = createSignal(
deserialize(
parseCookie(isServer ? useRequest().request.headers.get("cookie") ?? "" : document.cookie)[
name
],
),
);
createEffect(p => {
const string = serialize(cookie());
if (p !== string) document.cookie = `${name}=${string};max-age=${cookieMaxAge}`;
return string;
});
return [cookie, setCookie];
}
export type Theme = "light" | "dark";
export type UserThemeOptions = MaxAgeOptions & {
/**
* The default theme to be used if the cookie is not set
*/
defaultValue?: Theme;
};
/**
* Composes {@link createServerCookie} to provide a type safe way to store a theme and access it on the server or client.
*
* @param name The name of the cookie to be set
* @param options Options for the cookie {@link UserThemeOptions}
*/
export function createUserTheme(
name: string | undefined,
options: UserThemeOptions & { defaultValue: Theme },
): Signal<Theme>;
export function createUserTheme(
name?: string,
options?: UserThemeOptions,
): Signal<Theme | undefined>;
export function createUserTheme(name = "theme", options?: UserThemeOptions): Signal<any> {
const defaultValue = options?.defaultValue;
return createServerCookie(name, {
...options,
deserialize: str => (str === "light" || str === "dark" ? str : defaultValue),
serialize: String,
});
}