Coder Social home page Coder Social logo

greymind / webapitotypescript Goto Github PK

View Code? Open in Web Editor NEW
25.0 11.0 10.0 475 KB

A tool for code generating TypeScript endpoints for your ASP.NET Web API controllers

License: MIT License

C# 74.70% ASP 0.06% HTML 0.98% TypeScript 23.51% PowerShell 0.75%
typescript angular asp-net webapi webapi-2 c-sharp csharp code-generation code-generator

webapitotypescript's Introduction

Web API To TypeScript

Join the chat at https://gitter.im/WebApiToTypeScript/Lobby

A tool for code generating TypeScript endpoints for your ASP.NET Web API controllers and their actions. No more need for dumb repositories!

  • Generates typed endpoints with required and optional parameters for GET, PUT, POST and DELETE verbs
    • Generates cached endpoints for GET verb actions
  • Generates enumerations and interfaces for DTOs used in all controller actions
    • Generates enum descriptions based on the [Description] attribute
  • Generates Angular service to expose typed call() function to fetch response from server
  • Generates typed views to use on front-end
  • Generates resources files based on .resx files

Examples

Take a look at the generated files and sample test files here

Getting the tool from NuGet

https://www.nuget.org/packages/WebApiToTypeScript/

Install-Package WebApiToTypeScript

Using as standalone tool

Just run the executable with the config file as the argument

watts.exe "Path/To/Config/File.json"

Using as MSBuild Target

I haven't tested this in a while, so if you find any inconsistencies with working directory and such, feel free to file an issue or better yet, submit a pull request!

Ensure the DLL is part of the build process, so you have access to it. Easiest way is to reference it in your project.

<UsingTask TaskName="WebApiToTypeScript.WebApiToTypeScript" AssemblyFile="$(ProjectDir)..\WebApiToTypeScript\bin\Debug\WebApiToTypeScript.dll" />
<Target Name="AfterBuild">
    <WebApiToTypeScript ConfigFilePath="$(ProjectDir)Watts.config.json" />
</Target>

T4 Template

If you wish to integrate the executable as part of your workflow from within Visual Studio, you may consider wiring it up to a T4 template. Here is a sample.

Protip

You can build just the C# parts of the solution by adding a condition to the TypeScript target in the csproj file. This way you can ensure that before you run WebApiToTypeScript, you have a hassle-free build of the backend code.

  <PropertyGroup>
    <SkipTypeScript Condition="'$(SkipTypeScript)'==''">False</SkipTypeScript>
  </PropertyGroup>
  <Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\TypeScript\Microsoft.TypeScript.targets" Condition="Exists('$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\TypeScript\Microsoft.TypeScript.targets') AND '$(SkipTypeScript)'=='False'" />
Path\To\msbuild.exe /t:build /p:SkipTypeScript=True Path\To\Project.csproj

Using Angular Endpoints Service

You'll need to register the Endpoints service to your app and inject it as is typical in Angular

angular.module('framework').service('AngularEndpointsService', Framework.Endpoints.AngularEndpointsService);

Config

{
  "WebApiModuleFileName": "string",
  "WebApiModuleFileNames": [ "string" ]
  
  "GenerateEndpointsReturnTypes": "boolean",
  "GenerateEndpoints": "boolean",  
  "EndpointsOutputDirectory": "string",
  "EndpointsSupportCaching": "boolean",
  "EndpointsFileName": "string",
  "EndpointsNamespace": "string",
  
  "GenerateMobileEndpoints": "boolean",
  "MobileEndpointsOutputDirectory": "string",
  "MobileEndpointsFileName": "string",
  "MobileEndpointsNamespace": "string",
  "MobileEndpointAttributeName": "string",

  "GenerateService": "boolean",
  "ServiceOutputDirectory": "string",
  "ServiceFileName": "string",
  "ServiceNamespace": "string",
  "ServicesName": "string",

  "GenerateEnums": "boolean",
  "GenerateEnumDescriptions": "boolean",
  "EnumsOutputDirectory": "string",
  "EnumsFileName": "string",
  "EnumsNamespace": "string",

  "GenerateInterfaces": "boolean",
  "InterfacesOutputDirectory": "string",
  "InterfacesFileName": "string",
  "InterfacesNamespace": "string",
  "InterfaceMembersInCamelCase": "boolean",
  "InterfaceCamelCaseCustomAttribute": "string",
  "InterfaceMatches": [
    "Match": "string",
    "ExcludeMatch": "string",
    "BaseTypeName": "string"
  ],

  "GenerateViews": "boolean",
  "ViewConfigs": [
    {
      "SourceDirectory": "string",
      "Namespace": "string",
      "OutputFilename": "string",
      "Prefix": "string",
      "UrlEncodePath": "boolean"
    }
  ],
  "ViewsPattern": "string",
  "ViewsOutputDirectory": "string",
  "UseViewsGroupingNamespace": "boolean",
  "ViewsIgnoredExtensions": [
      "string"
  ]

  "ScanOtherModules": "boolean",
  "WriteNamespaceAsModule": "boolean",

  "TypeMappings": [
    {
      "WebApiTypeName": "string",
      "TypeScriptTypeName": "string",
      "AutoInitialize": "boolean",
      "TreatAsAttribute": "boolean"
      "TreatAsConstraint": "boolean",
      "Match": "string"
    }
  ]
}

License

MIT

webapitotypescript's People

Contributors

apavlinovic avatar chferdinand avatar dborovic avatar dborovic-neogov avatar dsperac avatar gitter-badger avatar greymind avatar omittones avatar visweswaranr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

webapitotypescript's Issues

Add support for more enum functionality

We have a tool that generates enums and one enum looks like this:

AccountType: {
  "Basic": {
    "Value": 1,
    "ValueName": "Basic",
    "Title": "Basic"
  },

This gives us more flexibility if we need to generate some text from enum or similar.
It would be nice if this tool provided a way to fetch similar data.

Figure out best mechanism to support return data types

It is a fairly challenging problem to figure out the best scheme to detect and support return types for controller actions. Many different control flows could exist, and often times the signature of actions just reads IHttpActionResult!

Watts.exe fails when base type is a generic one

Watts.exe fails when a base type is a generic one.

We have multiple base types with single generic argument and watts is working fine, however it fails with a generic base type that has 3 generic arguments (not sure about 2, did not check that out).

It fails inside InterfaceService in WriteInterfaces(TypeScriptBlock interfacesBlock, InterfaceNode interfaceNode) method on the foreach line. The exception we get is that the collection was modified.

Looking forward to this fix, because we had to utilize interfaces to go around this and that introduced some code duplication.

Thanks!

Add support for caching last request.

Add support for .callCached() method in AngularEndpointsService.

If the same method was called before, this method would grab whatever was received last time.
In case this method is being called first time, it should automatically fetch data from .call() version.

Add support for scanning multiple DLLs

Currently watts supports scanning of only one DLL for controllers.

Our use case requires scanning of multiple DLLs.

Could you please add the support for this requirement :)

Code clean up

Clean up code. Please use this thread to identify overall points of cleanup or spawn off into a new issue if it is focused and actionable.

Handle collisions in enums by growing the name prefix with namespace parts

Currently, two enums with same name, but under different namespaces collide and we generate a warning. Instead, we can "grow" the name by prefixing the minimum namespace parts required to make it identical.

For example:

namespace Something.Else.Here {
    enum EnumType {
    }
}

namespace Something.There.Here {
    enum EnumType {
    }
}

The generated names can be

ElseHereEnumType
ThereHereEnumType

Perhaps do this for other types as well

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.