|
| 1 | +using System.Linq; |
| 2 | +using WebApiToTypeScript.Block; |
| 3 | +using WebApiToTypeScript.Interfaces; |
| 4 | +using WebApiToTypeScript.WebApi; |
| 5 | + |
| 6 | +namespace WebApiToTypeScript.Endpoints |
| 7 | +{ |
| 8 | + public class EndpointsService : ServiceAware |
| 9 | + { |
| 10 | + public TypeScriptBlock CreateEndpointBlock() |
| 11 | + { |
| 12 | + return new TypeScriptBlock($"{Config.NamespaceOrModuleName} {Config.EndpointsNamespace}") |
| 13 | + .AddAndUseBlock($"export interface {IEndpoint}") |
| 14 | + .AddStatement("verb: string") |
| 15 | + .AddStatement("toString(): string") |
| 16 | + .Parent |
| 17 | + .AddAndUseBlock($"export interface {IHaveQueryParams}") |
| 18 | + .AddStatement("getQueryParams(): Object") |
| 19 | + .Parent; |
| 20 | + } |
| 21 | + |
| 22 | + public void WriteEndpointClassToBlock(TypeScriptBlock endpointBlock, WebApiController webApiController) |
| 23 | + { |
| 24 | + var controllerBlock = endpointBlock |
| 25 | + .AddAndUseBlock($"export {Config.NamespaceOrModuleName} {webApiController.Name}"); |
| 26 | + |
| 27 | + var actions = webApiController.Actions; |
| 28 | + |
| 29 | + foreach (var action in actions) |
| 30 | + { |
| 31 | + foreach (var verb in action.Verbs) |
| 32 | + { |
| 33 | + var actionName = action.GetActionNameForVerb(verb); |
| 34 | + |
| 35 | + var interfaceBlock = controllerBlock |
| 36 | + .AddAndUseBlock($"export interface I{actionName} extends {IEndpoint}"); |
| 37 | + |
| 38 | + WriteInterfaceToBlock(interfaceBlock, action); |
| 39 | + |
| 40 | + var classBlock = controllerBlock |
| 41 | + .AddAndUseBlock($"export class {actionName} implements {IEndpoint}") |
| 42 | + .AddStatement($"verb = '{verb.VerbMethod}';"); |
| 43 | + |
| 44 | + WriteConstructorToBlock(classBlock, action); |
| 45 | + |
| 46 | + WriteGetQueryStringToBlock(classBlock, action); |
| 47 | + |
| 48 | + WriteToStringToBlock(classBlock, action); |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + private void WriteInterfaceToBlock(TypeScriptBlock interfaceBlock, WebApiAction action) |
| 54 | + { |
| 55 | + var constructorParameterMappings = action.GetConstructorParameterMappings(); |
| 56 | + foreach (var constructorParameterMapping in constructorParameterMappings) |
| 57 | + { |
| 58 | + interfaceBlock |
| 59 | + .AddStatement($"{constructorParameterMapping.String};"); |
| 60 | + } |
| 61 | + |
| 62 | + var callArguments = action.BodyParameters; |
| 63 | + |
| 64 | + var callArgumentStrings = callArguments |
| 65 | + .Select(a => a.GetParameterString(false)) |
| 66 | + .ToList(); |
| 67 | + |
| 68 | + var callArgumentsList = string.Join(", ", callArgumentStrings); |
| 69 | + |
| 70 | + interfaceBlock |
| 71 | + .AddStatement($"call({callArgumentsList});"); |
| 72 | + } |
| 73 | + |
| 74 | + private void WriteToStringToBlock(TypeScriptBlock classBlock, WebApiAction action) |
| 75 | + { |
| 76 | + var toStringBlock = classBlock |
| 77 | + .AddAndUseBlock("toString = (): string =>"); |
| 78 | + |
| 79 | + var queryString = action.QueryStringParameters.Any() |
| 80 | + ? " + this.getQueryString()" |
| 81 | + : string.Empty; |
| 82 | + |
| 83 | + toStringBlock |
| 84 | + .AddStatement($"return `{action.Controller.BaseEndpoint}{action.Endpoint}`{queryString};"); |
| 85 | + } |
| 86 | + |
| 87 | + private void WriteGetQueryStringToBlock(TypeScriptBlock classBlock, WebApiAction action) |
| 88 | + { |
| 89 | + var queryStringParameters = action.QueryStringParameters; |
| 90 | + |
| 91 | + if (!queryStringParameters.Any()) |
| 92 | + return; |
| 93 | + |
| 94 | + var queryStringBlock = classBlock |
| 95 | + .AddAndUseBlock("private getQueryString = (): string =>") |
| 96 | + .AddStatement("var parameters: string[] = [];"); |
| 97 | + |
| 98 | + foreach (var routePart in queryStringParameters) |
| 99 | + { |
| 100 | + var argumentName = routePart.Name; |
| 101 | + |
| 102 | + var block = queryStringBlock |
| 103 | + .AddAndUseBlock($"if (this.{argumentName} != null)"); |
| 104 | + |
| 105 | + var argumentType = routePart.GetTypeScriptType(); |
| 106 | + |
| 107 | + if (argumentType.IsPrimitive || argumentType.IsEnum) |
| 108 | + { |
| 109 | + if (argumentType.IsCollection) |
| 110 | + { |
| 111 | + block |
| 112 | + .AddStatement($"parameters.push(`{argumentName}=${{this.{argumentName}.join(',')}}`);"); |
| 113 | + } |
| 114 | + else |
| 115 | + { |
| 116 | + block |
| 117 | + .AddStatement($"parameters.push(`{argumentName}=${{encodeURIComponent(this.{argumentName}.toString())}}`);"); |
| 118 | + } |
| 119 | + } |
| 120 | + else |
| 121 | + { |
| 122 | + block |
| 123 | + .AddStatement($"var {argumentName}Params = this.{argumentName}.getQueryParams();") |
| 124 | + .AddAndUseBlock($"Object.keys({argumentName}Params).forEach((key) =>", isFunctionBlock: true, terminationString: ";") |
| 125 | + .AddAndUseBlock($"if ({argumentName}Params[key] != null)") |
| 126 | + .AddStatement($"parameters.push(`${{key}}=${{encodeURIComponent({argumentName}Params[key].toString())}}`);"); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + queryStringBlock |
| 131 | + .AddAndUseBlock("if (parameters.length > 0)") |
| 132 | + .AddStatement("return '?' + parameters.join('&');") |
| 133 | + .Parent |
| 134 | + .AddStatement("return '';"); |
| 135 | + } |
| 136 | + |
| 137 | + private void WriteConstructorToBlock(TypeScriptBlock classBlock, WebApiAction action) |
| 138 | + { |
| 139 | + var constructorParameterMappings = action.GetConstructorParameterMappings(); |
| 140 | + |
| 141 | + if (!constructorParameterMappings.Any()) |
| 142 | + return; |
| 143 | + |
| 144 | + var constructorParameterStrings = constructorParameterMappings |
| 145 | + .Select(p => $"public {p.String}"); |
| 146 | + |
| 147 | + var constructorParametersList = |
| 148 | + string.Join(", ", constructorParameterStrings); |
| 149 | + |
| 150 | + var constructorBlock = classBlock |
| 151 | + .AddAndUseBlock($"constructor({constructorParametersList})"); |
| 152 | + |
| 153 | + foreach (var mapping in constructorParameterMappings) |
| 154 | + { |
| 155 | + if (mapping.TypeMapping?.AutoInitialize ?? false) |
| 156 | + { |
| 157 | + constructorBlock |
| 158 | + .AddAndUseBlock($"if (this.{mapping.Name} == null)") |
| 159 | + .AddStatement($"this.{mapping.Name} = new {mapping.TypeMapping.TypeScriptTypeName}();"); |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | +} |
0 commit comments