Repository navigation
Expand file tree
/
Copy pathnotify.ts
More file actions
95 lines (76 loc) · 3.22 KB
/
Copy pathnotify.ts
File metadata and controls
95 lines (76 loc) · 3.22 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
/**
* Notification publish endpoint.
*
* POST /api/notify — validates Clerk JWT, publishes event to Upstash wm:events:notify channel
*
* Authentication: Clerk Bearer token in Authorization header.
* Requires UPSTASH_REDIS_REST_URL + UPSTASH_REDIS_REST_TOKEN 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 { validateBearerToken } from '../server/auth-session';
import { getEntitlements } from '../server/_shared/entitlement-check';
export default async function handler(req: Request): Promise<Response> {
if (isDisallowedOrigin(req)) {
return jsonResponse({ error: 'Origin not allowed' }, 403);
}
const cors = getCorsHeaders(req, 'POST, OPTIONS');
if (req.method === 'OPTIONS') {
return new Response(null, { status: 204, headers: cors });
}
if (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 ent = await getEntitlements(session.userId);
if (!ent || ent.features.tier < 1) {
return jsonResponse({ error: 'pro_required', message: 'Event publishing is available on the Pro plan.', upgradeUrl: 'https://worldmonitor.app/pro' }, 403, cors);
}
let body: { eventType?: unknown; payload?: unknown; severity?: unknown; variant?: unknown };
try {
body = await req.json();
} catch {
return jsonResponse({ error: 'Invalid JSON' }, 400, cors);
}
if (typeof body.eventType !== 'string' || !body.eventType || body.eventType.length > 64) {
return jsonResponse({ error: 'eventType required (string, max 64 chars)' }, 400, cors);
}
if (typeof body.payload !== 'object' || body.payload === null || Array.isArray(body.payload)) {
return jsonResponse({ error: 'payload must be an object' }, 400, cors);
}
const upstashUrl = process.env.UPSTASH_REDIS_REST_URL;
const upstashToken = process.env.UPSTASH_REDIS_REST_TOKEN;
if (!upstashUrl || !upstashToken) {
return jsonResponse({ error: 'Service unavailable' }, 503, cors);
}
const { eventType, payload } = body;
const severity = typeof body.severity === 'string' ? body.severity : 'high';
const variant = typeof body.variant === 'string' ? body.variant : undefined;
const msg = JSON.stringify({
eventType,
payload,
severity,
variant,
publishedAt: Date.now(),
userId: session.userId,
});
const res = await fetch(
`${upstashUrl}/lpush/wm:events:queue/${encodeURIComponent(msg)}`,
{ method: 'POST', headers: { Authorization: `Bearer ${upstashToken}`, 'User-Agent': 'worldmonitor-edge/1.0' } },
);
if (!res.ok) {
return jsonResponse({ error: 'Publish failed' }, 502, cors);
}
return jsonResponse({ ok: true }, 200, cors);
}