欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

SAP Spartacus 使用 customized API

发布时间:2023/12/19 编程问答 53 豆豆
生活随笔 收集整理的这篇文章主要介绍了 SAP Spartacus 使用 customized API 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

https://stackoverflow.com/questions/67810796/integrating-customised-apis-in-spartacus-storefront

场景

客户对 addEntry 这个 SAP Hybris API 做了增强,payload 里增加了一个新的 boolean 类型的字段,名为 fooBar.

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:

Payload 的例子:

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

为了让这个定制化后的 API 在 Spartacus 里消费,需要完成下列步骤:

  • Overridden the AddToCartComponent
  • 标准的 active-cart.service.ts 里,addEntry 方法只有两个参数:

    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’)
  • 需要拷贝 CartAddEntry 成一个新的 action:

    3.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

  • 使用新的 providers:
  • [{ provide: ActiveCartService, useClass: E2ActiveCartService },{ provide: MultiCartService, useClass: E2MultiCartService },E2CartEntryEffects,{ provide: CartEntryConnector, useClass: E2CartEntryConnector },{ provide: E2CartEntryAdapter, useClass: E2OccCartEntryAdapter }, ]

    更多Jerry的原创文章,尽在:“汪子熙”:

    总结

    以上是生活随笔为你收集整理的SAP Spartacus 使用 customized API的全部内容,希望文章能够帮你解决所遇到的问题。

    如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。