/// <summary> /// Transforme le type en type Typescript. /// </summary> /// <param name="property">La propriété dont on cherche le type.</param> /// <returns>Le type en sortie.</returns> private string CSharpToTSType(ModelProperty property) { if (property.Name == "Code") { return($"{property.Class.Name}Code"); } else if (property.Name.EndsWith("Code", StringComparison.Ordinal)) { return(property.Name.ToFirstUpper()); } return(TSUtils.CSharpToTSType(property)); }
/// <summary> /// Create the template output /// </summary> public virtual string TransformText() { Write("/*\r\n Ce fichier a été généré automatiquement.\r\n Toute modification sera per" + "due.\r\n*/\r\n\r\nimport {EntityToType, StoreNode} from \"focus4/entity\";\r\nimport {"); Write(string.Join(", ", GetDomainList())); Write("} from \"../../domains\";\r\n"); var imports = GetImportList(); foreach (var import in imports) { Write("\r\nimport {"); Write(import.import); Write("} from \""); Write(import.path); Write("\";"); } if (imports.Any()) { Write("\r\n"); } Write("\r\nexport type "); Write(Model.Name); Write(" = EntityToType<typeof "); Write(Model.Name); Write("Entity>;\r\nexport type "); Write(Model.Name); Write("Node = StoreNode<typeof "); Write(Model.Name); Write("Entity>;\r\n\r\nexport const "); Write(Model.Name); Write("Entity = {\r\n name: \""); Write(Model.Name.ToFirstLower()); Write("\",\r\n fields: {\r\n"); if (Model.ParentClass != null) { Write(" ..."); Write(Model.ParentClass.Name); Write("Entity.fields,\r\n"); } foreach (var property in Model.PropertyList) { Write(" "); Write(property.Name.ToFirstLower()); Write(": {\r\n type: "); if (IsArray(property)) { Write("\"list\" as \"list\""); } else if (property.IsFromComposition) { Write("\"object\" as \"object\""); } else { Write("\"field\" as \"field\""); } Write(",\r\n "); if (GetDomain(property) != null) { Write(" name: \""); Write(property.Name.ToFirstLower()); Write("\",\r\n fieldType: {} as "); Write(TSUtils.CSharpToTSType(property)); Write(",\r\n domain: "); Write(GetDomain(property)); Write(",\r\n isRequired: "); Write((property.DataMember.IsRequired && (!property.IsPrimaryKey || property.DataType != "int?")).ToString().ToFirstLower()); Write(",\r\n label: \""); Write(TSUtils.ToNamespace(Model.Namespace.Name)); Write("."); Write(Model.Name.ToFirstLower()); Write("."); Write(property.Name.ToFirstLower()); Write("\"\r\n "); } else { Write(" entity: "); Write(GetReferencedType(property)); Write("Entity\r\n "); } Write("}"); if (property != Model.PropertyList.Last()) { Write(","); } Write("\r\n"); } Write(" }\r\n};\r\n"); if (Model.IsReference) { Write("\r\nexport const "); Write(Model.Name.ToFirstLower()); Write(" = {type: {} as "); Write(Model.Name); Write(", valueKey: \""); Write(Model.PrimaryKey.First().Name.ToFirstLower()); Write("\", labelKey: \""); Write(Model.DefaultProperty?.ToFirstLower() ?? "libelle"); Write("\"};\r\n"); } return(GenerationEnvironment.ToString()); }
/// <summary> /// Create the template output /// </summary> public virtual string TransformText() { Write("/*\r\n Ce fichier a été généré automatiquement.\r\n Toute modification sera per" + "due.\r\n*/\r\n\r\nimport {fetch} from \""); Write(ServerPath); Write("\";\r\n"); if (GetImportList().Any()) { Write("\r\n"); } foreach (var import in GetImportList()) { Write("import {"); Write(import.import); Write("} from \""); Write(import.path); Write("\";\r\n"); } foreach (var service in Services) { Write("\r\n/**\r\n * "); Write(service.Documentation.Summary); Write("\r\n"); foreach (var param in service.Documentation.Parameters) { Write(" * @param "); Write(param.Item1); Write(" "); Write(param.Item2); Write("\r\n"); } Write(" * @param options Fetch options.\r\n */\r\nexport function "); Write(service.Name.ToFirstLower()); Write("("); foreach (var parameter in service.Parameters) { Write(parameter.Name); Write(parameter.IsOptional ? "?" : ""); Write(": "); Write(TSUtils.CSharpToTSType(parameter.Type)); if (parameter.Name != service.Parameters.Last().Name) { Write(", "); } } if (service.Parameters.Count() > 0) { Write(", "); } Write("options: RequestInit = {}): Promise<"); Write(TSUtils.CSharpToTSType(service.ReturnType)); Write("> {\r\n return fetch(\""); Write(service.Verb); Write("\", `./"); Write(Regex.Replace(service.Route.Replace("{", "${"), ":([a-z]+)", string.Empty)); Write("`, {"); if (service.BodyParameter != null) { Write("body: "); Write(service.BodyParameter.Name); } if (service.BodyParameter != null && service.QueryParameters.Any()) { Write(", "); } if (service.QueryParameters.Any()) { Write("query: {"); foreach (var qParam in service.QueryParameters) { Write(qParam.Name); if (qParam.Name != service.QueryParameters.Last().Name) { Write(", "); } } Write("}"); } Write("}, options);\r\n}\r\n"); } return(GenerationEnvironment.ToString()); }