@sudowealth/schwab-api
Version:
TypeScript client for Charles Schwab API with OAuth support, market data, trading functionality, and complete type safety
108 lines (107 loc) • 2.93 kB
TypeScript
import { SchwabAuthError, AuthErrorCode, SchwabApiError, ApiErrorCode } from '../errors';
/**
* Result of error mapping operation
*/
export interface ErrorMappingResult {
/**
* Mapped error code
*/
code: AuthErrorCode | ApiErrorCode;
/**
* Human-readable error message
*/
message: string;
/**
* HTTP status code to use in response
*/
httpStatus: number;
/**
* Whether the error is retryable
*/
isRetryable: boolean;
/**
* Whether reauthentication is required
*/
requiresReauth: boolean;
/**
* Additional context about the error
*/
context?: Record<string, any>;
}
/**
* Interface for custom error mappers
*/
export interface ErrorMapper {
/**
* Map an error to a result
*/
map(error: unknown): ErrorMappingResult | null;
}
/**
* Enhanced error mapper with extensible mapping support
*/
export declare class SchwabErrorMapper {
private customMappers;
private authErrorMappings;
private apiErrorMappings;
constructor(options?: {
customMappers?: ErrorMapper[];
customAuthMappings?: Record<AuthErrorCode, Partial<ErrorMappingResult>>;
customApiMappings?: Record<ApiErrorCode, Partial<ErrorMappingResult>>;
});
/**
* Initialize default error mappings
*/
private initializeDefaultMappings;
/**
* Map a Schwab error to an error mapping result
*/
map(error: unknown): ErrorMappingResult;
/**
* Map an auth error
*/
mapAuthError(error: SchwabAuthError): ErrorMappingResult;
/**
* Map an API error
*/
mapApiError(error: SchwabApiError): ErrorMappingResult;
/**
* Add a custom error mapper
*/
addMapper(mapper: ErrorMapper): void;
/**
* Override a specific auth error mapping
*/
setAuthMapping(code: AuthErrorCode, mapping: Partial<ErrorMappingResult>): void;
/**
* Override a specific API error mapping
*/
setApiMapping(code: ApiErrorCode, mapping: Partial<ErrorMappingResult>): void;
}
/**
* Default error mapper instance
*/
export declare const defaultErrorMapper: SchwabErrorMapper;
/**
* Map a Schwab SDK error to appropriate error metadata
* This is a convenience function that uses the default mapper
*/
export declare function mapSchwabError(error: unknown): ErrorMappingResult;
/**
* Create an error handler function for Express/Hono style frameworks
*/
export declare function schwabErrorHandler(options?: {
includeStackTrace?: boolean;
customMapper?: SchwabErrorMapper;
}): (error: unknown, req: any, res: any) => void;
/**
* Check if an error requires reauthentication
*/
export declare function requiresReauthentication(error: unknown): boolean;
/**
* Get retry information from an error
*/
export declare function getRetryInfo(error: unknown): {
isRetryable: boolean;
retryAfterMs?: number;
};