watchtower-node-sdk
Version:
A TypeScript Node.js SDK for the Watchtower API, providing API key management, connection string generation, and more
116 lines • 4.61 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.IntelligenceEndpoint = void 0;
const base_1 = require("../base");
const errors_1 = require("../../errors");
class IntelligenceEndpoint extends base_1.BaseEndpoint {
constructor(client) {
super(client, '/api/intelligence');
}
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 prediction configuration and available options
* @returns Promise with the prediction configuration
* @throws {AuthenticationError} If API keys are invalid
* @throws {ServerError} If server encounters an error
*/
async getPredictConfig() {
try {
return await this.get('/predict/config');
}
catch (error) {
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 prediction for an item
* @param data - The prediction request parameters
* @returns Promise with the prediction 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 predict(data) {
this.validateRequiredKeys(data);
if (!data.item_id) {
throw new errors_1.InvalidRequestError('item_id is required');
}
if (!data.prediction_type) {
throw new errors_1.InvalidRequestError('prediction_type is required');
}
if (!data.timeframe) {
throw new errors_1.InvalidRequestError('timeframe is required');
}
try {
return await this.post('/predict', 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 decision based on log data
* @param data - The decision request parameters
* @returns Promise with the decision 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 decide(data) {
this.validateRequiredKeys(data);
if (!data.log_data) {
throw new errors_1.InvalidRequestError('log_data is required');
}
if (!data.log_data.metrics || !Array.isArray(data.log_data.metrics) || data.log_data.metrics.length === 0) {
throw new errors_1.InvalidRequestError('log_data.metrics must be a non-empty array');
}
if (!data.log_data.context) {
throw new errors_1.InvalidRequestError('log_data.context is required');
}
if (!data.log_data.item_name) {
throw new errors_1.InvalidRequestError('log_data.item_name is required');
}
if (!data.allowed_actions || Object.keys(data.allowed_actions).length === 0) {
throw new errors_1.InvalidRequestError('allowed_actions must be a non-empty object');
}
try {
return await this.post('/decide', 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;
}
}
}
exports.IntelligenceEndpoint = IntelligenceEndpoint;
//# sourceMappingURL=handler.js.map