< Summary

Information
Class: Elsa.Http.Scripting.JavaScript.HttpJavaScriptHandler
Assembly: Elsa.Http
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Http/Scripting/JavaScript/HttpJavaScriptHandler.cs
Line coverage
36%
Covered lines: 12
Uncovered lines: 21
Coverable lines: 33
Total lines: 84
Line coverage: 36.3%
Branch coverage
0%
Covered branches: 0
Total branches: 4
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Http/Scripting/JavaScript/HttpJavaScriptHandler.cs

#LineLine coverage
 1using Elsa.Extensions;
 2using Elsa.Http.Resilience;
 3using Elsa.Expressions.JavaScript.Notifications;
 4using Elsa.Expressions.JavaScript.TypeDefinitions.Builders;
 5using Elsa.Expressions.JavaScript.TypeDefinitions.Contracts;
 6using Elsa.Expressions.JavaScript.TypeDefinitions.Models;
 7using Elsa.Mediator.Contracts;
 8using JetBrains.Annotations;
 9
 10namespace Elsa.Http.Scripting.JavaScript;
 11
 12/// <summary>
 13/// Configures the JavaScript engine with additional .NET types that can be instantiated.
 14/// </summary>
 15[PublicAPI]
 16public class HttpJavaScriptHandler : INotificationHandler<EvaluatingJavaScript>, ITypeDefinitionProvider, IFunctionDefin
 17{
 18    private readonly ITypeDescriber _typeDescriber;
 19
 20    /// <summary>
 21    /// Initializes a new instance of the <see cref="HttpJavaScriptHandler"/> class.
 22    /// </summary>
 32323    public HttpJavaScriptHandler(ITypeDescriber typeDescriber)
 24    {
 32325        _typeDescriber = typeDescriber;
 32326    }
 27
 28    /// <inheritdoc />
 29    Task INotificationHandler<EvaluatingJavaScript>.HandleAsync(EvaluatingJavaScript notification, CancellationToken can
 30    {
 1931        var engine = notification.Engine;
 1932        engine.RegisterType<HttpHeaders>();
 1933        engine.RegisterType<Downloadable>();
 1934        engine.RegisterType<HttpResilienceStrategy>();
 35
 1936        var activityExecutionContext = notification.Context;
 37
 1938        engine.SetValue("createEventTriggerUrl", (Func<string, object?, string>)((eventName, lifetimeOrExpiryDate) =>
 1939        {
 040            return lifetimeOrExpiryDate switch
 041            {
 042                TimeSpan lifetime => activityExecutionContext.GenerateEventTriggerUrl(eventName, lifetime),
 043                DateTimeOffset expiryDate => activityExecutionContext.GenerateEventTriggerUrl(eventName, expiryDate),
 044                _ => activityExecutionContext.GenerateEventTriggerUrl(eventName)
 045            };
 1946        }));
 1947        return Task.CompletedTask;
 48    }
 49
 50    /// <inheritdoc />
 51    ValueTask<IEnumerable<TypeDefinition>> ITypeDefinitionProvider.GetTypeDefinitionsAsync(TypeDefinitionContext context
 52    {
 053        var definitions = GetTypeDefinitions(context);
 054        return new(definitions);
 55    }
 56
 57    /// <inheritdoc />
 58    ValueTask<IEnumerable<FunctionDefinition>> IFunctionDefinitionProvider.GetFunctionDefinitionsAsync(TypeDefinitionCon
 59    {
 060        var definitions = GetFunctionDefinitions(context);
 061        return new(definitions);
 62    }
 63
 64    private IEnumerable<TypeDefinition> GetTypeDefinitions(TypeDefinitionContext context)
 65    {
 066        yield return _typeDescriber.DescribeType(typeof(HttpHeaders));
 067        yield return _typeDescriber.DescribeType(typeof(Downloadable));
 068        yield return _typeDescriber.DescribeType(typeof(HttpResilienceStrategy));
 069    }
 70
 71    private IEnumerable<FunctionDefinition> GetFunctionDefinitions(TypeDefinitionContext context)
 72    {
 073        yield return CreateFunctionDefinition(function => function.Name("createEventTriggerUrl").ReturnType("string").Pa
 074        yield return CreateFunctionDefinition(function => function.Name("createEventTriggerUrl").ReturnType("string").Pa
 075        yield return CreateFunctionDefinition(function => function.Name("createEventTriggerUrl").ReturnType("string").Pa
 076    }
 77
 78    private FunctionDefinition CreateFunctionDefinition(Action<FunctionDefinitionBuilder> setup)
 79    {
 080        var builder = new FunctionDefinitionBuilder();
 081        setup(builder);
 082        return builder.BuildFunctionDefinition();
 83    }
 84}