Repository navigation
Expand file tree
/
Copy pathuser-prefs.ts
More file actions
104 lines (89 loc) · 3.55 KB
/
Copy pathuser-prefs.ts
File metadata and controls
104 lines (89 loc) · 3.55 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
102
103
104
/**
* User preferences sync endpoint.
*
* GET /api/user-prefs?variant=<variant> — returns current cloud prefs for signed-in user
* POST /api/user-prefs — saves prefs blob for signed-in user
*
* Authentication: Clerk Bearer token in Authorization header.
* Requires CONVEX_URL + CLERK_JWT_ISSUER_DOMAIN env vars.
*/
export const config = { runtime: 'edge' };
// @ts-expect-error — JS module, no declaration file
import { getCorsHeaders, isDisallowedOrigin } from './_cors.js';
// @ts-expect-error — JS module, no declaration file
import { jsonResponse } from './_json-response.js';
import { ConvexHttpClient } from 'convex/browser';
import { validateBearerToken } from '../server/auth-session';
export default async function handler(req: Request): Promise<Response> {
if (isDisallowedOrigin(req)) {
return jsonResponse({ error: 'Origin not allowed' }, 403);
}
const cors = getCorsHeaders(req, 'GET, POST, OPTIONS');
if (req.method === 'OPTIONS') {
return new Response(null, { status: 204, headers: cors });
}
if (req.method !== 'GET' && req.method !== 'POST') {
return jsonResponse({ error: 'Method not allowed' }, 405, cors);
}
const authHeader = req.headers.get('Authorization') ?? '';
const token = authHeader.startsWith('Bearer ') ? authHeader.slice(7) : '';
if (!token) {
return jsonResponse({ error: 'UNAUTHENTICATED' }, 401, cors);
}
const session = await validateBearerToken(token);
if (!session.valid || !session.userId) {
return jsonResponse({ error: 'UNAUTHENTICATED' }, 401, cors);
}
const convexUrl = process.env.CONVEX_URL;
if (!convexUrl) {
return jsonResponse({ error: 'Service unavailable' }, 503, cors);
}
const client = new ConvexHttpClient(convexUrl);
client.setAuth(token);
if (req.method === 'GET') {
const url = new URL(req.url);
const variant = url.searchParams.get('variant') ?? 'full';
try {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const prefs = await client.query('userPreferences:getPreferences' as any, { variant });
return jsonResponse(prefs ?? null, 200, cors);
} catch (err) {
console.error('[user-prefs] GET error:', err);
return jsonResponse({ error: 'Failed to fetch preferences' }, 500, cors);
}
}
// POST — save prefs
let body: { variant?: unknown; data?: unknown; expectedSyncVersion?: unknown; schemaVersion?: unknown };
try {
body = await req.json();
} catch {
return jsonResponse({ error: 'Invalid JSON' }, 400, cors);
}
if (
typeof body.variant !== 'string' ||
body.data === undefined ||
typeof body.expectedSyncVersion !== 'number'
) {
return jsonResponse({ error: 'MISSING_FIELDS' }, 400, cors);
}
try {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const result = await client.mutation('userPreferences:setPreferences' as any, {
variant: body.variant,
data: body.data,
expectedSyncVersion: body.expectedSyncVersion,
schemaVersion: typeof body.schemaVersion === 'number' ? body.schemaVersion : undefined,
});
return jsonResponse(result, 200, cors);
} catch (err: unknown) {
const msg = err instanceof Error ? err.message : String(err);
if (msg.includes('CONFLICT')) {
return jsonResponse({ error: 'CONFLICT' }, 409, cors);
}
if (msg.includes('BLOB_TOO_LARGE')) {
return jsonResponse({ error: 'BLOB_TOO_LARGE' }, 400, cors);
}
console.error('[user-prefs] POST error:', err);
return jsonResponse({ error: 'Failed to save preferences' }, 500, cors);
}
}