@contiva/sap-integration-suite-client
Version:
SAP Cloud Platform Integration API Client
284 lines (283 loc) • 10.1 kB
TypeScript
/**
* SAP Cloud Platform Integration API Client
*
* This module provides a client for interacting with the SAP Cloud Platform Integration API.
* It handles authentication, request management, and provides type-safe access to all SAP API endpoints.
*
* @module sap-integration-suite-client
* @packageDocumentation
*/
import { IntegrationContentClient } from '../wrapper/integration-content-client';
import { IntegrationContentAdvancedClient } from '../wrapper/custom/integration-content-advanced-client';
import { MessageProcessingLogsClient } from '../wrapper/message-processing-logs-client';
import { LogFilesClient } from '../wrapper/log-files-client';
import { MessageStoreClient } from '../wrapper/message-store-client';
import { SecurityContentClient } from '../wrapper/security-content-client';
import { BaseCustomClient, CustomClientFactory } from '../wrapper/custom/base-custom-client';
/**
* Configuration interface for SapClient
*
* @interface SapClientConfig
* @example
* // Create a client with explicit configuration
* const client = new SapClient({
* baseUrl: 'https://my-tenant.sap-api.com/api/v1',
* oauthClientId: 'my-client-id',
* oauthClientSecret: 'my-client-secret',
* oauthTokenUrl: 'https://my-tenant.authentication.sap.hana.ondemand.com/oauth/token'
* });
*/
export interface SapClientConfig {
/**
* Base URL of the SAP API (e.g. https://tenant.sap-api.com/api/v1)
* If not provided, the SAP_BASE_URL environment variable will be used
*/
baseUrl?: string;
/**
* OAuth client ID for authentication
* If not provided, the SAP_OAUTH_CLIENT_ID environment variable will be used
*/
oauthClientId?: string;
/**
* OAuth client secret for authentication
* If not provided, the SAP_OAUTH_CLIENT_SECRET environment variable will be used
*/
oauthClientSecret?: string;
/**
* OAuth token URL for retrieving tokens
* If not provided, the SAP_OAUTH_TOKEN_URL environment variable will be used
*/
oauthTokenUrl?: string;
/**
* Enable response format normalization
* If true, the client will attempt to normalize different SAP API response formats
* Default: true
*/
normalizeResponses?: boolean;
/**
* Maximum number of retries for failed requests
* Default: 0 (no retries)
*/
maxRetries?: number;
/**
* Delay between retries in milliseconds
* Default: 1000 (1 second)
*/
retryDelay?: number;
/**
* Aktiviere oder deaktiviere benutzerdefinierte Client-Erweiterungen
* Default: true
*/
enableCustomClients?: boolean;
}
/**
* Main SAP Client class that provides access to all SAP API endpoints
*
* This client handles:
* - Authentication via OAuth 2.0 client credentials flow
* - Token management (expiration and renewal)
* - CSRF token handling for write operations
* - Type-safe API access through generated API clients
* - Error handling
* - Response format normalization
*
* @example
* // Basic usage with environment variables
* import SapClient from 'sap-integration-suite-client';
* const client = new SapClient();
*
* // With explicit configuration
* const client = new SapClient({
* baseUrl: 'https://tenant.sap-api.com/api/v1',
* oauthClientId: 'client-id',
* oauthClientSecret: 'client-secret',
* oauthTokenUrl: 'https://tenant.authentication.sap.hana.ondemand.com/oauth/token'
* });
*
* // Making API calls
* async function getPackages() {
* const response = await client.integrationContent.integrationPackages.integrationPackagesList();
* return response.data;
* }
*/
declare class SapClient {
/** Axios instance used for HTTP requests */
private axiosInstance;
/** Base URL of the SAP API */
private baseUrl;
/** CSRF token for write operations */
private csrfToken;
/** OAuth token with expiration information */
private oauthToken;
/** OAuth client ID */
private oauthClientId;
/** OAuth client secret */
private oauthClientSecret;
/** OAuth token URL */
private oauthTokenUrl;
/** Whether to normalize API responses */
private normalizeResponses;
/** Maximum number of retries for failed requests */
private maxRetries;
/** Delay between retries in milliseconds */
private retryDelay;
/** Whether to enable custom client extensions */
private enableCustomClients;
/** Registry für benutzerdefinierte Client-Erweiterungen */
private customClientRegistry;
/** Map der erstellten benutzerdefinierten Clients */
private customClients;
/**
* Integration Content API client
* Provides access to integration packages, artifacts, and related operations
*/
integrationContent: IntegrationContentClient;
/**
* Integration Content Advanced API client
* Provides extended functionality for integration content operations
*/
integrationContentAdvanced: IntegrationContentAdvancedClient;
/**
* Log Files API client
* Access system logs and log archives
*/
logFiles: LogFilesClient;
/**
* Message Processing Logs API client
* Access and query message processing logs
*/
messageProcessingLogs: MessageProcessingLogsClient;
/**
* Message Store API client
* Access stored messages and attachments
*/
messageStore: MessageStoreClient;
/**
* Security Content API client
* Manage security artifacts like credentials and certificates
*/
securityContent: SecurityContentClient;
/**
* Creates a new SAP Client instance
*
* @param {SapClientConfig} [config] - Configuration options for the client
* @throws {Error} If required configuration is missing
*
* @example
* // Using environment variables
* const client = new SapClient();
*
* @example
* // Using explicit configuration
* const client = new SapClient({
* baseUrl: 'https://tenant.sap-api.com/api/v1',
* oauthClientId: 'client-id',
* oauthClientSecret: 'client-secret',
* oauthTokenUrl: 'https://tenant.authentication.sap.hana.ondemand.com/oauth/token'
* });
*/
constructor(config?: SapClientConfig);
/**
* Validates that the required configuration is provided
*
* @private
* @throws {Error} If baseUrl is missing
* @throws {Error} If any OAuth configuration is incomplete
*/
private validateConfig;
/**
* Gets or refreshes the OAuth token
* - Returns existing token if it's still valid
* - Fetches a new token if the current one is expired or doesn't exist
* - Adds expiration timestamp to the token
*
* @private
* @returns {Promise<OAuthToken>} Promise resolving to the OAuth token
* @throws {Error} If token cannot be obtained
*/
private getOAuthToken;
/**
* Normalizes different SAP API response formats into a consistent structure
*
* @private
* @param {any} data - The response data to normalize
* @returns {any} Normalized response data
*/
private normalizeResponseFormat;
/**
* Enhances error objects with more detailed information
*
* @private
* @param {any} error - The error object
* @param {string} [message] - Optional message to prefix the error
* @returns {any} Enhanced error object
*/
private enhanceError;
/**
* Performs an HTTP request with retry logic for transient errors
*
* @private
* @param {Function} requestFn - Function that performs the request
* @param {number} retries - Number of retries left
* @returns {Promise<any>} Promise resolving to the response
*/
private requestWithRetry;
/**
* Custom fetch implementation that uses axios to make HTTP requests
* - Handles CSRF token for write operations
* - Adds OAuth token to requests
* - Transforms axios responses to fetch API Response objects
* - Implements retry logic
* - Normalizes response formats
*
* @private
* @param {string | URL | Request} input - URL or Request object
* @param {RequestInit} [init] - Request initialization options
* @returns {Promise<Response>} Promise resolving to a Response object
* @throws {Error} If the request fails
*/
private customFetch;
/**
* Ensures a CSRF token is available for write operations
* - Fetches a new token if one doesn't exist
* - Uses OAuth token for authentication
*
* @private
* @returns {Promise<void>}
* @throws {Error} If the CSRF token cannot be fetched
*/
private ensureCsrfToken;
/**
* Initialisiert alle benutzerdefinierten Client-Erweiterungen
*/
private initializeCustomClients;
/**
* Gibt einen vorhandenen benutzerdefinierten Client zurück oder erstellt einen neuen
*
* @param type - Der Typ des benutzerdefinierten Clients
* @param baseClient - Der zugrundeliegende Standard-Client
* @returns Eine Instanz des benutzerdefinierten Clients
*/
private getOrCreateCustomClient;
/**
* Gibt einen benutzerdefinierten Client nach Typ zurück
*
* @param type - Der Typ des benutzerdefinierten Clients
* @returns Der benutzerdefinierte Client oder undefined, wenn nicht gefunden
*/
getCustomClient<C extends BaseCustomClient<any>>(type: string): C | undefined;
/**
* Registriert einen benutzerdefinierten Client-Factory
*
* Diese Methode erlaubt es, zur Laufzeit neue benutzerdefinierte Client-Factories
* zu registrieren, was die dynamische Erweiterung des Clients ermöglicht.
*
* @param type - Der Typ des benutzerdefinierten Clients
* @param factory - Die Factory für die Erstellung des Clients
*/
registerCustomClientFactory<T, C extends BaseCustomClient<T>>(type: string, factory: CustomClientFactory<T, C>): void;
}
/**
* Export the SapClient class as default export
*/
export default SapClient;