Skip to content
This repository has been archived by the owner on Jan 18, 2024. It is now read-only.

Commit

Permalink
feat: allow passing viewer FID to user API
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
alex-grover committed Sep 24, 2023
1 parent 9218481 commit b10e6bf
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
```
Expand Down
22 changes: 15 additions & 7 deletions src/server/NeynarClient.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -45,13 +52,14 @@ export default class NeynarClient {
})
}

async getUserByFid(fid: number) {
getUserByFid(fid: number, viewer?: null): Promise<User>
getUserByFid(fid: number, viewer: number): Promise<UserWithViewerContext>
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
}

Expand Down
5 changes: 3 additions & 2 deletions src/server/types/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit b10e6bf

Please sign in to comment.