shopware/frontends - composables-next
Set of Vue.js composition functions that can be used in any Vue.js project. They provide state management, UI logic and data fetching and are the base for all guides in our building section.
Features
-
createShopwareContext
method to create a Vue 3 plugin to install - State management
- Logic for UI
- Communication with Store-API via api-client package
Setup
Install npm packages (composables & api-client):
# Using pnpm
pnpm add @shopware-pwa/composables-next @shopware-pwa/api-client
# Using yarn
yarn add @shopware-pwa/composables-next @shopware-pwa/api-client
# Using npm
npm i @shopware-pwa/composables-next @shopware-pwa/api-client
Initialize the api-client instance:
import { createInstance } from "@shopware-pwa/api-client";
const apiInstance = createInstance({
endpoint: "https://your-api-instance.com",
accessToken: "your-sales-channel-access-token",
});
Now, we can create a Vue 3 plugin to install a Shopware context in an app:
// app variable in type of App
const shopwareContext = createShopwareContext(app, {
apiInstance, // apiInstance from previous step
devStorefrontUrl: "https://your-sales-channel-configured-domain.com",
});
// register a plugin in a Vue instance
app.use(shopwareContext);
The example does not provide the session handling and that means you need to do few additional steps if you need to keep your session after the page reload (see the chapter below with 🍪)
Basic usage
Now you can use any composable function in your setup function:
<script setup>
import { useUser, useSessionContext } from "@shopware-pwa/composables-next";
const { login } = useUser();
const { refreshSessionContext, sessionContext } = useSessionContext();
refreshSessionContext();
</script>
<template>
<pre>{{ sessionContext }}</pre>
<button @click="login({
username: "some-user",
password: "secret-passwd"
})">
Try to login!
</button>
</template>
Session persistence with 🍪
By default, the API-Client is stateless, but accepts an optional context token as a parameter while initializing an instance. In order to keep a session, install some cookie parser to work with cookies easier:
# Using pnpm
pnpm add js-cookie
# Using yarn
yarn add js-cookie
# Using npm
npm i js-cookie
Let's get back to the step where the api-client
was initialized:
import { createInstance } from "@shopware-pwa/api-client";
import Cookies from "js-cookie";
const apiInstance = createInstance({
endpoint: "https://your-api-instance.com",
accessToken: "your-sales-channel-access-token",
contextToken: Cookies.get("sw-context-token"), // get the context token if exists in the cookies
});
// callback to detect a `sw-context-token` in the response
apiInstance.onConfigChange(({ config }) => {
// set the context-token in the cookie
Cookies.set("sw-context-token", config.contextToken || "", {
expires: 365, // days
path: "/",
sameSite: "lax",
});
});
Thanks to this, the session will be kept to the corresponding sw-context-token
saved in the cookie, so it can be reachable also in the SSR. Check the example to see it in action:
TypeScript support
All composable functions are fully typed with TypeScript and they are registed globally in Nuxt.js application, so the type hinting will help you to work with all of them.
Links
-
👥 Community Slack (
#shopware-frontends
&#shopware-pwa
channel)
Changelog
Full changelog for stable version is available here
Latest changes: 0.14.1
Patch Changes
-
#462
c3aa09ee
Thanks @patzick! - Dependency changes:- Changed dependency @vueuse/core from ^10.5.0 to ^10.6.1
-
#467
0e031efe
Thanks @patzick! - Dependency changes:- Changed dependency scule from ^1.0.0 to ^1.1.0
-
Updated dependencies [
729d03a5
]:- @shopware-pwa/helpers-next@0.5.1
- @shopware-pwa/api-client@0.7.0