readonly ClientApiTsFunctionGenAbstract apiFunctionGen; //to be injected in ctor of derived class. /// <summary> /// /// </summary> /// <param name="jsOutput"></param> /// <param name="apiFunctionGen"></param> /// <remarks>The client data types should better be generated through SvcUtil.exe with the DC option. The client namespace will then be the original namespace plus suffix ".client". </remarks> protected ControllersTsClientApiGenBase(JSOutput jsOutput, ClientApiTsFunctionGenAbstract apiFunctionGen) { this.jsOutput = jsOutput ?? throw new ArgumentNullException("jsOutput"); this.apiFunctionGen = apiFunctionGen; this.apiSelections = jsOutput.ApiSelections; TargetUnit = new CodeCompileUnit(); poco2TsGen = CreatePoco2TsGen(); TsCodeGenerationOptions options = TsCodeGenerationOptions.Instance; options.BracingStyle = "JS"; options.IndentString = "\t"; options.CamelCase = jsOutput.CamelCase ?? false; }
/// <summary> /// Generate CodeDom of the client API for ApiDescriptions. /// </summary> /// <param name="descriptions">Web Api descriptions exposed by Configuration.Services.GetApiExplorer().ApiDescriptions</param> public void CreateCodeDom(OpenApiPaths paths, OpenApiComponents components) { if (paths == null && components == null) { return; } clientNamespace = new CodeNamespace(settings.ClientNamespace); CodeCompileUnit.Namespaces.Add(clientNamespace); //namespace added to Dom ComponentsToTsTypes componentsToTsTypes = new ComponentsToTsTypes(settings, CodeCompileUnit, clientNamespace); componentsToTsTypes.CreateCodeDom(components); if (paths == null) { return; } AddBasicReferences(); string[] containerClassNames = GetContainerClassNames(paths); CodeTypeDeclaration[] newClassesCreated = containerClassNames.Select(d => CreateControllerClientClass(clientNamespace, d)).ToArray(); foreach (KeyValuePair <string, OpenApiPathItem> p in paths) { string relativePath = p.Key; foreach (KeyValuePair <OperationType, OpenApiOperation> op in p.Value.Operations) { ClientApiTsFunctionGenAbstract apiFunctionGen = apiFunctionGenFactory(); CodeMemberMethod apiFunction = apiFunctionGen.CreateApiFunction(settings, relativePath, op.Key, op.Value, componentsToTsTypes); if (apiFunction == null) { System.Diagnostics.Trace.TraceWarning($"Not to generate TS for {p.Key} {op.Key}."); continue; } string containerClassName = nameComposer.GetContainerName(op.Value, p.Key); CodeTypeDeclaration existingClass = LookupExistingClass(containerClassName); existingClass.Members.Add(apiFunction); } } foreach (CodeTypeDeclaration c in newClassesCreated) { AddHelperFunctionsInClass(c); } }