UNPKG

vulncheck-sdk

Version:

A comprehensive TypeScript/JavaScript SDK for the VulnCheck API - vulnerability intelligence platform with enriched CVE data, threat intelligence, and security tooling

80 lines 2.95 kB
import axios from 'axios'; import { createErrorFromResponse } from '../errors/index.js'; import { sleep } from '../utils/index.js'; export class HttpClient { constructor(config) { this.config = config; this.client = axios.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 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 sleep(retryAfter); continue; } } if (error?.name === 'VulnCheckServerError' && attempt < maxRetries) { const backoffTime = Math.pow(2, attempt) * 1000; await sleep(backoffTime); continue; } if (attempt === maxRetries) { throw error; } } } throw lastError; } } //# sourceMappingURL=http.js.map