diff --git a/README.md b/README.md index 78fb504..c13697c 100644 --- a/README.md +++ b/README.md @@ -220,7 +220,11 @@ export async function GET(request: Request, { params }: Props) { const fid = parseInt(params.fid) if (!fid) return new Response('fid is invalid', { status: 400 }) - const user = await neynarClient.getUserByFid(fid) + // You can pass an optional viewer FID to get back the mutual following status as well, for example to display on another user's profile page + // const { searchParams } = new URL(request.url) + // const viewer = searchParams.get('viewer') + + const user = await neynarClient.getUserByFid(fid /*, viewer */) return NextResponse.json(signer) } ``` diff --git a/src/server/NeynarClient.ts b/src/server/NeynarClient.ts index 7d868de..5c0ae38 100644 --- a/src/server/NeynarClient.ts +++ b/src/server/NeynarClient.ts @@ -1,6 +1,13 @@ import { Hash } from 'viem' import { mnemonicToAccount } from 'viem/accounts' -import { Cast, GeneratedSigner, PendingSigner, Signer, User } from './types' +import { + Cast, + GeneratedSigner, + PendingSigner, + Signer, + User, + UserWithViewerContext, +} from './types' type Pagination = { cursor?: string @@ -45,13 +52,14 @@ export default class NeynarClient { }) } - async getUserByFid(fid: number) { + getUserByFid(fid: number, viewer?: null): Promise + getUserByFid(fid: number, viewer: number): Promise + async getUserByFid(fid: number, viewer?: number | null) { const params = new URLSearchParams({ fid: fid.toString() }) - const response = await this.get<{ result: { user: User } }>( - 'user', - params, - 1, - ) + if (viewer) params.set('viewerFid', viewer.toString()) + const response = await this.get<{ + result: { user: User | UserWithViewerContext } + }>('user', params, 1) return response.result.user } diff --git a/src/server/types/User.ts b/src/server/types/User.ts index 0f9cce3..31eb722 100644 --- a/src/server/types/User.ts +++ b/src/server/types/User.ts @@ -17,9 +17,10 @@ export type User = { followingCount: number verifications: Hash[] activeStatus: 'active' | 'inactive' +} - // Only returned if you pass `viewerFid` - viewerContext?: { +export type UserWithViewerContext = User & { + viewerContext: { following: boolean followedBy: boolean }