Search code examples
javascriptnode.jsreactjstypescriptjavascript-decorators

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)?

EDIT: perhaps I could do this in javascript with a prototype class somehow... generate all the methods to call a method with signature

Promise<Object> invoke(String method, object[] paramsToMethod);

Then, I can look up the method and @Path and @GET/@POST properties on that method. Can a prototype class extend a class in javascript such that any api defined (perhaps defined in typescript) is implemented by this one class? Then I have every api implemented by the same code for every microservice like we do in java? (This then means the platform team can swap protocols and developers do not care about protocols anymore like http, http2, binary, etc)


Solution

  • As per request, I have took some time to tinker around with reflection. The reflection is mostly needed to make the client automatically conform to what the service expects (type of parameters/return type), and I think it might be possible with reflect-metadata.

    Ok so the idea is to have decorators store metadata about the method in a map, where a key is the class and the value is a collection of the methods with metadata.

    Then where you get the client, it aggregates all the metadata for each method into one function that can be used.

    It's a vague start but I think it can work. I actually might also turn this into a small snippet or library too if I have the time.

    But actually, this should be a statically generated client, not a dynamic one. Because then it is far easier to validate and do the code generation.

    Playground