2023-03-07 13:54:21 +00:00
|
|
|
using Elsa.Extensions;
|
|
|
|
|
using Elsa.Http.Models;
|
|
|
|
|
using Elsa.JavaScript.Notifications;
|
2023-09-16 09:06:56 +00:00
|
|
|
using Elsa.JavaScript.TypeDefinitions.Contracts;
|
|
|
|
|
using Elsa.JavaScript.TypeDefinitions.Models;
|
2023-03-07 13:54:21 +00:00
|
|
|
using Elsa.Mediator.Contracts;
|
|
|
|
|
using JetBrains.Annotations;
|
|
|
|
|
|
|
|
|
|
namespace Elsa.Http.Scripting.JavaScript;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2023-09-16 09:06:56 +00:00
|
|
|
/// Configures the JavaScript engine with additional .NET types that can be instantiated.
|
2023-03-07 13:54:21 +00:00
|
|
|
/// </summary>
|
|
|
|
|
[PublicAPI]
|
2023-09-16 09:06:56 +00:00
|
|
|
public class HttpJavaScriptHandler : INotificationHandler<EvaluatingJavaScript>, ITypeDefinitionProvider
|
2023-03-07 13:54:21 +00:00
|
|
|
{
|
2023-09-16 09:06:56 +00:00
|
|
|
private readonly ITypeDescriber _typeDescriber;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes a new instance of the <see cref="HttpJavaScriptHandler"/> class.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public HttpJavaScriptHandler(ITypeDescriber typeDescriber)
|
|
|
|
|
{
|
|
|
|
|
_typeDescriber = typeDescriber;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-07 13:54:21 +00:00
|
|
|
/// <inheritdoc />
|
2023-08-14 12:47:35 +00:00
|
|
|
public Task HandleAsync(EvaluatingJavaScript notification, CancellationToken cancellationToken)
|
2023-03-07 13:54:21 +00:00
|
|
|
{
|
|
|
|
|
var engine = notification.Engine;
|
|
|
|
|
engine.RegisterType<HttpRequestHeaders>();
|
2023-09-16 09:06:56 +00:00
|
|
|
engine.RegisterType<Downloadable>();
|
2023-08-14 12:47:35 +00:00
|
|
|
return Task.CompletedTask;
|
2023-03-07 13:54:21 +00:00
|
|
|
}
|
2023-09-16 09:06:56 +00:00
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public ValueTask<IEnumerable<TypeDefinition>> GetTypeDefinitionsAsync(TypeDefinitionContext context)
|
|
|
|
|
{
|
|
|
|
|
var definitions = GetTypeDefinitions(context);
|
|
|
|
|
return new(definitions);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IEnumerable<TypeDefinition> GetTypeDefinitions(TypeDefinitionContext context)
|
|
|
|
|
{
|
|
|
|
|
yield return _typeDescriber.DescribeType(typeof(HttpRequestHeaders));
|
|
|
|
|
yield return _typeDescriber.DescribeType(typeof(Downloadable));
|
|
|
|
|
}
|
2023-03-07 13:54:21 +00:00
|
|
|
}
|