r/programminghelp • • Dec 13 '20

JavaScript I'd like to write a class to handle interaction between my server and an API. Is this typical?

Hello all,

I have been given an extremely rare opportunity to write code for my tech company despite being in a non-software position, and I'd like to impress.

I have a background in Python and a bit of C++, but for this project I'm using Node.js. In an effort to make my code as clean as possible, I'm trying to create a class that handles and abstracts all my API interactions.

However, every single tutorial I read/watch and every example source code has a much more freeform structure.

What is the typical, recommended approach here? Any good source code examples?

Any help at all is much appreciated, thank you.

4 Upvotes

2 comments sorted by

1

u/amoliski Dec 13 '20

In my opinion, that's a great path to take. Make an interface and interact through it- no need to have API endpoint URLs all over the application. Having it separated out makes it much easier to debug, and handle API changes.

You can make something as simple as an api.js that provides functional representations of each endpoint, or you can get a bit more elaborate like with how VueX works with Vue.js. They have a concept of a 'store' that handles the state of the application, and all of the interactions with the API are handled there- kicked off by the GUI firing off 'actions'. The application itself watches for changes to the store, and re-renders/handles changes as appropriate.

2

u/brandondunbar Dec 14 '20

Ahh that's very interesting, this will be a good starting point for research. Thank you!