-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhooks.server.ts
74 lines (64 loc) · 1.86 KB
/
hooks.server.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { dev } from '$app/environment';
import { SaferwallClient } from '$lib/clients/saferwall';
import { SESSION_KEY, THEME_KEY } from '$lib/config';
import { parseTheme } from '$lib/stores/theme';
import type { Saferwall } from '$lib/types';
import type { Handle, HandleServerError } from '@sveltejs/kit';
const resolveEvent: Handle = ({ resolve, event }) => {
const theme = parseTheme(event.cookies.get(THEME_KEY));
return resolve(event, {
transformPageChunk: ({ html }) => {
return html.replace('data-theme', `class="${theme}"`);
}
});
};
export const handle: Handle = (async ({ event, resolve }) => {
if (dev) {
const { fallBackPlatformToMiniFlareInDev } = await import('$lib/clients/miniflare');
event.platform = await fallBackPlatformToMiniFlareInDev(event.platform!);
}
const client = new SaferwallClient(undefined, event.fetch);
event.locals.client = client;
try {
const sessionData = event.cookies.get(SESSION_KEY);
if (!sessionData) {
return await resolveEvent({ event, resolve });
}
const session = JSON.parse(sessionData);
client.setSession(session);
const user = await client.getUser(session.username);
event.locals.session = session;
event.locals.user = [
'username',
'name',
'location',
'url',
'bio',
'confirmed',
'member_since',
'admin',
'email'
].reduce(
(finalUserData, key) => ({
...finalUserData,
[key]: user[key as keyof typeof user]
}),
{} as Saferwall.User
);
} catch (error) {
console.error('hooks error: ', error);
}
return resolveEvent({ event, resolve });
}) satisfies Handle;
export const handleError = (async ({ error, event }: any) => {
event.cookies.delete(SESSION_KEY, {
httpOnly: true,
secure: true,
path: '/'
});
console.error('[hook] handle error', error);
return {
message: error?.message,
status: error?.status
};
}) satisfies HandleServerError;