Redux-toolkit: Documentation request: an example of clientAPI.ts please?

Created on 5 Apr 2020  路  7Comments  路  Source: reduxjs/redux-toolkit

Hi. I'm just learning the use of createAsyncThunk(). I really like it, and basically have something running with it now.

In the RTK usage guide example code there are imports for clientAPI. Whose implementation has been left for the developer to figure/out provide. Could one of you please provide an example of clientAPI.ts please? I'd like to ensure I'm using best practices developing my equivalent of userAPI.fetchById(userId).

Thank you so much!

All 7 comments

That's actually kind of the point. It doesn't matter whether you're using xhr, fetch, axios, or some other library - you ultimately just need to return a Promise from your payload callback. We specifically _don't_ want to get bogged down in details of how the different libraries and APIs work. For example, fetch doesn't throw rejected promises if you get an HTTP 400 response - you have to check response.ok yourself.

Those nuances aren't relevant to how the RTK code works, so we don't want to have those details in our docs.

How you choose to make your API calls is up to you, as is whether you make the API calls directly like fetch("/some-url").then(response => response.json()), or wrap it up in an abstraction layer like userAPI.fetchById().

@markerikson, I understand fully. But, I'm a promises n00b still. I'm so close to having working code, it actually runs, using Axios, but I'm getting some annoying tsc errors when I validate code. Just a hint of code please? lol. You don't have to update the documentation, I get that. Just help a n00b out?

The issue tracker here on GitHub is reserved for bug reports and feature requests. For usage questions, please use Stack Overflow or Reactiflux where there are a lot more people ready to help you out.

Sorry, I should simply study a bit more. I got this far, bringing our app up to using the latest RTK stuff, I can surely take this last step. Thank you for your time. (I'm also new to RTK in general.)

I got excited that I got this far. It's really cool you responded right away too. RTK is great.

Sure. To be clear, I'm generally happy to answer questions, this just isn't the right venue for it, and I'm also currently busy working on some stuff atm.

Addendum: I learned a LOT about writing API clients since this conversation. I'm really happy how clean it came out. During code review, I got a "clean af" comment on the work I did. Thank you, @markerikson for keeping me honest. :-)

Heh, you're welcome :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Brodzko picture Brodzko  路  4Comments

cole-robertson picture cole-robertson  路  3Comments

amankkg picture amankkg  路  4Comments

Darrekt picture Darrekt  路  3Comments

nonissue picture nonissue  路  3Comments