UNPKG

smartapi-typescript

Version:

TypeScript library for Angel One SmartAPI broker API

137 lines (136 loc) 4.94 kB
import { AxiosInstance } from 'axios'; import { SmartAPIConfig, ApiResponse, SessionData, UserProfile } from '../../types'; /** * Authentication module for SmartAPI * Handles login, session management, token refresh and logout */ export declare class Auth { private apiKey; private clientId?; private jwtToken?; private refreshToken?; private feedToken?; private debug; private httpClient; private lastTokenRefresh; private minRefreshInterval; private totpSecret?; /** * Initialize authentication module */ constructor(config: SmartAPIConfig, httpClient: AxiosInstance, debug?: boolean); /** * Generate a TOTP code using the configured TOTP secret * @returns Generated TOTP code or undefined if no secret is set */ generateTOTP(): string | undefined; /** * Set the TOTP secret key * @param secret The TOTP secret key */ setTOTPSecret(secret: string): void; /** * Get common headers required for API requests * Headers conform to the SmartAPI documentation requirements * @param options Optional parameters to customize headers * @returns Headers object for API requests */ getHeaders(options?: { clientLocalIP?: string; clientPublicIP?: string; macAddress?: string; }): Record<string, string>; /** * Log debug messages if debug mode is enabled * @param message Message to log * @param data Optional data to log */ private log; /** * Check if user is authenticated with a valid token * @returns Boolean indicating if authenticated */ isAuthenticated(): boolean; /** * Get the JWT token * @returns Current JWT token */ getJwtToken(): string | undefined; /** * Get the feed token for WebSocket connections * @returns Feed token */ getFeedToken(): string | undefined; /** * Get the refresh token * @returns Current refresh token */ getRefreshToken(): string | undefined; /** * Set tokens received from external sources (like publisher login) * @param jwtToken JWT token * @param refreshToken Refresh token * @param feedToken Feed token */ setTokens(jwtToken?: string, refreshToken?: string, feedToken?: string): void; /** * Authenticate user with Angel One API using password * @param password User's password * @param totp TOTP code from authenticator app for two-factor authentication * (if not provided and totpSecret is set, will be generated automatically) * @param state Optional state variable for external applications * @param options Network configuration options * @returns Authentication result containing jwt token, refresh token and feed token */ login(password: string, totp?: string, state?: string, options?: { clientLocalIP?: string; clientPublicIP?: string; macAddress?: string; }): Promise<ApiResponse<SessionData>>; /** * Generate a new session using refresh token * @param jwtToken JWT token (optional if already set in constructor) * @param refreshToken Refresh token (optional if already set in constructor) * @param options Network configuration options * @returns Session data containing new jwtToken, refreshToken and feedToken */ generateSession(jwtToken?: string, refreshToken?: string, options?: { clientLocalIP?: string; clientPublicIP?: string; macAddress?: string; }): Promise<ApiResponse<SessionData>>; /** * Logout the current user session * @param options Network configuration options * @returns Logout result */ logout(options?: { clientLocalIP?: string; clientPublicIP?: string; macAddress?: string; }): Promise<ApiResponse>; /** * Get user profile information * @param options Network configuration options * @returns User profile data */ getProfile(options?: { clientLocalIP?: string; clientPublicIP?: string; macAddress?: string; }): Promise<ApiResponse<UserProfile>>; /** * Generate a publisher login URL for redirecting users to the SmartAPI login endpoint * @param redirectUrl URL to redirect after successful login (must be registered in your MyApps settings) * @param state Optional state variable to track session (will be returned in the redirect) * @returns The URL to redirect users for login */ getPublisherLoginUrl(redirectUrl?: string, state?: string): string; /** * Handle API errors, attempting to refresh token if appropriate * @param error Original error * @param retryFn Function to retry after token refresh * @returns API response */ handleApiError<T>(error: any, retryFn?: () => Promise<ApiResponse<T>>): Promise<ApiResponse<T>>; }