'Can I use a decorator on interface methods in typescript to save information to a config class?
I am baby stepping this one (though my end goal is at the bottom for context). Can I use @POST/@path decorators to save all the information about an interface to be used later in creation of a prototype at runtime? Specifically, I would like to save
- method name
- request type maybe if needed though I may be able to just write any object to json in javascript so I may not need that?
- response type though a hack of @ResponseType(AuthResponse) might be ok though it is not very DRY, it could work for our purposes right now.
- perhaps the interface name as a key if I store all the meta data of each api to one global place so they do not all conflict
I would like to know the above regardless of my end goal just so I can understand decorators better and try to use them a bit.
My end goal is that if I have something like this
class AuthRequest {}
class AuthResponse {}
class LoginRequest {}
class LoginResponse {}
interface AuthApi {
@Path("/authenticate")
@POST
authenticate(request: AuthRequest): Promise<AuthResponse>;
@Path("/login")
@POST
login(request: LoginRequest): Promise<LoginResponse>;
};
I can do something like this
const authApi: AuthApi = factory.createApi(configForAuthApiFromDecorators);
const userApi: UserApi = factory.createApi(configForUserApiFromDecorators)
NOTE: Basically, no developer has to ever write code for any api and instead just creates the interfaces and the implementation is generated with a javascript prototype object(I hope). I know gRPC sort of cheats(in a good way) and generates code ahead of time and maybe that is another path to explore.
In all cases, the code behind all apis is the same and only the paths through the code vary a little depending on @POST/@GET, etc.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
