Skip to content

Commit 8ea533e

Browse files
refactor: use closure to create multistore extension (#7026)
* fix multistore types export * refactor: rename multistore directory * docs: add multistore documentation * docs: add multistore documentation * regenerate docs changelogs and api references * refactor: implement a factory to create an extension * docs: update multistore docs * add changeset * add changeset for fix * update changesets * fix merge issues
1 parent 3ab1aa8 commit 8ea533e

File tree

12 files changed

+173
-256
lines changed

12 files changed

+173
-256
lines changed

Diff for: .changeset/six-dolls-accept.md

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
"@vue-storefront/multistore": major
3+
---
4+
5+
[CHANGED] We standardized the way of creating and configuring multistore extension.
6+
Previously, the extension was created by importing `multistoreExtension` from `@vue-storefront/multistore` and passing it to the `extensions` function.
7+
Configuration was passed to the extension by adding `multistore` property to the `configuration` object.
8+
Now, the extension is created by calling `createMultistoreExtension` from `@vue-storefront/multistore` and passing the multistore configuration to it.
9+
10+
```diff [middleware.config.ts]
11+
- import { multistoreExtension } from "@vue-storefront/multistore";
12+
+ import { createMultistoreExtension } from "@vue-storefront/multistore";
13+
import { multistoreConfig } from "./multistore.config";
14+
15+
export default {
16+
integrations: {
17+
sapcc: {
18+
location: "@vue-storefront/sapcc-api/server",
19+
configuration: {
20+
// ...
21+
- multistore: multistoreConfig,
22+
},
23+
extensions: (predefinedExtensions) => [
24+
...predefinedExtensions,
25+
- multistoreExtension,
26+
+ createMultistoreExtension(multistoreConfig),
27+
],
28+
},
29+
},
30+
};
31+
```

Diff for: .changeset/soft-kiwis-fly.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@vue-storefront/multistore": major
3+
---
4+
5+
[FIXED] singleton cache issue, previously the cache was a singleton which could lead to unexpected behaviour when extension was used in different integrations in parallel. Now, the cache is being created during extension creation, what ensures proper cache behaviour.

Diff for: docs/content/3.middleware/2.guides/6.multistore.md

+15-39
Original file line numberDiff line numberDiff line change
@@ -23,33 +23,9 @@ Ensure the following prerequisites are met for the unified multistore solution:
2323

2424
To configure multistore in your middleware, follow these steps:
2525

26-
1. Extend Middleware Config with multistore Extension
26+
1. Prepare multistore configuration:
2727

28-
- Import `multistoreExtension` from `@vue-storefront/multistore`.
29-
- Extend the middleware config in `middleware.config.ts`.
30-
31-
Example: Add `multistoreExtension` to the extensions array for SAP integration.
32-
33-
```ts [middleware.config.ts]
34-
import { multistoreExtension } from '@vue-storefront/multistore';
35-
36-
export default {
37-
integrations: {
38-
sapcc: {
39-
location: '@vue-storefront/sapcc-api/server',
40-
configuration: { ... },
41-
extensions: (predefinedExtensions) => [
42-
...predefinedExtensions,
43-
multistoreExtension
44-
]
45-
}
46-
}
47-
};
48-
```
49-
50-
2. Create multistore Configuration
51-
52-
- Prepare a `multistore.config.ts` file with methods:
28+
- Create a `multistore.config.ts` file with methods:
5329
- `fetchConfiguration({ domain })`: Returns store-specific configurations based on domain.
5430
- `mergeConfigurations({ baseConfig, storeConfig })`: Merges base configuration with store-specific settings.
5531
- `cacheManagerFactory()`: Implements cache manager with get and set methods.
@@ -100,29 +76,29 @@ export const multistoreConfig = {
10076
};
10177
```
10278

103-
3. Integrate multistore Configuration
79+
2. Extend middleware config with multistore extension:
10480

105-
- Add the multistore configuration from `multistore.config.ts` to your `middleware.config.ts`.
81+
- Import `createMultistoreExtension` from `@vue-storefront/multistore`.
82+
- Import multistore configuration from `multistore.config.ts`.
83+
- Extend the middleware config in `middleware.config.ts`.
10684

107-
Example: Add multistore configuration to `middleware.config.ts`.
85+
Example: Extending middleware config with multistore extension.
10886

10987
```ts [middleware.config.ts]
110-
import { multistoreConfig } from "./multistore.config";
88+
import { multistoreExtension } from '@vue-storefront/multistore';
89+
import { multistoreConfig } from './multistore.config';
11190

11291
export default {
11392
integrations: {
11493
sapcc: {
115-
location: "@vue-storefront/sapcc-api/server",
116-
configuration: {
117-
// ...
118-
multistore: multistoreConfig,
119-
},
94+
location: '@vue-storefront/sapcc-api/server',
95+
configuration: { ... },
12096
extensions: (predefinedExtensions) => [
12197
...predefinedExtensions,
122-
multistoreExtension,
123-
],
124-
},
125-
},
98+
createMultistoreExtension(multistoreConfig)
99+
]
100+
}
101+
}
126102
};
127103
```
128104

Diff for: packages/multistore/README.md

+20-102
Original file line numberDiff line numberDiff line change
@@ -2,120 +2,38 @@
22

33
The `@vue-storefront/multistore` package provides a middleware extension for multistore functionality. It changes the middleware configuration to support multiple stores based on the domain configuration.
44

5-
## Prerequisites
5+
## Usage
66

7-
Ensure the following prerequisites are met for the unified multistore solution:
7+
To learn about the prerequisites, setup and architecture of the `@vue-storefront/multistore` package, please refer to the [multistore documentation](https://docs.vuestorefront.io/middleware/multistore).
88

9-
- It works within the VSF infrastructure.
10-
- Requires three headers for proper functionality:
11-
1. `origin` for client-server communication.
12-
2. `x-forwarded-host` for server-server communication.
13-
3. `host` as a fallback for server-server communication if `x-forwarded-host` is absent.
14-
- The client communicating with the middleware must include these headers in requests.
9+
## Development
1510

16-
## Setup Steps
11+
To start development on the `@vue-storefront/multistore` package, clone the repository and install dependencies:
1712

18-
To configure multistore in your middleware, follow these steps:
19-
20-
1. Extend Middleware Config with multistore Extension
21-
22-
- Import `multistoreExtension` from `@vue-storefront/multistore`.
23-
- Extend the middleware config in `middleware.config.ts`.
24-
25-
Example: Add `multistoreExtension` to the extensions array for SAP integration.
26-
27-
```ts [middleware.config.ts]
28-
import { multistoreExtension } from '@vue-storefront/multistore';
29-
30-
export default {
31-
integrations: {
32-
sap: {
33-
location: '@vue-storefront/sapcc-api/server',
34-
configuration: { ... },
35-
extensions: (predefinedExtensions) => [
36-
...predefinedExtensions,
37-
multistoreExtension
38-
]
39-
}
40-
}
41-
};
13+
```shell
14+
yarn install
4215
```
4316

44-
2. Create multistore Configuration
17+
### Build
4518

46-
- Prepare a `multistore.config.ts` file with methods:
47-
- `fetchConfiguration({ domain })`: Returns store-specific configurations based on domain.
48-
- `mergeConfigurations({ baseConfig, storeConfig })`: Merges base configuration with store-specific settings.
49-
- `cacheManagerFactory()`: Implements cache manager with get and set methods.
19+
To build the package, run:
5020

51-
Example: Configuration that modifies the api parameter and uses `node-cache`.
21+
```shell
22+
yarn build
23+
```
5224

53-
```ts [multistore.config.ts]
54-
import NodeCache from "node-cache";
25+
### Linting
5526

56-
export const multistoreConfig = {
57-
fetchConfiguration(/* { domain } */) {
58-
return {
59-
"my-apparel-domain.io": {
60-
baseSiteId: "apparel-uk",
61-
defaultCurrency: "GBP",
62-
// ...
63-
},
64-
"my-electronics-domain.io": {
65-
baseSiteId: "electronics",
66-
defaultCurrency: "USD",
67-
// ...
68-
},
69-
};
70-
},
71-
mergeConfigurations({ baseConfig, storeConfig }) {
72-
return {
73-
...baseConfig,
74-
api: {
75-
...baseConfig.api,
76-
...storeConfig,
77-
},
78-
};
79-
},
80-
cacheManagerFactory() {
81-
const client = new NodeCache({
82-
stdTTL: 10,
83-
});
27+
To lint the package, run:
8428

85-
return {
86-
get(key) {
87-
return client.get(key);
88-
},
89-
set(key, value) {
90-
return client.set(key, value);
91-
},
92-
};
93-
},
94-
};
29+
```shell
30+
yarn lint
9531
```
9632

97-
3. Integrate multistore Configuration
98-
99-
- Add the multistore configuration from `multistore.config.ts` to your `middleware.config.ts`.
33+
### Testing
10034

101-
Example: Add multistore configuration to `middleware.config.ts`.
35+
To test the package, run:
10236

103-
```ts [middleware.config.ts]
104-
import { multistoreConfig } from "./multistore.config";
105-
106-
export default {
107-
integrations: {
108-
sap: {
109-
location: "@vue-storefront/sapcc-api/server",
110-
configuration: {
111-
// ...
112-
multistore: multistoreConfig,
113-
},
114-
extensions: (predefinedExtensions) => [
115-
...predefinedExtensions,
116-
multistoreExtension,
117-
],
118-
},
119-
},
120-
};
121-
```
37+
```shell
38+
yarn test
39+
```

Diff for: packages/multistore/__mocks__/middleware.config.mock.ts

-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
/* eslint-disable no-useless-return */
22
/* eslint-disable @typescript-eslint/no-unused-vars */
3-
/* eslint-disable no-unused-vars */
4-
import { mockMultistoreConfig } from "./multistore.config.mock";
5-
6-
const multistore = mockMultistoreConfig();
7-
83
export const mockMiddlewareConfig = () => {
94
return {
105
OAuth: {
@@ -25,6 +20,5 @@ export const mockMiddlewareConfig = () => {
2520
defaultLanguage: "en",
2621
defaultCurrency: "GBP",
2722
},
28-
multistore,
2923
};
3024
};

0 commit comments

Comments
 (0)