Skip to content

Commit

Permalink
Add methods for notifications and reactions
Browse files Browse the repository at this point in the history
Feature/client methods for notifications and reactions
  • Loading branch information
Shreyaschorge authored Oct 30, 2023
2 parents 9c9b888 + 915a915 commit ad376f8
Show file tree
Hide file tree
Showing 9 changed files with 1,168 additions and 532 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@neynar/nodejs-sdk",
"version": "0.3.2",
"version": "0.4.0",
"description": "SDK to interact with Neynar APIs (https://docs.neynar.com/)",
"main": "./build/index.js",
"types": "./build/index.d.ts",
Expand Down
177 changes: 176 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 @@ -29,6 +29,8 @@ export class NeynarV1APIClient {
cast: CastApi;
follows: FollowsApi;
verification: VerificationApi;
notifications: NotificationsApi;
reactions: ReactionsApi;
};

/**
Expand Down Expand Up @@ -75,6 +77,8 @@ export class NeynarV1APIClient {
cast: new CastApi(config, undefined, axiosInstance),
follows: new FollowsApi(config, undefined, axiosInstance),
verification: new VerificationApi(config, undefined, axiosInstance),
notifications: new NotificationsApi(config, undefined, axiosInstance),
reactions: new ReactionsApi(config, undefined, axiosInstance),
};
}

Expand Down Expand Up @@ -335,7 +339,178 @@ export class NeynarV1APIClient {
}
}

// ------------ Follow ------------
// ------------ Notifications ------------

/**
* Gets a list of mentions and replies to the user’s casts in reverse chronological order.
* See [Neynar documentation](https://docs.neynar.com/reference/mentions-and-replies-v1)
*
*/
public async *fetchMentionAndReplyNotifications(
fid: number,
options?: { viewerFid?: number; pageSize?: number }
): AsyncGenerator<Cast, void, undefined> {
let cursor: string | undefined;

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

// yield current page
yield* response.data.result.notifications;

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

/**
* Get a list of likes and recasts to the users’s casts in reverse chronological order.
* See [Neynar documentation](https://docs.neynar.com/reference/reactions-and-recasts-v1)
*
*/
public async *fetchUserLikesAndRecasts(
fid: number,
options?: { viewerFid?: number; pageSize?: number }
): AsyncGenerator<ReactionsAndRecastsNotification, void, undefined> {
let cursor: string | undefined;

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

// yield current page
yield* response.data.result.notifications;

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

// ------------ Reactions ------------

/**
* Lists a given cast's likes.
* See [Neynar documentation](https://docs.neynar.com/reference/cast-likes-v1)
*
*/
public async *fetchCastLikes(
castOrCastHash: Cast | string,
options?: { viewerFid?: number; pageSize?: number }
): AsyncGenerator<Reaction, void, undefined> {
let cursor: string | undefined;
let castHash: string;
if (typeof castOrCastHash === "string") {
castHash = castOrCastHash;
} else {
castHash = castOrCastHash.hash;
}

while (true) {
const response = await this.apis.reactions.castLikes({
castHash: castHash,
viewerFid: options?.viewerFid,
cursor: cursor,
limit: options?.pageSize,
});

yield* response.data.result.likes;

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

/**
* Get All Reactions For a Cast.
* See [Neynar documentation](https://docs.neynar.com/reference/cast-reactions-v1)
*
*/
public async *fetchCastReactions(
castOrCastHash: Cast | string,
options?: { viewerFid?: number; pageSize?: number }
): AsyncGenerator<Reaction, void, undefined> {
let cursor: string | undefined;
let castHash: string;
if (typeof castOrCastHash === "string") {
castHash = castOrCastHash;
} else {
castHash = castOrCastHash.hash;
}

while (true) {
const response = await this.apis.reactions.castReactions({
castHash: castHash,
viewerFid: options?.viewerFid,
cursor: cursor,
limit: options?.pageSize,
});

yield* response.data.result.casts;

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

/**
* Get the list of users who have recasted a specific cast.
* See [Neynar documentation](https://docs.neynar.com/reference/cast-recasters-v1)
*
*/
public async *fetchRecasters(
castOrCastHash: Cast | string,
options?: { viewerFid?: number; pageSize?: number }
): AsyncGenerator<Recaster, void, undefined> {
let cursor: string | undefined;
let castHash: string;
if (typeof castOrCastHash === "string") {
castHash = castOrCastHash;
} else {
castHash = castOrCastHash.hash;
}

while (true) {
const response = await this.apis.reactions.castRecasters({
castHash: castHash,
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;
}
}

// ------------ Follows ------------

/**
* Get all users that follow the specified user.
Expand Down
2 changes: 1 addition & 1 deletion src/neynar-api/neynar-v1-api/openapi/apis/cast-api.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* tslint:disable */
/* eslint-disable */
/**
* v1 Farcaster
* Farcaster API V1
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
*
* The version of the OpenAPI document: 1.0
Expand Down
2 changes: 1 addition & 1 deletion src/neynar-api/neynar-v1-api/openapi/apis/follows-api.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* tslint:disable */
/* eslint-disable */
/**
* v1 Farcaster
* Farcaster API V1
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
*
* The version of the OpenAPI document: 1.0
Expand Down
Loading

0 comments on commit ad376f8

Please sign in to comment.