Skip to content

Commit

Permalink
Add publishFarcasterAction method
Browse files Browse the repository at this point in the history
Add `publishFarcasterAction` method
  • Loading branch information
Shreyaschorge authored Oct 23, 2024
2 parents 524b69c + 99ec331 commit c1d79c0
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 58 deletions.
68 changes: 55 additions & 13 deletions src/neynar-api/neynar-api-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ import {
ChannelMemberInviteListResponse,
ChannelMemberListResponse,
FollowersResponse,
FarcasterActionReqBodyAction,
} from "./v2/openapi-farcaster";

import {
Expand Down Expand Up @@ -264,7 +265,7 @@ export class NeynarAPIClient {
/**
* @deprecated
* Now deprecated, use v2's `lookupUserByUsernameV2` instead.
*
*
* Retrieves the specified user via their username (if found).
*
* @param {string} username - The username of the user whose information is being retrieved.
Expand Down Expand Up @@ -678,6 +679,48 @@ export class NeynarAPIClient {

// ============ v2 APIs ============

// ------------ Action ------------

/**
* Perform actions on behalf of users across different apps.
*
* This method enables an application to securely communicate and trigger actions in another app on behalf of a mutual user.
* The actions are performed by signing messages using the user's Farcaster signer, ensuring that all actions are authenticated and authorized.
*
* @param {FarcasterActionReqBody} farcasterActionReqBody - The request body containing the action details and the necessary information to perform the action.
*
* @returns {Promise<Object>} A promise that resolves to an object containing the response data.
* The structure of the response can vary depending on the action performed, as defined by the API's response schema by the app performing the action.
*
* @example
* // Example: Perform an action on behalf of a user
* const signerUuid = '19d0c5fd-9b33-4a48-a0e2-bc7b0555baec';
* const base_url = "https://appb.example.com",
* const action = {
* type: "sendMessage",
* payload: {
* "message": "Hello from App A!"
* }
* };
*
* client.publishFarcasterAction(signerUuid, url, action).then(response => {
* console.log('Action Response:', response); // Outputs the response of the action
* });
*
* For more information, refer to the [Neynar documentation](https://docs.neynar.com/docs/farcaster-actions-spec).
*/
public async publishFarcasterAction(
signerUuid: string,
base_url: string,
action: FarcasterActionReqBodyAction
): Promise<Object> {
return await this.clients.v2.publishFarcasterAction(
signerUuid,
base_url,
action
);
}

// ------------ Signer ------------

/**
Expand Down Expand Up @@ -1359,14 +1402,14 @@ export class NeynarAPIClient {
* console.log('Cast Conversation Information:', response); // Displays the detailed structure of the specified cast conversation
* });
*
*
*
* @example
* // Implement above and below 'the fold', generally seen in clients as "show more replies".
* // Fetch first page above the fold:
* client.lookupCastConversation(
* 'https://warpcast.com/rish/0x9288c1',
* CastParamType.Url,
* {
* {
* replyDepth: 2,
* includeChronologicalParentCasts: true,
* fold: 'above',
Expand All @@ -1380,7 +1423,7 @@ export class NeynarAPIClient {
* client.lookupCastConversation(
* 'https://warpcast.com/rish/0x9288c1',
* CastParamType.Url,
* {
* {
* replyDepth: 2,
* includeChronologicalParentCasts: false,
* fold: 'above',
Expand All @@ -1394,7 +1437,7 @@ export class NeynarAPIClient {
* client.lookupCastConversation(
* 'https://warpcast.com/rish/0x9288c1',
* CastParamType.Url,
* {
* {
* replyDepth: 2,
* includeChronologicalParentCasts: false,
* fold: 'below',
Expand All @@ -1404,8 +1447,8 @@ export class NeynarAPIClient {
* }).then(response => {
* console.log('Casts from below the fold:', response.conversation.cast.direct_replies);
* });
*
*
*
*
* // Refer to the Neynar API documentation for more details and advanced options:
* // https://docs.neynar.com/reference/cast-conversation
*/
Expand All @@ -1417,7 +1460,7 @@ export class NeynarAPIClient {
includeChronologicalParentCasts?: boolean;
viewerFid?: number;
sortType?: CastConversationSortType;
fold?: 'above' | 'below';
fold?: "above" | "below";
limit?: number;
cursor?: string;
}
Expand Down Expand Up @@ -3008,7 +3051,8 @@ export class NeynarAPIClient {
*
* For more information, refer to the [Neynar documentation](https://docs.neynar.com/reference/user-channel-memberships).
*/
public async fetchUserChannelMemberships(fid: number,
public async fetchUserChannelMemberships(
fid: number,
options?: {
limit?: number;
cursor?: string;
Expand Down Expand Up @@ -3787,7 +3831,7 @@ export class NeynarAPIClient {
public async fetchBanList(
options: { limit?: number; cursor?: string } = {}
): Promise<BanListResponse> {
const { limit, cursor = '' } = options;
const { limit, cursor = "" } = options;
return await this.clients.v2.fetchBanList({ limit, cursor });
}

Expand All @@ -3807,9 +3851,7 @@ export class NeynarAPIClient {
* For more information, refer to the [Neynar documentation](https://docs.neynar.com/reference/add-ban).
*
*/
public async publishBans(
fids: number[]
): Promise<BanResponse> {
public async publishBans(fids: number[]): Promise<BanResponse> {
return await this.clients.v2.publishBans(fids);
}

Expand Down
137 changes: 92 additions & 45 deletions src/neynar-api/v2/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ import {
ChannelMemberListResponse,
ChannelFollowReqBody,
FollowersResponse,
FarcasterActionReqBody,
FarcasterActionReqBodyAction,
ActionApi,
} from "./openapi-farcaster";
import axios, { AxiosError, AxiosInstance } from "axios";
import { silentLogger, Logger } from "../common/logger";
Expand All @@ -125,6 +128,7 @@ export class NeynarV2APIClient {
private readonly apiKey: string;

public readonly apis: {
action: ActionApi;
signer: SignerApi;
user: UserApi;
cast: CastApi;
Expand Down Expand Up @@ -208,6 +212,7 @@ export class NeynarV2APIClient {
});

this.apis = {
action: new ActionApi(config, undefined, axiosInstance),
signer: new SignerApi(config, undefined, axiosInstance),
user: new UserApi(config, undefined, axiosInstance),
cast: new CastApi(config, undefined, axiosInstance),
Expand Down Expand Up @@ -243,6 +248,54 @@ export class NeynarV2APIClient {
);
}

// ------------ Action ------------

/**
* Perform actions on behalf of users across different apps.
*
* This method enables an application to securely communicate and trigger actions in another app on behalf of a mutual user.
* The actions are performed by signing messages using the user's Farcaster signer, ensuring that all actions are authenticated and authorized.
*
* @param {FarcasterActionReqBody} farcasterActionReqBody - The request body containing the action details and the necessary information to perform the action.
*
* @returns {Promise<Object>} A promise that resolves to an object containing the response data.
* The structure of the response can vary depending on the action performed, as defined by the API's response schema by the app performing the action.
*
* @example
* // Example: Perform an action on behalf of a user
* const signerUuid = '19d0c5fd-9b33-4a48-a0e2-bc7b0555baec';
* const base_url = "https://appb.example.com",
* const action = {
* type: "sendMessage",
* payload: {
* "message": "Hello from App A!"
* }
* };
*
* client.publishFarcasterAction(signerUuid, url, action).then(response => {
* console.log('Action Response:', response); // Outputs the response of the action
* });
*
* For more information, refer to the [Neynar documentation](https://docs.neynar.com/docs/farcaster-actions-spec).
*/
public async publishFarcasterAction(
signerUuid: string,
baseUrl: string,
action: FarcasterActionReqBodyAction
) {
const farcasterActionReqBody: FarcasterActionReqBody = {
signer_uuid: signerUuid,
base_url: baseUrl,
action: action,
};

const response = await this.apis.action.publishFarcasterAction(
this.apiKey,
farcasterActionReqBody
);
return response.data;
}

// ------------ Signer ------------

/**
Expand Down Expand Up @@ -1029,19 +1082,19 @@ export class NeynarV2APIClient {
*
* For more information, refer to the [Neynar documentation](https://docs.neynar.com/reference/user-by-username-v2).
*/
public async lookupUserByUsernameV2(
username: string,
options?: {
viewerFid?: number;
}
): Promise<UserResponse> {
const response = await this.apis.user.userByUsernameV2(
this.apiKey,
username,
options?.viewerFid
);
return response.data;
public async lookupUserByUsernameV2(
username: string,
options?: {
viewerFid?: number;
}
): Promise<UserResponse> {
const response = await this.apis.user.userByUsernameV2(
this.apiKey,
username,
options?.viewerFid
);
return response.data;
}

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

Expand Down Expand Up @@ -1168,7 +1221,7 @@ export class NeynarV2APIClient {
* client.lookupCastConversation(
* 'https://warpcast.com/rish/0x9288c1',
* CastParamType.Url,
* {
* {
* replyDepth: 2,
* includeChronologicalParentCasts: true,
* fold: 'above',
Expand All @@ -1182,7 +1235,7 @@ export class NeynarV2APIClient {
* client.lookupCastConversation(
* 'https://warpcast.com/rish/0x9288c1',
* CastParamType.Url,
* {
* {
* replyDepth: 2,
* includeChronologicalParentCasts: false,
* fold: 'above',
Expand All @@ -1196,7 +1249,7 @@ export class NeynarV2APIClient {
* client.lookupCastConversation(
* 'https://warpcast.com/rish/0x9288c1',
* CastParamType.Url,
* {
* {
* replyDepth: 2,
* includeChronologicalParentCasts: false,
* fold: 'below',
Expand All @@ -1206,7 +1259,7 @@ export class NeynarV2APIClient {
* }).then(response => {
* console.log('Casts from below the fold:', response.conversation.cast.direct_replies);
* });
*
*
* For more information, refer to the [Neynar documentation](https://docs.neynar.com/reference/cast-conversation).
*/
public async lookupCastConversation(
Expand All @@ -1217,7 +1270,7 @@ export class NeynarV2APIClient {
includeChronologicalParentCasts?: boolean;
viewerFid?: number;
sortType?: CastConversationSortType;
fold? : 'above' | 'below';
fold?: "above" | "below";
limit?: number;
cursor?: string;
}
Expand Down Expand Up @@ -3075,21 +3128,21 @@ export class NeynarV2APIClient {
*
* For more information, refer to the [Neynar documentation](https://docs.neynar.com/reference/user-channel-memberships).
*/
public async fetchUserChannelMemberships(
fid: number,
options?: {
limit?: number;
cursor?: string;
}
): Promise<ChannelMemberListResponse> {
const response = await this.apis.channel.userChannelMemberships(
this.apiKey,
fid,
options?.limit,
options?.cursor
);
return response.data;
public async fetchUserChannelMemberships(
fid: number,
options?: {
limit?: number;
cursor?: string;
}
): Promise<ChannelMemberListResponse> {
const response = await this.apis.channel.userChannelMemberships(
this.apiKey,
fid,
options?.limit,
options?.cursor
);
return response.data;
}

// ------------ Storage ------------

Expand Down Expand Up @@ -3932,9 +3985,10 @@ export class NeynarV2APIClient {
*
* For more information, refer to the [Neynar documentation](https://docs.neynar.com/reference/ban-list).
*/
public async fetchBanList(
options?: { limit?: number; cursor: string }
): Promise<BanListResponse> {
public async fetchBanList(options?: {
limit?: number;
cursor: string;
}): Promise<BanListResponse> {
const response = await this.apis.ban.banList(
this.apiKey,
options?.limit,
Expand All @@ -3959,11 +4013,9 @@ export class NeynarV2APIClient {
* For more information, refer to the [Neynar documentation](https://docs.neynar.com/reference/add-ban).
*
*/
public async publishBans(
fids: number[]
): Promise<BanResponse> {
public async publishBans(fids: number[]): Promise<BanResponse> {
const addBanBody = {
fids
fids,
};
const response = await this.apis.ban.addBan(this.apiKey, addBanBody);
return response.data;
Expand All @@ -3984,16 +4036,11 @@ export class NeynarV2APIClient {
*
* For more information, refer to the [Neynar documentation](https://docs.neynar.com/reference/delete-ban).
*/
public async deleteBans(
fids: number[]
): Promise<BanResponse> {
public async deleteBans(fids: number[]): Promise<BanResponse> {
const deleteBanBody = {
fids,
};
const response = await this.apis.ban.deleteBan(
this.apiKey,
deleteBanBody
);
const response = await this.apis.ban.deleteBan(this.apiKey, deleteBanBody);
return response.data;
}

Expand Down

0 comments on commit c1d79c0

Please sign in to comment.