UNPKG

@snehal96/unimail

Version:

Unified email fetching & document extraction layer for modern web apps

96 lines (95 loc) 3.94 kB
import { IAdapter, PaginatedEmailsResponse } from './IAdapter.js'; import { NormalizedEmail, FetchOptions, GmailCredentials, EmailStreamOptions, EmailStreamCallbacks, HistoryResponse, PushNotificationConfig, PushNotificationSetup, SyncOptions, SyncResult } from '../interfaces.js'; export declare class GmailAdapter implements IAdapter { private oauth2Client_?; private gmail_?; private credentials_?; private emailParserService; private initialized; private oauthService?; constructor(); /** * Initialize the Gmail adapter with credentials. * This method now supports both traditional refresh token authentication * and the new OAuth flow using an auth code. */ initialize(credentials: GmailCredentials): Promise<void>; /** * Start the OAuth flow to get authorization from the user * @returns The authorization URL that the user should visit */ static startOAuthFlow(clientId: string, clientSecret: string, redirectUri: string, port?: number, callbackPath?: string): Promise<string>; /** * Handle the OAuth callback manually (for server-side applications) * @returns TokenData containing access and refresh tokens */ static handleOAuthCallback(code: string, clientId: string, clientSecret: string, redirectUri: string): Promise<{ accessToken: string; refreshToken?: string; }>; private ensureInitialized; authenticate(): Promise<void>; fetchEmails(options: FetchOptions): Promise<PaginatedEmailsResponse>; /** * Stream emails in batches using async generator * Memory-efficient way to process large numbers of emails */ streamEmails(options: EmailStreamOptions): AsyncGenerator<NormalizedEmail[], void, unknown>; /** * Stream emails with callback-based progress tracking * Provides detailed progress information and error handling */ fetchEmailsStream(options: EmailStreamOptions, callbacks: EmailStreamCallbacks): Promise<void>; /** * Helper method to build Gmail query from stream options */ private buildGmailQuery; /** * Helper method to determine message format from options */ private determineMessageFormat; /** * Fetches a single page of emails */ private fetchEmailPage; /** * Fetches all pages of emails up to the specified limit */ private fetchAllEmailPages; /** * Parses a Gmail message from structured data (when using 'full' or 'metadata' format) * @param message The Gmail message object from the API * @param includeBody Whether to include message body content * @param includeAttachments Whether to include attachment data */ private parseStructuredMessage; /** * Get the current history ID for this Gmail account. * This serves as a starting point for tracking changes. */ getCurrentHistoryId(): Promise<string>; /** * Get history records since the specified history ID. * This allows you to see what changed in the mailbox. */ getHistory(startHistoryId: string, options?: SyncOptions): Promise<HistoryResponse>; /** * Get a specific email by its ID. * Useful for fetching full details of emails found in history records. */ getEmailById(id: string): Promise<NormalizedEmail | null>; /** * Set up Gmail push notifications to receive real-time updates. * Requires a Google Cloud Pub/Sub topic and proper webhook setup. */ setupPushNotifications(config: PushNotificationConfig): Promise<PushNotificationSetup>; /** * Stop Gmail push notifications. */ stopPushNotifications(): Promise<void>; /** * Process sync changes from a given history ID. * This is a higher-level method that processes history records and returns structured results. */ processSync(options?: SyncOptions): Promise<SyncResult>; }