diff --git a/packages/default-theme/nuxt.config.js b/packages/default-theme/nuxt.config.js index 0c3688a3b..ba691790c 100644 --- a/packages/default-theme/nuxt.config.js +++ b/packages/default-theme/nuxt.config.js @@ -29,7 +29,7 @@ export default { /* ** Plugins to load before mounting the App */ - plugins: [], + plugins: ['~/plugins/api-client'], /* ** Nuxt.js dev-modules */ @@ -53,7 +53,8 @@ export default { modules: [ // Doc: https://axios.nuxtjs.org/usage '@nuxtjs/axios', - '@nuxtjs/pwa' + '@nuxtjs/pwa', + 'cookie-universal-nuxt' ], /* ** Axios module configuration @@ -82,8 +83,8 @@ export default { ** You can extend webpack config here */ extend(config, { isDev, isClient }) { - if (isClient && !isDev) { - config.optimization.splitChunks.cacheGroups.commons.minChunks = 2 + if (isClient && !isDev) { + config.optimization.splitChunks.cacheGroups.commons.minChunks = 2 } } } diff --git a/packages/default-theme/package.json b/packages/default-theme/package.json index ae8b44f5b..8cf4941e6 100644 --- a/packages/default-theme/package.json +++ b/packages/default-theme/package.json @@ -25,14 +25,15 @@ "@nuxtjs/pwa": "^3.0.0-beta.19", "@shopware-pwa/shopware-6-client": "^0.1.0", "@storefront-ui/vue": "^0.3.3", + "cookie-universal-nuxt": "^2.0.18", "nuxt": "^2.10.2" }, "devDependencies": { "@babel/runtime-corejs3": "^7.7.1", "@nuxtjs/eslint-config": "^1.0.2", "@nuxtjs/eslint-module": "^1.1.0", - "@vue/test-utils": "^1.0.0-beta.29", "@vue-storefront/nuxt": "^0.0.1", + "@vue/test-utils": "^1.0.0-beta.29", "babel-eslint": "^10.0.3", "babel-jest": "^24.9.0", "eslint": "^6.6.0", diff --git a/packages/default-theme/plugins/api-client.js b/packages/default-theme/plugins/api-client.js new file mode 100644 index 000000000..58f81b7d7 --- /dev/null +++ b/packages/default-theme/plugins/api-client.js @@ -0,0 +1,24 @@ +import { setup, onConfigChange } from '@shopware-pwa/shopware-6-client' + +export default ({ app }) => { + if (!app.$cookies) { + throw 'Error cookie-universal-nuxt module is not applied in nuxt.config.js' + } + const contextToken = app.$cookies.get('sw-context-token') || '' + /** + * Setup Shopware API client + */ + setup({ + endpoint: 'https://shopware-2.vuestorefront.io/sales-channel-api/v1', + accessToken: 'SWSCBVBBZET1RTFIYWY4YVLICA', + contextToken + }) + /** + * Save current contextToken when its change + */ + onConfigChange(({ config }) => { + app.$cookies.set('sw-context-token', config.contextToken, { + maxAge: 60 * 60 * 24 * 365 + }) + }) +} diff --git a/packages/shopware-6-client/src/index.ts b/packages/shopware-6-client/src/index.ts index 3d27d3368..47258fe89 100644 --- a/packages/shopware-6-client/src/index.ts +++ b/packages/shopware-6-client/src/index.ts @@ -1,5 +1,6 @@ import { ClientSettings, setupConfig, updateConfig } from "./settings"; import { reloadConfiguration } from "./apiService"; +import { config } from "./settings"; export { config } from "./settings"; export * from "./services/categoryService"; @@ -24,4 +25,17 @@ export function setup(config: ClientSettings = {}): void { export function update(config: ClientSettings = {}): void { updateConfig(config); reloadConfiguration(); + configChanged(); } + +export interface ConfigChangedArgs { + config:ClientSettings +} +const callbackMethods: ((context: ConfigChangedArgs) => void)[] = [] + +export function onConfigChange (fn: (context: ConfigChangedArgs) => void) { + callbackMethods.push(fn) +} +function configChanged (): void { + callbackMethods.forEach(fn => fn({config})) +} \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index d7c0eeff3..91e9cbef3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2269,6 +2269,11 @@ dependencies: "@types/node" "*" +"@types/cookie@^0.3.1": + version "0.3.3" + resolved "https://registry.yarnpkg.com/@types/cookie/-/cookie-0.3.3.tgz#85bc74ba782fb7aa3a514d11767832b0e3bc6803" + integrity sha512-LKVP3cgXBT9RYj+t+9FDKwS5tdI+rPBXaNSkma7hvqy35lc7mAokC2zsqWJH0LaqIt3B962nuYI77hsJoT1gow== + "@types/estree@*": version "0.0.39" resolved "https://registry.yarnpkg.com/@types/estree/-/estree-0.0.39.tgz#e177e699ee1b8c22d23174caaa7422644389509f" @@ -4641,6 +4646,22 @@ cookie-signature@1.0.6: resolved "https://registry.yarnpkg.com/cookie-signature/-/cookie-signature-1.0.6.tgz#e303a882b342cc3ee8ca513a79999734dab3ae2c" integrity sha1-4wOogrNCzD7oylE6eZmXNNqzriw= +cookie-universal-nuxt@^2.0.18: + version "2.0.18" + resolved "https://registry.yarnpkg.com/cookie-universal-nuxt/-/cookie-universal-nuxt-2.0.18.tgz#95e762a88b5a5b6c23db05521c146260b5576358" + integrity sha512-+5ciWAm1B15JN5e4LVnU4Ovs9KqBeYFYwaHrm9ThDZr/12u9REJfxH3wji0iY9NnF2ard3ULlD+R4uEQ0vUNKg== + dependencies: + "@types/cookie" "^0.3.1" + cookie-universal "^2.0.16" + +cookie-universal@^2.0.16: + version "2.0.16" + resolved "https://registry.yarnpkg.com/cookie-universal/-/cookie-universal-2.0.16.tgz#ec8b55789b502a377ef02ad230923c1dfa5c1061" + integrity sha512-EHtQ5Tg3UoUHG7LmeV3rlV3iYthkhUuYZ0y86EseypxGcUuvzxuHExEb6mHKDhDPrIrdewAHdG/aCHuG/T4zEg== + dependencies: + "@types/cookie" "^0.3.1" + cookie "^0.3.1" + cookie@0.4.0: version "0.4.0" resolved "https://registry.yarnpkg.com/cookie/-/cookie-0.4.0.tgz#beb437e7022b3b6d49019d088665303ebe9c14ba"