amazon-seller-mcp
Version:
Model Context Protocol (MCP) client for Amazon Selling Partner API
164 lines (163 loc) • 4.49 kB
TypeScript
/**
* Base API client for Amazon Selling Partner API
*/
import { AxiosInstance } from 'axios';
/**
* Rate limit queue item
*/
interface RateLimitQueueItem<T = unknown> {
resolve: (value: T) => void;
reject: (error: Error) => void;
fn: () => Promise<T>;
}
/**
* Batch manager entry
*/
interface BatchManagerEntry<T = unknown> {
promise: Promise<T>;
timestamp: number;
}
import { AmazonAuth } from '../auth/amazon-auth.js';
import { ApiClientConfig, ApiRequestOptions, ApiResponse, RetryStrategy } from '../types/api.js';
import { AuthConfig } from '../types/auth.js';
import { ErrorRecoveryManager } from '../utils/error-handler.js';
import { CacheManager } from '../utils/cache-manager.js';
/**
* Base API client for Amazon Selling Partner API
*/
export declare class BaseApiClient {
/**
* Axios instance for making HTTP requests
*/
protected axios: AxiosInstance;
/**
* Authentication client
*/
protected auth: AmazonAuth;
/**
* API client configuration
*/
protected config: ApiClientConfig;
/**
* Retry strategy
*/
protected retryStrategy: RetryStrategy;
/**
* Cache manager for API responses
*/
protected cache: CacheManager;
/**
* Rate limiting state
*/
protected rateLimitState: {
/**
* Timestamp of the last request
*/
lastRequestTime: number;
/**
* Number of requests in the current time window
*/
requestCount: number;
/**
* Queue of pending requests
*/
queue: Array<RateLimitQueueItem>;
/**
* Whether a queue processor is running
*/
processingQueue: boolean;
};
/**
* Error recovery manager
*/
protected errorRecoveryManager: ErrorRecoveryManager;
/**
* Request batch manager for combining similar requests
*/
protected batchManager: Map<string, BatchManagerEntry>;
/**
* Create a new BaseApiClient instance
*
* @param authConfig Authentication configuration
* @param apiConfig API client configuration
*/
constructor(authConfig: AuthConfig, apiConfig?: Partial<ApiClientConfig>);
/**
* Make an API request
*
* @param options Request options
* @returns Promise resolving to the API response
*/
request<T = unknown>(options: ApiRequestOptions): Promise<ApiResponse<T>>;
/**
* Execute an API request with retries
*
* @param options Request options
* @returns Promise resolving to the API response
*/
private executeRequest;
/**
* Make a single API request
*
* @param options Request options
* @returns Promise resolving to the API response
*/
private makeRequest;
/**
* Build a URL from a path and query parameters
*
* @param path URL path
* @param query Query parameters
* @returns URL string
*/
private buildUrl;
/**
* Parse rate limit headers from a response
*
* @param response Axios response
* @returns Rate limit information
*/
private parseRateLimitHeaders;
/**
* Apply rate limiting to a function
*
* @param fn Function to rate limit
* @returns Promise resolving to the function result
*/
private rateLimit;
/**
* Process the rate limit queue
*/
private processRateLimitQueue;
/**
* Get a cached response or execute a function and cache the result
*
* @param cacheKey Cache key
* @param fn Function to execute if cache miss
* @param ttl Time to live in seconds
* @returns Promise resolving to the function result
*/
protected withCache<T>(cacheKey: string, fn: () => Promise<T>, ttl?: number): Promise<T>;
/**
* Clear the cache
*
* @param cacheKey Optional cache key to clear
*/
clearCache(cacheKey?: string): Promise<void>;
/**
* Batch similar requests together to reduce API calls
*
* @param key Batch key
* @param fn Function to execute
* @param maxAge Maximum age of a batch in milliseconds
* @returns Promise resolving to the function result
*/
protected batchRequest<T>(key: string, fn: () => Promise<T>, maxAge?: number): Promise<T>;
/**
* Clean up old batches
*
* @param maxAge Maximum age of a batch in milliseconds
*/
private cleanupBatches;
}
export {};