@plentymarkets/shop-core 1.0.0

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.
Files changed (35) hide show
  1. package/LICENSE.md +9 -0
  2. package/README.md +65 -0
  3. package/dist/module.cjs +5 -0
  4. package/dist/module.d.mts +6 -0
  5. package/dist/module.d.ts +6 -0
  6. package/dist/module.json +9 -0
  7. package/dist/module.mjs +35 -0
  8. package/dist/runtime/composables/__tests__/useCookieBar.spec.d.ts +1 -0
  9. package/dist/runtime/composables/__tests__/useCookieBar.spec.js +143 -0
  10. package/dist/runtime/composables/__tests__/useCookieConsent.spec.d.ts +1 -0
  11. package/dist/runtime/composables/__tests__/useCookieConsent.spec.js +54 -0
  12. package/dist/runtime/composables/__tests__/usePlentyEvent.spec.d.ts +1 -0
  13. package/dist/runtime/composables/__tests__/usePlentyEvent.spec.js +44 -0
  14. package/dist/runtime/composables/__tests__/useRegisterCookie.spec.d.ts +1 -0
  15. package/dist/runtime/composables/__tests__/useRegisterCookie.spec.js +124 -0
  16. package/dist/runtime/composables/useCookieBar.d.ts +21 -0
  17. package/dist/runtime/composables/useCookieBar.js +119 -0
  18. package/dist/runtime/composables/useCookieConsent.d.ts +9 -0
  19. package/dist/runtime/composables/useCookieConsent.js +23 -0
  20. package/dist/runtime/composables/usePlentyEvent.d.ts +2 -0
  21. package/dist/runtime/composables/usePlentyEvent.js +5 -0
  22. package/dist/runtime/composables/useRegisterCookie.d.ts +8 -0
  23. package/dist/runtime/composables/useRegisterCookie.js +26 -0
  24. package/dist/runtime/server/tsconfig.json +3 -0
  25. package/dist/runtime/types/cookies.d.ts +23 -0
  26. package/dist/runtime/types/cookies.js +0 -0
  27. package/dist/runtime/types/events.d.ts +10 -0
  28. package/dist/runtime/types/events.js +0 -0
  29. package/dist/runtime/types/index.d.ts +2 -0
  30. package/dist/runtime/types/index.js +2 -0
  31. package/dist/runtime/utils/cookieBarHelper.d.ts +4 -0
  32. package/dist/runtime/utils/cookieBarHelper.js +22 -0
  33. package/dist/types.d.mts +7 -0
  34. package/dist/types.d.ts +7 -0
  35. package/package.json +66 -0
package/LICENSE.md ADDED
@@ -0,0 +1,9 @@
1
+ MIT License
2
+
3
+ Copyright 2025 plentysystems AG
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,65 @@
1
+ # Shop Core
2
+
3
+ [![npm version][npm-version-src]][npm-version-href]
4
+ [![npm downloads][npm-downloads-src]][npm-downloads-href]
5
+ [![License][license-src]][license-href]
6
+ [![Nuxt][nuxt-src]][nuxt-href]
7
+
8
+ My new Nuxt module for doing amazing things.
9
+
10
+ - [✨  Release Notes](/CHANGELOG.md)
11
+ <!-- - [🏀 Online playground](https://stackblitz.com/github/your-org/shop-core?file=playground%2Fapp.vue) -->
12
+ <!-- - [📖 &nbsp;Documentation](https://example.com) -->
13
+
14
+ ## Features
15
+
16
+ - EventBus to emit and listen to PlentyONE Shop events
17
+ - Register cookies
18
+ - React to cookie consent changes
19
+
20
+ ## Quick Setup
21
+
22
+ Install the module to your Nuxt application with one command:
23
+
24
+ ```bash
25
+ npx nuxi module add shop-core
26
+ ```
27
+
28
+ That's it! You can now use My Module in your Nuxt app ✨
29
+
30
+ ## Contribution
31
+
32
+ ```bash
33
+ # Install dependencies
34
+ bun install
35
+
36
+ # Generate type stubs
37
+ bun run dev:prepare
38
+
39
+ # Develop with the playground
40
+ bun run dev
41
+
42
+ # Build the playground
43
+ bun run dev:build
44
+
45
+ # Run ESLint
46
+ bun run lint
47
+
48
+ # Run Vitest
49
+ bun run test
50
+ bun run test:watch
51
+
52
+ # Release new version
53
+ bun run release
54
+ ```
55
+
56
+ <!-- Badges -->
57
+
58
+ [npm-version-src]: https://img.shields.io/npm/v/shop-core/latest.svg?style=flat&colorA=020420&colorB=00DC82
59
+ [npm-version-href]: https://npmjs.com/package/shop-core
60
+ [npm-downloads-src]: https://img.shields.io/npm/dm/shop-core.svg?style=flat&colorA=020420&colorB=00DC82
61
+ [npm-downloads-href]: https://npm.chart.dev/shop-core
62
+ [license-src]: https://img.shields.io/npm/l/shop-core.svg?style=flat&colorA=020420&colorB=00DC82
63
+ [license-href]: https://npmjs.com/package/shop-core
64
+ [nuxt-src]: https://img.shields.io/badge/Nuxt-020420?logo=nuxt.js
65
+ [nuxt-href]: https://nuxt.com
@@ -0,0 +1,5 @@
1
+ module.exports = function(...args) {
2
+ return import('./module.mjs').then(m => m.default.call(this, ...args))
3
+ }
4
+ const _meta = module.exports.meta = require('./module.json')
5
+ module.exports.getMeta = () => Promise.resolve(_meta)
@@ -0,0 +1,6 @@
1
+ import * as _nuxt_schema from '@nuxt/schema';
2
+ export { Cookie, CookieGroup, CookieGroupFromNuxtConfig } from '../dist/runtime/types/index.js';
3
+
4
+ declare const _default: _nuxt_schema.NuxtModule<_nuxt_schema.ModuleOptions, _nuxt_schema.ModuleOptions, false>;
5
+
6
+ export { _default as default };
@@ -0,0 +1,6 @@
1
+ import * as _nuxt_schema from '@nuxt/schema';
2
+ export { Cookie, CookieGroup, CookieGroupFromNuxtConfig } from '../dist/runtime/types/index.js';
3
+
4
+ declare const _default: _nuxt_schema.NuxtModule<_nuxt_schema.ModuleOptions, _nuxt_schema.ModuleOptions, false>;
5
+
6
+ export { _default as default };
@@ -0,0 +1,9 @@
1
+ {
2
+ "name": "shop-core",
3
+ "configKey": "shopCore",
4
+ "version": "1.0.0",
5
+ "builder": {
6
+ "@nuxt/module-builder": "0.8.4",
7
+ "unbuild": "2.0.0"
8
+ }
9
+ }
@@ -0,0 +1,35 @@
1
+ import { defineNuxtModule, createResolver, addImports } from '@nuxt/kit';
2
+
3
+ const module = defineNuxtModule({
4
+ meta: {
5
+ name: "shop-core",
6
+ configKey: "shopCore"
7
+ },
8
+ // Default configuration options of the Nuxt module
9
+ defaults: {},
10
+ setup(_options, _nuxt) {
11
+ const resolver = createResolver(import.meta.url);
12
+ addImports({
13
+ name: "usePlentyEvent",
14
+ as: "usePlentyEvent",
15
+ from: resolver.resolve("./runtime/composables/usePlentyEvent")
16
+ });
17
+ addImports({
18
+ name: "useCookieBar",
19
+ as: "useCookieBar",
20
+ from: resolver.resolve("./runtime/composables/useCookieBar")
21
+ });
22
+ addImports({
23
+ name: "useCookieConsent",
24
+ as: "useCookieConsent",
25
+ from: resolver.resolve("./runtime/composables/useCookieConsent")
26
+ });
27
+ addImports({
28
+ name: "useRegisterCookie",
29
+ as: "useRegisterCookie",
30
+ from: resolver.resolve("./runtime/composables/useRegisterCookie")
31
+ });
32
+ }
33
+ });
34
+
35
+ export { module as default };
@@ -0,0 +1,143 @@
1
+ import { useRuntimeConfig } from "nuxt/app";
2
+ import { describe, it, expect } from "vitest";
3
+ import { mockNuxtImport } from "@nuxt/test-utils/runtime";
4
+ import { useCookieBar } from "../useCookieBar.js";
5
+ const { useCookie } = vi.hoisted(() => ({
6
+ useCookie: vi.fn().mockReturnValue({})
7
+ }));
8
+ const { useRouter } = vi.hoisted(() => ({
9
+ useRouter: vi.fn().mockReturnValue({
10
+ go: vi.fn()
11
+ })
12
+ }));
13
+ mockNuxtImport("useRouter", () => useRouter);
14
+ mockNuxtImport("useCookie", () => useCookie);
15
+ describe("useCookieBar", () => {
16
+ beforeEach(() => {
17
+ const cookieInit = {
18
+ barTitle: "Test",
19
+ barDescription: "Test",
20
+ groups: [
21
+ {
22
+ id: 0,
23
+ name: "CookieBar.essentials.label",
24
+ showMore: false,
25
+ description: "CookieBar.essentials.description",
26
+ cookies: [
27
+ {
28
+ name: "CookieBar.essentials.cookies.payPal.name",
29
+ Provider: "CookieBar.essentials.cookies.payPal.provider",
30
+ Status: "CookieBar.essentials.cookies.payPal.status",
31
+ PrivacyPolicy: "/PrivacyPolicy",
32
+ Lifespan: "Session"
33
+ }
34
+ ]
35
+ },
36
+ {
37
+ name: "marketing",
38
+ cookies: [
39
+ {
40
+ name: "testCookie",
41
+ accepted: false,
42
+ Lifespan: "100",
43
+ script: []
44
+ },
45
+ {
46
+ name: "externalScript",
47
+ accepted: false,
48
+ Lifespan: "100",
49
+ script: ["https://cdn02.plentymarkets.com/mevofvd5omld/frontend/test-cookie-external-script.js"]
50
+ }
51
+ ]
52
+ }
53
+ ]
54
+ };
55
+ const { public: config } = useRuntimeConfig();
56
+ config.cookieGroups = cookieInit;
57
+ });
58
+ it("initializes cookies correctly", () => {
59
+ const { initializeCookies, data, setConsent } = useCookieBar();
60
+ initializeCookies();
61
+ setConsent();
62
+ expect(data.value.groups[0].cookies[0].accepted).toBe(true);
63
+ expect(data.value.groups[1].cookies[0].accepted).toBe(false);
64
+ });
65
+ it("changes visibility state correctly", () => {
66
+ const { changeVisibilityState, visible } = useCookieBar();
67
+ changeVisibilityState();
68
+ expect(visible.value).toBe(true);
69
+ });
70
+ it("accept all cookies state correctly", () => {
71
+ const { setAllCookiesState, data } = useCookieBar();
72
+ setAllCookiesState(true);
73
+ const allCookiesAccepted = data.value.groups[1].cookies.every((cookie) => {
74
+ return cookie.accepted;
75
+ });
76
+ expect(allCookiesAccepted).toBe(true);
77
+ });
78
+ it("decline all cookies state correctly", () => {
79
+ const { setAllCookiesState, data } = useCookieBar();
80
+ setAllCookiesState(false);
81
+ const allCookiesDeclined = data.value.groups[1].cookies.every((cookie) => {
82
+ return !cookie.accepted;
83
+ });
84
+ expect(allCookiesDeclined).toBe(true);
85
+ });
86
+ it("removes cookies correctly", () => {
87
+ document.cookie = "testCookie=testValue";
88
+ const cookie = {
89
+ name: "testCookie",
90
+ cookieNames: ["testCookie"]
91
+ };
92
+ const { removeCookies } = useCookieBar();
93
+ removeCookies(cookie);
94
+ expect(document.cookie).not.toContain("testCookie=testValue");
95
+ });
96
+ it("should set state read from browser-cookie", () => {
97
+ const { initializeCookies, data } = useCookieBar();
98
+ const browserCookies = {
99
+ marketing: {
100
+ testCookie: true
101
+ }
102
+ };
103
+ useCookie.mockReturnValue({ value: browserCookies });
104
+ initializeCookies();
105
+ expect(data.value.groups[1].cookies[0].accepted).toBe(true);
106
+ });
107
+ it("should reload the page if a consent cookie is revoked", () => {
108
+ const { setConsent, initializeCookies, data } = useCookieBar();
109
+ const RouterGoSpy = vi.fn();
110
+ useRouter.mockReturnValue({
111
+ go: RouterGoSpy
112
+ });
113
+ initializeCookies();
114
+ data.value.groups[1].cookies[0].accepted = false;
115
+ setConsent();
116
+ expect(RouterGoSpy).toHaveBeenCalledTimes(1);
117
+ });
118
+ it("should not reload the page if a consent cookie is accepted", () => {
119
+ const { setConsent, initializeCookies } = useCookieBar();
120
+ const RouterGoSpy = vi.fn();
121
+ useRouter.mockReturnValue({
122
+ go: RouterGoSpy
123
+ });
124
+ initializeCookies();
125
+ setConsent();
126
+ expect(RouterGoSpy).toHaveBeenCalledTimes(0);
127
+ });
128
+ it("should load external scripts", () => {
129
+ const { initializeCookies, data, setConsent } = useCookieBar();
130
+ const scriptSpy = vi.fn();
131
+ const script = document.createElement("script");
132
+ scriptSpy.mockReturnValue(script);
133
+ document.createElement = scriptSpy;
134
+ initializeCookies();
135
+ data.value.groups[1].cookies[1].accepted = true;
136
+ setConsent();
137
+ expect(scriptSpy).toHaveBeenCalledTimes(1);
138
+ expect(scriptSpy).toHaveBeenCalledWith("script");
139
+ expect(scriptSpy().src).toBe(
140
+ "https://cdn02.plentymarkets.com/mevofvd5omld/frontend/test-cookie-external-script.js"
141
+ );
142
+ });
143
+ });
@@ -0,0 +1,54 @@
1
+ import { describe, it, expect } from "vitest";
2
+ import { useRuntimeConfig } from "nuxt/app";
3
+ import { useCookieConsent } from "../useCookieConsent.js";
4
+ import { useCookieBar } from "../useCookieBar.js";
5
+ describe("useCookieConsent", () => {
6
+ beforeEach(() => {
7
+ const cookieInit = {
8
+ barTitle: "Test",
9
+ barDescription: "Test",
10
+ groups: [
11
+ {
12
+ id: 0,
13
+ name: "CookieBar.essentials.label",
14
+ showMore: false,
15
+ description: "CookieBar.essentials.description",
16
+ cookies: [
17
+ {
18
+ name: "CookieBar.essentials.cookies.payPal.name",
19
+ Provider: "CookieBar.essentials.cookies.payPal.provider",
20
+ Status: "CookieBar.essentials.cookies.payPal.status",
21
+ PrivacyPolicy: "/PrivacyPolicy",
22
+ Lifespan: "Session"
23
+ }
24
+ ]
25
+ },
26
+ {
27
+ name: "marketing",
28
+ cookies: [
29
+ {
30
+ name: "testCookie",
31
+ accepted: false,
32
+ script: []
33
+ }
34
+ ]
35
+ }
36
+ ]
37
+ };
38
+ const { public: config } = useRuntimeConfig();
39
+ config.cookieGroups = cookieInit;
40
+ const { initializeCookies } = useCookieBar();
41
+ initializeCookies();
42
+ });
43
+ it("initializes consent state correctly", () => {
44
+ const { consent } = useCookieConsent("CookieBar.essentials.cookies.payPal.name");
45
+ expect(consent.value).toBe(true);
46
+ });
47
+ it("accepts the specific cookie correctly", () => {
48
+ const { data } = useCookieBar();
49
+ const { accept, consent } = useCookieConsent("testCookie");
50
+ accept();
51
+ expect(data.value.groups[1].cookies[0].accepted).toBe(true);
52
+ expect(consent.value).toBe(true);
53
+ });
54
+ });
@@ -0,0 +1,44 @@
1
+ import { describe, expect, it, beforeEach, vi } from "vitest";
2
+ import { usePlentyEvent } from "../usePlentyEvent.js";
3
+ describe("usePlentyEvent", () => {
4
+ beforeEach(() => {
5
+ usePlentyEvent().all.clear();
6
+ });
7
+ it("should register event listener", () => {
8
+ const { on, all } = usePlentyEvent();
9
+ on("userLogin", () => {
10
+ });
11
+ expect(all.get("userLogin")).toHaveLength(1);
12
+ });
13
+ it("should register multiple event listeners", () => {
14
+ const { on, all } = usePlentyEvent();
15
+ on("userLogin", () => {
16
+ });
17
+ on("userLogin", () => {
18
+ });
19
+ expect(all.get("userLogin")).toHaveLength(2);
20
+ });
21
+ it("should emit and listen to event", () => {
22
+ const { on, emit } = usePlentyEvent();
23
+ const mock = vi.fn();
24
+ on("userLogin", mock);
25
+ emit("userLogin", { id: 1, firstName: "John Doe" });
26
+ expect(mock).toHaveBeenCalled();
27
+ });
28
+ it("should clear all event listeners", () => {
29
+ const { on, all } = usePlentyEvent();
30
+ on("userLogin", () => {
31
+ });
32
+ on("userLogout", () => {
33
+ });
34
+ all.clear();
35
+ expect(all.size).toBe(0);
36
+ });
37
+ it("should turn off event listener", () => {
38
+ const { on, off, all } = usePlentyEvent();
39
+ const mock = vi.fn();
40
+ on("userLogin", mock);
41
+ off("userLogin", mock);
42
+ expect(all.get("userLogin")).toHaveLength(0);
43
+ });
44
+ });
@@ -0,0 +1,124 @@
1
+ import { useRuntimeConfig } from "nuxt/app";
2
+ import { useRegisterCookie } from "../useRegisterCookie.js";
3
+ vi.mock("nuxt/app", () => ({
4
+ useRuntimeConfig: vi.fn()
5
+ }));
6
+ describe("useRegisterCookie", () => {
7
+ it("adds a cookie to the specified group", () => {
8
+ const { add } = useRegisterCookie();
9
+ const runtimeConfig = {
10
+ public: {
11
+ cookieGroups: {
12
+ groups: [
13
+ {
14
+ name: "marketing",
15
+ cookies: []
16
+ }
17
+ ]
18
+ }
19
+ }
20
+ };
21
+ useRuntimeConfig.mockReturnValue(runtimeConfig);
22
+ const cookie = {
23
+ name: "testCookie",
24
+ Provider: "testProvider",
25
+ Status: "testStatus",
26
+ PrivacyPolicy: "https://example.com/privacy",
27
+ Lifespan: "Session",
28
+ cookieNames: ["testCookieName"],
29
+ accepted: true
30
+ };
31
+ add(cookie, "marketing");
32
+ expect(runtimeConfig.public.cookieGroups.groups[0].cookies).toContainEqual(cookie);
33
+ });
34
+ it("does not add a cookie if it already exists in the group", () => {
35
+ const { add } = useRegisterCookie();
36
+ const runtimeConfig = {
37
+ public: {
38
+ cookieGroups: {
39
+ groups: [
40
+ {
41
+ name: "marketing",
42
+ cookies: [
43
+ {
44
+ name: "testCookie",
45
+ Provider: "testProvider",
46
+ Status: "testStatus",
47
+ PrivacyPolicy: "https://example.com/privacy",
48
+ Lifespan: "Session",
49
+ cookieNames: ["testCookieName"],
50
+ accepted: true
51
+ }
52
+ ]
53
+ }
54
+ ]
55
+ }
56
+ }
57
+ };
58
+ useRuntimeConfig.mockReturnValue(runtimeConfig);
59
+ const cookie = {
60
+ name: "testCookie",
61
+ Provider: "testProvider",
62
+ Status: "testStatus",
63
+ PrivacyPolicy: "https://example.com/privacy",
64
+ Lifespan: "Session",
65
+ cookieNames: ["testCookieName"],
66
+ accepted: true
67
+ };
68
+ add(cookie, "marketing");
69
+ expect(runtimeConfig.public.cookieGroups.groups[0].cookies.length).toBe(1);
70
+ });
71
+ it("does not add a cookie if the group does not exist", () => {
72
+ const { add } = useRegisterCookie();
73
+ const runtimeConfig = {
74
+ public: {
75
+ cookieGroups: {
76
+ groups: [
77
+ {
78
+ name: "analytics",
79
+ cookies: []
80
+ }
81
+ ]
82
+ }
83
+ }
84
+ };
85
+ useRuntimeConfig.mockReturnValue(runtimeConfig);
86
+ const cookie = {
87
+ name: "testCookie",
88
+ Provider: "testProvider",
89
+ Status: "testStatus",
90
+ PrivacyPolicy: "https://example.com/privacy",
91
+ Lifespan: "Session",
92
+ cookieNames: ["testCookieName"],
93
+ accepted: true
94
+ };
95
+ add(cookie, "marketing");
96
+ expect(runtimeConfig.public.cookieGroups.groups[0].cookies.length).toBe(0);
97
+ });
98
+ it("add empty cookie array if group does not have cookies", () => {
99
+ const { add } = useRegisterCookie();
100
+ const runtimeConfig = {
101
+ public: {
102
+ cookieGroups: {
103
+ groups: [
104
+ {
105
+ name: "marketing"
106
+ }
107
+ ]
108
+ }
109
+ }
110
+ };
111
+ useRuntimeConfig.mockReturnValue(runtimeConfig);
112
+ const cookie = {
113
+ name: "testCookie",
114
+ Provider: "testProvider",
115
+ Status: "testStatus",
116
+ PrivacyPolicy: "https://example.com/privacy",
117
+ Lifespan: "Session",
118
+ cookieNames: ["testCookieName"],
119
+ accepted: true
120
+ };
121
+ add(cookie, "marketing");
122
+ expect(runtimeConfig.public.cookieGroups.groups[0].cookies).toContainEqual(cookie);
123
+ });
124
+ });
@@ -0,0 +1,21 @@
1
+ import type { Cookie, CookieGroupFromNuxtConfig } from '../types/index.js';
2
+ /**
3
+ * @description Composable for managing cookie consent bar.
4
+ * @example
5
+ * ``` ts
6
+ * const {
7
+ * data, visible, loading, changeVisibilityState, setConsent, initializeCookies, setAllCookiesState
8
+ * } = useCookieBar();
9
+ * ```
10
+ */
11
+ export declare const useCookieBar: () => {
12
+ changeVisibilityState: ChangeVisibilityState;
13
+ setConsent: () => void;
14
+ initializeCookies: () => void;
15
+ setAllCookiesState: (accepted: boolean) => void;
16
+ removeCookies: (cookie: Cookie) => void;
17
+ data: import("vue").Ref<CookieGroupFromNuxtConfig, CookieGroupFromNuxtConfig>;
18
+ visible: import("vue").Ref<boolean, boolean>;
19
+ loading: import("vue").Ref<boolean, boolean>;
20
+ };
21
+ export declare const fetchScripts: (scripts: string[]) => void;
@@ -0,0 +1,119 @@
1
+ import { useRuntimeConfig, useState, useCookie } from "nuxt/app";
2
+ import { toRefs } from "vue";
3
+ import { cookieBarHelper } from "../utils/cookieBarHelper.js";
4
+ import { useCookieConsent } from "./useCookieConsent.js";
5
+ const checkIfScriptIsExternal = (scriptName) => {
6
+ return scriptName.startsWith("http");
7
+ };
8
+ const ESSENTIAL_COOKIE_GROUP = "CookieBar.essentials.label";
9
+ export const useCookieBar = () => {
10
+ const state = useState("useCookieBar", () => ({
11
+ data: {},
12
+ visible: false,
13
+ loading: false
14
+ }));
15
+ const runtimeConfig = useRuntimeConfig();
16
+ const initialCookies = runtimeConfig.public.cookieGroups;
17
+ const changeVisibilityState = () => {
18
+ state.value.visible = !state.value.visible;
19
+ };
20
+ const initializeCookies = () => {
21
+ const cookies = JSON.parse(JSON.stringify(initialCookies));
22
+ const browserCookies = useCookie("consent-cookie");
23
+ cookies.groups.forEach((group) => {
24
+ group.cookies.forEach((cookie) => {
25
+ const isAccepted = group.name === ESSENTIAL_COOKIE_GROUP || !!browserCookies.value?.[group.name]?.[cookie.name] || false;
26
+ if (browserCookies.value?.[group.name]?.[cookie.name] !== void 0 || group.name === ESSENTIAL_COOKIE_GROUP) {
27
+ cookie.accepted = isAccepted;
28
+ if (isAccepted && cookie.script && cookie.script.length) {
29
+ fetchScripts(cookie.script);
30
+ }
31
+ }
32
+ const { consent } = useCookieConsent(cookie.name);
33
+ consent.value = isAccepted;
34
+ });
35
+ group.accepted = group.cookies.some((cookie) => cookie.accepted);
36
+ });
37
+ state.value.data = cookies;
38
+ if (!browserCookies.value) {
39
+ state.value.visible = true;
40
+ }
41
+ };
42
+ const setConsent = () => {
43
+ const { getMinimumLifeSpan } = cookieBarHelper();
44
+ const router = useRouter();
45
+ const browserCookies = useCookie("consent-cookie");
46
+ let cookieRevoke = false;
47
+ const jsonCookie = state.value.data.groups.reduce((accumulator, group) => {
48
+ accumulator[group.name] = group.cookies.reduce((childAccumulator, cookie) => {
49
+ const currentStatus = !!browserCookies.value?.[group.name]?.[cookie.name] || false;
50
+ const { consent } = useCookieConsent(cookie.name);
51
+ childAccumulator[cookie.name] = cookie.accepted || false;
52
+ consent.value = cookie.accepted || false;
53
+ if (currentStatus && !consent.value) {
54
+ cookieRevoke = true;
55
+ removeCookies(cookie);
56
+ } else if (!currentStatus && consent.value && cookie.script && cookie.script.length) {
57
+ fetchScripts(cookie.script);
58
+ }
59
+ return childAccumulator;
60
+ }, {});
61
+ return accumulator;
62
+ }, {});
63
+ const consentCookie = useCookie("consent-cookie", {
64
+ path: "/",
65
+ maxAge: getMinimumLifeSpan(state.value.data.groups)
66
+ });
67
+ consentCookie.value = JSON.stringify(jsonCookie);
68
+ changeVisibilityState();
69
+ if (cookieRevoke) {
70
+ router.go(0);
71
+ }
72
+ };
73
+ const setAllCookiesState = (accepted) => {
74
+ state.value.data.groups.forEach((group) => {
75
+ const isAccepted = group.name === ESSENTIAL_COOKIE_GROUP || accepted;
76
+ group.accepted = isAccepted;
77
+ group.cookies.forEach((cookie) => cookie.accepted = isAccepted);
78
+ });
79
+ setConsent();
80
+ };
81
+ const removeCookies = (cookie) => {
82
+ if (cookie.cookieNames) {
83
+ cookie.cookieNames.forEach((cookieName) => {
84
+ const browserCookie = document.cookie.split(";");
85
+ browserCookie.forEach((cookie2) => {
86
+ const cookiePair = cookie2.split("=");
87
+ const name = cookiePair[0].trim();
88
+ if (new RegExp(cookieName).test(name)) {
89
+ document.cookie = `${name}=; expires=Thu, 01 Jan 1970 00:00:00 UTC;`;
90
+ }
91
+ });
92
+ });
93
+ }
94
+ };
95
+ return {
96
+ ...toRefs(state.value),
97
+ changeVisibilityState,
98
+ setConsent,
99
+ initializeCookies,
100
+ setAllCookiesState,
101
+ removeCookies
102
+ };
103
+ };
104
+ export const fetchScripts = (scripts) => {
105
+ scripts.forEach((script) => {
106
+ try {
107
+ if (checkIfScriptIsExternal(script)) {
108
+ const scriptElement = document.createElement("script");
109
+ scriptElement.setAttribute("src", script);
110
+ scriptElement.setAttribute("type", "text/javascript");
111
+ document.head.append(scriptElement);
112
+ } else if (cookieScripts[script]) {
113
+ cookieScripts[script]();
114
+ }
115
+ } catch (error) {
116
+ console.error(error);
117
+ }
118
+ });
119
+ };
@@ -0,0 +1,9 @@
1
+ /**
2
+ * @description Helper function to read and react to a specific cookie registered in the cookie-bar
3
+ * @param name name of the cookie registered in the cookie bar apps/web/configuration/cookie.config.ts
4
+ * @example const { consent } = useCookieConsent('CookieBar.essentials.cookies.payPal.name');
5
+ */
6
+ export declare const useCookieConsent: (name: string) => {
7
+ accept: () => void;
8
+ consent: import("vue").Ref<boolean, boolean>;
9
+ };
@@ -0,0 +1,23 @@
1
+ import { useState } from "nuxt/app";
2
+ import { toRefs } from "vue";
3
+ import { useCookieBar } from "./useCookieBar.js";
4
+ export const useCookieConsent = (name) => {
5
+ const state = useState("useCookieConsent_" + name, () => ({
6
+ consent: false
7
+ }));
8
+ const accept = () => {
9
+ const { data, setConsent } = useCookieBar();
10
+ data.value.groups.forEach((group) => {
11
+ group.cookies.forEach((cookie) => {
12
+ if (cookie.name === name) {
13
+ cookie.accepted = true;
14
+ setConsent();
15
+ }
16
+ });
17
+ });
18
+ };
19
+ return {
20
+ ...toRefs(state.value),
21
+ accept
22
+ };
23
+ };
@@ -0,0 +1,2 @@
1
+ import type { Events } from '../types/events.js';
2
+ export declare function usePlentyEvent(): import("mitt").Emitter<Events>;
@@ -0,0 +1,5 @@
1
+ import mitt from "mitt";
2
+ const plentyEventBus = mitt();
3
+ export function usePlentyEvent() {
4
+ return plentyEventBus;
5
+ }
@@ -0,0 +1,8 @@
1
+ import type { Cookie } from '../types/cookies.js';
2
+ /**
3
+ * @description Composable to register a cookie in the cookie bar
4
+ * @example const { add } = useRegisterCookie();
5
+ */
6
+ export declare const useRegisterCookie: () => {
7
+ add: (cookie: Cookie, group: string) => void;
8
+ };
@@ -0,0 +1,26 @@
1
+ import { useRuntimeConfig } from "nuxt/app";
2
+ export const useRegisterCookie = () => {
3
+ const add = (cookie, group) => {
4
+ const runtimeConfig = useRuntimeConfig();
5
+ const initialCookies = runtimeConfig.public.cookieGroups;
6
+ if (initialCookies && initialCookies.groups) {
7
+ initialCookies.groups.forEach((cookieGroup) => {
8
+ if (cookieGroup.name === group) {
9
+ if (!cookieGroup.cookies) {
10
+ cookieGroup.cookies = [];
11
+ }
12
+ if (cookieGroup.cookies.some((existingCookie) => {
13
+ return existingCookie.name === cookie.name;
14
+ })) {
15
+ return;
16
+ }
17
+ cookieGroup.cookies.push(cookie);
18
+ return;
19
+ }
20
+ });
21
+ }
22
+ };
23
+ return {
24
+ add
25
+ };
26
+ };
@@ -0,0 +1,3 @@
1
+ {
2
+ "extends": "../../../.nuxt/tsconfig.server.json",
3
+ }
@@ -0,0 +1,23 @@
1
+ export type Cookie = {
2
+ name: string;
3
+ accepted?: boolean;
4
+ Lifespan: string;
5
+ script?: string[];
6
+ Provider: string;
7
+ Status: string;
8
+ PrivacyPolicy: string;
9
+ cookieNames?: string[];
10
+ };
11
+ export type CookieGroup = {
12
+ id?: number;
13
+ name: string;
14
+ accepted: boolean;
15
+ showMore?: boolean;
16
+ description: string;
17
+ cookies: Cookie[];
18
+ };
19
+ export type CookieGroupFromNuxtConfig = {
20
+ groups: CookieGroup[];
21
+ barTitle: string;
22
+ barDescription: string;
23
+ };
File without changes
@@ -0,0 +1,10 @@
1
+ import type { User, Order } from '@plentymarkets/shop-api';
2
+ export type Events = {
3
+ orderCreated: Partial<Order>;
4
+ cartUpdated: {
5
+ cartId: string;
6
+ itemCount: number;
7
+ };
8
+ userLogin: User;
9
+ userLogout: User;
10
+ };
File without changes
@@ -0,0 +1,2 @@
1
+ export * from './cookies.js';
2
+ export * from './events.js';
@@ -0,0 +1,2 @@
1
+ export * from "./cookies.js";
2
+ export * from "./events.js";
@@ -0,0 +1,4 @@
1
+ import type { CookieGroup } from '~/configuration/cookie.config';
2
+ export declare const cookieBarHelper: () => {
3
+ getMinimumLifeSpan: (cookieJsonFromConfig: CookieGroup[]) => number;
4
+ };
@@ -0,0 +1,22 @@
1
+ const convertToDays = (daysInString) => {
2
+ return Number.parseInt(daysInString.split(" ")[0]);
3
+ };
4
+ const getMinimumLifeSpan = (cookieJsonFromConfig) => {
5
+ let minimum = 999999;
6
+ cookieJsonFromConfig.forEach((group) => {
7
+ const accepted = group.cookies.filter((cookie) => {
8
+ return cookie.accepted;
9
+ });
10
+ accepted.forEach((cookie) => {
11
+ if (cookie.Lifespan && minimum > convertToDays(cookie.Lifespan)) {
12
+ minimum = convertToDays(cookie.Lifespan);
13
+ }
14
+ });
15
+ });
16
+ return 60 * 60 * 24 * minimum;
17
+ };
18
+ export const cookieBarHelper = () => {
19
+ return {
20
+ getMinimumLifeSpan
21
+ };
22
+ };
@@ -0,0 +1,7 @@
1
+ import type { NuxtModule } from '@nuxt/schema'
2
+
3
+ import type { default as Module } from './module.js'
4
+
5
+ export type ModuleOptions = typeof Module extends NuxtModule<infer O> ? Partial<O> : Record<string, any>
6
+
7
+ export { type Cookie, type CookieGroup, type CookieGroupFromNuxtConfig } from './module.js'
@@ -0,0 +1,7 @@
1
+ import type { NuxtModule } from '@nuxt/schema'
2
+
3
+ import type { default as Module } from './module'
4
+
5
+ export type ModuleOptions = typeof Module extends NuxtModule<infer O> ? Partial<O> : Record<string, any>
6
+
7
+ export { type Cookie, type CookieGroup, type CookieGroupFromNuxtConfig } from './module'
package/package.json ADDED
@@ -0,0 +1,66 @@
1
+ {
2
+ "name": "@plentymarkets/shop-core",
3
+ "version": "1.0.0",
4
+ "description": "Core module for PlentyONE Shop",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/plentymarkets/shop-core.git"
8
+ },
9
+ "private": false,
10
+ "publishConfig": {
11
+ "access": "public",
12
+ "registry": "https://registry.npmjs.org/"
13
+ },
14
+ "license": "MIT",
15
+ "type": "module",
16
+ "exports": {
17
+ ".": {
18
+ "types": "./dist/types.d.ts",
19
+ "import": "./dist/module.mjs",
20
+ "require": "./dist/module.cjs"
21
+ }
22
+ },
23
+ "main": "./dist/module.cjs",
24
+ "types": "./dist/types.d.ts",
25
+ "files": [
26
+ "dist"
27
+ ],
28
+ "scripts": {
29
+ "prepack": "nuxt-module-build build",
30
+ "dev": "nuxi dev playground",
31
+ "dev:build": "nuxi build playground",
32
+ "dev:prepare": "nuxt-module-build build --stub && nuxt-module-build prepare && nuxi prepare playground",
33
+ "release": "npm run lint && npm run test && npm run prepack && changelogen --release && npm publish && git push --follow-tags",
34
+ "lint": "eslint .",
35
+ "lint:fix": "eslint . --fix",
36
+ "format": "prettier --check \"**/*.{ts,vue,css,scss,md}\"",
37
+ "format:fix": "prettier --write \"**/*.{ts,vue,css,scss,md}\"",
38
+ "test": "vitest run",
39
+ "test:coverage": "vitest run --coverage",
40
+ "test:watch": "vitest watch",
41
+ "test:types": "vue-tsc --noEmit && cd playground && vue-tsc --noEmit"
42
+ },
43
+ "dependencies": {
44
+ "@plentymarkets/shop-api": "^0.88.0",
45
+ "mitt": "^3.0.1"
46
+ },
47
+ "devDependencies": {
48
+ "@nuxt/devtools": "^1.7.0",
49
+ "@nuxt/eslint-config": "^0.7.5",
50
+ "@nuxt/kit": "^3.15.1",
51
+ "@nuxt/module-builder": "^0.8.4",
52
+ "@nuxt/schema": "^3.15.1",
53
+ "@nuxt/test-utils": "^3.15.4",
54
+ "@types/node": "latest",
55
+ "@vitest/coverage-v8": "2.1.8",
56
+ "@vue-storefront/eslint-config": "^4.1.0",
57
+ "@vue/test-utils": "^2.4.6",
58
+ "changelogen": "^0.5.7",
59
+ "eslint": "^9.18.0",
60
+ "happy-dom": "^16.7.1",
61
+ "nuxt": "^3.15.1",
62
+ "typescript": "~5.7.3",
63
+ "vitest": "^2.1.8",
64
+ "vue-tsc": "^2.2.0"
65
+ }
66
+ }