-
Notifications
You must be signed in to change notification settings - Fork 123
Expand file tree
/
Copy pathroute.ts
More file actions
66 lines (58 loc) · 1.71 KB
/
Copy pathroute.ts
File metadata and controls
66 lines (58 loc) · 1.71 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
import {
SendNotificationRequest,
sendNotificationResponseSchema,
} from "@farcaster/frame-sdk";
import { NextRequest } from "next/server";
import { z } from "zod";
const requestSchema = z.object({
token: z.string(),
url: z.string(),
targetUrl: z.string(),
});
export async function POST(request: NextRequest) {
const requestJson = await request.json();
const requestBody = requestSchema.safeParse(requestJson);
if (requestBody.success === false) {
return Response.json(
{ success: false, errors: requestBody.error.errors },
{ status: 400 }
);
}
const response = await fetch(requestBody.data.url, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
notificationId: crypto.randomUUID(),
title: "Hello from Frames v2!",
body: "This is a test notification",
targetUrl: requestBody.data.targetUrl,
tokens: [requestBody.data.token],
} satisfies SendNotificationRequest),
});
const responseJson = await response.json();
if (response.status === 200) {
// Ensure correct response
const responseBody = sendNotificationResponseSchema.safeParse(responseJson);
if (responseBody.success === false) {
return Response.json(
{ success: false, errors: responseBody.error.errors },
{ status: 500 }
);
}
// Fail when rate limited
if (responseBody.data.result.rateLimitedTokens.length) {
return Response.json(
{ success: false, error: "Rate limited" },
{ status: 429 }
);
}
return Response.json({ success: true });
} else {
return Response.json(
{ success: false, error: responseJson },
{ status: 500 }
);
}
}