Skip to content
This repository has been archived by the owner on Oct 31, 2024. It is now read-only.

Commit

Permalink
feat: implement change config feature; listen to config changes; stor…
Browse files Browse the repository at this point in the history
…e contextToken in cookie
  • Loading branch information
patzick committed Nov 14, 2019
1 parent 8db6666 commit e99a346
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 5 deletions.
9 changes: 5 additions & 4 deletions packages/default-theme/nuxt.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default {
/*
** Plugins to load before mounting the App
*/
plugins: [],
plugins: ['~/plugins/api-client'],
/*
** Nuxt.js dev-modules
*/
Expand All @@ -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
Expand Down Expand Up @@ -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
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion packages/default-theme/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
24 changes: 24 additions & 0 deletions packages/default-theme/plugins/api-client.js
Original file line number Diff line number Diff line change
@@ -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
})
})
}
14 changes: 14 additions & 0 deletions packages/shopware-6-client/src/index.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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}))
}
21 changes: 21 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit e99a346

Please sign in to comment.