@shopware/nuxt-module 0.0.0-canary-20250116171244
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +142 -0
- package/dist/index.d.mts +49 -0
- package/dist/index.d.ts +49 -0
- package/dist/index.mjs +56 -0
- package/index.cjs +6 -0
- package/package.json +60 -0
- package/plugin.ts +111 -0
- package/shopware.d.ts +19 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022-2023 Shopware
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
# shopware/frontends - nuxt-module
|
|
2
|
+
|
|
3
|
+
[](https://npmjs.com/package/@shopware/nuxt-module)
|
|
4
|
+
[](https://github.com/shopware/frontends/tree/main/packages/nuxt-module)
|
|
5
|
+
[](https://github.com/shopware/frontends/issues?q=is%3Aopen+is%3Aissue+label%3Anuxt-module)
|
|
6
|
+
[](#)
|
|
7
|
+
|
|
8
|
+
Nuxt [module](https://nuxt.com/docs/guide/going-further/modules) that allows you to set up a Nuxt 3 project with Shopware Frontends. It provides the composables and api-client packages.
|
|
9
|
+
|
|
10
|
+
If you want to use these packages with a different Vue.js framework, see [the guide](https://frontends.shopware.com/getting-started/templates/custom-vue-project.html) for using Shopware Frontends in a custom project.
|
|
11
|
+
|
|
12
|
+
## Features
|
|
13
|
+
|
|
14
|
+
- Business logic covered by [Composables](https://npmjs.com/package/@shopware/composables) package. Registering all composable functions globally. [See the reference](https://frontends.shopware.com/packages/composables.html).
|
|
15
|
+
- Shopware context shared in Nuxt application.
|
|
16
|
+
- Configured [API Client](https://npmjs.com/package/@shopware/api-client) package.
|
|
17
|
+
|
|
18
|
+
## Setup
|
|
19
|
+
|
|
20
|
+
Install npm package:
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
# Using pnpm
|
|
24
|
+
pnpm add -D @shopware/nuxt-module
|
|
25
|
+
|
|
26
|
+
# Using yarn
|
|
27
|
+
yarn add --dev @shopware/nuxt-module
|
|
28
|
+
|
|
29
|
+
# Using npm
|
|
30
|
+
npm i @shopware/nuxt-module --save-dev
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Then, register the module by editing `nuxt.config.js` or (`.ts`) file (by extending `modules` array):
|
|
34
|
+
|
|
35
|
+
```js
|
|
36
|
+
/* nuxt.config.ts */
|
|
37
|
+
|
|
38
|
+
export default defineNuxtConfig({
|
|
39
|
+
/* ... */
|
|
40
|
+
modules: [, /* ... */ "@shopware/nuxt-module"],
|
|
41
|
+
// set the module config
|
|
42
|
+
shopware: {
|
|
43
|
+
// connect to your Shopware 6 API instance
|
|
44
|
+
endpoint: "https://demo-frontends.shopware.store",
|
|
45
|
+
accessToken: "SWSCBHFSNTVMAWNZDNFKSHLAYW",
|
|
46
|
+
},
|
|
47
|
+
// or directly in the runtime config
|
|
48
|
+
// this config will override the base one
|
|
49
|
+
runtimeConfig: {
|
|
50
|
+
public: {
|
|
51
|
+
shopware: {
|
|
52
|
+
endpoint: "https://demo-frontends.shopware.store",
|
|
53
|
+
accessToken: "SWSCBHFSNTVMAWNZDNFKSHLAYW",
|
|
54
|
+
},
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
});
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
Set up your own API instance under `shopware` key or by extending public `runtimeConfiguration` in the same file. The nuxt module (and vue plugin) will use those values (runtimeConfig will always override the base ones).
|
|
61
|
+
|
|
62
|
+
## Basic usage
|
|
63
|
+
|
|
64
|
+
Now you can use any composable function you need without extra import:
|
|
65
|
+
|
|
66
|
+
```html
|
|
67
|
+
<script setup>
|
|
68
|
+
const { login } = useUser();
|
|
69
|
+
const { refreshSessionContext } = useSessionContext();
|
|
70
|
+
refreshSessionContext();
|
|
71
|
+
</script>
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
The information about the session is kept in a cookie (`sw-context-token`) and used in every request made by any composable or directly, invoked by `api instance`:
|
|
75
|
+
|
|
76
|
+
```html
|
|
77
|
+
<script>
|
|
78
|
+
const { apiClient } = useShopwareContext();
|
|
79
|
+
const apiResponse = await apiClient.invoke(/** params omitted */);
|
|
80
|
+
</script>
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## TypeScript support
|
|
84
|
+
|
|
85
|
+
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.
|
|
86
|
+
|
|
87
|
+
## 📦 Advanced packaging
|
|
88
|
+
|
|
89
|
+
Internally, the module uses [API Client](https://npmjs.com/package/@shopware/api-client) and [Composables](https://npmjs.com/package/@shopware/composables) packages, configured together to make everything working well. If you need to check how it's working on a different version of one of them, install a package locally in your project (to be installed and available in project's `package.json` file), then the Nuxt module will use yours. Keep in mind that the different configuration may lead to unexpected behavior.
|
|
90
|
+
|
|
91
|
+
## API Default Headers
|
|
92
|
+
|
|
93
|
+
You can use Nuxt config to set the default API call headers.
|
|
94
|
+
More about Nuxt configuration can be found [HERE](https://nuxt.com/docs/getting-started/configuration).
|
|
95
|
+
|
|
96
|
+
> **_NOTE:_** By default, the values in `runtimeConfig` are only available on the server-side. However, keys within `runtimeConfig.public` are also accessible on the client-side. [MORE](https://nuxt.com/docs/getting-started/configuration#environment-variables-and-private-tokens)
|
|
97
|
+
|
|
98
|
+
```json
|
|
99
|
+
{
|
|
100
|
+
"runtimeConfig": {
|
|
101
|
+
"public": {
|
|
102
|
+
"apiClientConfig": {
|
|
103
|
+
"headers": {
|
|
104
|
+
"global-heder-example": "global-header-example-value"
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
},
|
|
108
|
+
"apiClientConfig": {
|
|
109
|
+
"headers": {
|
|
110
|
+
"ssr-heder-example": "ssr-header-example-value"
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
## Links
|
|
118
|
+
|
|
119
|
+
- [📘 Documentation](https://frontends.shopware.com)
|
|
120
|
+
|
|
121
|
+
- [👥 Community](https://shopwarecommunity.slack.com) (`#composable-frontends`)
|
|
122
|
+
<!-- AUTO GENERATED CHANGELOG -->
|
|
123
|
+
|
|
124
|
+
## Changelog
|
|
125
|
+
|
|
126
|
+
Full changelog for stable version is available [here](https://github.com/shopware/frontends/blob/main/packages/nuxt-module/CHANGELOG.md)
|
|
127
|
+
|
|
128
|
+
### Latest changes: 0.0.0-canary-20250116171244
|
|
129
|
+
|
|
130
|
+
### Minor Changes
|
|
131
|
+
|
|
132
|
+
- [#1602](https://github.com/shopware/frontends/pull/1602) [`bb7d1cb`](https://github.com/shopware/frontends/commit/bb7d1cbc4204ff1d48f77416f94f550bc235e5ed) Thanks [@patzick](https://github.com/patzick)! - Package `@shopware-pwa/nuxt3-module` is deprecated. Use [@shopware/nuxt-module](https://www.npmjs.com/package/@shopware/nuxt-module) instead.
|
|
133
|
+
|
|
134
|
+
- [#1602](https://github.com/shopware/frontends/pull/1602) [`bb7d1cb`](https://github.com/shopware/frontends/commit/bb7d1cbc4204ff1d48f77416f94f550bc235e5ed) Thanks [@patzick](https://github.com/patzick)! - Switch from `@shopware-pwa/helpers-next` to `@shopware/helpers` package.
|
|
135
|
+
|
|
136
|
+
- [#1602](https://github.com/shopware/frontends/pull/1602) [`bb7d1cb`](https://github.com/shopware/frontends/commit/bb7d1cbc4204ff1d48f77416f94f550bc235e5ed) Thanks [@patzick](https://github.com/patzick)! - Switch from `@shopware-pwa/cms-base` to `@shopware/cms-base-layer` package.
|
|
137
|
+
|
|
138
|
+
### Patch Changes
|
|
139
|
+
|
|
140
|
+
- Updated dependencies [[`bb7d1cb`](https://github.com/shopware/frontends/commit/bb7d1cbc4204ff1d48f77416f94f550bc235e5ed), [`bb7d1cb`](https://github.com/shopware/frontends/commit/bb7d1cbc4204ff1d48f77416f94f550bc235e5ed)]:
|
|
141
|
+
- @shopware/composables@0.0.0-canary-20250116171244
|
|
142
|
+
- @shopware/helpers@0.0.0-canary-20250116171244
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import * as _nuxt_schema from '@nuxt/schema';
|
|
2
|
+
|
|
3
|
+
declare const _default: _nuxt_schema.NuxtModule<ShopwareNuxtOptions, ShopwareNuxtOptions, false>;
|
|
4
|
+
|
|
5
|
+
type ShopwareNuxtOptions = {
|
|
6
|
+
/**
|
|
7
|
+
* Endpoint for your shopware backend.
|
|
8
|
+
*
|
|
9
|
+
* Default demo store: "https://demo-frontends.swstage.store/"
|
|
10
|
+
*/
|
|
11
|
+
endpoint?: string;
|
|
12
|
+
shopwareEndpoint?: string;
|
|
13
|
+
accessToken?: string;
|
|
14
|
+
shopwareAccessToken?: string;
|
|
15
|
+
devStorefrontUrl?: string;
|
|
16
|
+
apiClientConfig?: {
|
|
17
|
+
timeout?: number | string;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Use user context in SSR mode. Warning: with wrong edge caching it can cause serving another user's data.
|
|
21
|
+
* Use when edge caching is configured properly.
|
|
22
|
+
*
|
|
23
|
+
* @default false
|
|
24
|
+
*/
|
|
25
|
+
useUserContextInSSR?: boolean;
|
|
26
|
+
};
|
|
27
|
+
declare module "@nuxt/schema" {
|
|
28
|
+
interface NuxtConfig {
|
|
29
|
+
shopware?: ShopwareNuxtOptions;
|
|
30
|
+
}
|
|
31
|
+
interface NuxtOptions {
|
|
32
|
+
shopware?: ShopwareNuxtOptions;
|
|
33
|
+
}
|
|
34
|
+
interface ApiClientConfig {
|
|
35
|
+
headers?: {
|
|
36
|
+
[key: string]: string;
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
interface RuntimeConfig {
|
|
40
|
+
shopware?: Pick<ShopwareNuxtOptions, "endpoint" | "shopwareEndpoint" | "useUserContextInSSR">;
|
|
41
|
+
apiClientConfig?: ApiClientConfig;
|
|
42
|
+
}
|
|
43
|
+
interface PublicRuntimeConfig {
|
|
44
|
+
shopware: ShopwareNuxtOptions;
|
|
45
|
+
apiClientConfig?: ApiClientConfig;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export { type ShopwareNuxtOptions, _default as default };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import * as _nuxt_schema from '@nuxt/schema';
|
|
2
|
+
|
|
3
|
+
declare const _default: _nuxt_schema.NuxtModule<ShopwareNuxtOptions, ShopwareNuxtOptions, false>;
|
|
4
|
+
|
|
5
|
+
type ShopwareNuxtOptions = {
|
|
6
|
+
/**
|
|
7
|
+
* Endpoint for your shopware backend.
|
|
8
|
+
*
|
|
9
|
+
* Default demo store: "https://demo-frontends.swstage.store/"
|
|
10
|
+
*/
|
|
11
|
+
endpoint?: string;
|
|
12
|
+
shopwareEndpoint?: string;
|
|
13
|
+
accessToken?: string;
|
|
14
|
+
shopwareAccessToken?: string;
|
|
15
|
+
devStorefrontUrl?: string;
|
|
16
|
+
apiClientConfig?: {
|
|
17
|
+
timeout?: number | string;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Use user context in SSR mode. Warning: with wrong edge caching it can cause serving another user's data.
|
|
21
|
+
* Use when edge caching is configured properly.
|
|
22
|
+
*
|
|
23
|
+
* @default false
|
|
24
|
+
*/
|
|
25
|
+
useUserContextInSSR?: boolean;
|
|
26
|
+
};
|
|
27
|
+
declare module "@nuxt/schema" {
|
|
28
|
+
interface NuxtConfig {
|
|
29
|
+
shopware?: ShopwareNuxtOptions;
|
|
30
|
+
}
|
|
31
|
+
interface NuxtOptions {
|
|
32
|
+
shopware?: ShopwareNuxtOptions;
|
|
33
|
+
}
|
|
34
|
+
interface ApiClientConfig {
|
|
35
|
+
headers?: {
|
|
36
|
+
[key: string]: string;
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
interface RuntimeConfig {
|
|
40
|
+
shopware?: Pick<ShopwareNuxtOptions, "endpoint" | "shopwareEndpoint" | "useUserContextInSSR">;
|
|
41
|
+
apiClientConfig?: ApiClientConfig;
|
|
42
|
+
}
|
|
43
|
+
interface PublicRuntimeConfig {
|
|
44
|
+
shopware: ShopwareNuxtOptions;
|
|
45
|
+
apiClientConfig?: ApiClientConfig;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export { type ShopwareNuxtOptions, _default as default };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { addCustomTab } from '@nuxt/devtools-kit';
|
|
2
|
+
import { defineNuxtModule, useLogger, createResolver, addPlugin } from '@nuxt/kit';
|
|
3
|
+
import { defu } from 'defu';
|
|
4
|
+
import 'node:fs';
|
|
5
|
+
import { dirname, resolve } from 'node:path';
|
|
6
|
+
import { fileURLToPath } from 'node:url';
|
|
7
|
+
|
|
8
|
+
const distDir = dirname(fileURLToPath(import.meta.url));
|
|
9
|
+
const pkgDir = resolve(distDir, "..");
|
|
10
|
+
resolve(pkgDir, "./node_modules");
|
|
11
|
+
function isConfigDeprecated(config) {
|
|
12
|
+
return Boolean(config?.shopwareEndpoint || config?.shopwareAccessToken);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const MODULE_ID = "@shopware/nuxt3";
|
|
16
|
+
const index = defineNuxtModule({
|
|
17
|
+
meta: {
|
|
18
|
+
name: MODULE_ID,
|
|
19
|
+
configKey: "shopware"
|
|
20
|
+
},
|
|
21
|
+
async setup(options, nuxt) {
|
|
22
|
+
const logger = useLogger(MODULE_ID);
|
|
23
|
+
const resolver = createResolver(import.meta.url);
|
|
24
|
+
nuxt.options.runtimeConfig.public.shopware = defu(
|
|
25
|
+
nuxt.options.runtimeConfig.public.shopware || {},
|
|
26
|
+
options || {}
|
|
27
|
+
);
|
|
28
|
+
if (isConfigDeprecated(nuxt.options?.runtimeConfig?.public?.shopware)) {
|
|
29
|
+
logger.warn(
|
|
30
|
+
"You are using deprecated configuration (shopwareEndpoint or shopwareAccessToken). 'shopware' prefix is not needed anymore. Please update your _nuxt.config.ts_ "
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
if (nuxt.options?.runtimeConfig?.shopware?.endpoint) {
|
|
34
|
+
logger.info(
|
|
35
|
+
`You are using SSR Shopware API endpoint: ${nuxt.options.runtimeConfig.shopware.endpoint}`
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
logger.info(
|
|
39
|
+
`CSR Shopware API endpoint: ${nuxt.options.runtimeConfig.public?.shopware?.endpoint ?? nuxt.options.runtimeConfig.public?.shopware?.shopwareEndpoint}`
|
|
40
|
+
);
|
|
41
|
+
addPlugin({
|
|
42
|
+
src: resolver.resolve("../plugin.ts")
|
|
43
|
+
});
|
|
44
|
+
addCustomTab({
|
|
45
|
+
name: "shopware-frontends",
|
|
46
|
+
title: "Shopware Frontends",
|
|
47
|
+
icon: "fa6-brands:shopware",
|
|
48
|
+
view: {
|
|
49
|
+
type: "iframe",
|
|
50
|
+
src: "https://frontends.shopware.com/"
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
export { index as default };
|
package/index.cjs
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@shopware/nuxt-module",
|
|
3
|
+
"version": "0.0.0-canary-20250116171244",
|
|
4
|
+
"description": "Nuxt 3 module for Shopware Frontends",
|
|
5
|
+
"author": "Shopware",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/shopware/frontends.git"
|
|
9
|
+
},
|
|
10
|
+
"homepage": "https://frontends.shopware.com/framework/internal-structure.html#nuxt-module",
|
|
11
|
+
"keywords": [
|
|
12
|
+
"Nuxt",
|
|
13
|
+
"Shopware",
|
|
14
|
+
"Vue"
|
|
15
|
+
],
|
|
16
|
+
"bugs": {
|
|
17
|
+
"url": "https://github.com/shopware/frontends/issues"
|
|
18
|
+
},
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"main": "./index.cjs",
|
|
21
|
+
"module": "./dist/index.mjs",
|
|
22
|
+
"types": "./dist/index.d.ts",
|
|
23
|
+
"exports": {
|
|
24
|
+
".": {
|
|
25
|
+
"types": "./dist/index.d.ts",
|
|
26
|
+
"import": "./dist/index.mjs",
|
|
27
|
+
"require": "./index.cjs"
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"files": [
|
|
31
|
+
"dist",
|
|
32
|
+
"plugin.ts",
|
|
33
|
+
"shopware.d.ts",
|
|
34
|
+
"index.cjs"
|
|
35
|
+
],
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"@nuxt/kit": "3.14.1592",
|
|
38
|
+
"defu": "6.1.4",
|
|
39
|
+
"js-cookie": "3.0.5",
|
|
40
|
+
"@shopware/composables": "0.0.0-canary-20250116171244",
|
|
41
|
+
"@shopware/helpers": "0.0.0-canary-20250116171244",
|
|
42
|
+
"@shopware/api-client": "1.2.0"
|
|
43
|
+
},
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"@biomejs/biome": "1.8.3",
|
|
46
|
+
"@nuxt/schema": "3.14.1592",
|
|
47
|
+
"nuxt": "3.14.1592",
|
|
48
|
+
"typescript": "5.6.3",
|
|
49
|
+
"unbuild": "2.0.0",
|
|
50
|
+
"tsconfig": "0.0.0"
|
|
51
|
+
},
|
|
52
|
+
"scripts": {
|
|
53
|
+
"build": "unbuild",
|
|
54
|
+
"dev": "unbuild --stub",
|
|
55
|
+
"lint": "biome check .",
|
|
56
|
+
"lint:fix": "biome check . --write && pnpm run typecheck",
|
|
57
|
+
"typecheck": "tsc --noEmit",
|
|
58
|
+
"test": "echo \"Warn: no test specified yet\""
|
|
59
|
+
}
|
|
60
|
+
}
|
package/plugin.ts
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { createAPIClient } from "@shopware/api-client";
|
|
2
|
+
import { isMaintenanceMode } from "@shopware/helpers";
|
|
3
|
+
import { defu } from "defu";
|
|
4
|
+
import { getCookie } from "h3";
|
|
5
|
+
import Cookies from "js-cookie";
|
|
6
|
+
import { ref } from "vue";
|
|
7
|
+
import {
|
|
8
|
+
createShopwareContext,
|
|
9
|
+
defineNuxtPlugin,
|
|
10
|
+
showError,
|
|
11
|
+
useRuntimeConfig,
|
|
12
|
+
useState,
|
|
13
|
+
} from "#imports";
|
|
14
|
+
import type { ApiClient } from "#shopware";
|
|
15
|
+
|
|
16
|
+
declare module "#app" {
|
|
17
|
+
interface NuxtApp {
|
|
18
|
+
$shopwareApiClient: ApiClient;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
declare module "vue" {
|
|
23
|
+
interface ComponentCustomProperties {
|
|
24
|
+
$shopwareApiClient: ApiClient;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export default defineNuxtPlugin((NuxtApp) => {
|
|
29
|
+
const runtimeConfig = useRuntimeConfig();
|
|
30
|
+
const shopwareEndpointCSR =
|
|
31
|
+
runtimeConfig.public?.shopware?.endpoint ??
|
|
32
|
+
runtimeConfig.public?.shopware?.shopwareEndpoint;
|
|
33
|
+
|
|
34
|
+
const shopwareEndpointSSR =
|
|
35
|
+
runtimeConfig.shopware?.endpoint ?? shopwareEndpointCSR;
|
|
36
|
+
|
|
37
|
+
const shopwareEndpoint = import.meta.server
|
|
38
|
+
? shopwareEndpointSSR
|
|
39
|
+
: shopwareEndpointCSR;
|
|
40
|
+
|
|
41
|
+
const shopwareAccessToken =
|
|
42
|
+
runtimeConfig.public?.shopware?.accessToken ??
|
|
43
|
+
runtimeConfig.public?.shopware?.shopwareAccessToken;
|
|
44
|
+
|
|
45
|
+
if (!shopwareEndpoint || !shopwareAccessToken) {
|
|
46
|
+
throw new Error(
|
|
47
|
+
"Make sure that endpoint and accessToken are settled in the configuration",
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const shouldUseSessionContextInServerRender =
|
|
52
|
+
!NuxtApp.ssrContext ||
|
|
53
|
+
!!runtimeConfig.public?.shopware?.useUserContextInSSR ||
|
|
54
|
+
!!runtimeConfig?.shopware?.useUserContextInSSR;
|
|
55
|
+
|
|
56
|
+
const contextTokenFromCookie = NuxtApp.ssrContext
|
|
57
|
+
? getCookie(NuxtApp.ssrContext?.event, "sw-context-token")
|
|
58
|
+
: Cookies.get("sw-context-token");
|
|
59
|
+
|
|
60
|
+
const apiClient = createAPIClient({
|
|
61
|
+
baseURL: shopwareEndpoint,
|
|
62
|
+
accessToken: shopwareAccessToken,
|
|
63
|
+
contextToken: shouldUseSessionContextInServerRender
|
|
64
|
+
? contextTokenFromCookie
|
|
65
|
+
: "",
|
|
66
|
+
defaultHeaders: defu(
|
|
67
|
+
runtimeConfig.apiClientConfig?.headers,
|
|
68
|
+
runtimeConfig.public?.apiClientConfig?.headers,
|
|
69
|
+
),
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
apiClient.hook("onContextChanged", (newContextToken) => {
|
|
73
|
+
Cookies.set("sw-context-token", newContextToken, {
|
|
74
|
+
expires: 365, // days
|
|
75
|
+
path: "/",
|
|
76
|
+
sameSite: "lax",
|
|
77
|
+
secure: shopwareEndpoint.startsWith("https://"),
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
apiClient.hook("onResponseError", (response) => {
|
|
82
|
+
// @ts-expect-error TODO: check maintenance mode and fix typongs here
|
|
83
|
+
const error = isMaintenanceMode(response._data?.errors ?? []);
|
|
84
|
+
if (error) {
|
|
85
|
+
throw showError({
|
|
86
|
+
statusCode: 503,
|
|
87
|
+
statusMessage: "MAINTENANCE_MODE",
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
NuxtApp.vueApp.provide("apiClient", apiClient);
|
|
93
|
+
// Shopware context
|
|
94
|
+
const shopwareContext = createShopwareContext(NuxtApp.vueApp, {
|
|
95
|
+
enableDevtools: true,
|
|
96
|
+
devStorefrontUrl: runtimeConfig.public.shopware?.devStorefrontUrl || null,
|
|
97
|
+
});
|
|
98
|
+
NuxtApp.vueApp.provide("shopware", shopwareContext);
|
|
99
|
+
|
|
100
|
+
// Session Context
|
|
101
|
+
const sessionContextData = ref();
|
|
102
|
+
NuxtApp.vueApp.provide("swSessionContext", sessionContextData);
|
|
103
|
+
// in case someone tries to use it in nuxt specific code like middleware
|
|
104
|
+
useState("swSessionContext", () => sessionContextData);
|
|
105
|
+
|
|
106
|
+
return {
|
|
107
|
+
provide: {
|
|
108
|
+
shopwareApiClient: apiClient as ApiClient,
|
|
109
|
+
},
|
|
110
|
+
};
|
|
111
|
+
});
|
package/shopware.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
declare module "#shopware" {
|
|
2
|
+
import type { createAPIClient } from "@shopware/api-client";
|
|
3
|
+
|
|
4
|
+
// for default types
|
|
5
|
+
export type operations =
|
|
6
|
+
import("@shopware/api-client/store-api-types").operations;
|
|
7
|
+
// or for locally generated types
|
|
8
|
+
// export type operations = import("./api-types/storeApiTypes").operations;
|
|
9
|
+
|
|
10
|
+
// for default types
|
|
11
|
+
export type Schemas =
|
|
12
|
+
import("@shopware/api-client/store-api-types").components["schemas"];
|
|
13
|
+
// or for locally generated types
|
|
14
|
+
// export type Schemas =
|
|
15
|
+
// import("./api-types/storeApiTypes").components["schemas"];
|
|
16
|
+
|
|
17
|
+
// we're exporting our own Api Client definition as it depends on our own instance
|
|
18
|
+
export type ApiClient = ReturnType<typeof createAPIClient<operations>>;
|
|
19
|
+
}
|