diff --git a/lib/types/farcaster-types.js b/lib/types/farcaster-types.js --- a/lib/types/farcaster-types.js +++ b/lib/types/farcaster-types.js @@ -42,6 +42,14 @@ ... }; +export type NeynarCast = { + +hash: string, + +thread_hash: string, + +text: string, + +author: NeynarUser, + ... +}; + export type NeynarWebhookCastAuthor = { +object: 'user', +fid: string, diff --git a/lib/utils/neynar-client.js b/lib/utils/neynar-client.js --- a/lib/utils/neynar-client.js +++ b/lib/utils/neynar-client.js @@ -3,7 +3,11 @@ import invariant from 'invariant'; import { getMessageForException } from './errors.js'; -import type { NeynarChannel, NeynarUser } from '../types/farcaster-types.js'; +import type { + NeynarChannel, + NeynarUser, + NeynarCast, +} from '../types/farcaster-types.js'; type FetchRelevantFollowersResponse = { +all_relevant_followers_dehydrated: $ReadOnlyArray<{ @@ -31,6 +35,10 @@ +channel: NeynarChannel, }; +type FetchFarcasterCastByHashResponse = { + +cast: NeynarCast, +}; + export type FarcasterUser = { +username: string, +pfpURL: string, @@ -325,6 +333,30 @@ throw error; } } + + async fetchFarcasterCastByHash(hash: string): Promise { + const url = getNeynarURL('2', 'cast', { identifier: hash, type: 'hash' }); + + try { + const response = await fetch(url, { + method: 'GET', + headers: { + Accept: 'application/json', + api_key: this.apiKey, + }, + }); + + const json: FetchFarcasterCastByHashResponse = await response.json(); + + return json.cast; + } catch (error) { + console.log( + 'Failed to fetch Farcaster cast:', + getMessageForException(error) ?? 'unknown', + ); + throw error; + } + } } export { NeynarClient };