英文:
How can we access store API endpoints from javascript in Shopware 6.5?
问题
In 6.5,我们应该如何从JavaScript访问商店的API端点?比如,在6.4中的JavaScript插件文件如下:
self._storeApiClient.get('/store-api/script/modigChatApp/initiate-chat', function (json) {
const response = JSON.parse(json);
if (!response.hasOwnProperty('subscriberToken')) {
reject(response);
return;
}
resolve(response.subscriberToken);
});
在6.5中会是什么样子呢?
英文:
I see store-api-client.service.js was deleted in 6.5, however the docs are still referencing it: https://developer.shopware.com/docs/guides/plugins/plugins/storefront/fetching-data-with-javascript#call-the-store-api-from-the-storefront.
How should we access store API endpoints from javascript in 6.5?
Let's say a javascript plugin file that previously in 6.4 had:
self._storeApiClient.get('/store-api/script/modigChatApp/initiate-chat', function (json) {
const response = JSON.parse(json);
if (!response.hasOwnProperty('subscriberToken')) {
reject(response);
return;
}
resolve(response.subscriberToken);
});
How would this look like for 6.5?
答案1
得分: 0
以下是翻译好的部分:
在更新日志中引用了弃用通知:
“store-api”代理路由已删除。请直接使用store-api。如果不可能,请使用自定义控制器,该控制器在内部调用“StoreApiRoute”。
来自“storefront/src/service/store-api-client.service.js”的“StoreApiClient”类也已删除,因为该类依赖于代理路由。
提到的代理路由允许您在不需要显式授权的情况下使用store-api,因为代理在内部授权请求。这被认为是一种不可取的解决方法。如果要使用store-api,您必须创建一个适当的集成,该集成将为您提供访问密钥。然后,在请求store-api时,您可以使用访问密钥进行授权。
正如更新日志所述,如果您不想处理授权,还可以创建一个自定义控制器,该控制器在内部调用store-api端点,绕过授权。请参考原始代理控制器。
英文:
I quote the deprecation notice in the changelog:
> The store-api
proxy route was removed. Please use the store-api directly.
If that is not possible use a custom controller, that calls the StoreApiRoute
internally.
The StoreApiClient
class from storefront/src/service/store-api-client.service.js
was also removed, as that class relied on the proxy route.
That proxy route mentioned allowed you to use the store-api without explicit authorization, as the proxy used to authorize the request internally. This was deemed an undesirable workaround. If you want to use the store-api you'll have to create a proper integration, that will give you an access key. You then use the access key for authorization when you request the store-api.
As the changelog mentions, if you don't want to handle authorization, you can also create a custom controller that calls a store-api endpoint internally, bypassing the authorization. See the original proxy controller for reference.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论