migrate from hono

This commit is contained in:
JCEEE
2026-08-17 07:41:26 +01:00
parent e2b99c45b6
commit 5204e7bdbc
56 changed files with 1815 additions and 3357 deletions
+35 -44
View File
@@ -1,42 +1,40 @@
import { PROXY_URL } from '$app/env/public';
import { pbAdmin } from '$lib/server/pocketbase';
import { pbAdmin, createPbClient } from '$lib/server/pocketbase';
import { createServices, type ChatActor } from '$lib/server/services';
const HONO_URL = PROXY_URL;
async function paydayCheck(famId: string, headers: Record<string, string>) {
async function paydayCheck(famId: string, pbToken: string) {
try {
const res = await fetch(`${HONO_URL}/api/fam/${famId}/payday`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
...headers
}
});
// Best-effort: never block render on the payday heartbeat.
await res.json().catch(() => null);
const s = createServices(createPbClient(pbToken));
await s.fam.payday(famId);
} catch {}
}
async function resolveChatIdentity(
api: 'admin' | 'member',
opts: {
session?: { famId: string; id: string };
pbToken?: string;
}
) {
function actorFrom(session: {
famId: string;
id: string;
role: string;
name?: string;
color?: string;
}): ChatActor {
return {
id: session.id,
type: session.role === 'parent' ? 'admin' : 'member',
name: session.name || '',
color: session.color || '#6366f1'
};
}
async function resolveChatIdentity(session: {
famId: string;
id: string;
role: string;
name?: string;
color?: string;
}, pbToken: string) {
try {
const headers: Record<string, string> = { 'Content-Type': 'application/json' };
if (api === 'admin' && opts.session) {
headers['x-session-famid'] = opts.session.famId;
headers['x-session-userid'] = opts.session.id;
} else if (api === 'member' && opts.pbToken) {
headers['Authorization'] = `Bearer ${opts.pbToken}`;
} else {
return null;
}
const res = await fetch(`${HONO_URL}/api/chat/me`, { headers });
if (!res.ok) return null;
return await res.json();
const s = createServices(createPbClient(pbToken));
const actor = actorFrom(session);
return await s.chat.me(session.famId, actor);
} catch {
return null;
}
@@ -49,19 +47,12 @@ export async function load(event) {
const pbToken = event.cookies.get('pb_token') || '';
let famId = '';
let chat: { famId: string; actor: any } | null = null;
let chat: { famId: string; actor: ChatActor } | null = null;
if (session && isParent) {
if (session && pbToken) {
famId = session.famId;
await paydayCheck(famId, {
'x-session-famid': session.famId,
'x-session-userid': session.id
});
chat = await resolveChatIdentity('admin', { session });
} else if (role === 'child' && pbToken && session) {
famId = session.famId;
await paydayCheck(famId, { Authorization: `Bearer ${pbToken}` });
chat = await resolveChatIdentity('member', { pbToken });
await paydayCheck(famId, pbToken);
chat = await resolveChatIdentity(session, pbToken);
}
return {
@@ -85,4 +76,4 @@ export async function load(event) {
? await pbAdmin.getOne('fams', famId).catch(() => null)
: null
};
}
}