@standard-crypto/farcaster-js-warpcast
Version:
A tool for interacting with the private APIs of the Warpcast client.
74 lines • 2.95 kB
JavaScript
import axios from 'axios';
import { Configuration, DirectCastsApi, UsersApi } from './index.js';
import { silentLogger } from './logger.js';
export const WARPCAST_API_URL = 'https://client.warpcast.com';
export class WarpcastAPIClient {
logger;
apis;
constructor(authToken, { axiosInstance, logger = silentLogger, } = {}) {
this.logger = logger;
if (axiosInstance === undefined) {
axiosInstance = axios.create();
}
axiosInstance.defaults.decompress = true;
axiosInstance.defaults.headers.common.Authorization = `Bearer ${authToken}`;
const config = new Configuration({ basePath: WARPCAST_API_URL });
this.apis = {
directCasts: new DirectCastsApi(config, undefined, axiosInstance),
users: new UsersApi(config, undefined, axiosInstance),
};
}
async getCurrentUser() {
const onboardingState = await this.apis.users.v2OnboardingStateGet();
return onboardingState.data.result.state.user;
}
async getUserByFid(fid) {
const response = await this.apis.users.v2UserGet({ fid });
return response.data.result.user;
}
async *listConversations() {
let cursor;
while (true) {
// fetch one page
const response = await this.apis.directCasts.v2DirectCastConversationListGet({ cursor });
yield* response.data.result.conversations;
// prep for next page
if (response.data.next?.cursor === undefined) {
break;
}
cursor = response.data.next.cursor;
}
}
/**
* Get the details of a conversation.
*
* Be aware that the `messages` field is paginated by the server.
* To iterate through all messages, see `listConversationMessages`.
* @param conversationId The ID of the conversation
*/
async getConversationDetails(conversationId) {
const response = await this.apis.directCasts.v2DirectCastConversationDetailsGet({ conversationId });
return response.data.result;
}
/**
* Paginates through all messages of a given conversation.
* The server will return messages in reverse-chronological order (most recent messages first).
* @param conversationId The ID of the conversation
*/
async *listConversationMessages(conversationId) {
let cursor;
while (true) {
// fetch one page
const response = await this.apis.directCasts.v2DirectCastConversationDetailsGet({ conversationId, cursor });
if (response.data.result.messages !== undefined) {
yield* response.data.result.messages;
}
// prep for next page
if (response.data.next?.cursor === undefined) {
break;
}
cursor = response.data.next.cursor;
}
}
}
//# sourceMappingURL=warpcastApiClient.js.map