mirror of
https://github.com/neynarxyz/create-farcaster-mini-app.git
synced 2025-11-15 23:58:56 -05:00
This reverts commit b1fdfc19a92241638692d58494f48ce1bb25df74, reversing changes made to b9e2087bd8cd9e8ed7a5862936609b5bf29aa911.
65 lines
1.6 KiB
TypeScript
65 lines
1.6 KiB
TypeScript
import {
|
|
SendNotificationRequest,
|
|
sendNotificationResponseSchema,
|
|
} from "@farcaster/miniapp-sdk";
|
|
import { getUserNotificationDetails } from "~/lib/kv";
|
|
import { APP_URL } from "./constants";
|
|
|
|
type SendMiniAppNotificationResult =
|
|
| {
|
|
state: "error";
|
|
error: unknown;
|
|
}
|
|
| { state: "no_token" }
|
|
| { state: "rate_limit" }
|
|
| { state: "success" };
|
|
|
|
export async function sendMiniAppNotification({
|
|
fid,
|
|
title,
|
|
body,
|
|
}: {
|
|
fid: number;
|
|
title: string;
|
|
body: string;
|
|
}): Promise<SendMiniAppNotificationResult> {
|
|
const notificationDetails = await getUserNotificationDetails(fid);
|
|
if (!notificationDetails) {
|
|
return { state: "no_token" };
|
|
}
|
|
|
|
const response = await fetch(notificationDetails.url, {
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
body: JSON.stringify({
|
|
notificationId: crypto.randomUUID(),
|
|
title,
|
|
body,
|
|
targetUrl: APP_URL,
|
|
tokens: [notificationDetails.token],
|
|
} satisfies SendNotificationRequest),
|
|
});
|
|
|
|
const responseJson = await response.json();
|
|
|
|
if (response.status === 200) {
|
|
const responseBody = sendNotificationResponseSchema.safeParse(responseJson);
|
|
if (responseBody.success === false) {
|
|
// Malformed response
|
|
return { state: "error", error: responseBody.error.errors };
|
|
}
|
|
|
|
if (responseBody.data.result.rateLimitedTokens.length) {
|
|
// Rate limited
|
|
return { state: "rate_limit" };
|
|
}
|
|
|
|
return { state: "success" };
|
|
} else {
|
|
// Error response
|
|
return { state: "error", error: responseJson };
|
|
}
|
|
}
|