UNPKG

@sudowealth/schwab-api

Version:

TypeScript client for Charles Schwab API with OAuth support, market data, trading functionality, and complete type safety

125 lines (124 loc) 5.63 kB
import * as authNs from './auth/index.js'; import { type AuthDiagnosticsOptions, type AuthDiagnosticsResult } from './auth/auth-diagnostics.js'; import { type EnhancedTokenManager } from './auth/enhanced-token-manager.js'; import { type SchwabApiConfig } from './core/config.js'; import { type ProcessNamespaceResult } from './core/endpoint-types.js'; import { type RequestContext, createEndpoint as coreHttpCreateEndpoint, // Aliased import type EndpointMetadata, type HttpMethod } from './core/http.js'; import * as errorsNs from './errors.js'; import { SchwabError, SchwabApiError, SchwabAuthError, SchwabRateLimitError, SchwabAuthorizationError, SchwabInvalidRequestError, SchwabNotFoundError, SchwabServerError, SchwabNetworkError, SchwabTimeoutError, isSchwabError, isSchwabApiError, ApiErrorCode, AuthErrorCode, ErrorResponseSchema, parseErrorResponse } from './errors.js'; import * as marketDataNs from './market-data/index.js'; import { type MiddlewarePipelineOptions } from './middleware/pipeline.js'; import * as schemasNs from './schemas/index.js'; import * as traderNs from './trader/index.js'; /** * Options for creating a Schwab API client */ export interface CreateApiClientOptions { /** * API configuration options * These will be merged with the default configuration */ config?: Partial<SchwabApiConfig>; /** * Authentication token, token manager, or auth config * * This can be one of: * - A string containing an access token * - An EnhancedTokenManager instance * - An AuthFactoryConfig object that will be passed to createSchwabAuth * * Concurrency protection is automatically applied for refresh-capable token managers. */ auth?: string | EnhancedTokenManager | authNs.AuthFactoryConfig; /** * Middleware configuration options * This provides a flexible way to configure the middleware pipeline */ middleware?: MiddlewarePipelineOptions; } /** * The Schwab API client * Contains all namespaces and utilities for interacting with the Schwab API */ export interface SchwabApiClient { /** * Market Data API (quotes, price history, instruments, etc.) * These namespaces contain both the metadata objects (with 'Meta' suffix) and * the actual endpoint functions (without 'Meta' suffix) that are created during client initialization. */ marketData: ProcessNamespaceResult<typeof marketDataNs>; /** * Trader API (accounts, orders, transactions, etc.) * These namespaces contain both the metadata objects (with 'Meta' suffix) and * the actual endpoint functions (without 'Meta' suffix) that are created during client initialization. */ trader: ProcessNamespaceResult<typeof traderNs>; /** * Schemas for API requests and responses */ schemas: typeof schemasNs; /** * Auth utilities and types */ auth: typeof authNs; /** * Error types and utilities */ errors: { SchwabError: typeof SchwabError; isSchwabError: typeof isSchwabError; SchwabApiError: typeof SchwabApiError; isSchwabApiError: typeof isSchwabApiError; SchwabRateLimitError: typeof SchwabRateLimitError; SchwabAuthorizationError: typeof SchwabAuthorizationError; SchwabInvalidRequestError: typeof SchwabInvalidRequestError; SchwabNotFoundError: typeof SchwabNotFoundError; SchwabServerError: typeof SchwabServerError; SchwabNetworkError: typeof SchwabNetworkError; SchwabTimeoutError: typeof SchwabTimeoutError; SchwabAuthError: typeof SchwabAuthError; ApiErrorCode: typeof ApiErrorCode; AuthErrorCode: typeof AuthErrorCode; ErrorResponse: typeof ErrorResponseSchema; parseErrorResponse: typeof parseErrorResponse; }; /** * Request context that can be used with context-aware functions * @internal Exposed for advanced use cases */ _context: RequestContext; /** * Unified discovery object * Contains all APIs, schemas, auth utilities, and error types in a single object. * The marketData and trader namespaces contain both metadata objects and * callable endpoint functions created during client initialization. */ all: { marketData: ProcessNamespaceResult<typeof marketDataNs>; trader: ProcessNamespaceResult<typeof traderNs>; schemas: typeof schemasNs; auth: typeof authNs; errors: typeof errorsNs; }; /** * Create an endpoint function using the shared API client context */ createEndpoint<P = unknown, Q = unknown, B = unknown, R = unknown, M extends HttpMethod = HttpMethod, E = unknown>(meta: EndpointMetadata<P, Q, B, R, M, E>): ReturnType<typeof coreHttpCreateEndpoint<P, Q, B, R, M, E>>; /** * Debug authentication issues and provide detailed information about the current auth state. * Use this when experiencing 401 Unauthorized errors to diagnose token problems. * @param options Options for debugging auth * @returns Detailed diagnostics information about the auth state */ debugAuth(options?: AuthDiagnosticsOptions): Promise<AuthDiagnosticsResult>; } export declare function createApiClient(options: CreateApiClientOptions & { auth: EnhancedTokenManager; }): SchwabApiClient; export declare function createApiClient(options: CreateApiClientOptions & { auth: string; }): Promise<SchwabApiClient>; export declare function createApiClient(options: CreateApiClientOptions & { auth: authNs.AuthFactoryConfig; }): Promise<SchwabApiClient>;