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