n8n
Version:
n8n Workflow Automation Tool
107 lines • 6.52 kB
JavaScript
;
const api_types_1 = require("@n8n/api-types");
const backend_network_1 = require("@n8n/backend-network");
const config_1 = require("@n8n/config");
const di_1 = require("@n8n/di");
const bad_request_error_1 = require("../../../../errors/response-errors/bad-request.error");
const conflict_error_1 = require("../../../../errors/response-errors/conflict.error");
const not_found_error_1 = require("../../../../errors/response-errors/not-found.error");
const event_message_classes_1 = require("../../../../eventbus/event-message-classes");
const message_event_bus_1 = require("../../../../eventbus/message-event-bus/message-event-bus");
const create_message_event_bus_destination_1 = require("../../../../modules/log-streaming.ee/create-message-event-bus-destination");
const log_streaming_destination_service_1 = require("../../../../modules/log-streaming.ee/log-streaming-destination.service");
const log_streaming_mapper_1 = require("./log-streaming.mapper");
const global_middleware_1 = require("../../shared/middlewares/global.middleware");
const assertNotManagedByEnv = () => {
if (di_1.Container.get(config_1.InstanceSettingsLoaderConfig).logStreamingManagedByEnv) {
throw new conflict_error_1.ConflictError('Log streaming destinations are managed via environment variables and cannot be modified through the API');
}
};
const findDestinationOrFail = async (id) => {
const [destination] = await di_1.Container.get(log_streaming_destination_service_1.LogStreamingDestinationService).findDestination(id);
if (!destination) {
throw new not_found_error_1.NotFoundError(`Log streaming destination with id "${id}" could not be found`);
}
return destination;
};
const logStreamingHandlers = {
getEventTypes: [
(0, global_middleware_1.isLicensed)('feat:logStreaming'),
(0, global_middleware_1.apiKeyHasScopeWithGlobalScopeFallback)({ scope: 'eventBusDestination:list' }),
async (_req, res) => {
return res.json({ data: event_message_classes_1.eventNamesAll });
},
],
getDestinations: [
(0, global_middleware_1.isLicensed)('feat:logStreaming'),
(0, global_middleware_1.apiKeyHasScopeWithGlobalScopeFallback)({ scope: 'eventBusDestination:list' }),
async (_req, res) => {
const destinations = await di_1.Container.get(log_streaming_destination_service_1.LogStreamingDestinationService).findDestination();
return res.json({ data: destinations.map(log_streaming_mapper_1.toPublicDestination) });
},
],
getDestination: [
(0, global_middleware_1.isLicensed)('feat:logStreaming'),
(0, global_middleware_1.apiKeyHasScopeWithGlobalScopeFallback)({ scope: 'eventBusDestination:read' }),
async (req, res) => {
const destination = await findDestinationOrFail(req.params.id);
return res.json((0, log_streaming_mapper_1.toPublicDestination)(destination));
},
],
createDestination: [
(0, global_middleware_1.isLicensed)('feat:logStreaming'),
(0, global_middleware_1.apiKeyHasScopeWithGlobalScopeFallback)({ scope: 'eventBusDestination:create' }),
async (req, res) => {
assertNotManagedByEnv();
const parseResult = api_types_1.PublicCreateDestinationDto.safeParse(req.body);
if (!parseResult.success) {
throw new bad_request_error_1.BadRequestError(parseResult.error.errors[0].message);
}
const destination = (0, create_message_event_bus_destination_1.createMessageEventBusDestination)(di_1.Container.get(message_event_bus_1.MessageEventBus), di_1.Container.get(backend_network_1.OutboundHttp), (0, log_streaming_mapper_1.toInternalDestinationOptions)(parseResult.data));
const result = await di_1.Container.get(log_streaming_destination_service_1.LogStreamingDestinationService).addDestination(destination);
return res.json((0, log_streaming_mapper_1.toPublicDestination)(result.serialize()));
},
],
updateDestination: [
(0, global_middleware_1.isLicensed)('feat:logStreaming'),
(0, global_middleware_1.apiKeyHasScopeWithGlobalScopeFallback)({ scope: 'eventBusDestination:update' }),
async (req, res) => {
assertNotManagedByEnv();
await findDestinationOrFail(req.params.id);
const parseResult = api_types_1.PublicCreateDestinationDto.safeParse(req.body);
if (!parseResult.success) {
throw new bad_request_error_1.BadRequestError(parseResult.error.errors[0].message);
}
const destination = (0, create_message_event_bus_destination_1.createMessageEventBusDestination)(di_1.Container.get(message_event_bus_1.MessageEventBus), di_1.Container.get(backend_network_1.OutboundHttp), { ...(0, log_streaming_mapper_1.toInternalDestinationOptions)(parseResult.data), id: req.params.id });
const result = await di_1.Container.get(log_streaming_destination_service_1.LogStreamingDestinationService).addDestination(destination);
return res.json((0, log_streaming_mapper_1.toPublicDestination)(result.serialize()));
},
],
testDestination: [
(0, global_middleware_1.isLicensed)('feat:logStreaming'),
(0, global_middleware_1.apiKeyHasScopeWithGlobalScopeFallback)({ scope: 'eventBusDestination:test' }),
async (req, res) => {
await findDestinationOrFail(req.params.id);
let success;
try {
success = await di_1.Container.get(log_streaming_destination_service_1.LogStreamingDestinationService).testDestination(req.params.id);
}
catch {
success = false;
}
return res.json({ success });
},
],
deleteDestination: [
(0, global_middleware_1.isLicensed)('feat:logStreaming'),
(0, global_middleware_1.apiKeyHasScopeWithGlobalScopeFallback)({ scope: 'eventBusDestination:delete' }),
async (req, res) => {
assertNotManagedByEnv();
const destination = await findDestinationOrFail(req.params.id);
await di_1.Container.get(log_streaming_destination_service_1.LogStreamingDestinationService).removeDestination(req.params.id);
return res.json((0, log_streaming_mapper_1.toPublicDestination)(destination));
},
],
};
module.exports = logStreamingHandlers;
//# sourceMappingURL=log-streaming.handler.js.map