| | | 1 | | using Elsa.Authorization; |
| | | 2 | | using System.Text; |
| | | 3 | | using Elsa.Abstractions; |
| | | 4 | | using Elsa.Common.Models; |
| | | 5 | | using Elsa.Expressions.JavaScript.Permissions; |
| | | 6 | | using Elsa.Expressions.JavaScript.TypeDefinitions.Contracts; |
| | | 7 | | using Elsa.Expressions.JavaScript.TypeDefinitions.Models; |
| | | 8 | | using Elsa.Workflows.Management; |
| | | 9 | | using Elsa.Workflows.Models; |
| | | 10 | | using JetBrains.Annotations; |
| | | 11 | | |
| | | 12 | | namespace Elsa.Expressions.JavaScript.Endpoints.TypeDefinitions; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Returns a TypeScript definition that is used by the Monaco editor to display intellisense for JavaScript expressions |
| | | 16 | | /// </summary> |
| | | 17 | | [PublicAPI] |
| | | 18 | | internal class Get : ElsaEndpoint<Request> |
| | | 19 | | { |
| | | 20 | | private readonly ITypeDefinitionService _typeDefinitionService; |
| | | 21 | | private readonly IWorkflowDefinitionService _workflowDefinitionService; |
| | | 22 | | |
| | | 23 | | /// <inheritdoc /> |
| | 3 | 24 | | public Get(ITypeDefinitionService typeDefinitionService, IWorkflowDefinitionService workflowDefinitionService) |
| | | 25 | | { |
| | 3 | 26 | | _typeDefinitionService = typeDefinitionService; |
| | 3 | 27 | | _workflowDefinitionService = workflowDefinitionService; |
| | 3 | 28 | | } |
| | | 29 | | |
| | | 30 | | /// <inheritdoc /> |
| | | 31 | | public override void Configure() |
| | | 32 | | { |
| | 3 | 33 | | Post("scripting/javascript/type-definitions/{workflowDefinitionId}"); |
| | 3 | 34 | | RequirePermission(JavaScriptPermissions.Scripting, CoreVerbs.View); |
| | 3 | 35 | | } |
| | | 36 | | |
| | | 37 | | /// <inheritdoc /> |
| | | 38 | | public override async Task HandleAsync(Request request, CancellationToken cancellationToken) |
| | | 39 | | { |
| | 0 | 40 | | var workflowGraph = await GetWorkflowGraphAsync(request.WorkflowDefinitionId, cancellationToken); |
| | | 41 | | |
| | 0 | 42 | | if (workflowGraph == null) |
| | | 43 | | { |
| | 0 | 44 | | AddError($"Workflow definition {request.WorkflowDefinitionId} not found"); |
| | 0 | 45 | | await Send.ErrorsAsync(cancellation: cancellationToken); |
| | 0 | 46 | | return; |
| | | 47 | | } |
| | | 48 | | |
| | 0 | 49 | | var typeDefinitionContext = new TypeDefinitionContext(workflowGraph, request.ActivityTypeName, request.PropertyN |
| | 0 | 50 | | var typeDefinitions = await _typeDefinitionService.GenerateTypeDefinitionsAsync(typeDefinitionContext); |
| | 0 | 51 | | var fileName = $"elsa.{request.WorkflowDefinitionId}.d.ts"; |
| | 0 | 52 | | var data = Encoding.UTF8.GetBytes(typeDefinitions); |
| | | 53 | | |
| | 0 | 54 | | await Send.BytesAsync(data, fileName, "application/x-typescript", cancellation: cancellationToken); |
| | 0 | 55 | | } |
| | | 56 | | |
| | | 57 | | private async Task<WorkflowGraph?> GetWorkflowGraphAsync(string workflowDefinitionId, CancellationToken cancellation |
| | | 58 | | { |
| | 0 | 59 | | return await _workflowDefinitionService.FindWorkflowGraphAsync(workflowDefinitionId, VersionOptions.Latest, canc |
| | 0 | 60 | | } |
| | | 61 | | } |
| | | 62 | | |
| | | 63 | | internal record Request(string WorkflowDefinitionId, string? ActivityTypeName, string? PropertyName) |
| | | 64 | | { |
| | | 65 | | public Request() : this(null!, null!, null) |
| | | 66 | | { |
| | | 67 | | } |
| | | 68 | | } |