@plentymarkets/shop-core 1.15.0 → 1.16.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
@@ -5,6 +5,13 @@ interface ModuleOptions {
5
5
  apiUrl: string;
6
6
  apiEndpoint: string;
7
7
  configId: number;
8
+ logger?: {
9
+ sdkPlugin?: {
10
+ enabled: boolean;
11
+ verbose: boolean;
12
+ serverOnly: boolean;
13
+ };
14
+ };
8
15
  }
9
16
  declare const _default: _nuxt_schema.NuxtModule<ModuleOptions, ModuleOptions, false>;
10
17
 
package/dist/module.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@plentymarkets/shop-core",
3
3
  "configKey": "shopCore",
4
- "version": "1.15.0",
4
+ "version": "1.16.0",
5
5
  "builder": {
6
6
  "@nuxt/module-builder": "1.0.2",
7
7
  "unbuild": "unknown"
package/dist/module.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { defineNuxtModule, createResolver, addImports } from '@nuxt/kit';
1
+ import { defineNuxtModule, createResolver, addPlugin, addImports } from '@nuxt/kit';
2
2
  import { ofetch } from 'ofetch';
3
3
  import { access, mkdir, writeFile } from 'fs/promises';
4
4
  import { join } from 'path';
@@ -31,7 +31,14 @@ const module$1 = defineNuxtModule({
31
31
  const resolver = createResolver(import.meta.url);
32
32
  nuxt.options.runtimeConfig.public.shopCore = {
33
33
  apiUrl: _options.apiUrl,
34
- configId: _options.configId
34
+ configId: _options.configId,
35
+ logger: {
36
+ sdkPlugin: {
37
+ enabled: _options.logger?.sdkPlugin?.enabled ?? false,
38
+ verbose: _options.logger?.sdkPlugin?.verbose ?? false,
39
+ serverOnly: _options.logger?.sdkPlugin?.serverOnly ?? true
40
+ }
41
+ }
35
42
  };
36
43
  const overridesDir = resolver.resolve("./runtime/lang/overrides");
37
44
  const allLocalesForOverride = [];
@@ -67,6 +74,7 @@ const module$1 = defineNuxtModule({
67
74
  });
68
75
  }
69
76
  });
77
+ addPlugin(resolver.resolve("./runtime/plugins/sdk"));
70
78
  addImports({
71
79
  name: "useSdk",
72
80
  as: "useSdk",
@@ -1,6 +1,6 @@
1
1
  import type { Endpoints } from '@plentymarkets/shop-api';
2
2
  /**
3
- * @description Composable for using the SDK.
3
+ * @description Composable for wrapping $sdk plugin.
4
4
  */
5
5
  export declare const useSdk: () => {
6
6
  plentysystems: Endpoints;
@@ -1,84 +1,5 @@
1
- import { initSDK, buildModule, middlewareModule } from "@vue-storefront/sdk";
2
- import { httpClient } from "./sdk.client.js";
3
- import { updateSsrCookie } from "../utils/sdk.helper.js";
4
- import { useCookie, useNuxtApp, useRequestHeaders, useRuntimeConfig, useState } from "#imports";
5
- import { useCsrfToken } from "./useCsrfToken.js";
1
+ import { useNuxtApp } from "#imports";
6
2
  export const useSdk = () => {
7
- const nuxtApp = useNuxtApp();
8
- if (nuxtApp._sdk) {
9
- return nuxtApp._sdk;
10
- }
11
- const runtimeConfig = useRuntimeConfig();
12
- const moduleConfig = runtimeConfig.public.shopCore;
13
- const { $router, $i18n } = useNuxtApp();
14
- const { token } = useCsrfToken();
15
- const requestHeaders = useRequestHeaders(["cookie"]);
16
- const ssrCookie = useState("ssr-cookie", () => requestHeaders.cookie ?? "");
17
- const getFreshHeaders = () => {
18
- const locale = $i18n.locale.value;
19
- const referrerId = $router.currentRoute.value.query?.ReferrerID?.toString() ?? "";
20
- const noCache = runtimeConfig.public.noCache || $router.currentRoute.value.query?.noCache?.toString() || "";
21
- const configId = runtimeConfig.public.configId?.toString();
22
- const pwaHashCookie = useCookie("pwa");
23
- const headers = {
24
- ...configId && { "x-config-id": configId },
25
- ...token.value && { "x-csrf-token": token.value },
26
- ...pwaHashCookie.value && { "x-pwa-edit-hash": pwaHashCookie.value },
27
- ...locale && { locale },
28
- ...referrerId && { referrerID: referrerId },
29
- ...noCache && { noCache },
30
- ...useRequestHeaders(),
31
- ...import.meta.server && ssrCookie.value && { cookie: ssrCookie.value }
32
- };
33
- return {
34
- ...headers
35
- };
36
- };
37
- const headersProxy = new Proxy(
38
- {},
39
- {
40
- get(_target, prop, receiver) {
41
- return Reflect.get(getFreshHeaders(), prop, receiver);
42
- },
43
- ownKeys() {
44
- return Reflect.ownKeys(getFreshHeaders());
45
- },
46
- getOwnPropertyDescriptor() {
47
- return {
48
- enumerable: true,
49
- configurable: true
50
- };
51
- }
52
- }
53
- );
54
- const sdkConfig = {
55
- plentysystems: buildModule(
56
- middlewareModule,
57
- {
58
- apiUrl: moduleConfig.apiUrl + "/plentysystems",
59
- defaultRequestConfig: {
60
- headers: headersProxy
61
- },
62
- httpClient
63
- },
64
- () => ({
65
- subscribers: {
66
- "*_after": (payload) => {
67
- const { ssrContext } = useNuxtApp();
68
- if (payload.csrfToken) {
69
- token.value = payload.csrfToken;
70
- }
71
- if (import.meta.server && payload.setCookie && ssrContext?.event?.node?.res) {
72
- updateSsrCookie(ssrCookie, payload.setCookie);
73
- ssrContext.event.node.res.setHeader("Access-Control-Expose-Headers", "Set-Cookie");
74
- ssrContext.event.node.res.setHeader("Set-Cookie", payload.setCookie);
75
- }
76
- }
77
- }
78
- })
79
- )
80
- };
81
- const sdk = initSDK(sdkConfig);
82
- nuxtApp._sdk = sdk;
83
- return sdk;
3
+ const { $sdk } = useNuxtApp();
4
+ return $sdk;
84
5
  };
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Plugin to provide the SDK instance globally via $sdk singleton
3
+ * The SDK is initialized once and reused across requests using the NuxtApp instance
4
+ */
5
+ declare const _default: import("#app").Plugin<{
6
+ sdk: import("@vue-storefront/sdk").SDKApi<{
7
+ plentysystems: {
8
+ connector: import("@vue-storefront/sdk").Methods<import("@vue-storefront/middleware").WithoutContext<typeof import("@plentymarkets/shop-api/lib/api")>>;
9
+ context: {
10
+ requestSender: import("@vue-storefront/sdk").RequestSender;
11
+ };
12
+ } & object;
13
+ }>;
14
+ }> & import("#app").ObjectPlugin<{
15
+ sdk: import("@vue-storefront/sdk").SDKApi<{
16
+ plentysystems: {
17
+ connector: import("@vue-storefront/sdk").Methods<import("@vue-storefront/middleware").WithoutContext<typeof import("@plentymarkets/shop-api/lib/api")>>;
18
+ context: {
19
+ requestSender: import("@vue-storefront/sdk").RequestSender;
20
+ };
21
+ } & object;
22
+ }>;
23
+ }>;
24
+ export default _default;
@@ -0,0 +1,66 @@
1
+ import { defineNuxtPlugin, ref, useCookie, useRequestHeaders, useRuntimeConfig } from "#imports";
2
+ import { useCsrfToken } from "../composables/useCsrfToken.js";
3
+ import { buildModule, initSDK, middlewareModule } from "@vue-storefront/sdk";
4
+ import { httpClient as OriginalHttpClient } from "../utils/sdk/sdk.client.js";
5
+ import { createSdkLogger } from "../utils/sdk/sdk.logger.js";
6
+ import { buildSdkHeaders, handleSdkResponse } from "../utils/sdk/sdk.header.js";
7
+ export default defineNuxtPlugin((nuxtApp) => {
8
+ const runtimeConfig = useRuntimeConfig();
9
+ const moduleConfig = runtimeConfig.public.shopCore;
10
+ const logger = createSdkLogger({ ...moduleConfig.logger?.sdkPlugin, prefix: "[shop-core][sdk plugin]: " });
11
+ const configId = runtimeConfig.public.configId?.toString();
12
+ const noCache = runtimeConfig.public.noCache || nuxtApp.$router.currentRoute.value.query?.noCache?.toString() || "";
13
+ const referrerId = nuxtApp.$router.currentRoute.value.query?.ReferrerID?.toString() ?? "";
14
+ const { token: csrfToken } = useCsrfToken();
15
+ const ssrCookie = ref("");
16
+ logger.log("\u{1F680} initializing SDK plugin");
17
+ const getHeaders = () => {
18
+ logger.log("\u{1F9FE} generating fresh headers for SDK request");
19
+ const requestHeaders = useRequestHeaders(["cookie"]);
20
+ const pwaHashCookie = useCookie("pwa");
21
+ const locale = nuxtApp.$i18n.locale.value ?? "en";
22
+ const headers = buildSdkHeaders({
23
+ configId,
24
+ csrfToken: csrfToken.value,
25
+ pwaHash: pwaHashCookie.value ?? "",
26
+ locale,
27
+ referrerId,
28
+ noCache,
29
+ requestHeaders,
30
+ ssrCookie: ssrCookie.value
31
+ });
32
+ logger.verbose(`\u{1F4EC} prepared headers: `, JSON.stringify(headers));
33
+ return {
34
+ ...headers
35
+ };
36
+ };
37
+ const sdkConfig = {
38
+ plentysystems: buildModule(middlewareModule, {
39
+ apiUrl: moduleConfig.apiUrl + "/plentysystems",
40
+ httpClient: async (url, params, config) => {
41
+ return nuxtApp.runWithContext(async () => {
42
+ logger.log(`\u{1F4E1} making SDK request to ${url}`);
43
+ const freshHeaders = getHeaders();
44
+ const response = await OriginalHttpClient(url, params, {
45
+ ...config,
46
+ headers: { ...config?.headers, ...freshHeaders }
47
+ });
48
+ handleSdkResponse({
49
+ response,
50
+ csrfToken,
51
+ ssrCookie,
52
+ ssrContext: nuxtApp.ssrContext,
53
+ logger
54
+ });
55
+ return response;
56
+ });
57
+ }
58
+ })
59
+ };
60
+ const sdk = initSDK(sdkConfig);
61
+ return {
62
+ provide: {
63
+ sdk
64
+ }
65
+ };
66
+ });
@@ -1 +1,2 @@
1
1
  export declare const isClient: () => boolean;
2
+ export declare const isServer: () => boolean;
@@ -1 +1,2 @@
1
1
  export const isClient = () => import.meta.client;
2
+ export const isServer = () => import.meta.server;
@@ -0,0 +1,3 @@
1
+ import type { HandleSdkResponseContext, SdkHeaderContext } from './types.js';
2
+ export declare const buildSdkHeaders: (context: SdkHeaderContext) => Record<string, string>;
3
+ export declare const handleSdkResponse: (context: HandleSdkResponseContext) => void;
@@ -0,0 +1,29 @@
1
+ import { isServer } from "../runtime.js";
2
+ import { updateSsrCookie } from "./sdk.helper.js";
3
+ export const buildSdkHeaders = (context) => {
4
+ const headers = {
5
+ ...context.configId && { "x-config-id": context.configId },
6
+ ...context.csrfToken && { "x-csrf-token": context.csrfToken },
7
+ ...context.pwaHash && { "x-pwa-edit-hash": context.pwaHash },
8
+ ...context.locale && { locale: context.locale },
9
+ ...context.referrerId && { referrerID: context.referrerId },
10
+ ...context.noCache && { noCache: context.noCache },
11
+ ...context.requestHeaders,
12
+ ...isServer() && context.ssrCookie && { cookie: context.ssrCookie }
13
+ };
14
+ return headers;
15
+ };
16
+ export const handleSdkResponse = (context) => {
17
+ if (context.response?.csrfToken) {
18
+ context.logger.verbose("\u{1F510} updating CSRF token from response", context.response.csrfToken);
19
+ context.csrfToken.value = context.response.csrfToken;
20
+ }
21
+ if (isServer() && context.response?.setCookie) {
22
+ context.logger.verbose("\u{1F36A} updating SSR cookies from response", JSON.stringify(context.response?.setCookie));
23
+ if (context.ssrContext?.event?.node?.res) {
24
+ updateSsrCookie(context.ssrCookie, context.response.setCookie);
25
+ context.ssrContext.event.node.res.setHeader("Access-Control-Expose-Headers", "Set-Cookie");
26
+ context.ssrContext.event.node.res.setHeader("Set-Cookie", context.response.setCookie);
27
+ }
28
+ }
29
+ };
@@ -0,0 +1,9 @@
1
+ export declare const createSdkLogger: (options: {
2
+ enabled?: boolean;
3
+ verbose?: boolean;
4
+ prefix?: string;
5
+ serverOnly?: boolean;
6
+ }) => {
7
+ log: (message: string, args?: string) => void;
8
+ verbose: (message: string, args?: string) => void;
9
+ };
@@ -0,0 +1,35 @@
1
+ export const createSdkLogger = (options) => {
2
+ const config = {
3
+ enabled: options.enabled ?? false,
4
+ verbose: options.verbose ?? false,
5
+ prefix: options.prefix ?? "",
6
+ serverOnly: options.serverOnly ?? true
7
+ };
8
+ if (!config.enabled || config.serverOnly && !import.meta.server) {
9
+ return {
10
+ log: () => {
11
+ },
12
+ verbose: () => {
13
+ }
14
+ };
15
+ }
16
+ const log = (message, args = "") => {
17
+ try {
18
+ console.log(config.prefix + message, args);
19
+ } catch {
20
+ }
21
+ };
22
+ const verbose = (message, args = "") => {
23
+ if (!config.verbose) {
24
+ return;
25
+ }
26
+ try {
27
+ console.debug(config.prefix + message, args);
28
+ } catch {
29
+ }
30
+ };
31
+ return {
32
+ log,
33
+ verbose
34
+ };
35
+ };
@@ -0,0 +1,19 @@
1
+ import type { Ref } from '#imports';
2
+ import type { createSdkLogger } from './sdk.logger.js';
3
+ export interface SdkHeaderContext {
4
+ configId?: string;
5
+ csrfToken?: string;
6
+ pwaHash?: string;
7
+ locale?: string;
8
+ referrerId?: string;
9
+ noCache?: string;
10
+ requestHeaders?: Record<string, string>;
11
+ ssrCookie?: string;
12
+ }
13
+ export interface HandleSdkResponseContext {
14
+ response: any;
15
+ csrfToken: Ref<string>;
16
+ ssrCookie: Ref<string>;
17
+ ssrContext: any;
18
+ logger: ReturnType<typeof createSdkLogger>;
19
+ }
File without changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plentymarkets/shop-core",
3
- "version": "1.15.0",
3
+ "version": "1.16.0",
4
4
  "description": "Core module for PlentyONE Shop",
5
5
  "repository": {
6
6
  "type": "git",
@@ -45,7 +45,7 @@
45
45
  "test:types": "vue-tsc --noEmit"
46
46
  },
47
47
  "dependencies": {
48
- "@plentymarkets/shop-api": "^0.149.0",
48
+ "@plentymarkets/shop-api": "^0.150.0",
49
49
  "@vue-storefront/sdk": "^3.4.1",
50
50
  "cookie": "^1.0.2",
51
51
  "js-sha256": "^0.11.0",