n8n
Version:
n8n Workflow Automation Tool
209 lines • 10.2 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.WorkflowsPublicController = void 0;
const api_types_1 = require("@n8n/api-types");
const config_1 = require("@n8n/config");
const decorators_1 = require("@n8n/decorators");
const bad_request_error_1 = require("../../../errors/response-errors/bad-request.error");
const not_found_error_1 = require("../../../errors/response-errors/not-found.error");
const shared_workflow_not_found_error_1 = require("../../../errors/shared-workflow-not-found.error");
const event_service_1 = require("../../../events/event.service");
const pagination_service_1 = require("../../../public-api/v1/shared/services/pagination.service");
const workflow_finder_service_1 = require("../../../workflows/workflow-finder.service");
const workflow_history_service_1 = require("../../../workflows/workflow-history/workflow-history.service");
function toPublicJson(value) {
return value !== null && typeof value === 'object' && !Array.isArray(value)
? value
: null;
}
function toPublicTag(tag) {
return {
id: tag.id,
name: tag.name,
createdAt: tag.createdAt.toISOString(),
updatedAt: tag.updatedAt.toISOString(),
};
}
function toPublicSharedWorkflow(sharedWorkflow) {
return {
role: sharedWorkflow.role,
workflowId: sharedWorkflow.workflowId,
projectId: sharedWorkflow.projectId,
project: {
id: sharedWorkflow.project.id,
name: sharedWorkflow.project.name,
type: sharedWorkflow.project.type,
icon: sharedWorkflow.project.icon,
description: sharedWorkflow.project.description,
customTelemetryTags: sharedWorkflow.project.customTelemetryTags,
creatorId: sharedWorkflow.project.creatorId,
createdAt: sharedWorkflow.project.createdAt.toISOString(),
updatedAt: sharedWorkflow.project.updatedAt.toISOString(),
},
createdAt: sharedWorkflow.createdAt.toISOString(),
updatedAt: sharedWorkflow.updatedAt.toISOString(),
};
}
function toPublicWorkflowPublishHistory(entry) {
return {
id: entry.id,
workflowId: entry.workflowId,
versionId: entry.versionId,
event: entry.event,
userId: entry.userId,
createdAt: entry.createdAt.toISOString(),
};
}
function toPublicActiveVersion(activeVersion) {
return {
versionId: activeVersion.versionId,
workflowId: activeVersion.workflowId,
nodes: activeVersion.nodes,
connections: activeVersion.connections,
nodeGroups: activeVersion.nodeGroups,
authors: activeVersion.authors,
name: activeVersion.name,
description: activeVersion.description,
autosaved: activeVersion.autosaved,
createdAt: activeVersion.createdAt.toISOString(),
updatedAt: activeVersion.updatedAt.toISOString(),
workflowPublishHistory: activeVersion.workflowPublishHistory.map(toPublicWorkflowPublishHistory),
};
}
let WorkflowsPublicController = class WorkflowsPublicController {
constructor(workflowHistoryService, workflowFinderService, eventService, globalConfig) {
this.workflowHistoryService = workflowHistoryService;
this.workflowFinderService = workflowFinderService;
this.eventService = eventService;
this.globalConfig = globalConfig;
}
async getWorkflow(req, _res, workflowId, query) {
const workflow = await this.workflowFinderService.findWorkflowForUser(workflowId, req.user, ['workflow:read'], {
includeTags: !this.globalConfig.tags.disabled,
includeActiveVersion: true,
});
if (!workflow) {
throw new not_found_error_1.NotFoundError('Not Found');
}
this.eventService.emit('user-retrieved-workflow', {
userId: req.user.id,
publicApi: true,
});
return this.toWorkflowPublicDto(workflow, { excludePinnedData: query.excludePinnedData });
}
toWorkflowPublicDto(workflow, options = {}) {
return {
id: workflow.id,
name: workflow.name,
description: workflow.description,
active: workflow.active,
activeVersionId: workflow.activeVersionId,
createdAt: workflow.createdAt.toISOString(),
updatedAt: workflow.updatedAt.toISOString(),
isArchived: workflow.isArchived,
versionId: workflow.versionId,
versionCounter: workflow.versionCounter,
sourceWorkflowId: workflow.sourceWorkflowId,
triggerCount: workflow.triggerCount,
nodes: workflow.nodes,
connections: workflow.connections,
nodeGroups: workflow.nodeGroups,
settings: toPublicJson(workflow.settings),
staticData: toPublicJson(workflow.staticData),
meta: toPublicJson(workflow.meta),
...(options.excludePinnedData ? {} : { pinData: toPublicJson(workflow.pinData) }),
...(workflow.tags ? { tags: workflow.tags.map(toPublicTag) } : {}),
shared: workflow.shared.map(toPublicSharedWorkflow),
activeVersion: workflow.activeVersion ? toPublicActiveVersion(workflow.activeVersion) : null,
};
}
async getWorkflowHistory(req, _res, workflowId, query) {
let { limit, offset } = query;
if (query.cursor) {
try {
const decoded = (0, pagination_service_1.decodeCursor)(query.cursor);
if (!('offset' in decoded)) {
throw new bad_request_error_1.BadRequestError('An invalid cursor was provided');
}
offset = decoded.offset;
limit = decoded.limit;
}
catch (error) {
if (error instanceof bad_request_error_1.BadRequestError)
throw error;
throw new bad_request_error_1.BadRequestError('An invalid cursor was provided');
}
}
try {
const versions = await this.workflowHistoryService.getList(req.user, workflowId, limit + 1, offset);
const hasMore = versions.length > limit;
const data = hasMore ? versions.slice(0, limit) : versions;
return {
data: data.map((version) => ({
...version,
createdAt: version.createdAt.toISOString(),
updatedAt: version.updatedAt.toISOString(),
})),
nextCursor: (0, pagination_service_1.encodeNextCursor)({
offset,
limit,
numberOfTotalRecords: hasMore ? offset + limit + 1 : offset + data.length,
}),
};
}
catch (error) {
if (error instanceof shared_workflow_not_found_error_1.SharedWorkflowNotFoundError) {
throw new not_found_error_1.NotFoundError('Not Found');
}
throw error;
}
}
};
exports.WorkflowsPublicController = WorkflowsPublicController;
__decorate([
(0, decorators_1.Get)('/:workflowId'),
(0, decorators_1.ApiKeyScope)('workflow:read'),
(0, decorators_1.ProjectScope)('workflow:read'),
(0, decorators_1.ApiSummary)('Retrieve a workflow'),
(0, decorators_1.ApiDescription)('Retrieve a workflow.'),
(0, decorators_1.ApiTags)(['Workflow']),
(0, decorators_1.ApiResponse)(200, api_types_1.WorkflowPublicDto),
(0, decorators_1.ApiErrorResponse)(404),
__param(2, (0, decorators_1.Param)('workflowId')),
__param(3, decorators_1.Query),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object, Object, String, api_types_1.GetWorkflowQueryDto]),
__metadata("design:returntype", Promise)
], WorkflowsPublicController.prototype, "getWorkflow", null);
__decorate([
(0, decorators_1.Get)('/:workflowId/history'),
(0, decorators_1.ApiKeyScope)('workflow:read'),
(0, decorators_1.ProjectScope)('workflow:read'),
(0, decorators_1.ApiSummary)('Retrieve workflow version history'),
(0, decorators_1.ApiDescription)('Returns a paginated list of workflow versions (version IDs and metadata) for a workflow.'),
(0, decorators_1.ApiTags)(['Workflow']),
(0, decorators_1.ApiResponse)(200, api_types_1.WorkflowVersionHistoryListPublicDto),
(0, decorators_1.ApiErrorResponse)(404),
__param(2, (0, decorators_1.Param)('workflowId')),
__param(3, decorators_1.Query),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object, Object, String, api_types_1.ListWorkflowHistoryQueryDto]),
__metadata("design:returntype", Promise)
], WorkflowsPublicController.prototype, "getWorkflowHistory", null);
exports.WorkflowsPublicController = WorkflowsPublicController = __decorate([
(0, decorators_1.PublicApiController)('/workflows'),
__metadata("design:paramtypes", [workflow_history_service_1.WorkflowHistoryService, workflow_finder_service_1.WorkflowFinderService, event_service_1.EventService, config_1.GlobalConfig])
], WorkflowsPublicController);
//# sourceMappingURL=workflows.public.controller.js.map