karlvr/openapi-generator-plus-generators

`typescript-express-example-server-generator`: inject request handlers?

kindaro opened this issue · 2 comments

Currently, the work flows like this:

  1. Generate an example server that serves some example values.
  2. Rewrite the request handlers to use the actual logic (say, querying the data base).

This has the disadvantage that the generated code has to be edited (request handlers written inline) and therefore committed. However, as a rule, generated code should not be committed. Committing a patch over the generated code also has obvious drawbacks, such as being fragile and unwieldy.

The solution is to parametrize the generated code by the request handlers. This can be done in two ways:

  • Soft wiring: provide a function that accepts a map ⟨path; method⟩ ↦ request handler and gives a server for the user to run.
  • Hard wiring: put in place an import statement that imports a (non-existent) file where request handlers are supposed to be defined. The user has to write this file.

@kindaro thanks for this. The intention of this particular generator is to create a server that just returns example content, rather than as a basis for a node server.

I have actually worked on a node server with this in mind... it's separate from this repository, as probably a number of these generators should end up being! Check out https://github.com/karlvr/openapi-generator-plus-express-passport and see if this is going in the right direction for you. I haven't updated it in a little while, but we are currently using it in production in its current form.

Thanks!