From b10e6bfad311a0e1cefa77b7f51e6e3a03be15ba Mon Sep 17 00:00:00 2001 From: Alex Grover Date: Sun, 24 Sep 2023 15:20:59 -0700 Subject: [PATCH] feat: allow passing viewer FID to user API Adds a `viewer` parameter to `getUserByFid` and updates the return type to be more accurate BREAKING CHANGE: The return type of `getUserByFid` no longer contains the `viewerContext` field unless the `viewer` parameter is passed. --- README.md | 6 +++++- src/server/NeynarClient.ts | 22 +++++++++++++++------- src/server/types/User.ts | 5 +++-- 3 files changed, 23 insertions(+), 10 deletions(-) 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 }