recallrai
Version:
Official Node.js SDK for RecallrAI - Revolutionary contextual memory system that enables AI assistants to form meaningful connections between conversations, just like human memory.
125 lines (124 loc) • 3.92 kB
JavaScript
"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");
class HTTPClient {
constructor(options) {
const { apiKey, projectId, baseUrl, timeout } = options;
this.client = axios_1.default.create({
baseURL: baseUrl.replace(/\/$/, ''),
timeout,
headers: {
'X-Api-Key': apiKey,
'X-Project-Id': projectId,
'Content-Type': 'application/json',
'Accept': 'application/json',
'User-Agent': 'RecallrAI-Node-SDK',
},
});
}
/**
* Make a GET request.
*
* @param path API endpoint path
* @param config Additional request configuration
* @returns Response data
*/
async get(path, config) {
try {
return await this.client.get(path, config);
}
catch (error) {
throw this.handleError(error);
}
}
/**
* Make a POST request.
*
* @param path API endpoint path
* @param data Request body data
* @param config Additional request configuration
* @returns Response data
*/
async post(path, data, config) {
try {
return await this.client.post(path, data, config);
}
catch (error) {
throw this.handleError(error);
}
}
/**
* Make a PUT request.
*
* @param path API endpoint path
* @param data Request body data
* @param config Additional request configuration
* @returns Response data
*/
async put(path, data, config) {
try {
return await this.client.put(path, data, config);
}
catch (error) {
throw this.handleError(error);
}
}
/**
* Make a DELETE request.
*
* @param path API endpoint path
* @param config Additional request configuration
* @returns Response data
*/
async delete(path, config) {
try {
return await this.client.delete(path, config);
}
catch (error) {
throw this.handleError(error);
}
}
/**
* Handle errors and convert to appropriate SDK errors.
*
* @param error The original error
* @returns A standardized error
*/
handleError(error) {
if (axios_1.default.isAxiosError(error)) {
const status = error.response?.status;
const data = error.response?.data || {};
const detail = data.detail || 'An unknown error occurred';
if (error.code === 'ECONNABORTED') {
return new errors_1.TimeoutError('Request timed out');
}
if (error.code === 'ENOTFOUND' || error.code === 'ECONNREFUSED') {
return new errors_1.ConnectionError(`Failed to connect to the API: ${error.message}`);
}
if (!error.response) {
return new errors_1.NetworkError(`Network error occurred: ${error.message}`);
}
if (status === 422) {
return new errors_1.ValidationError(detail);
}
if (status === 500) {
return new errors_1.InternalServerError(detail);
}
if (status === 401) {
return new errors_1.AuthenticationError(detail);
}
// Add error information to the error object
const customError = new Error(detail);
customError.status = status;
customError.data = data;
return customError;
}
return error;
}
}
exports.HTTPClient = HTTPClient;