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

Commit

Permalink
feat: align type for v1 and v2 users
Browse files Browse the repository at this point in the history
Convert v1 User objects into v2 shape for ease of use

BREAKING CHANGE: User APIs now return v2 user shape

Resolves #36
  • Loading branch information
alex-grover committed Oct 24, 2023
1 parent cad0852 commit 5ecaea3
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 21 deletions.
11 changes: 7 additions & 4 deletions src/server/NeynarClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ import {
CastV1WithViewerContext,
CastWithViewerContext,
convertCasts,
convertUser,
GeneratedSigner,
Notification,
NotificationWithViewerContext,
PendingSigner,
Signer,
User,
UserV1,
UserV1WithViewerContext,
UserWithViewerContext,
} from './types'

Expand Down Expand Up @@ -78,9 +81,9 @@ export default class NeynarClient {
const params = new URLSearchParams({ fid: fid.toString() })
if (viewer) params.set('viewerFid', viewer.toString())
const response = await this.get<{
result: { user: User | UserWithViewerContext }
result: { user: UserV1 | UserV1WithViewerContext }
}>('user', params, 1)
return response.result.user
return convertUser(response.result.user)
}

getUserByUsername(username: string, viewer?: null): Promise<User>
Expand All @@ -92,9 +95,9 @@ export default class NeynarClient {
const params = new URLSearchParams({ username })
if (viewer) params.set('viewerFid', viewer.toString())
const response = await this.get<{
result: { user: User | UserWithViewerContext }
result: { user: UserV1 | UserV1WithViewerContext }
}>('user-by-username', params, 1)
return response.result.user
return convertUser(response.result.user)
}

getFollowingFeed(fid: number, { cursor, limit }: Pagination = {}) {
Expand Down
17 changes: 2 additions & 15 deletions src/server/types/Cast.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Address, Hash } from 'viem'
import { User } from './User'

export type Cast = {
hash: Hash
Expand All @@ -8,21 +9,7 @@ export type Cast = {
parent_author: {
fid: number | null
}
author: {
fid: number
username: string
display_name: string
pfp_url: string
profile: {
bio: {
text: string
}
}
follower_count: number
following_count: number
verifications: Address[]
active_status: 'active' | 'inactive'
}
author: User
text: string
timestamp: string
embeds: Embed[]
Expand Down
65 changes: 63 additions & 2 deletions src/server/types/User.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,31 @@
import { Address } from 'viem'

export type ActiveStatus = 'active' | 'inactive'

export type User = {
fid: number
username: string
display_name: string
pfp_url: string
profile: {
bio: {
text: string
}
}
follower_count: number
following_count: number
verifications: Address[]
active_status: ActiveStatus
}

export type UserWithViewerContext = User & {
viewer_context: {
following: boolean
followed_by: boolean
}
}

export type UserV1 = {
fid: number
username: string
displayName: string
Expand All @@ -16,12 +41,48 @@ export type User = {
followerCount: number
followingCount: number
verifications: Address[]
activeStatus: 'active' | 'inactive'
activeStatus: ActiveStatus
}

export type UserWithViewerContext = User & {
export type UserV1WithViewerContext = UserV1 & {
viewerContext: {
following: boolean
followedBy: boolean
}
}

export function convertUser(user: UserV1): User
export function convertUser(
user: UserV1WithViewerContext,
): UserWithViewerContext
export function convertUser(
userV1: UserV1 | UserV1WithViewerContext,
): User | UserWithViewerContext {
const user: User = {
fid: userV1.fid,
username: userV1.username,
display_name: userV1.displayName,
pfp_url: userV1.pfp.url,
profile: {
bio: {
text: userV1.profile.bio.text,
},
},
follower_count: userV1.followerCount,
following_count: userV1.followingCount,
verifications: userV1.verifications,
active_status: userV1.activeStatus,
}

if ('viewerContext' in userV1) {
return {
...user,
viewer_context: {
following: userV1.viewerContext.following,
followed_by: userV1.viewerContext.followedBy,
},
}
}

return user
}

0 comments on commit 5ecaea3

Please sign in to comment.