UNPKG

@standard-crypto/farcaster-js-warpcast

Version:

A tool for interacting with the private APIs of the Warpcast client.

81 lines 3.32 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.WarpcastAPIClient = exports.WARPCAST_API_URL = void 0; const axios_1 = __importDefault(require("axios")); const index_js_1 = require("./index.js"); const logger_js_1 = require("./logger.js"); exports.WARPCAST_API_URL = 'https://client.warpcast.com'; class WarpcastAPIClient { logger; apis; constructor(authToken, { axiosInstance, logger = logger_js_1.silentLogger, } = {}) { this.logger = logger; if (axiosInstance === undefined) { axiosInstance = axios_1.default.create(); } axiosInstance.defaults.decompress = true; axiosInstance.defaults.headers.common.Authorization = `Bearer ${authToken}`; const config = new index_js_1.Configuration({ basePath: exports.WARPCAST_API_URL }); this.apis = { directCasts: new index_js_1.DirectCastsApi(config, undefined, axiosInstance), users: new index_js_1.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; } } } exports.WarpcastAPIClient = WarpcastAPIClient; //# sourceMappingURL=warpcastApiClient.js.map