UNPKG

@omniconvert/server-side-testing-sdk

Version:

TypeScript SDK for server-side A/B testing and experimentation

183 lines 5.36 kB
import { HttpClientInterface } from './HttpClientInterface'; import { BrowserLogger } from '../logger/BrowserLogger'; /** * HTTP client implementation using fetch API * Provides browser-compatible HTTP functionality for making requests to the Omniconvert API * * @example * ```typescript * const client = new HttpClient('your-api-key'); * const response = await client.requestExperiments(); * ``` * * @since 1.0.0 * @author Omniconvert * @category HTTP */ export declare class HttpClient implements HttpClientInterface { /** * Default base URL for Omniconvert API * @default 'https://app.omniconvert.com/' * @readonly */ private static readonly DEFAULT_API_BASE_URL; /** * API route for experiments endpoint * @readonly */ private static readonly ROUTE_EXPERIMENTS; /** * API route for tracking endpoint * @readonly */ private static readonly ROUTE_TRACKING; /** * Whether cache bypass is enabled for GET requests * @private */ private cacheBypass; /** * Base URI for all API requests * @private */ private baseUri; /** * Default headers to include with every request * @private */ private defaultHeaders; /** * Logger instance for HTTP client * @private */ private logger; /** * Create a new HTTP client instance * * @param apiKey - API key for authentication with Omniconvert * @param baseUri - Base URI for API requests (defaults to 'https://app.omniconvert.com/') * @param cacheBypass - Whether to enable cache bypass for GET requests * * @example * ```typescript * // Basic usage with API key only * const client = new HttpClient('your-api-key'); * * // Custom base URI * const client = new HttpClient('your-api-key', 'https://staging.omniconvert.com/'); * * // With cache bypass enabled * const client = new HttpClient('your-api-key', undefined, true); * ``` * * @since 1.0.0 */ constructor(apiKey: string, baseUri?: string, cacheBypass?: boolean, logger?: BrowserLogger); /** * Make a generic HTTP request to the specified endpoint * * @param method - The HTTP method to use (GET, POST, PUT, DELETE, etc.) * @param uri - The URI path to append to the base URL (optional) * @param options - Additional fetch options including headers, body, etc. * @returns Promise that resolves to the Response object * * @example * ```typescript * const response = await client.request('POST', 'api/users', { * body: JSON.stringify({ name: 'John' }), * headers: { 'Content-Type': 'application/json' } * }); * ``` * * @throws {Error} When the request fails or network error occurs * * @since 1.0.0 */ request(method: string, uri?: string, options?: RequestInit): Promise<Response>; /** * Make a tracking request to the marketing tracking endpoint * * @param method - The HTTP method to use for the tracking request * @param queryParams - Query parameters to include in the tracking request * @returns Promise that resolves to the Response object * * @example * ```typescript * const response = await client.requestTracking('POST', { * event: 'page_view', * page: '/homepage' * }); * ``` * * @since 1.0.0 */ requestTracking(method: string, queryParams?: Record<string, unknown>): Promise<Response>; /** * Request experiments configuration from the API * * @returns Promise that resolves to the Response object containing experiments data * * @example * ```typescript * const response = await client.requestExperiments(); * const experiments = await response.json(); * ``` * * @since 1.0.0 */ requestExperiments(): Promise<Response>; /** * Check if cache bypass is enabled for this client * * @returns true if cache bypass is enabled, false otherwise * * @example * ```typescript * if (client.hasCacheBypass()) { * console.log('Cache bypass is enabled'); * } * ``` * * @since 1.0.0 */ hasCacheBypass(): boolean; /** * Get the base URI */ getBaseUri(): string; /** * Build full URL from base URI and relative path */ private buildUrl; /** * Build query string from parameters object */ private buildQueryString; /** * Generate cache bypass hash * Matches PHP implementation: hash('sha256', str_rot13(base64_encode($input))) */ private generateCacheBypassHash; /** * Fallback cache bypass hash generation */ private generateCacheBypassHashFallback; /** * Simple ROT13 implementation */ private rot13; /** * SHA-256 fallback implementation for older browsers * Note: For production use with legacy browsers, consider including crypto-js library */ private sha256; /** * Check if the client is in a browser environment */ static isBrowser(): boolean; /** * Get user agent string (browser only) */ static getUserAgent(): string; } //# sourceMappingURL=HttpClient.d.ts.map