Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integrating customised APIs in Spartacus Storefront

our Hybris instance has some custom REST APIs, for example the addEntry. This version of the API requires an extra boolean parameter in the payload, let's call it fooBar. Here’s an example of payload:

{"quantity": 1, "product": {"code": "1234567"}, "fooBar": false}

Here’s the list of what we’ve done in order to have this extra parameter in the service that actually made the http call:

  • Overridden the AddToCartComponent
    • Modified the addToCart method passing the fooBar parameter to the addEntry method of the E2ActiveCartService
  • Extended ActiveCartService in E2ActiveCartService
    • Modified the addEntry method passing the fooBar parameter to the addEntry method of the E2MultiCartService
  • Extended MultiCartService in E2MultiCartService
    • Modified the addEntry method passing the fooBar parameter to the payload of the E2CartAddEntry action
  • Implemented a copy of the CartAddEntry action (called E2CartAddEntry) with its own type (i.e. '[E2-Cart-entry] Add Entry')
  • Implemented a new CartEntryEffects (called E2CartEntryEffects) that listens to the E2CartAddEntry action
    • Created a second effect called processesIncrement$ that dispatches the CartActions.CartProcessesIncrement action (we did this because the E2CartAddEntry cannot extends the EntityProcessesIncrementAction class)
    • Copied the addEntry$ effect from the original CartEntryEffects adding the fooBar parameter to the add method of the E2CartEntryConnector
  • Extended CartEntryConnector in E2CartEntryConnector
    • Modified the add method passing the fooBar parameter to the add method of the E2CartEntryAdapter
  • Extended CartEntryAdapter in E2CartEntryAdapter
    • Modified the abstract add method adding the fooBar parameter
  • Created E2OccCartEntryAdapter that extends OccCartEntryAdapter and implements E2CartEntryAdapter
    • Modified the add method adding fooBar to the payload of the POST call made from HttpClient
  • Finally, in our main module we changed the providers:
[
    { provide: ActiveCartService, useClass: E2ActiveCartService },
    { provide: MultiCartService, useClass: E2MultiCartService },
    E2CartEntryEffects,
    { provide: CartEntryConnector, useClass: E2CartEntryConnector },
    { provide: E2CartEntryAdapter, useClass: E2OccCartEntryAdapter },
]

This solution seems to work but we think that's pretty complex for a relatively simple change and we would like to know if our approach is correct or if there is a better, cleaner way.

Kind regards

like image 291
Filippo Grecchi Avatar asked Nov 15 '22 21:11

Filippo Grecchi


1 Answers

Currently, it seems like the correct way for this specific use case.

You did not exactly specify what fooBar is used for, but I assume, it's used and can be toggled inside the top-level layer (in UI component) and has to be passed by all other layers down to the adapter.

On the other hand, if for example, fooBar would just be a property of the Product model, then it would be a matter of extending only the 'Product' model + the changes where it's important (component, adapter), and all other places could be left intact (would just pass extended model).

Can you please give some more context of the required change, so we can better understand it, and make it easier in the future?

In near future, we are considering unifying the payload of our facade services through all the layers in Spartacus, so adding more context to any core logic will be much simplified (basically will come down to the second example, regarding extending the Product model).

like image 190
dunqan Avatar answered Jun 28 '23 05:06

dunqan