watchtower-node-sdk
Version:
A TypeScript Node.js SDK for the Watchtower API, providing API key management, connection string generation, and more
123 lines • 4.89 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.AnalyzeEndpoint = void 0;
const base_1 = require("../base");
const errors_1 = require("../../errors");
class AnalyzeEndpoint extends base_1.BaseEndpoint {
constructor(client) {
super(client, '/api/analyze');
}
validateRequiredKeys(data) {
if (!data.organization_apikey) {
throw new errors_1.InvalidRequestError('organization_apikey is required');
}
if (!data.app_apikey) {
throw new errors_1.InvalidRequestError('app_apikey is required');
}
}
/**
* Get the latest analysis for an item
* @param data - The request parameters
* @returns Promise with the analysis result
* @throws {InvalidRequestError} If required fields are missing or invalid
* @throws {AuthenticationError} If API keys are invalid
* @throws {ServerError} If server encounters an error
*/
async getLatest(data) {
this.validateRequiredKeys(data);
if (!data.item_id) {
throw new errors_1.InvalidRequestError('item_id is required');
}
try {
return await this.get('/latest', { params: data });
}
catch (error) {
if (error.response?.status === 400) {
throw new errors_1.InvalidRequestError(error.response.data?.message || 'Invalid request');
}
if (error.response?.status === 401) {
throw new errors_1.AuthenticationError('Invalid API keys');
}
if (error.response?.status === 500) {
throw new errors_1.ServerError('Internal server error');
}
throw error;
}
}
/**
* Analyze logs for an item within a specified time range
* @param data - The request parameters
* @returns Promise with the analysis result
* @throws {InvalidRequestError} If required fields are missing or invalid
* @throws {AuthenticationError} If API keys are invalid
* @throws {ServerError} If server encounters an error
*/
async analyzeLogs(data) {
this.validateRequiredKeys(data);
if (!data.item_id) {
throw new errors_1.InvalidRequestError('item_id is required');
}
// Validate timestamp format if provided
if (data.after && !this.isValidISODate(data.after)) {
throw new errors_1.InvalidRequestError('after must be a valid ISO 8601 timestamp');
}
if (data.before && !this.isValidISODate(data.before)) {
throw new errors_1.InvalidRequestError('before must be a valid ISO 8601 timestamp');
}
try {
return await this.get('', { params: data });
}
catch (error) {
if (error.response?.status === 400) {
throw new errors_1.InvalidRequestError(error.response.data?.message || 'Invalid request');
}
if (error.response?.status === 401) {
throw new errors_1.AuthenticationError('Invalid API keys');
}
if (error.response?.status === 500) {
throw new errors_1.ServerError('Internal server error');
}
throw error;
}
}
/**
* Get the current analysis for an item
* @param data - The request parameters
* @returns Promise with the current analysis response
* @throws {InvalidRequestError} If required fields are missing or invalid
* @throws {AuthenticationError} If API keys are invalid
* @throws {ServerError} If server encounters an error
*/
async analyzeCurrent(data) {
this.validateRequiredKeys(data);
if (!data.item_id) {
throw new errors_1.InvalidRequestError('item_id is required');
}
try {
return await this.get('/current', { params: data });
}
catch (error) {
if (error.response?.status === 400) {
throw new errors_1.InvalidRequestError(error.response.data?.message || 'Invalid request');
}
if (error.response?.status === 401) {
throw new errors_1.AuthenticationError('Invalid API keys');
}
if (error.response?.status === 500) {
throw new errors_1.ServerError('Internal server error');
}
throw error;
}
}
/**
* Validate if a string is a valid ISO 8601 date
* @param dateString - The date string to validate
* @returns boolean indicating if the string is a valid ISO 8601 date
*/
isValidISODate(dateString) {
const date = new Date(dateString);
return date instanceof Date && !isNaN(date.getTime()) && dateString === date.toISOString();
}
}
exports.AnalyzeEndpoint = AnalyzeEndpoint;
//# sourceMappingURL=handler.js.map