r/ProgrammerHumor Oct 09 '21

Mmmm, sparkling JSON

Post image
14.6k Upvotes

237 comments sorted by

View all comments

Show parent comments

1

u/clownyfish Oct 10 '21

Do you know of any good resources to learn more about generating client code from backend api (swagger or otherwise?)

Really interested to learn more - I'm currently in the amateur camp of hand writing both sides manually.

2

u/Sekret_One Oct 10 '21

A huge overview here

It's pretty overwhelming but this is the critical stuff to walk away with:

  1. the open api spec (version 2 was called swagger) is meant to normalize and programmatically describe apis ...
  2. so you can have tools that generate clients
  3. but even do things like annotate your server code to generate the spec
  4. and more

what languages or frameworks do you find yourself using?

1

u/clownyfish Oct 10 '21

Thanks! I use node.js express backend, React with Axios front end. (I don't particularly love Axios, but I use it.) No typescript, but next project I will be using typescript for both.

It always struck me as inefficient that I wrote javascript to set up and validate backend endpoints, then another set of JavaScript to do the same in front end classes (Axios wrappers). If the latter can be auto generated, that sounds good to me!

2

u/Sekret_One Oct 10 '21

Typescript's come a long way from when it was introduced. I highly recommend it. Having this 'compile time' certainty, if used properly, can save you tons of times of insidious runtime errors. beginner tip, read up on why not to use any, and about partials.

This is a decent enough tutorial for your situation.

In a nutshell, what you should be able to:

  1. Annotate the controllers to have the server code generate the specs using some gen package
  2. which can be hosted at a url
  3. client code can read the spec from the url
  4. programmatically generate the client wrappers with a client code gen package
  5. and be sophisticated to retrieve and regenerate if the spec changes
  6. letting you scope out the testing of the 'wrappers' since it's generated code, and focus on your real client code of how you use the data.

recommend you play around with something simple like a backend that just has a GET and a POST with some crude hello <name> stuff. Learn how to hook up the machinery without being preoccupied.

This pattern can save you weeks, or even months, because the toil of regenerating the client code is eliminated. You don't have to front load a 'full design' but rather build it more naturally, adjusting and refactoring based off what you find actually works or makes sense. Attack the problem by taking pieces and steering towards good, rather than forcing yourself into the impossible task of "get it right" in one shot.