UNPKG

@sudowealth/schwab-api

Version:

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

40 lines (39 loc) 1.78 kB
import { type ZodType, type ZodTypeDef } from 'zod'; import { type SchwabApiConfig, type SchwabApiLogger } from './config'; export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'OPTIONS' | 'HEAD'; export interface EndpointMetadata<PType = unknown, // Inferred type for path params QType = unknown, // Inferred type for query params BType = unknown, // Inferred type for body RType = unknown, // Inferred type for response M extends HttpMethod = HttpMethod, // Allow broader HttpMethod from MCP ErrorType = unknown> { method: M; path: string; pathSchema?: ZodType<PType, ZodTypeDef, any>; querySchema?: ZodType<QType, ZodTypeDef, any>; bodySchema?: ZodType<BType, ZodTypeDef, any>; responseSchema: ZodType<RType, ZodTypeDef, any>; errorSchema?: ZodType<ErrorType, ZodTypeDef, any>; description?: string; } export interface SchwabFetchRequestOptions<P = unknown, Q = unknown, B = unknown> { pathParams?: P; queryParams?: Q; body?: B; headers?: Record<string, string>; init?: Omit<RequestInit, 'body' | 'method'>; } export interface RequestContext { config: SchwabApiConfig; fetchFn: (req: Request) => Promise<Response>; logger: SchwabApiLogger; } /** * Create a new request context with the given config and fetch function */ export declare function createRequestContext(config?: SchwabApiConfig, fetchFn?: (req: Request) => Promise<Response>): RequestContext; /** * Create an endpoint function using the provided context * This is the primary way to create API endpoint functions */ export declare function createEndpoint<P, Q, B, R, M extends HttpMethod, E = unknown>(context: RequestContext, meta: EndpointMetadata<P, Q, B, R, M, E>): (options?: SchwabFetchRequestOptions<P, Q, B>) => Promise<R>;