UNPKG

memofai

Version:

Revolutionary dual-track AI memory infrastructure SDK for JavaScript/TypeScript applications

102 lines 4.48 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.HTTPClient = void 0; const axios_1 = __importDefault(require("axios")); const errors_1 = require("../errors"); const utils_1 = require("../utils"); const types_1 = require("../types"); class HTTPClient { constructor(config) { this.config = config; this.client = axios_1.default.create({ baseURL: this.getBaseURL(), timeout: config.timeout || 30000, headers: Object.assign({ 'Content-Type': 'application/json', Authorization: `Bearer ${config.apiKey}`, 'User-Agent': (0, utils_1.createUserAgent)('0.1.0') }, config.headers), }); this.setupInterceptors(); } getBaseURL() { if (this.config.baseURL) { return this.config.baseURL; } const envConfig = types_1.ENVIRONMENTS[this.config.environment]; const apiVersion = this.config.apiVersion || 'v1'; return `${envConfig.baseURL}/api/${apiVersion}`; } setupInterceptors() { this.client.interceptors.request.use(config => { var _a, _b; if (typeof process !== 'undefined' && ((_a = process.env) === null || _a === void 0 ? void 0 : _a.NODE_ENV) === 'development') { console.log(`[MOA SDK] ${(_b = config.method) === null || _b === void 0 ? void 0 : _b.toUpperCase()} ${config.url}`); } return config; }, (error) => Promise.reject(error)); this.client.interceptors.response.use((response) => response, (error) => { var _a; if (error.response) { const moaError = (0, errors_1.createErrorFromResponse)(error.response.status, error.response.data, ((_a = error.response.data) === null || _a === void 0 ? void 0 : _a.message) || error.message); return Promise.reject(moaError); } else if (error.request) { if (error.code === 'ECONNABORTED') { return Promise.reject(new errors_1.MOATimeoutError(this.config.timeout || 30000)); } return Promise.reject(new errors_1.MOANetworkError(error.message || 'Network error occurred')); } else { return Promise.reject(new errors_1.MOAError(error.message || 'Unknown error occurred', 'UNKNOWN_ERROR')); } }); } async request(config) { var _a, _b; const maxRetries = (_b = (_a = config.retryAttempts) !== null && _a !== void 0 ? _a : this.config.retryAttempts) !== null && _b !== void 0 ? _b : 3; let lastError; for (let attempt = 0; attempt <= maxRetries; attempt++) { try { const response = await this.client.request(config); return response.data; } catch (error) { lastError = error; if (error instanceof errors_1.MOAError && (error.statusCode === 401 || error.statusCode === 404 || error.statusCode === 422)) { throw error; } if (attempt === maxRetries) { break; } const delayMs = (0, utils_1.calculateBackoffDelay)(attempt); await (0, utils_1.delay)(delayMs); } } throw lastError; } async get(url, config) { return this.request(Object.assign(Object.assign({}, config), { method: 'GET', url })); } async post(url, data, config) { return this.request(Object.assign(Object.assign({}, config), { method: 'POST', url, data })); } async put(url, data, config) { return this.request(Object.assign(Object.assign({}, config), { method: 'PUT', url, data })); } async delete(url, config) { return this.request(Object.assign(Object.assign({}, config), { method: 'DELETE', url })); } updateApiKey(apiKey) { this.config.apiKey = apiKey; this.client.defaults.headers['Authorization'] = `Bearer ${apiKey}`; } updateBaseURL(baseURL) { this.config.baseURL = baseURL; this.client.defaults.baseURL = this.getBaseURL(); } } exports.HTTPClient = HTTPClient; //# sourceMappingURL=http-client.js.map