Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

111
Visualizações
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)

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

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

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda