This is pretty good, and I think you're on the right track, but it could be improved in a few ways.
You don't need to create so many instances. You're in JS which is single threaded. Your code is mostly stateless.
Use unknown instead of any. It saves mistakes later, and communicates your intent better to the caller.
Abstract more. The caller of your code should know nothing of HTTP. You stated that a goal was to avoid change if your API changes. That's a good goal.
Hello, world! I'm a professional self taught full-stack developer from Moscow. I truly love web-development and all that it concerns. Making websites is awesome. Follow me if you need some help
This ia a bit over engineered, isn't it!? It's easier and cleaner to use native es modules over classes. Then, there's no need to have a createClient in all methods, just important the axios instance. 😉
Hello, world! I'm a professional self taught full-stack developer from Moscow. I truly love web-development and all that it concerns. Making websites is awesome. Follow me if you need some help
Hello, world! I'm a professional self taught full-stack developer from Moscow. I truly love web-development and all that it concerns. Making websites is awesome. Follow me if you need some help
Hello, world! I'm a professional self taught full-stack developer from Moscow. I truly love web-development and all that it concerns. Making websites is awesome. Follow me if you need some help
Hello, world! I'm a professional self taught full-stack developer from Moscow. I truly love web-development and all that it concerns. Making websites is awesome. Follow me if you need some help
I'd recommend not to write an API client but instead generate it. I wrote this github.com/nfroidure/openapi-ts-sd... for generating a SDK from OpenAPI, it is still highly customizable but It avoids repeating myself.
Hello, world! I'm a professional self taught full-stack developer from Moscow. I truly love web-development and all that it concerns. Making websites is awesome. Follow me if you need some help
This is pretty good, and I think you're on the right track, but it could be improved in a few ways.
You don't need to create so many instances. You're in JS which is single threaded. Your code is mostly stateless.
Use
unknown
instead ofany
. It saves mistakes later, and communicates your intent better to the caller.Abstract more. The caller of your code should know nothing of HTTP. You stated that a goal was to avoid change if your API changes. That's a good goal.
Thanks
Curious about the abstraction part? I may be missing it but how does the caller know anything of the HTTP?
Do you have a video of this? As a newbie i hardly able to catch that.
This ia a bit over engineered, isn't it!? It's easier and cleaner to use native es modules over classes. Then, there's no need to have a createClient in all methods, just important the axios instance. 😉
It's an irregular approach :)
Irregular!? Are you sure!? 🙃
I mean my approach that I've shown in this article
Great article.
Thank you
Great Article Sir
Thanks a lot
I'd recommend not to write an API client but instead generate it. I wrote this github.com/nfroidure/openapi-ts-sd... for generating a SDK from OpenAPI, it is still highly customizable but It avoids repeating myself.
I agree with other comments to avoid classes too.
This is really a good alternative