@limitly/limitly-js
Version:
Official Node.js SDK for Limitly - API Key management, plans, users and request validation
106 lines • 3.86 kB
JavaScript
;
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 types_1 = require("./types");
/**
* HTTP client for making requests to the Limitly API
*/
class HttpClient {
constructor(config) {
this.apiKey = config.apiKey;
this.baseUrl = config.baseUrl || 'https://xfkyofkqbukqtxcuapvf.supabase.co/functions/v1';
this.timeout = config.timeout || 30000;
}
/**
* Makes an HTTP request to the Limitly API
* @param endpoint - The API endpoint to call
* @param options - Axios request configuration
* @param requestOptions - Additional request options
* @returns Promise with the response data
*/
async makeRequest(endpoint, options = {}, requestOptions) {
const url = `${this.baseUrl}${endpoint}`;
const requestConfig = {
...options,
headers: {
'Authorization': `Bearer ${this.apiKey}`,
'Content-Type': 'application/json',
...options.headers,
...requestOptions?.headers,
},
timeout: requestOptions?.timeout || this.timeout,
};
try {
const response = await (0, axios_1.default)(url, requestConfig);
return response.data;
}
catch (error) {
if (error instanceof types_1.LimitlyError) {
throw error;
}
if (error.response) {
// Server response error
const data = error.response.data;
throw new types_1.LimitlyError(data.error || `HTTP ${error.response.status}: ${error.response.statusText}`, error.response.status, data);
}
else if (error.request) {
// Network error
throw new types_1.LimitlyError(`Network error: ${error.message}`, 0, { originalError: error.message });
}
else {
// Unexpected error
throw new types_1.LimitlyError('Unknown error occurred', 0);
}
}
}
/**
* Makes a GET request to the API
* @param endpoint - The API endpoint
* @param requestOptions - Additional request options
* @returns Promise with the response data
*/
async get(endpoint, requestOptions) {
return this.makeRequest(endpoint, { method: 'GET' }, requestOptions);
}
/**
* Makes a POST request to the API
* @param endpoint - The API endpoint
* @param body - Request body data
* @param requestOptions - Additional request options
* @returns Promise with the response data
*/
async post(endpoint, body, requestOptions) {
return this.makeRequest(endpoint, {
method: 'POST',
data: body,
}, requestOptions);
}
/**
* Makes a PUT request to the API
* @param endpoint - The API endpoint
* @param body - Request body data
* @param requestOptions - Additional request options
* @returns Promise with the response data
*/
async put(endpoint, body, requestOptions) {
return this.makeRequest(endpoint, {
method: 'PUT',
data: body,
}, requestOptions);
}
/**
* Makes a DELETE request to the API
* @param endpoint - The API endpoint
* @param requestOptions - Additional request options
* @returns Promise with the response data
*/
async delete(endpoint, requestOptions) {
return this.makeRequest(endpoint, { method: 'DELETE' }, requestOptions);
}
}
exports.HttpClient = HttpClient;
//# sourceMappingURL=client.js.map