watchtower-node-sdk
Version:
A TypeScript Node.js SDK for the Watchtower API, providing API key management, connection string generation, and more
171 lines • 6.7 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.LogsEndpoint = void 0;
const base_1 = require("../base");
const errors_1 = require("../../errors");
class LogsEndpoint extends base_1.BaseEndpoint {
constructor(client) {
super(client, '/api/logs');
}
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');
}
}
validateTimestamp(timestamp) {
if (!/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d{3})?Z$/.test(timestamp)) {
throw new errors_1.InvalidRequestError('timestamp must be in ISO 8601 format');
}
}
/**
* Create a log entry
* @param data - The log creation request
* @returns Promise that resolves if the log is created (201)
* @throws {InvalidRequestError} If required fields are missing or invalid
* @throws {AuthenticationError} If API keys are invalid
* @throws {ServerError} If server encounters an error
*/
async createLog(data) {
this.validateRequiredKeys(data);
this.validateTimestamp(data.timestamp);
try {
await this.post('', 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;
}
}
/**
* Create a batch of log entries
* @param data - The batch log creation request
* @returns Promise with the batch log response
* @throws {InvalidRequestError} If batch size exceeds limit or required fields are missing
* @throws {AuthenticationError} If API keys are invalid
* @throws {ServerError} If server encounters an error
*/
async createBatchLogs(data) {
this.validateRequiredKeys(data);
if (data.logs.length > errors_1.BATCH_SIZE_LIMIT) {
throw new errors_1.InvalidRequestError(`Batch size cannot exceed ${errors_1.BATCH_SIZE_LIMIT} logs`);
}
data.logs.forEach(log => {
this.validateTimestamp(log.timestamp);
});
try {
return await this.post('/batch', 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 logs for a specific item
* @param data - The get logs request parameters
* @returns Promise with the paginated log response
* @throws {InvalidRequestError} If required fields are missing
* @throws {AuthenticationError} If API keys are invalid
* @throws {ServerError} If server encounters an error
*/
async getLogs(data) {
this.validateRequiredKeys(data);
if (data.before)
this.validateTimestamp(data.before);
if (data.after)
this.validateTimestamp(data.after);
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 latest logs for a specific item
* @param data - The get latest logs request parameters
* @returns Promise with the latest logs response
* @throws {InvalidRequestError} If required fields are missing
* @throws {AuthenticationError} If API keys are invalid
* @throws {ServerError} If server encounters an error
*/
async getLatestLogs(data) {
this.validateRequiredKeys(data);
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;
}
}
/**
* Get a specific log by ID
* @param id - The log ID
* @param data - The get log by ID request parameters
* @returns Promise with the log response
* @throws {InvalidRequestError} If required fields are missing
* @throws {AuthenticationError} If API keys are invalid
* @throws {NotFoundError} If log is not found
* @throws {ServerError} If server encounters an error
*/
async getLogById(id, data) {
this.validateRequiredKeys(data);
try {
return await this.get(`/${id}`, { 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 === 404) {
throw new errors_1.NotFoundError(`Log with ID ${id} not found`);
}
if (error.response?.status === 500) {
throw new errors_1.ServerError('Internal server error');
}
throw error;
}
}
}
exports.LogsEndpoint = LogsEndpoint;
//# sourceMappingURL=handler.js.map