| | | 1 | | using Elsa.Extensions; |
| | | 2 | | using Elsa.Expressions.JavaScript.Contracts; |
| | | 3 | | using Elsa.Expressions.JavaScript.Extensions; |
| | | 4 | | using Elsa.Expressions.JavaScript.Options; |
| | | 5 | | using Elsa.Expressions.JavaScript.TypeDefinitions.Abstractions; |
| | | 6 | | using Elsa.Expressions.JavaScript.TypeDefinitions.Models; |
| | | 7 | | using Humanizer; |
| | | 8 | | using JetBrains.Annotations; |
| | | 9 | | using Microsoft.Extensions.Options; |
| | | 10 | | |
| | | 11 | | namespace Elsa.Expressions.JavaScript.Providers; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Produces <see cref="FunctionDefinition"/>s for common functions. |
| | | 15 | | /// </summary> |
| | | 16 | | [UsedImplicitly] |
| | 1 | 17 | | internal class CommonFunctionsDefinitionProvider(ITypeAliasRegistry typeAliasRegistry, IOptions<JintOptions> options) : |
| | | 18 | | { |
| | | 19 | | protected override IEnumerable<FunctionDefinition> GetFunctionDefinitions(TypeDefinitionContext context) |
| | | 20 | | { |
| | 0 | 21 | | yield return CreateFunctionDefinition(builder => builder |
| | 0 | 22 | | .Name("getWorkflowDefinitionId") |
| | 0 | 23 | | .ReturnType("string")); |
| | | 24 | | |
| | 0 | 25 | | yield return CreateFunctionDefinition(builder => builder |
| | 0 | 26 | | .Name("getWorkflowDefinitionVersionId") |
| | 0 | 27 | | .ReturnType("string")); |
| | | 28 | | |
| | 0 | 29 | | yield return CreateFunctionDefinition(builder => builder |
| | 0 | 30 | | .Name("getWorkflowDefinitionVersion") |
| | 0 | 31 | | .ReturnType("number")); |
| | | 32 | | |
| | 0 | 33 | | yield return CreateFunctionDefinition(builder => builder |
| | 0 | 34 | | .Name("getWorkflowInstanceId") |
| | 0 | 35 | | .ReturnType("string")); |
| | | 36 | | |
| | 0 | 37 | | yield return CreateFunctionDefinition(builder => builder |
| | 0 | 38 | | .Name("getCorrelationId") |
| | 0 | 39 | | .ReturnType("string")); |
| | | 40 | | |
| | 0 | 41 | | yield return CreateFunctionDefinition(builder => builder |
| | 0 | 42 | | .Name("setCorrelationId") |
| | 0 | 43 | | .Parameter("value", "string")); |
| | | 44 | | |
| | 0 | 45 | | yield return CreateFunctionDefinition(builder => builder |
| | 0 | 46 | | .Name("getWorkflowInstanceName") |
| | 0 | 47 | | .ReturnType("string")); |
| | | 48 | | |
| | 0 | 49 | | yield return CreateFunctionDefinition(builder => builder |
| | 0 | 50 | | .Name("setWorkflowInstanceName") |
| | 0 | 51 | | .Parameter("value", "string")); |
| | | 52 | | |
| | 0 | 53 | | yield return CreateFunctionDefinition(builder => builder |
| | 0 | 54 | | .Name("setVariable") |
| | 0 | 55 | | .Parameter("name", "string") |
| | 0 | 56 | | .Parameter("value", "any")); |
| | | 57 | | |
| | 0 | 58 | | yield return CreateFunctionDefinition(builder => builder |
| | 0 | 59 | | .Name("getVariable") |
| | 0 | 60 | | .Parameter("name", "string") |
| | 0 | 61 | | .ReturnType("any")); |
| | | 62 | | |
| | 0 | 63 | | yield return CreateFunctionDefinition(builder => builder |
| | 0 | 64 | | .Name("getInput") |
| | 0 | 65 | | .Parameter("name", "string") |
| | 0 | 66 | | .ReturnType("any")); |
| | | 67 | | |
| | 0 | 68 | | yield return CreateFunctionDefinition(builder => builder |
| | 0 | 69 | | .Name("getOutputFrom") |
| | 0 | 70 | | .Parameter("activityId", "string") |
| | 0 | 71 | | .Parameter("outputName", "string", true) |
| | 0 | 72 | | .ReturnType("any")); |
| | | 73 | | |
| | 0 | 74 | | yield return CreateFunctionDefinition(builder => builder |
| | 0 | 75 | | .Name("getLastResult") |
| | 0 | 76 | | .ReturnType("any")); |
| | | 77 | | |
| | 0 | 78 | | yield return CreateFunctionDefinition(builder => builder |
| | 0 | 79 | | .Name("isNullOrWhiteSpace") |
| | 0 | 80 | | .Parameter("value", "string") |
| | 0 | 81 | | .ReturnType("boolean")); |
| | | 82 | | |
| | 0 | 83 | | yield return CreateFunctionDefinition(builder => builder |
| | 0 | 84 | | .Name("isNullOrEmpty") |
| | 0 | 85 | | .Parameter("value", "string") |
| | 0 | 86 | | .ReturnType("boolean")); |
| | | 87 | | |
| | 0 | 88 | | yield return CreateFunctionDefinition(builder => builder |
| | 0 | 89 | | .Name("parseGuid") |
| | 0 | 90 | | .Parameter("value", "string") |
| | 0 | 91 | | .ReturnType("Guid")); |
| | | 92 | | |
| | 0 | 93 | | yield return CreateFunctionDefinition(builder => builder |
| | 0 | 94 | | .Name("getShortGuid") |
| | 0 | 95 | | .ReturnType("string")); |
| | | 96 | | |
| | 0 | 97 | | yield return CreateFunctionDefinition(builder => builder |
| | 0 | 98 | | .Name("getGuid") |
| | 0 | 99 | | .ReturnType("Guid")); |
| | | 100 | | |
| | 0 | 101 | | yield return CreateFunctionDefinition(builder => builder |
| | 0 | 102 | | .Name("getGuidString") |
| | 0 | 103 | | .ReturnType("string")); |
| | | 104 | | |
| | 0 | 105 | | yield return CreateFunctionDefinition(builder => builder |
| | 0 | 106 | | .Name("toJson") |
| | 0 | 107 | | .Parameter("value", "any") |
| | 0 | 108 | | .ReturnType("string")); |
| | | 109 | | |
| | 0 | 110 | | yield return CreateFunctionDefinition(builder => builder |
| | 0 | 111 | | .Name("bytesToString") |
| | 0 | 112 | | .Parameter("value", "Byte[]") |
| | 0 | 113 | | .ReturnType("string")); |
| | | 114 | | |
| | 0 | 115 | | yield return CreateFunctionDefinition(builder => builder |
| | 0 | 116 | | .Name("bytesFromString") |
| | 0 | 117 | | .Parameter("value", "string") |
| | 0 | 118 | | .ReturnType("Byte[]")); |
| | | 119 | | |
| | 0 | 120 | | yield return CreateFunctionDefinition(builder => builder |
| | 0 | 121 | | .Name("bytesToBase64") |
| | 0 | 122 | | .Parameter("value", "Byte[]") |
| | 0 | 123 | | .ReturnType("string")); |
| | | 124 | | |
| | 0 | 125 | | yield return CreateFunctionDefinition(builder => builder |
| | 0 | 126 | | .Name("bytesFromBase64") |
| | 0 | 127 | | .Parameter("value", "string") |
| | 0 | 128 | | .ReturnType("Byte[]")); |
| | | 129 | | |
| | 0 | 130 | | yield return CreateFunctionDefinition(builder => builder |
| | 0 | 131 | | .Name("stringFromBase64") |
| | 0 | 132 | | .Parameter("value", "string") |
| | 0 | 133 | | .ReturnType("string")); |
| | | 134 | | |
| | 0 | 135 | | yield return CreateFunctionDefinition(builder => builder |
| | 0 | 136 | | .Name("stringToBase64") |
| | 0 | 137 | | .Parameter("value", "string") |
| | 0 | 138 | | .ReturnType("string")); |
| | | 139 | | |
| | 0 | 140 | | yield return CreateFunctionDefinition(builder => builder |
| | 0 | 141 | | .Name("streamToBytes") |
| | 0 | 142 | | .Parameter("value", "Stream") |
| | 0 | 143 | | .ReturnType("Byte[]")); |
| | | 144 | | |
| | 0 | 145 | | yield return CreateFunctionDefinition(builder => builder |
| | 0 | 146 | | .Name("streamToBase64") |
| | 0 | 147 | | .Parameter("value", "Stream") |
| | 0 | 148 | | .ReturnType("string")); |
| | | 149 | | |
| | 0 | 150 | | if (!options.Value.DisableWrappers) |
| | | 151 | | { |
| | | 152 | | // Variable getter and setters. |
| | 0 | 153 | | foreach (var variable in context.WorkflowGraph.Workflow.Variables.Where(x => x.Name.IsValidVariableName())) |
| | | 154 | | { |
| | 0 | 155 | | var pascalName = variable.Name.Pascalize(); |
| | 0 | 156 | | var variableType = variable.GetVariableType(); |
| | 0 | 157 | | var typeAlias = typeAliasRegistry.TryGetAlias(variableType, out var alias) ? alias : "any"; |
| | | 158 | | |
| | | 159 | | // get{Variable}. |
| | 0 | 160 | | yield return CreateFunctionDefinition(builder => builder.Name($"get{pascalName}").ReturnType(typeAlias)) |
| | | 161 | | |
| | | 162 | | // set{Variable}. |
| | 0 | 163 | | yield return CreateFunctionDefinition(builder => builder.Name($"set{pascalName}").Parameter("value", typ |
| | 0 | 164 | | } |
| | | 165 | | } |
| | 0 | 166 | | } |
| | | 167 | | } |