Search code examples
angularngrx

How to structure/manipulate data before a POST or after a GET request using Effects


I'm setting up a new project using @NgRx and it's going fairly well.

The only problem right now is that I structured my store in a way that's better for the front-end, removing nests and making everything simpler but now I need to make requests to my API (get/post) which requires inputs/returns in a specific format.

How am I supposed to deal with this kind of data?


Solution

  • In the functions/services that make the HTTP requests.

    Your application state has a model, if your API requests another state, then it's ouside of your application scope : you just have to make mappers that will convert your application state to the model expected by the server.

    You can either do it in the related services, in a specific converting service, or in util classes/functions.

    The reason is that if, tommorow, you create another app that requests to the same server, then by copy/pasting the services/utils, you won't have to change anything.