Skip to content

Commit

Permalink
Add User methods in neynar v1 client
Browse files Browse the repository at this point in the history
  • Loading branch information
Shreyaschorge committed Oct 27, 2023
1 parent c3c9306 commit 7308a8e
Showing 1 changed file with 111 additions and 1 deletion.
112 changes: 111 additions & 1 deletion src/neynar-api/neynar-v1-api/neynar-api-v1-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export class NeynarV1APIClient {
private readonly logger: Logger;

public readonly apis: {
user: UserApi;
cast: CastApi;
follows: FollowsApi;
};
Expand Down Expand Up @@ -69,6 +70,7 @@ export class NeynarV1APIClient {
apiKey: apiKey,
});
this.apis = {
user: new UserApi(config, undefined, axiosInstance),
cast: new CastApi(config, undefined, axiosInstance),
follows: new FollowsApi(config, undefined, axiosInstance),
};
Expand All @@ -89,14 +91,122 @@ export class NeynarV1APIClient {
);
}

// ------------ User ------------

/**
* A list of users in reverse chronological order based on sign up.
* See [Neynar documentation](https://docs.neynar.com/reference/recent-users-v1)
*
*/
public async *fetchRecentUsers(options?: {
viewerFid?: number;
pageSize?: number;
}): AsyncGenerator<User, void, undefined> {
let cursor: string | undefined;

while (true) {
// fetch one page of casts (with refreshed auth if necessary)
const response = await this.apis.user.recentUsers({
viewerFid: options?.viewerFid,
cursor: cursor,
limit: options?.pageSize,
});

yield* response.data.result.users;
// prep for next page
if (response.data.result.next.cursor === null) {
break;
}
cursor = response.data.result.next.cursor;
}
}

/**
* Fetch all likes by a given user.
* See [Neynar documentation](https://docs.neynar.com/reference/user-cast-likes-v1)
*
*/
public async *fetchUserCastLikes(
fid: number,
options?: { viewerFid?: number; pageSize?: number }
): AsyncGenerator<ReactionWithCastMeta, void, undefined> {
let cursor: string | undefined;

while (true) {
// fetch one page of likes
const response = await this.apis.user.userCastLikes({
fid: fid,
viewerFid: options?.viewerFid,
limit: options?.pageSize,
cursor: cursor,
});

yield* response.data.result.likes;

// prep for next page
if (response.data.result.next.cursor === null) {
break;
}
cursor = response.data.result.next.cursor;
}
}

/**
* Gets the specified user via their FID (if found).
* See [Neynar documentation](https://docs.neynar.com/reference/user-v1)
*
*/
public async lookupUserByFid(
fid: number,
viewerFid?: number
): Promise<User | null> {
try {
const response = await this.apis.user.user({ fid, viewerFid });
return response.data.result.user;
} catch (error) {
if (NeynarV1APIClient.isApiErrorResponse(error)) {
if (error.response.status === 404) return null;
}
throw error;
}
}

/**
* Gets the specified user via their username (if found).
* See [Neynar documentation](https://docs.neynar.com/reference/user-by-username-v1)
*
*/
public async lookupUserByUsername(
username: string,
viewerFid?: number
): Promise<User | null> {
const response = await this.apis.user.userByUsername({
username,
viewerFid,
});
// result.user is undefined if not found
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
return response.data.result.user ?? null;
}

/**
* Gets the custody address for the specified user via their username (if found).
* See [Neynar documentation](https://docs.neynar.com/reference/custody-address-v1)
*
*/
public async fetchCustodyAddressForUser(fid: number): Promise<string | null> {
const response = await this.apis.user.custodyAddress({ fid });
return response.data.result.custodyAddress;
}

// ------------ Cast ------------

/**
* Fetches a single cast by its hash.
* See [Neynar documentation](https://docs.neynar.com/reference/cast-v1)
*
*/
public async fetchCast(
public async lookUpCastByHash(
hash: string,
options?: { viewerFid?: number }
): Promise<Cast | null> {
Expand Down

0 comments on commit 7308a8e

Please sign in to comment.