@plentymarkets/shop-core 1.2.1 → 1.3.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.
package/dist/module.d.mts CHANGED
@@ -1,6 +1,9 @@
1
1
  import * as _nuxt_schema from '@nuxt/schema';
2
2
  export { Cookie, CookieGroup, CookieGroupFromNuxtConfig } from '../dist/runtime/types/index.js';
3
3
 
4
- declare const _default: _nuxt_schema.NuxtModule<_nuxt_schema.ModuleOptions, _nuxt_schema.ModuleOptions, false>;
4
+ interface ModuleOptions {
5
+ apiUrl: string;
6
+ }
7
+ declare const _default: _nuxt_schema.NuxtModule<ModuleOptions, ModuleOptions, false>;
5
8
 
6
- export { _default as default };
9
+ export { type ModuleOptions, _default as default };
package/dist/module.d.ts CHANGED
@@ -1,6 +1,9 @@
1
1
  import * as _nuxt_schema from '@nuxt/schema';
2
2
  export { Cookie, CookieGroup, CookieGroupFromNuxtConfig } from '../dist/runtime/types/index.js';
3
3
 
4
- declare const _default: _nuxt_schema.NuxtModule<_nuxt_schema.ModuleOptions, _nuxt_schema.ModuleOptions, false>;
4
+ interface ModuleOptions {
5
+ apiUrl: string;
6
+ }
7
+ declare const _default: _nuxt_schema.NuxtModule<ModuleOptions, ModuleOptions, false>;
5
8
 
6
- export { _default as default };
9
+ export { type ModuleOptions, _default as default };
package/dist/module.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "shop-core",
3
3
  "configKey": "shopCore",
4
- "version": "1.2.1",
4
+ "version": "1.3.0",
5
5
  "builder": {
6
6
  "@nuxt/module-builder": "0.8.4",
7
7
  "unbuild": "2.0.0"
package/dist/module.mjs CHANGED
@@ -7,8 +7,14 @@ const module = defineNuxtModule({
7
7
  },
8
8
  // Default configuration options of the Nuxt module
9
9
  defaults: {},
10
- setup(_options, _nuxt) {
10
+ async setup(_options, _nuxt) {
11
11
  const resolver = createResolver(import.meta.url);
12
+ _nuxt.options.runtimeConfig.public.shopCore = _options;
13
+ addImports({
14
+ name: "useSdk",
15
+ as: "useSdk",
16
+ from: resolver.resolve("./runtime/composables/useSdk")
17
+ });
12
18
  addImports({
13
19
  name: "usePlentyEvent",
14
20
  as: "usePlentyEvent",
@@ -1,4 +1,4 @@
1
- import { describe, it, expect } from "vitest";
1
+ import { describe, it, expect, beforeEach } from "vitest";
2
2
  import { useRuntimeConfig } from "nuxt/app";
3
3
  import { useCookieConsent } from "../useCookieConsent.js";
4
4
  import { useCookieBar } from "../useCookieBar.js";
@@ -0,0 +1,9 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { useCsrfToken } from "../useCsrfToken.js";
3
+ describe("useCsrfToken", () => {
4
+ it("should set and csrf token", () => {
5
+ const { token } = useCsrfToken();
6
+ token.value = "test";
7
+ expect(token.value).toBe("test");
8
+ });
9
+ });
@@ -1,4 +1,5 @@
1
1
  import { useRuntimeConfig } from "nuxt/app";
2
+ import { describe, it, vi } from "vitest";
2
3
  import { useRegisterCookie } from "../useRegisterCookie.js";
3
4
  vi.mock("nuxt/app", () => ({
4
5
  useRuntimeConfig: vi.fn()
@@ -0,0 +1,26 @@
1
+ import { describe } from "node:test";
2
+ import { expect, it, vi } from "vitest";
3
+ import { mockNuxtImport } from "@nuxt/test-utils/runtime";
4
+ import { useSdk } from "../useSdk.js";
5
+ const { useRuntimeConfig } = vi.hoisted(() => {
6
+ return {
7
+ useRuntimeConfig: vi.fn().mockImplementation(() => {
8
+ return {
9
+ public: {
10
+ shopCore: {
11
+ apiUrl: "https://example.com"
12
+ }
13
+ }
14
+ };
15
+ })
16
+ };
17
+ });
18
+ mockNuxtImport("useRuntimeConfig", () => {
19
+ return useRuntimeConfig;
20
+ });
21
+ describe("useSdk", () => {
22
+ it("should return sdk", () => {
23
+ const sdk = useSdk();
24
+ expect(sdk.plentysystems.getCategoryTree).toBeDefined();
25
+ });
26
+ });
@@ -0,0 +1,2 @@
1
+ import type { AxiosRequestConfig } from 'axios';
2
+ export declare const httpClient: (url: string, params: unknown, config: AxiosRequestConfig<unknown> | undefined) => Promise<any>;
@@ -0,0 +1,55 @@
1
+ import axios from "axios";
2
+ import { ApiError } from "@plentymarkets/shop-api";
3
+ import { updateVsfLocale } from "../utils/sdkClientHelper.js";
4
+ import { useCsrfToken } from "./useCsrfToken.js";
5
+ import { tryUseNuxtApp, useCookie, useNuxtApp, useRuntimeConfig } from "#imports";
6
+ const createHttpClient = () => {
7
+ const client = axios.create({ withCredentials: true });
8
+ if (tryUseNuxtApp()) {
9
+ const { token } = useCsrfToken();
10
+ const { $router, $i18n } = useNuxtApp();
11
+ const locale = $i18n.locale.value;
12
+ const runtimeConfig = useRuntimeConfig();
13
+ const referrerId = $router.currentRoute.value.query?.ReferrerID?.toString() ?? "";
14
+ const noCache = runtimeConfig.public.noCache || $router.currentRoute.value.query?.noCache?.toString() || "";
15
+ const configId = runtimeConfig.public.configId;
16
+ const pwaHashCookie = useCookie("pwa");
17
+ client.interceptors.request.use((request) => {
18
+ if (token.value) request.headers["x-csrf-token"] = token.value;
19
+ if (referrerId) request.headers["referrerID"] = referrerId;
20
+ if (noCache) request.headers["noCache"] = noCache;
21
+ if (configId) request.headers["x-config-id"] = configId;
22
+ if (pwaHashCookie.value) request.headers["x-pwa-edit-hash"] = pwaHashCookie.value;
23
+ if (import.meta.server) {
24
+ request.headers["cookie"] = updateVsfLocale(request.headers["cookie"], locale);
25
+ }
26
+ return request;
27
+ });
28
+ client.interceptors.response.use((response) => {
29
+ if (response.headers["x-csrf-token"]) token.value = response.headers["x-csrf-token"];
30
+ return response;
31
+ });
32
+ }
33
+ return client;
34
+ };
35
+ const handleHttpError = (error) => {
36
+ const axiosError = error;
37
+ const data = axiosError?.response?.data?.data || axiosError?.response?.data;
38
+ const events = axiosError?.response?.data?.events;
39
+ throw new ApiError({
40
+ key: data?.key || "unknownError",
41
+ code: axiosError?.response?.data?.error?.code ?? axiosError?.response?.status ?? axiosError.status,
42
+ message: data?.message ?? axiosError.message ?? "",
43
+ cause: data?.errors ?? {},
44
+ events
45
+ });
46
+ };
47
+ export const httpClient = async (url, params, config) => {
48
+ try {
49
+ const client = createHttpClient();
50
+ const { data } = await client(url, { ...config, data: params });
51
+ return data;
52
+ } catch (error) {
53
+ handleHttpError(error);
54
+ }
55
+ };
@@ -115,7 +115,7 @@ export const useCookieBar = () => {
115
115
  export const fetchScripts = (scripts) => {
116
116
  scripts.forEach((script) => {
117
117
  try {
118
- if (checkIfScriptIsExternal(script)) {
118
+ if (checkIfScriptIsExternal(script) && document) {
119
119
  const scriptElement = document.createElement("script");
120
120
  scriptElement.setAttribute("src", script);
121
121
  scriptElement.setAttribute("type", "text/javascript");
@@ -0,0 +1,3 @@
1
+ export declare const useCsrfToken: () => {
2
+ token: import("vue").Ref<string, string>;
3
+ };
@@ -0,0 +1,10 @@
1
+ import { useState } from "nuxt/app";
2
+ import { toRefs } from "vue";
3
+ export const useCsrfToken = () => {
4
+ const state = useState(`useCsrfToken`, () => ({
5
+ token: ""
6
+ }));
7
+ return {
8
+ ...toRefs(state.value)
9
+ };
10
+ };
@@ -0,0 +1,8 @@
1
+ export declare const useSdk: () => import("@vue-storefront/sdk").SDKApi<{
2
+ plentysystems: {
3
+ connector: import("@vue-storefront/sdk").Methods<import("@vue-storefront/middleware").WithoutContext<typeof import("@plentymarkets/shop-api/lib/api")>>;
4
+ context: {
5
+ requestSender: import("@vue-storefront/sdk").RequestSender;
6
+ };
7
+ } & object;
8
+ }>;
@@ -0,0 +1,16 @@
1
+ import { initSDK, buildModule, middlewareModule } from "@vue-storefront/sdk";
2
+ import { httpClient } from "./sdk.client.js";
3
+ import { useRequestHeaders, useRuntimeConfig } from "#imports";
4
+ export const useSdk = () => {
5
+ const config = useRuntimeConfig().public.shopCore;
6
+ const sdkConfig = {
7
+ plentysystems: buildModule(middlewareModule, {
8
+ apiUrl: config.apiUrl + "/plentysystems",
9
+ defaultRequestConfig: {
10
+ headers: useRequestHeaders()
11
+ },
12
+ httpClient
13
+ })
14
+ };
15
+ return initSDK(sdkConfig);
16
+ };
@@ -0,0 +1,22 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { updateVsfLocale } from "../sdkClientHelper.js";
3
+ describe("sdk client helper", () => {
4
+ it("should update the locale", () => {
5
+ const cookie = "vsf-locale=en";
6
+ const locale = "fr";
7
+ const result = updateVsfLocale(cookie, locale);
8
+ expect(result).toBe("vsf-locale=fr");
9
+ });
10
+ it("should update the cookie in existing cookie string", () => {
11
+ const cookie = "vsf-locale=en; test=123";
12
+ const locale = "de";
13
+ const result = updateVsfLocale(cookie, locale);
14
+ expect(result).toBe("vsf-locale=de; test=123");
15
+ });
16
+ it("should update the locale when cookie is empty", () => {
17
+ const cookie = "";
18
+ const locale = "de";
19
+ const result = updateVsfLocale(cookie, locale);
20
+ expect(result).toBe("; vsf-locale=de");
21
+ });
22
+ });
@@ -0,0 +1 @@
1
+ export declare const updateVsfLocale: (cookie: string, locale: string) => string;
@@ -0,0 +1,16 @@
1
+ const VSL_LOCALE_COOKIE = "vsf-locale";
2
+ export const updateVsfLocale = (cookie, locale) => {
3
+ let cookieExists = false;
4
+ const cookies = (cookie ?? "").split(";");
5
+ const cookiesArray = cookies.map((cookie2) => {
6
+ if (cookie2.trim().includes(VSL_LOCALE_COOKIE)) {
7
+ cookieExists = true;
8
+ return `${VSL_LOCALE_COOKIE}=${locale}`;
9
+ }
10
+ return cookie2;
11
+ });
12
+ if (!cookieExists) {
13
+ cookiesArray.push(`${VSL_LOCALE_COOKIE}=${locale}`);
14
+ }
15
+ return cookiesArray.join("; ");
16
+ };
package/dist/types.d.mts CHANGED
@@ -1,7 +1 @@
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
1
  export { type Cookie, type CookieGroup, type CookieGroupFromNuxtConfig } from './module.js'
package/dist/types.d.ts CHANGED
@@ -1,7 +1 @@
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
1
  export { type Cookie, type CookieGroup, type CookieGroupFromNuxtConfig } from './module'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plentymarkets/shop-core",
3
- "version": "1.2.1",
3
+ "version": "1.3.0",
4
4
  "description": "Core module for PlentyONE Shop",
5
5
  "repository": {
6
6
  "type": "git",
@@ -41,16 +41,18 @@
41
41
  "test:types": "vue-tsc --noEmit && cd playground && vue-tsc --noEmit"
42
42
  },
43
43
  "dependencies": {
44
- "@plentymarkets/shop-api": "^0.88.0",
44
+ "@plentymarkets/shop-api": "^0.94.1",
45
+ "@vue-storefront/sdk": "^3.4.1",
45
46
  "mitt": "^3.0.1"
46
47
  },
47
48
  "devDependencies": {
48
49
  "@nuxt/devtools": "^1.7.0",
49
50
  "@nuxt/eslint-config": "^0.7.5",
50
- "@nuxt/kit": "^3.15.1",
51
+ "@nuxt/kit": "^3.16.0",
51
52
  "@nuxt/module-builder": "^0.8.4",
52
- "@nuxt/schema": "^3.15.1",
53
+ "@nuxt/schema": "^3.16.0",
53
54
  "@nuxt/test-utils": "^3.15.4",
55
+ "@nuxtjs/i18n": "9.3.1",
54
56
  "@types/node": "latest",
55
57
  "@vitest/coverage-v8": "2.1.8",
56
58
  "@vue-storefront/eslint-config": "^4.1.0",