mirror of
https://github.com/neynarxyz/create-farcaster-mini-app.git
synced 2025-11-18 17:09:47 -05:00
81 lines
1.9 KiB
TypeScript
81 lines
1.9 KiB
TypeScript
import {
|
|
NeynarAPIClient,
|
|
Configuration,
|
|
WebhookUserCreated,
|
|
} from '@neynar/nodejs-sdk';
|
|
import { APP_URL } from './constants';
|
|
|
|
let neynarClient: NeynarAPIClient | null = null;
|
|
|
|
// Example usage:
|
|
// const client = getNeynarClient();
|
|
// const user = await client.lookupUserByFid(fid);
|
|
export function getNeynarClient() {
|
|
if (!neynarClient) {
|
|
const apiKey = process.env.NEYNAR_API_KEY;
|
|
if (!apiKey) {
|
|
throw new Error('NEYNAR_API_KEY not configured');
|
|
}
|
|
const config = new Configuration({ apiKey });
|
|
neynarClient = new NeynarAPIClient(config);
|
|
}
|
|
return neynarClient;
|
|
}
|
|
|
|
type User = WebhookUserCreated['data'];
|
|
|
|
export async function getNeynarUser(fid: number): Promise<User | null> {
|
|
try {
|
|
const client = getNeynarClient();
|
|
const usersResponse = await client.fetchBulkUsers({ fids: [fid] });
|
|
return usersResponse.users[0];
|
|
} catch (error) {
|
|
console.error('Error getting Neynar user:', error);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
type SendMiniAppNotificationResult =
|
|
| {
|
|
state: 'error';
|
|
error: unknown;
|
|
}
|
|
| { state: 'no_token' }
|
|
| { state: 'rate_limit' }
|
|
| { state: 'success' };
|
|
|
|
export async function sendNeynarMiniAppNotification({
|
|
fid,
|
|
title,
|
|
body,
|
|
}: {
|
|
fid: number;
|
|
title: string;
|
|
body: string;
|
|
}): Promise<SendMiniAppNotificationResult> {
|
|
try {
|
|
const client = getNeynarClient();
|
|
const targetFids = [fid];
|
|
const notification = {
|
|
title,
|
|
body,
|
|
target_url: APP_URL,
|
|
};
|
|
|
|
const result = await client.publishFrameNotifications({
|
|
targetFids,
|
|
notification,
|
|
});
|
|
|
|
if (result.notification_deliveries.length > 0) {
|
|
return { state: 'success' };
|
|
} else if (result.notification_deliveries.length === 0) {
|
|
return { state: 'no_token' };
|
|
} else {
|
|
return { state: 'error', error: result || 'Unknown error' };
|
|
}
|
|
} catch (error) {
|
|
return { state: 'error', error };
|
|
}
|
|
}
|