vulncheck-sdk
Version:
A comprehensive TypeScript/JavaScript SDK for the VulnCheck API - vulnerability intelligence platform with enriched CVE data, threat intelligence, and security tooling
87 lines • 3.27 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 errors_1 = require("../errors");
const utils_1 = require("../utils");
class HttpClient {
constructor(config) {
this.config = config;
this.client = axios_1.default.create({
baseURL: config.baseURL || 'https://api.vulncheck.com',
timeout: config.timeout || 30000,
headers: {
'Authorization': `Bearer ${config.apiKey}`,
'Content-Type': 'application/json',
'User-Agent': 'vulncheck-sdk/0.1.0'
}
});
this.setupInterceptors();
}
setupInterceptors() {
this.client.interceptors.request.use((config) => {
config.metadata = { startTime: Date.now() };
return config;
}, (error) => Promise.reject(error));
this.client.interceptors.response.use((response) => response, (error) => {
const endpoint = error.config?.url;
throw (0, errors_1.createErrorFromResponse)(error, endpoint);
});
}
async get(url, config) {
return this.request('GET', url, undefined, config);
}
async post(url, data, config) {
return this.request('POST', url, data, config);
}
async put(url, data, config) {
return this.request('PUT', url, data, config);
}
async delete(url, config) {
return this.request('DELETE', url, undefined, config);
}
async request(method, url, data, config) {
const maxRetries = this.config.retryAttempts || 3;
let lastError;
for (let attempt = 0; attempt <= maxRetries; attempt++) {
try {
const response = await this.client.request({
method,
url,
data,
...config
});
return response.data;
}
catch (error) {
lastError = error;
if (error?.name === 'VulnCheckAuthError' ||
error?.name === 'VulnCheckValidationError' ||
error?.name === 'VulnCheckNotFoundError') {
throw error;
}
if (error?.name === 'VulnCheckRateLimitError') {
const retryAfter = error?.retryAfter || Math.pow(2, attempt) * 1000;
if (attempt < maxRetries) {
await (0, utils_1.sleep)(retryAfter);
continue;
}
}
if (error?.name === 'VulnCheckServerError' && attempt < maxRetries) {
const backoffTime = Math.pow(2, attempt) * 1000;
await (0, utils_1.sleep)(backoffTime);
continue;
}
if (attempt === maxRetries) {
throw error;
}
}
}
throw lastError;
}
}
exports.HttpClient = HttpClient;
//# sourceMappingURL=http.js.map