1
votes

We have exposed some new API’s from SAP Commerce and need to get/post data in some of the components or services.

Are there any pre-defined mechanisms to leverage new API integrations, or should we simply use HttpClient from Angular?

We are expecting that there should be a way to get data from new API’s like we have for OOTB API’s such as “StoreFinderService” which helps us to get data for store and there are some other services as well.

Spartacus is using NgRx to handle all API’s and then these services supply data from stores. Similarly, there must be a way to get data for new API’s created as per business requirements.

1

1 Answers

1
votes

Spartacus uses the Angular HttpClient under the hood, it's the recommended service to interact with http. However, using HttpClient directly from a component is generally considered a bad practice. You'd better separate concerns, and delegate interacting with the backend to a service.

Spartacus offers an extensive architecture to handle backend APIs, you can read more about it at https://sap.github.io/spartacus-docs/connecting-to-other-systems. You could follow this architecture, but for a project it's overcomplicated. The architecture really targets extension points, which is fair for a product, but most often not for a project.

https://github.com/SAP/spartacus-bootcamp/tree/master/src/app/features/state shows you some examples of introducing a custom state to the Spartacus state.

For projects introducing new features, I would either create and maintain state in a service, or introduce a custom state in ngrx. For the later, you can evaluate https://github.com/SAP/spartacus-bootcamp/tree/master/src/app/features/state that shows how to add new state into Spartacus, or read up on ngrx resources.