2023-December - New changes approaching for overriding composables, full types control and new API Client integration #471
patzick
announced in
Announcements
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
We're delighted to introduce you to some features coming to Composable Frontends in the next release.
IMPORTANT: Currently, this announcement is for
canary
releases only. After your feedback and broader testing, we'll release it as thelatest
stable version. Please provide your feedback and questions!Key takeaways:
Read more about specific points and migration steps you'll need to take to upgrade your project. We tried to make it as easy as possible, but it still requires some work. We hope you'll like the new features and the new API Client.
Overriding composables
Composables are one of the main features of the Composable Frontends. They are responsible for the logic of the application. We're introducing a new way of extending and overriding composables. With the new changes, you can:
useCart
will replaceuseCart
inuseAddToCart
composable. This gives maximum flexibility but also the responsibility - you can make things and break things, so be careful.Product
type inuseProduct
composable and across the whole application. This is a big step towards customisation and flexibility.Steps for migration
@shopware-pwa/composables-next
→ search for it in code and removetype
imports likeNotification
BoxLayout
DisplayMode
etc., update imports in the following pattern: exampleimport type { BoxLayout, DisplayMode } from "@shopware-pwa/composables-next";
package.json
- change package"@shopware-pwa/api-client"
to new API client"@shopware/api-client"
. Remove the old package.nuxt.config.ts
in nuxt configshopwareEndpoint
now needs to point exactly to the store API. Thanks to this whole URL could also be available behind a proxy. Example change:shopwareEndpoint: "https://demo-frontends.shopware.store/",
→shopwareEndpoint: "https://demo-frontends.shopware.store/store-api/",
nuxt.config.ts
- remove@shopware-pwa/cms-base
frommodules
arraynuxt.config.ts
- add nuxt layers of composables and cms - add lineextends: ["@shopware-pwa/composables-next/nuxt-layer", "@shopware-pwa/cms-base"],
Adaptation of the new API Client
The previous release introduced our new API Client package. At this point, we're integrating this package into our packages, and we're removing the old one.
With the new package, you can:
Steps for migration
@shopware-pwa/api-client
in your codeapiInstance
toapiClient
fromuseShopwareContext
. Example:const { apiInstance } = useShopwareContext();
→const { apiClient } = useShopwareContext();
@shopware-pwa/api-client
. With the code autocompletion, it should be very straightforward:so for example, fetching wishlist products from
becomes:
@shopware-pwa/api-client
Another examples
Newsletter confirmation:
into:
Hand written requests via
invokeGet example:
into
remember - we always have your back with type support
also with all the parameters required (and optional) in request
Error handling
With the new API client Error handling is also improved. You can log errors with descriptive messages and also see more informational Errors.
The old way of handling API client errors was similar to this:
the new way checks the instance of the error and helps you with the types
you can also console that error while developing so you’d get a clear error message. More info → https://www.npmjs.com/package/@shopware/api-client#error-handling
Overriding types
In order to have full control over your types we provided a possibility to override them. This is especially useful when you want to add some custom fields to the schema, change the type of the field, or there is some error in the OpenAPI schema, and you need to make a quick fix.
in the root of your project, add
shopware.d.ts
file:Server routes changes
In vue-demo store, we added a sitemap builder. This also requires adjustments for the new API client.
apiBuilder.ts
frominto
into
Remove dependency from
@shopware-pwa/types
Types are becoming yours to have. For specific versions, overrides. Current types are changed under the hood and deprecated. It should all still work for your project, but these are the recommended steps of migration:
@shopware-pwa/types
import type { Schemas } from "#shopware";
and then replace used entity. Example transitions:you have full auto-completion for all the schema types from your API instance:
Pure Vite application
This step is for someone who uses pure Vite application, without Nuxt modules, etc.
@shopware-pwa/api-client
to@shopware/api-client
apiClient.ts
file with your API client definition, for examplemain.ts
fileimports.d.ts
file with contentadd
shopware.d.ts
file with content from ## Overriding typesedit the vite config file, add
resolve.alias
config:tsconfig.json
file addcompilerOptions.paths
:Troubleshooting
apiClient
hasany
typewhen facing this problem, your IDE probably didn’t refresh the types properly. You can help a bit by restarting TypeScript and Vue Server. Just hit cmd+shift+p in VS code and pick the Restart TS Server option.
apiClient
is not completing my typesdo the same as
apiClient has any type
if that didn’t help - add
shopware.d.ts
file and restart ts server againno type support and code completion
If previous steps didn't work - in
tsconfig.json
, check thatmoduleResolution
is set tonode
(before it was bundler). This can cause a problem with the exported Types (see) for the new API Client. We'll investigate this topic more in the futureWhat's next?
We'd like to stabilize the API and release 1.0 version of the packages, in the meantime we're updating OpenAPI schemas to make them better and remove inconsistencies. This will also include some naming changes like removing usage of
@shopware-pwa
npm package in names or thenext
suffix. This shouldn't be anything that will make a migration hard, and we'll provide necessary migration steps. Keep an eye on our Github Discussions for more information and updates.Beta Was this translation helpful? Give feedback.
All reactions