Manweill / swagger-axios-codegen

swagger client to use axios and typescript
MIT License
306 stars 83 forks source link

Constants are not generated #51

Closed fairking closed 5 years ago

fairking commented 5 years ago

Hi guys,

Is the any way to tell codegen to generate constants?

My class looks like:

    public class DemoPermissions
    {
        public const string customers_view = "customers_view";
        public const string customers_manage = "customers_manage";
    }

Currently codegen generates the following ts:

export class DemoPermissions {
  constructor(data: undefined | any = {}) {}
}

I use those constants as permissions. The reason I use const instead of enum is to be able to pass them into the attributes and because I will have many permission classes (cannon have many enums against one property):

        [DataRouteAction(Perms = DemoPermissions.customers_view)]
        public async Task<IList<CustomerVm>> GetCustomers()
        {
            ...
        }

In the same time I would like to use those constants in vuejs:

<button v-if="isAllowed(DemoPermissions.customers_view)" />

I assume constants can be generated as static properties or as enums in typescript. The tricky thing is to pass the value instead of the property name. In my situation it wont be a case:

export class DemoPermissions {
  static customers_view = 'customers_view';
  static customers_manage = 'customers_manage';
  static some_property = 'some_value'; // value can be different from property name !!!
}
Manweill commented 5 years ago

A new swagger expansion?

fairking commented 5 years ago

I think guys I found a solution. I switched back to enums, so they are generated by codegen. Feel free to close the issue.

[ApiController]
public class UserService : ControllerBase
{
    [Route("api/update_user")]
    [Permissions(CorePermissions.users_manage, DemoPermissions.users_demo)]
    public class UpdateUserService
    {
        // Code
    }
}

    public class PermissionsAttribute : Attribute
    {
        public PermissionsAttribute(params object[] perms)
        {
            Perms = perms;
        }

        public object[] Perms { get; set; }
    }

    // Generated in typescript by swagger-axios-codegen
    [Swagger]
    public enum CorePermissions
    {
        users_view,
        users_manage,
    }

    // Generated in typescript by swagger-axios-codegen
    [Swagger]
    public enum DemoPermissions
    {
        companies_demo,
        users_demo,
    }