Saturday, 5 March 2022

Generating an http client behind a typescript interface? Is it possible?

I am trying to understand decorators which are not java's annotations but more like pre-processors in this article and I also found this SO question on setting info on a function. Let's say I have an interface like so

export default interface UserService {

    @Path("/users/create")
    @POST
    createUser(user: CreateUserRequest): Promise<CreateUserResponse>;

    @Path("/users/delete")
    @POST
    deleteUser(user: DeleteUserRequest): Promise<DeleteUserResponse>;

}

sidenote: It would be great to use this generated client in react UI as well as use in nodejs

I want my @Path and @POST, @GET to save info I can read and I think

class Path(path:string):
    def __init__(self, path):
        self.path = path

    def __call__(self, func):
        func.pathAnnotation = self
        return func

I read I cannot loop over methods in an interface yet I would like to generate the http client that implements this interface so any API developers create, it automatically creates the implementation. (In java, we use the Proxy.java to generate an implementation). On server side, the controller implements the same exact API and I like to generate the 'scaffolding' meaning the http request to which endpoint to call if possible (not sure I can do that in nodejs just yet).

EDIT: An idea: Perhaps, I create an abstract class or class and every method throws an exception "Use XXXFactory to generate the implementation of this class". How do I loop over method in that class? and can I create classes that extend all these 'apis' at runtime such that I can inject him for everyone to use(like Proxy.java in java world)?



from Generating an http client behind a typescript interface? Is it possible?

No comments:

Post a Comment