UNPKG

@dbs-portal/core-api

Version:

HTTP client and API utilities for DBS Portal

224 lines 7.22 kB
/** * Factory functions for creating API clients */ import { ApiClient } from './api-client'; import { createAuthInterceptor, createRequestIdInterceptor, createRequestLoggingInterceptor, createResponseLoggingInterceptor, } from '../interceptors/common-interceptors'; /** * Creates a basic API client with minimal configuration */ export function createApiClient(config = {}) { return new ApiClient(config); } /** * Creates an API client with authentication */ export function createAuthenticatedApiClient(baseURL, getToken, config = {}) { const client = new ApiClient({ baseURL, timeout: 30000, ...config, }); // Add authentication interceptor client.addRequestInterceptor(createAuthInterceptor(getToken).onFulfilled, createAuthInterceptor(getToken).onRejected); return client; } /** * Creates an API client with caching enabled */ export function createCachedApiClient(baseURL, config = {}) { return new ApiClient({ baseURL, timeout: 30000, cache: { enabled: true, ttl: 5 * 60 * 1000, // 5 minutes maxSize: 100, storage: 'memory', ...config.cache, }, ...config, }); } /** * Creates an API client with retry logic */ export function createRetryApiClient(baseURL, config = {}) { return new ApiClient({ baseURL, timeout: 30000, retry: { retries: 3, retryDelay: retryCount => Math.min(1000 * Math.pow(2, retryCount), 30000), ...config.retry, }, ...config, }); } /** * Creates an API client with rate limiting */ export function createRateLimitedApiClient(baseURL, maxRequests = 100, perMilliseconds = 60000, config = {}) { return new ApiClient({ baseURL, timeout: 30000, rateLimit: { maxRequests, perMilliseconds, ...config.rateLimit, }, ...config, }); } /** * Creates a fully featured API client with all advanced features */ export function createAdvancedApiClient(baseURL, getToken, config = {}) { const client = new ApiClient({ baseURL, timeout: 30000, cache: { enabled: true, ttl: 5 * 60 * 1000, maxSize: 100, storage: 'memory', }, retry: { retries: 3, retryDelay: retryCount => Math.min(1000 * Math.pow(2, retryCount), 30000), }, rateLimit: { maxRequests: 100, perMilliseconds: 60000, }, ...config, }); // Add common interceptors client.addRequestInterceptor(createAuthInterceptor(getToken).onFulfilled, createAuthInterceptor(getToken).onRejected); client.addRequestInterceptor(createRequestIdInterceptor().onFulfilled, createRequestIdInterceptor().onRejected); return client; } /** * Creates an API client for development with logging */ export function createDevelopmentApiClient(baseURL, config = {}) { const client = new ApiClient({ baseURL, timeout: 30000, ...config, }); // Add logging interceptors client.addRequestInterceptor(createRequestLoggingInterceptor().onFulfilled, createRequestLoggingInterceptor().onRejected); client.addResponseInterceptor(createResponseLoggingInterceptor().onFulfilled, createResponseLoggingInterceptor().onRejected); return client; } /** * Creates an API client for production with optimizations */ export function createProductionApiClient(baseURL, config = {}) { return new ApiClient({ baseURL, timeout: 15000, // Shorter timeout for production cache: { enabled: true, ttl: 10 * 60 * 1000, // 10 minutes maxSize: 200, storage: 'localStorage', }, retry: { retries: 2, // Fewer retries for production retryDelay: retryCount => Math.min(500 * Math.pow(2, retryCount), 10000), }, rateLimit: { maxRequests: 200, perMilliseconds: 60000, }, ...config, }); } /** * Creates an API client from configuration object */ export function createApiClientFromConfig(config) { const clientConfig = { baseURL: config.baseURL, timeout: 30000, ...config.options, }; // Enable features based on configuration if (config.features?.cache) { clientConfig.cache = { enabled: true, ttl: 5 * 60 * 1000, maxSize: 100, storage: 'memory', ...clientConfig.cache, }; } if (config.features?.retry) { clientConfig.retry = { retries: 3, retryDelay: retryCount => Math.min(1000 * Math.pow(2, retryCount), 30000), ...clientConfig.retry, }; } if (config.features?.rateLimit) { clientConfig.rateLimit = { maxRequests: 100, perMilliseconds: 60000, ...clientConfig.rateLimit, }; } const client = new ApiClient(clientConfig); // Add authentication if configured if (config.auth) { const getToken = () => { switch (config.auth.type) { case 'bearer': return config.auth.token || null; case 'basic': if (config.auth.username && config.auth.password) { return btoa(`${config.auth.username}:${config.auth.password}`); } return null; case 'apiKey': return config.auth.apiKey || null; default: return null; } }; if (config.auth.type === 'bearer') { client.addRequestInterceptor(createAuthInterceptor(getToken).onFulfilled, createAuthInterceptor(getToken).onRejected); } else if (config.auth.type === 'basic') { client.addRequestInterceptor(requestConfig => { const token = getToken(); if (token) { requestConfig.headers = { ...requestConfig.headers, Authorization: `Basic ${token}`, }; } return requestConfig; }); } else if (config.auth.type === 'apiKey') { client.addRequestInterceptor(requestConfig => { const apiKey = getToken(); if (apiKey) { requestConfig.headers = { ...requestConfig.headers, 'X-API-Key': apiKey, }; } return requestConfig; }); } } // Add logging if enabled if (config.features?.logging) { client.addRequestInterceptor(createRequestLoggingInterceptor().onFulfilled, createRequestLoggingInterceptor().onRejected); client.addResponseInterceptor(createResponseLoggingInterceptor().onFulfilled, createResponseLoggingInterceptor().onRejected); } return client; } //# sourceMappingURL=factory.js.map