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
|
import { Redis } from "@upstash/redis";
export const runtime = "edge";
const redis = new Redis({
url: process.env.KV_REST_API_URL!,
token: process.env.KV_REST_API_TOKEN!,
});
const corsHeaders = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, POST, OPTIONS",
"Access-Control-Allow-Headers": "Content-Type, Authorization",
};
export async function OPTIONS() {
return new Response(null, {
status: 204,
headers: corsHeaders,
});
}
// /api/notification/set?topic=<topic>&token=<fcm_token>&action=subscribe|unsubscribe
export async function GET(req: Request) {
const { searchParams } = new URL(req.url);
const topic = searchParams.get("topic");
const token = searchParams.get("token");
const action = searchParams.get("action"); // "subscribe" | "unsubscribe"
if (!topic || !token || !action) {
return new Response(JSON.stringify({ error: "Missing params" }), {
status: 400,
headers: corsHeaders,
});
}
const key = `fcm-${topic}`;
if (action === "subscribe") {
await redis.sadd(key, token);
} else if (action === "unsubscribe") {
await redis.srem(key, token);
} else {
return new Response(JSON.stringify({ error: "Invalid action" }), {
status: 400,
headers: corsHeaders,
});
}
return new Response(JSON.stringify({ success: true }), {
status: 200,
headers: corsHeaders,
});
}
|