/** * Yahoo Fantasy Sports API Client * * Main entry point for the Yahoo Fantasy Sports API wrapper. * Provides access to all fantasy sports resources with full TypeScript support. * * @module * * @example * ```typescript * import { YahooFantasyClient } from 'yahoo-fantasy-sports'; * * const client = new YahooFantasyClient({ * clientId: process.env.YAHOO_CLIENT_ID!, * clientSecret: process.env.YAHOO_CLIENT_SECRET!, * redirectUri: 'https://example.com/callback', * }); * * // Step 1: Get authorization URL * const authUrl = client.getAuthUrl(); * console.log('Visit this URL and authorize:', authUrl); * * // Step 2: User authorizes and gets redirected with code * // (Alternatively, ask the user to input the code manually) * const code = '...'; // User authorization code from yahoo * * // Step 3: Exchange code for tokens * await client.authenticate(code); * * // Make API calls * const league = await client.request().league('423.l.12345').execute(); * const roster = await client.request().team('423.l.12345.t.1').roster().execute(); * ``` */ import { type RootRequestBuilder } from '../builders/request.js'; import type { Config } from '../types/index.js'; import { type OAuth2Tokens } from './OAuth2Client.js'; /** * Callback interface for storing and retrieving OAuth 2.0 tokens * Implement this to persist tokens between sessions */ export interface TokenStorage { /** * Save tokens */ save(tokens: OAuth2Tokens): Promise | void; /** * Load saved tokens */ load(): Promise | OAuth2Tokens | null; /** * Clear saved tokens */ clear(): Promise | void; } /** * Main Yahoo Fantasy Sports API client * * Provides access to Yahoo Fantasy Sports through a fluent request-builder API. * * @example * ```typescript * const client = new YahooFantasyClient({ * clientId: 'your-client-id', * clientSecret: 'your-client-secret', * redirectUri: 'https://example.com/callback', * }); * * // Step 1: Get authorization URL * const authUrl = client.getAuthUrl(); * console.log('Visit this URL and authorize:', authUrl); * * // Step 2: User authorizes and gets redirected with code * // Extract code from redirect: ?code=AUTHORIZATION_CODE * * // Step 3: Complete authentication * await client.authenticate(code); * * // Use the request builder * const league = await client.request().league('423.l.12345').execute(); * const teams = await client.request().users().useLogin().games().teams().execute(); * ``` */ export declare class YahooFantasyClient { private config; private oauth2Client?; private oauth1Client?; private httpClient; private tokenStorage?; private tokens?; /** * Creates a new Yahoo Fantasy Sports API client * * @param config - Configuration options * @param tokenStorage - Optional token storage implementation * @throws {ConfigError} If required configuration is missing or invalid * * @example * ```typescript * const client = new YahooFantasyClient({ * clientId: process.env.YAHOO_CLIENT_ID!, * clientSecret: process.env.YAHOO_CLIENT_SECRET!, * redirectUri: 'https://example.com/callback', * debug: true, // Optional: enable debug logging * }); * ``` * * @example With token storage * ```typescript * const storage: TokenStorage = { * async save(tokens) { * await fs.writeFile('tokens.json', JSON.stringify(tokens)); * }, * async load() { * try { * const data = await fs.readFile('tokens.json', 'utf-8'); * return JSON.parse(data); * } catch { * return null; * } * }, * async clear() { * await fs.unlink('tokens.json'); * }, * }; * * const client = new YahooFantasyClient(config, storage); * * // Try to load existing tokens * await client.loadTokens(); * ``` */ constructor(config: Config, tokenStorage?: TokenStorage); /** * Create a new composable request builder * * A type-safe, chainable API for building Yahoo Fantasy API queries. * Provides autocomplete and sensible defaults. * * @returns A new RequestBuilder instance * * @example Query league settings * ```typescript * const league = await client.request() * .league('423.l.12345') * .settings() * .execute(); * ``` * * @example Query players with filters * ```typescript * const players = await client.request() * .league('423.l.12345') * .players() * .position('C') * .status('FA') * .count(25) * .execute(); * ``` * * @example Query team roster * ```typescript * const roster = await client.request() * .team('423.l.12345.t.1') * .roster({ week: 10 }) * .execute(); * ``` * * @example Query user's games * ```typescript * const games = await client.request() * .games() * .gameKeys(['nhl', 'nfl']) * .execute(); * ``` * * @example Query user's games * ```typescript * const userGames = await client.request() * .users() * .useLogin() * .games() * .execute(); * ``` */ request(): RootRequestBuilder; /** * Gets the authorization URL for the OAuth 2.0 flow * * Only available in user authentication mode (not in public mode). * * Step 1 of the OAuth flow. The user must visit this URL and authorize the application. * After authorization, Yahoo will redirect to your redirectUri with a code parameter. * * @param state - Optional state parameter for CSRF protection * @param language - Optional language code (default: 'en-us') * @returns Authorization URL that the user must visit * @throws {ConfigError} If called in public mode * * @example * ```typescript * const authUrl = client.getAuthUrl('random-state-string'); * console.log('Please visit:', authUrl); * console.log('After authorizing, you will be redirected with a code parameter.'); * ``` */ getAuthUrl(state?: string, language?: string): string; /** * Completes authentication with the authorization code * * Step 2 of the OAuth flow. After the user authorizes and is redirected with a code, * call this method to exchange it for access and refresh tokens. * * @param code - Authorization code from Yahoo OAuth redirect * @throws {AuthenticationError} If authentication fails * * @example * ```typescript * const authUrl = client.getAuthUrl(); * console.log('Visit:', authUrl); * * // After user authorizes and is redirected to: * // https://your-redirect-uri?code=AUTHORIZATION_CODE * * const code = '...'; // Extract from redirect URL * await client.authenticate(code); * * console.log('Authenticated successfully!'); * ``` */ /** * Completes authentication with the authorization code * * Only available in user authentication mode (not in public mode). * * Step 2 of the OAuth flow. After the user authorizes and is redirected with a code, * call this method to exchange it for access and refresh tokens. * * @param code - Authorization code from Yahoo OAuth redirect * @throws {AuthenticationError} If authentication fails * @throws {ConfigError} If called in public mode * * @example * ```typescript * const authUrl = client.getAuthUrl(); * console.log('Visit:', authUrl); * * // After user authorizes and is redirected to: * // https://your-redirect-uri?code=AUTHORIZATION_CODE * * const code = '...'; // Extract from redirect URL * await client.authenticate(code); * * console.log('Authenticated successfully!'); * ``` */ authenticate(code: string): Promise; /** * Loads tokens from storage * * If a TokenStorage implementation was provided, this loads previously saved tokens. * * @returns True if tokens were loaded, false otherwise * * @example * ```typescript * const client = new YahooFantasyClient(config, storage); * * if (await client.loadTokens()) { * console.log('Using saved tokens'); * } else { * console.log('No saved tokens, need to authenticate'); * const authUrl = client.getAuthUrl(); * // ... authenticate * } * ``` */ loadTokens(): Promise; /** * Refreshes the access token using the refresh token * * Only available in user authentication mode (not in public mode). * * OAuth 2.0 access tokens expire after 1 hour. Use this method to get a new access token * without requiring the user to re-authenticate. * * Note: The HttpClient automatically refreshes tokens before making requests, * so you typically don't need to call this manually. * * @throws {AuthenticationError} If refresh fails * @throws {ConfigError} If no refresh token is available or if called in public mode * * @example * ```typescript * try { * await client.refreshToken(); * console.log('Token refreshed successfully'); * } catch (error) { * console.log('Refresh failed, need to re-authenticate'); * await client.authenticate(code); * } * ``` */ refreshToken(): Promise; /** * Checks if the client is currently authenticated * * In public mode (OAuth 1.0), always returns true since no user auth is needed. * In user auth mode (OAuth 2.0), returns true if valid access tokens exist. * * @returns True if the client can make authenticated requests * * @example * ```typescript * if (!client.isAuthenticated()) { * await client.authenticate(code); * } * ``` */ isAuthenticated(): boolean; /** * Checks if the access token is expired or will expire soon * * Only applicable in user authentication mode (OAuth 2.0). * In public mode (OAuth 1.0), always returns false (tokens don't expire). * * @param bufferSeconds - Time buffer in seconds before actual expiration (default: 60) * @returns True if the token is expired or will expire within the buffer time * * @example * ```typescript * if (client.isTokenExpired()) { * await client.refreshToken(); * } * ``` */ isTokenExpired(bufferSeconds?: number): boolean; /** * Gets the current OAuth 2.0 tokens * * @returns Current tokens or null if not authenticated * * @example * ```typescript * const tokens = client.getTokens(); * if (tokens) { * // Save tokens for later use * await saveToDatabase(tokens); * } * ``` */ getTokens(): OAuth2Tokens | null; /** * Clears stored authentication tokens * * @example * ```typescript * await client.logout(); * console.log('Logged out successfully'); * ``` */ logout(): Promise; /** * Sets OAuth tokens (internal method) */ private setTokens; } //# sourceMappingURL=YahooFantasyClient.d.ts.map