UNPKG

sfcc-dev-mcp

Version:

MCP server for Salesforce B2C Commerce Cloud development assistance including logs, debugging, and development tools

117 lines 4.01 kB
/** * Base HTTP Client for SFCC API requests * * This module provides a foundation for making authenticated HTTP requests to SFCC APIs. * It handles common concerns like authentication, request/response formatting, and error handling. */ import { Logger } from '../../utils/logger.js'; /** * Base HTTP client for SFCC API communication */ export class BaseHttpClient { baseUrl; logger; constructor(baseUrl, loggerContext) { this.baseUrl = baseUrl; this.logger = Logger.getChildLogger(loggerContext); } /** * Handle authentication errors - can be overridden by subclasses */ async handleAuthError() { // Default implementation does nothing // Subclasses can override to clear tokens, retry, etc. } /** * Make an authenticated HTTP request */ async makeRequest(endpoint, options = {}) { const url = `${this.baseUrl}${endpoint}`; const method = options.method ?? 'GET'; this.logger.debug(`Making ${method} request to: ${endpoint}`); // Get authentication headers const authHeaders = await this.getAuthHeaders(); const requestOptions = { ...options, headers: { 'Content-Type': 'application/json', ...authHeaders, ...options.headers, }, }; try { const response = await fetch(url, requestOptions); if (!response.ok) { // Handle authentication errors if (response.status === 401) { this.logger.debug('Received 401, attempting to handle auth error'); await this.handleAuthError(); // Retry with fresh authentication const newAuthHeaders = await this.getAuthHeaders(); requestOptions.headers = { ...requestOptions.headers, ...newAuthHeaders, }; const retryResponse = await fetch(url, requestOptions); if (!retryResponse.ok) { const errorText = await retryResponse.text(); throw new Error(`Request failed after retry: ${retryResponse.status} ${retryResponse.statusText} - ${errorText}`); } this.logger.debug('Retry request successful'); return retryResponse.json(); } const errorText = await response.text(); throw new Error(`Request failed: ${response.status} ${response.statusText} - ${errorText}`); } this.logger.debug(`Request to ${endpoint} completed successfully`); return response.json(); } catch (error) { this.logger.error(`Network error during request to ${endpoint}: ${error}`); throw error; } } /** * GET request */ async get(endpoint) { return this.makeRequest(endpoint, { method: 'GET' }); } /** * POST request */ async post(endpoint, data) { const options = { method: 'POST' }; if (data) { options.body = JSON.stringify(data); } return this.makeRequest(endpoint, options); } /** * PUT request */ async put(endpoint, data) { const options = { method: 'PUT' }; if (data) { options.body = JSON.stringify(data); } return this.makeRequest(endpoint, options); } /** * PATCH request */ async patch(endpoint, data) { const options = { method: 'PATCH' }; if (data) { options.body = JSON.stringify(data); } return this.makeRequest(endpoint, options); } /** * DELETE request */ async delete(endpoint) { return this.makeRequest(endpoint, { method: 'DELETE' }); } } //# sourceMappingURL=http-client.js.map