@vue-storefront/next 7.0.0-next.2 → 7.0.0-next.4
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/client.cjs +223 -0
- package/dist/{client.d.ts → client.d.cts} +6 -7
- package/dist/client.d.mts +6 -7
- package/dist/client.mjs +180 -139
- package/dist/env-Be9N7zqo.cjs +115 -0
- package/dist/env-XPpRDRcE.mjs +97 -0
- package/dist/index-BXYjHxeC.d.mts +250 -0
- package/dist/index-D1weTw7N.d.cts +250 -0
- package/dist/index.cjs +258 -0
- package/dist/{index.d.ts → index.d.cts} +18 -20
- package/dist/index.d.mts +18 -20
- package/dist/index.mjs +214 -131
- package/package.json +15 -15
- package/dist/chunk-NSPLXZD5.mjs +0 -84
- package/dist/client.js +0 -228
- package/dist/index.js +0 -238
- package/dist/types-FGnrBhjy.d.mts +0 -241
- package/dist/types-FGnrBhjy.d.ts +0 -241
package/dist/client.cjs
ADDED
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
//#region rolldown:runtime
|
|
4
|
+
var __create = Object.create;
|
|
5
|
+
var __defProp = Object.defineProperty;
|
|
6
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
7
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
8
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
9
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
13
|
+
key = keys[i];
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except) {
|
|
15
|
+
__defProp(to, key, {
|
|
16
|
+
get: ((k) => from[k]).bind(null, key),
|
|
17
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return to;
|
|
23
|
+
};
|
|
24
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
25
|
+
value: mod,
|
|
26
|
+
enumerable: true
|
|
27
|
+
}) : target, mod));
|
|
28
|
+
|
|
29
|
+
//#endregion
|
|
30
|
+
const require_env = require('./env-Be9N7zqo.cjs');
|
|
31
|
+
let __alokai_instrumentation_next_component = require("@alokai/instrumentation-next-component");
|
|
32
|
+
let next_script = require("next/script");
|
|
33
|
+
next_script = __toESM(next_script);
|
|
34
|
+
let react = require("react");
|
|
35
|
+
react = __toESM(react);
|
|
36
|
+
let zustand = require("zustand");
|
|
37
|
+
|
|
38
|
+
//#region src/env/get-public-env.ts
|
|
39
|
+
/**
|
|
40
|
+
* Extracts all NEXT_PUBLIC_* environment variables from process.env
|
|
41
|
+
* This is used server-side to prepare env vars for injection into the client
|
|
42
|
+
*
|
|
43
|
+
* @returns Object containing all NEXT_PUBLIC_* environment variables
|
|
44
|
+
*/
|
|
45
|
+
function getPublicEnv() {
|
|
46
|
+
const publicEnv = {};
|
|
47
|
+
for (const key in process.env) if (key.startsWith("NEXT_PUBLIC_")) publicEnv[key] = process.env[key];
|
|
48
|
+
return publicEnv;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
//#endregion
|
|
52
|
+
//#region src/state.tsx
|
|
53
|
+
const createSfState = (initialData) => {
|
|
54
|
+
return (0, zustand.createStore)()((set) => require_env._objectSpread2(require_env._objectSpread2({
|
|
55
|
+
cart: null,
|
|
56
|
+
customer: null
|
|
57
|
+
}, initialData), {}, {
|
|
58
|
+
setCart: (cart) => set({ cart }),
|
|
59
|
+
setCurrencies: (currencies) => set({ currencies }),
|
|
60
|
+
setCurrency: (currency) => set({ currency }),
|
|
61
|
+
setCustomer: (customer) => set({ customer }),
|
|
62
|
+
setLocale: (locale) => set({ locale }),
|
|
63
|
+
setLocales: (locales) => set({ locales })
|
|
64
|
+
}));
|
|
65
|
+
};
|
|
66
|
+
function createSfStateProvider() {
|
|
67
|
+
const SfStateContext = (0, react.createContext)(null);
|
|
68
|
+
function SfStateProvider({ children, initialData }) {
|
|
69
|
+
const stateReference = (0, react.useRef)(null);
|
|
70
|
+
if (!stateReference.current) stateReference.current = createSfState(initialData);
|
|
71
|
+
return /* @__PURE__ */ react.default.createElement(SfStateContext.Provider, { value: stateReference.current }, children);
|
|
72
|
+
}
|
|
73
|
+
function getSfStateContext() {
|
|
74
|
+
const context = (0, react.useContext)(SfStateContext);
|
|
75
|
+
if (!context) throw new Error("Missing SfStateContext.Provider in the tree");
|
|
76
|
+
return context;
|
|
77
|
+
}
|
|
78
|
+
function useSfCurrencyState() {
|
|
79
|
+
const { currency, setCurrency } = (0, zustand.useStore)(getSfStateContext(), (state) => ({
|
|
80
|
+
currency: state.currency,
|
|
81
|
+
setCurrency: state.setCurrency
|
|
82
|
+
}));
|
|
83
|
+
return [currency, setCurrency];
|
|
84
|
+
}
|
|
85
|
+
function useSfCurrenciesState() {
|
|
86
|
+
const { currencies, setCurrencies } = (0, zustand.useStore)(getSfStateContext(), (state) => ({
|
|
87
|
+
currencies: state.currencies,
|
|
88
|
+
setCurrencies: state.setCurrencies
|
|
89
|
+
}));
|
|
90
|
+
return [currencies, setCurrencies];
|
|
91
|
+
}
|
|
92
|
+
function useSfLocaleState() {
|
|
93
|
+
const { locale, setLocale } = (0, zustand.useStore)(getSfStateContext(), (state) => ({
|
|
94
|
+
locale: state.locale,
|
|
95
|
+
setLocale: state.setLocale
|
|
96
|
+
}));
|
|
97
|
+
return [locale, setLocale];
|
|
98
|
+
}
|
|
99
|
+
function useSfLocalesState() {
|
|
100
|
+
const { locales, setLocales } = (0, zustand.useStore)(getSfStateContext(), (state) => ({
|
|
101
|
+
locales: state.locales,
|
|
102
|
+
setLocales: state.setLocales
|
|
103
|
+
}));
|
|
104
|
+
return [locales, setLocales];
|
|
105
|
+
}
|
|
106
|
+
function useSfCartState() {
|
|
107
|
+
const { cart, setCart } = (0, zustand.useStore)(getSfStateContext(), (state) => ({
|
|
108
|
+
cart: state.cart,
|
|
109
|
+
setCart: state.setCart
|
|
110
|
+
}));
|
|
111
|
+
return [cart, setCart];
|
|
112
|
+
}
|
|
113
|
+
function useSfCustomerState() {
|
|
114
|
+
const { customer, setCustomer } = (0, zustand.useStore)(getSfStateContext(), (state) => ({
|
|
115
|
+
customer: state.customer,
|
|
116
|
+
setCustomer: state.setCustomer
|
|
117
|
+
}));
|
|
118
|
+
return [customer, setCustomer];
|
|
119
|
+
}
|
|
120
|
+
return {
|
|
121
|
+
SfStateProvider,
|
|
122
|
+
useSfCartState,
|
|
123
|
+
useSfCurrenciesState,
|
|
124
|
+
useSfCurrencyState,
|
|
125
|
+
useSfCustomerState,
|
|
126
|
+
useSfLocalesState,
|
|
127
|
+
useSfLocaleState
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
//#endregion
|
|
132
|
+
//#region \0@oxc-project+runtime@0.101.0/helpers/objectWithoutPropertiesLoose.js
|
|
133
|
+
function _objectWithoutPropertiesLoose(r, e) {
|
|
134
|
+
if (null == r) return {};
|
|
135
|
+
var t = {};
|
|
136
|
+
for (var n in r) if ({}.hasOwnProperty.call(r, n)) {
|
|
137
|
+
if (e.includes(n)) continue;
|
|
138
|
+
t[n] = r[n];
|
|
139
|
+
}
|
|
140
|
+
return t;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
//#endregion
|
|
144
|
+
//#region \0@oxc-project+runtime@0.101.0/helpers/objectWithoutProperties.js
|
|
145
|
+
function _objectWithoutProperties(e, t) {
|
|
146
|
+
if (null == e) return {};
|
|
147
|
+
var o, r, i = _objectWithoutPropertiesLoose(e, t);
|
|
148
|
+
if (Object.getOwnPropertySymbols) {
|
|
149
|
+
var s = Object.getOwnPropertySymbols(e);
|
|
150
|
+
for (r = 0; r < s.length; r++) o = s[r], t.includes(o) || {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]);
|
|
151
|
+
}
|
|
152
|
+
return i;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
//#endregion
|
|
156
|
+
//#region src/alokai-provider.tsx
|
|
157
|
+
const _excluded = ["SfStateProvider"];
|
|
158
|
+
/**
|
|
159
|
+
* Creates a new Alokai context which is a combination of SDK and state contexts.
|
|
160
|
+
* This function is dedicated for the client-side usage.
|
|
161
|
+
*
|
|
162
|
+
* @example
|
|
163
|
+
* Create a new Alokai context somewhere in your application. It may be the `hooks/sdk.ts` file.
|
|
164
|
+
*
|
|
165
|
+
* ```tsx
|
|
166
|
+
* 'use client';
|
|
167
|
+
*
|
|
168
|
+
* import { createAlokaiContext } from "@vue-storefront/next/client";
|
|
169
|
+
* import type { Sdk } from './sdk.server';
|
|
170
|
+
* import type { SfContract } from 'storefront-middleware/types';
|
|
171
|
+
*
|
|
172
|
+
* export const {
|
|
173
|
+
* AlokaiProvider,
|
|
174
|
+
* useSdk,
|
|
175
|
+
* useSfCartState,
|
|
176
|
+
* useSfCurrenciesState,
|
|
177
|
+
* useSfCurrencyState,
|
|
178
|
+
* useSfCustomerState,
|
|
179
|
+
* } = createAlokaiContext<Sdk, SfContract>();
|
|
180
|
+
* ```
|
|
181
|
+
* This is also a place where you can import hooks for handling the state management.
|
|
182
|
+
* You can read more about the state management in the documentation https://docs.alokai.com/sdk/getting-started/state-management.
|
|
183
|
+
* Then use the `AlokaiProvider` in the root component of your application.
|
|
184
|
+
* For Pages Router it would be the `pages/_app.tsx` file,
|
|
185
|
+
* and for the App Router it would be the `app/layout.tsx` file.
|
|
186
|
+
* Finally, you can use the `useSdk` in any client component of your application.
|
|
187
|
+
* @returns AlokaiProvider - The Alokai Context Provider.
|
|
188
|
+
* @returns useSdk - The SDK provider and the `useSdk` hook.
|
|
189
|
+
* @returns useSfCartState - Hook that return Cart state with `SfCart` type.
|
|
190
|
+
* @returns useSfCurrenciesState - Hook that return Currencies state with `SfCurrency` type.
|
|
191
|
+
* @returns useSfCurrencyState - Hook that return Currency state with `SfCurrency[]` type.
|
|
192
|
+
* @returns useSfCustomerState - Hook that return Customer state with `SfCustomer` type.
|
|
193
|
+
*/
|
|
194
|
+
function createAlokaiContext() {
|
|
195
|
+
const SdkContext = (0, react.createContext)(null);
|
|
196
|
+
const _createSfStateProvide = createSfStateProvider(), { SfStateProvider } = _createSfStateProvide, rest = _objectWithoutProperties(_createSfStateProvide, _excluded);
|
|
197
|
+
function AlokaiProvider({ children, initialData, sdk }) {
|
|
198
|
+
const publicEnv = typeof window === "undefined" ? getPublicEnv() : void 0;
|
|
199
|
+
const envScript = publicEnv ? `window['${require_env.PUBLIC_ENV_KEY}'] = ${JSON.stringify(publicEnv)};` : "";
|
|
200
|
+
return /* @__PURE__ */ react.default.createElement(SdkContext.Provider, { value: sdk }, /* @__PURE__ */ react.default.createElement("meta", {
|
|
201
|
+
content: "Alokai Storefront",
|
|
202
|
+
name: "generator"
|
|
203
|
+
}), publicEnv && /* @__PURE__ */ react.default.createElement(next_script.default, {
|
|
204
|
+
id: "alokaiEnv",
|
|
205
|
+
strategy: "beforeInteractive"
|
|
206
|
+
}, envScript), /* @__PURE__ */ react.default.createElement(__alokai_instrumentation_next_component.AlokaiInstrumentation, null), /* @__PURE__ */ react.default.createElement(SfStateProvider, { initialData }, children));
|
|
207
|
+
}
|
|
208
|
+
const useSdk = () => {
|
|
209
|
+
const contextSdk = (0, react.useContext)(SdkContext);
|
|
210
|
+
if (!contextSdk) throw new Error("useSdk must be used within a AlokaiProvider");
|
|
211
|
+
return contextSdk;
|
|
212
|
+
};
|
|
213
|
+
return require_env._objectSpread2({
|
|
214
|
+
AlokaiProvider,
|
|
215
|
+
useSdk
|
|
216
|
+
}, rest);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
//#endregion
|
|
220
|
+
exports.__toESM = __toESM;
|
|
221
|
+
exports.createAlokaiContext = createAlokaiContext;
|
|
222
|
+
exports.createSfStateProvider = createSfStateProvider;
|
|
223
|
+
exports.env = require_env.env;
|
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
import 'react';
|
|
1
|
+
import { c as SfContract, d as createSfStateProvider, l as SfState, r as CreateSdkContextReturn, s as Maybe, t as env, u as SfStateProps } from "./index-D1weTw7N.cjs";
|
|
2
|
+
import { SDKApi } from "@alokai/connect/sdk";
|
|
3
|
+
|
|
4
|
+
//#region src/alokai-provider.d.ts
|
|
6
5
|
|
|
7
6
|
/**
|
|
8
7
|
* Creates a new Alokai context which is a combination of SDK and state contexts.
|
|
@@ -41,5 +40,5 @@ import 'react';
|
|
|
41
40
|
* @returns useSfCustomerState - Hook that return Customer state with `SfCustomer` type.
|
|
42
41
|
*/
|
|
43
42
|
declare function createAlokaiContext<TSdk extends SDKApi<any>, TSfContract extends SfContract>(): CreateSdkContextReturn<TSdk, TSfContract>;
|
|
44
|
-
|
|
45
|
-
export { CreateSdkContextReturn, SfContract, createAlokaiContext };
|
|
43
|
+
//#endregion
|
|
44
|
+
export { type CreateSdkContextReturn, Maybe, SfContract, SfState, SfStateProps, createAlokaiContext, createSfStateProvider, env };
|
package/dist/client.d.mts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
import 'react';
|
|
1
|
+
import { c as SfContract, d as createSfStateProvider, l as SfState, r as CreateSdkContextReturn, s as Maybe, t as env, u as SfStateProps } from "./index-BXYjHxeC.mjs";
|
|
2
|
+
import { SDKApi } from "@alokai/connect/sdk";
|
|
3
|
+
|
|
4
|
+
//#region src/alokai-provider.d.ts
|
|
6
5
|
|
|
7
6
|
/**
|
|
8
7
|
* Creates a new Alokai context which is a combination of SDK and state contexts.
|
|
@@ -41,5 +40,5 @@ import 'react';
|
|
|
41
40
|
* @returns useSfCustomerState - Hook that return Customer state with `SfCustomer` type.
|
|
42
41
|
*/
|
|
43
42
|
declare function createAlokaiContext<TSdk extends SDKApi<any>, TSfContract extends SfContract>(): CreateSdkContextReturn<TSdk, TSfContract>;
|
|
44
|
-
|
|
45
|
-
export { CreateSdkContextReturn, SfContract, createAlokaiContext };
|
|
43
|
+
//#endregion
|
|
44
|
+
export { type CreateSdkContextReturn, Maybe, SfContract, SfState, SfStateProps, createAlokaiContext, createSfStateProvider, env };
|
package/dist/client.mjs
CHANGED
|
@@ -1,150 +1,191 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import {
|
|
3
|
-
PUBLIC_ENV_KEY,
|
|
4
|
-
__objRest,
|
|
5
|
-
__spreadProps,
|
|
6
|
-
__spreadValues,
|
|
7
|
-
env,
|
|
8
|
-
getPublicEnv
|
|
9
|
-
} from "./chunk-NSPLXZD5.mjs";
|
|
10
2
|
|
|
11
|
-
|
|
3
|
+
import { n as _objectSpread2, r as PUBLIC_ENV_KEY, t as env } from "./env-XPpRDRcE.mjs";
|
|
12
4
|
import { AlokaiInstrumentation } from "@alokai/instrumentation-next-component";
|
|
13
5
|
import Script from "next/script";
|
|
14
|
-
import
|
|
15
|
-
|
|
16
|
-
// src/state.tsx
|
|
17
|
-
import React, {
|
|
18
|
-
createContext,
|
|
19
|
-
useContext,
|
|
20
|
-
useRef
|
|
21
|
-
} from "react";
|
|
6
|
+
import React, { createContext, useContext, useRef } from "react";
|
|
22
7
|
import { createStore, useStore } from "zustand";
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
8
|
+
|
|
9
|
+
//#region src/env/get-public-env.ts
|
|
10
|
+
/**
|
|
11
|
+
* Extracts all NEXT_PUBLIC_* environment variables from process.env
|
|
12
|
+
* This is used server-side to prepare env vars for injection into the client
|
|
13
|
+
*
|
|
14
|
+
* @returns Object containing all NEXT_PUBLIC_* environment variables
|
|
15
|
+
*/
|
|
16
|
+
function getPublicEnv() {
|
|
17
|
+
const publicEnv = {};
|
|
18
|
+
for (const key in process.env) if (key.startsWith("NEXT_PUBLIC_")) publicEnv[key] = process.env[key];
|
|
19
|
+
return publicEnv;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
//#endregion
|
|
23
|
+
//#region src/state.tsx
|
|
24
|
+
const createSfState = (initialData) => {
|
|
25
|
+
return createStore()((set) => _objectSpread2(_objectSpread2({
|
|
26
|
+
cart: null,
|
|
27
|
+
customer: null
|
|
28
|
+
}, initialData), {}, {
|
|
29
|
+
setCart: (cart) => set({ cart }),
|
|
30
|
+
setCurrencies: (currencies) => set({ currencies }),
|
|
31
|
+
setCurrency: (currency) => set({ currency }),
|
|
32
|
+
setCustomer: (customer) => set({ customer }),
|
|
33
|
+
setLocale: (locale) => set({ locale }),
|
|
34
|
+
setLocales: (locales) => set({ locales })
|
|
35
|
+
}));
|
|
35
36
|
};
|
|
36
37
|
function createSfStateProvider() {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
38
|
+
const SfStateContext = createContext(null);
|
|
39
|
+
function SfStateProvider({ children, initialData }) {
|
|
40
|
+
const stateReference = useRef(null);
|
|
41
|
+
if (!stateReference.current) stateReference.current = createSfState(initialData);
|
|
42
|
+
return /* @__PURE__ */ React.createElement(SfStateContext.Provider, { value: stateReference.current }, children);
|
|
43
|
+
}
|
|
44
|
+
function getSfStateContext() {
|
|
45
|
+
const context = useContext(SfStateContext);
|
|
46
|
+
if (!context) throw new Error("Missing SfStateContext.Provider in the tree");
|
|
47
|
+
return context;
|
|
48
|
+
}
|
|
49
|
+
function useSfCurrencyState() {
|
|
50
|
+
const { currency, setCurrency } = useStore(getSfStateContext(), (state) => ({
|
|
51
|
+
currency: state.currency,
|
|
52
|
+
setCurrency: state.setCurrency
|
|
53
|
+
}));
|
|
54
|
+
return [currency, setCurrency];
|
|
55
|
+
}
|
|
56
|
+
function useSfCurrenciesState() {
|
|
57
|
+
const { currencies, setCurrencies } = useStore(getSfStateContext(), (state) => ({
|
|
58
|
+
currencies: state.currencies,
|
|
59
|
+
setCurrencies: state.setCurrencies
|
|
60
|
+
}));
|
|
61
|
+
return [currencies, setCurrencies];
|
|
62
|
+
}
|
|
63
|
+
function useSfLocaleState() {
|
|
64
|
+
const { locale, setLocale } = useStore(getSfStateContext(), (state) => ({
|
|
65
|
+
locale: state.locale,
|
|
66
|
+
setLocale: state.setLocale
|
|
67
|
+
}));
|
|
68
|
+
return [locale, setLocale];
|
|
69
|
+
}
|
|
70
|
+
function useSfLocalesState() {
|
|
71
|
+
const { locales, setLocales } = useStore(getSfStateContext(), (state) => ({
|
|
72
|
+
locales: state.locales,
|
|
73
|
+
setLocales: state.setLocales
|
|
74
|
+
}));
|
|
75
|
+
return [locales, setLocales];
|
|
76
|
+
}
|
|
77
|
+
function useSfCartState() {
|
|
78
|
+
const { cart, setCart } = useStore(getSfStateContext(), (state) => ({
|
|
79
|
+
cart: state.cart,
|
|
80
|
+
setCart: state.setCart
|
|
81
|
+
}));
|
|
82
|
+
return [cart, setCart];
|
|
83
|
+
}
|
|
84
|
+
function useSfCustomerState() {
|
|
85
|
+
const { customer, setCustomer } = useStore(getSfStateContext(), (state) => ({
|
|
86
|
+
customer: state.customer,
|
|
87
|
+
setCustomer: state.setCustomer
|
|
88
|
+
}));
|
|
89
|
+
return [customer, setCustomer];
|
|
90
|
+
}
|
|
91
|
+
return {
|
|
92
|
+
SfStateProvider,
|
|
93
|
+
useSfCartState,
|
|
94
|
+
useSfCurrenciesState,
|
|
95
|
+
useSfCurrencyState,
|
|
96
|
+
useSfCustomerState,
|
|
97
|
+
useSfLocalesState,
|
|
98
|
+
useSfLocaleState
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
//#endregion
|
|
103
|
+
//#region \0@oxc-project+runtime@0.101.0/helpers/objectWithoutPropertiesLoose.js
|
|
104
|
+
function _objectWithoutPropertiesLoose(r, e) {
|
|
105
|
+
if (null == r) return {};
|
|
106
|
+
var t = {};
|
|
107
|
+
for (var n in r) if ({}.hasOwnProperty.call(r, n)) {
|
|
108
|
+
if (e.includes(n)) continue;
|
|
109
|
+
t[n] = r[n];
|
|
110
|
+
}
|
|
111
|
+
return t;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
//#endregion
|
|
115
|
+
//#region \0@oxc-project+runtime@0.101.0/helpers/objectWithoutProperties.js
|
|
116
|
+
function _objectWithoutProperties(e, t) {
|
|
117
|
+
if (null == e) return {};
|
|
118
|
+
var o, r, i = _objectWithoutPropertiesLoose(e, t);
|
|
119
|
+
if (Object.getOwnPropertySymbols) {
|
|
120
|
+
var s = Object.getOwnPropertySymbols(e);
|
|
121
|
+
for (r = 0; r < s.length; r++) o = s[r], t.includes(o) || {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]);
|
|
122
|
+
}
|
|
123
|
+
return i;
|
|
114
124
|
}
|
|
115
125
|
|
|
116
|
-
|
|
126
|
+
//#endregion
|
|
127
|
+
//#region src/alokai-provider.tsx
|
|
128
|
+
const _excluded = ["SfStateProvider"];
|
|
129
|
+
/**
|
|
130
|
+
* Creates a new Alokai context which is a combination of SDK and state contexts.
|
|
131
|
+
* This function is dedicated for the client-side usage.
|
|
132
|
+
*
|
|
133
|
+
* @example
|
|
134
|
+
* Create a new Alokai context somewhere in your application. It may be the `hooks/sdk.ts` file.
|
|
135
|
+
*
|
|
136
|
+
* ```tsx
|
|
137
|
+
* 'use client';
|
|
138
|
+
*
|
|
139
|
+
* import { createAlokaiContext } from "@vue-storefront/next/client";
|
|
140
|
+
* import type { Sdk } from './sdk.server';
|
|
141
|
+
* import type { SfContract } from 'storefront-middleware/types';
|
|
142
|
+
*
|
|
143
|
+
* export const {
|
|
144
|
+
* AlokaiProvider,
|
|
145
|
+
* useSdk,
|
|
146
|
+
* useSfCartState,
|
|
147
|
+
* useSfCurrenciesState,
|
|
148
|
+
* useSfCurrencyState,
|
|
149
|
+
* useSfCustomerState,
|
|
150
|
+
* } = createAlokaiContext<Sdk, SfContract>();
|
|
151
|
+
* ```
|
|
152
|
+
* This is also a place where you can import hooks for handling the state management.
|
|
153
|
+
* You can read more about the state management in the documentation https://docs.alokai.com/sdk/getting-started/state-management.
|
|
154
|
+
* Then use the `AlokaiProvider` in the root component of your application.
|
|
155
|
+
* For Pages Router it would be the `pages/_app.tsx` file,
|
|
156
|
+
* and for the App Router it would be the `app/layout.tsx` file.
|
|
157
|
+
* Finally, you can use the `useSdk` in any client component of your application.
|
|
158
|
+
* @returns AlokaiProvider - The Alokai Context Provider.
|
|
159
|
+
* @returns useSdk - The SDK provider and the `useSdk` hook.
|
|
160
|
+
* @returns useSfCartState - Hook that return Cart state with `SfCart` type.
|
|
161
|
+
* @returns useSfCurrenciesState - Hook that return Currencies state with `SfCurrency` type.
|
|
162
|
+
* @returns useSfCurrencyState - Hook that return Currency state with `SfCurrency[]` type.
|
|
163
|
+
* @returns useSfCustomerState - Hook that return Customer state with `SfCustomer` type.
|
|
164
|
+
*/
|
|
117
165
|
function createAlokaiContext() {
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
throw new Error("useSdk must be used within a AlokaiProvider");
|
|
141
|
-
}
|
|
142
|
-
return contextSdk;
|
|
143
|
-
};
|
|
144
|
-
return __spreadValues({ AlokaiProvider, useSdk }, rest);
|
|
166
|
+
const SdkContext = createContext(null);
|
|
167
|
+
const _createSfStateProvide = createSfStateProvider(), { SfStateProvider } = _createSfStateProvide, rest = _objectWithoutProperties(_createSfStateProvide, _excluded);
|
|
168
|
+
function AlokaiProvider({ children, initialData, sdk }) {
|
|
169
|
+
const publicEnv = typeof window === "undefined" ? getPublicEnv() : void 0;
|
|
170
|
+
const envScript = publicEnv ? `window['${PUBLIC_ENV_KEY}'] = ${JSON.stringify(publicEnv)};` : "";
|
|
171
|
+
return /* @__PURE__ */ React.createElement(SdkContext.Provider, { value: sdk }, /* @__PURE__ */ React.createElement("meta", {
|
|
172
|
+
content: "Alokai Storefront",
|
|
173
|
+
name: "generator"
|
|
174
|
+
}), publicEnv && /* @__PURE__ */ React.createElement(Script, {
|
|
175
|
+
id: "alokaiEnv",
|
|
176
|
+
strategy: "beforeInteractive"
|
|
177
|
+
}, envScript), /* @__PURE__ */ React.createElement(AlokaiInstrumentation, null), /* @__PURE__ */ React.createElement(SfStateProvider, { initialData }, children));
|
|
178
|
+
}
|
|
179
|
+
const useSdk = () => {
|
|
180
|
+
const contextSdk = useContext(SdkContext);
|
|
181
|
+
if (!contextSdk) throw new Error("useSdk must be used within a AlokaiProvider");
|
|
182
|
+
return contextSdk;
|
|
183
|
+
};
|
|
184
|
+
return _objectSpread2({
|
|
185
|
+
AlokaiProvider,
|
|
186
|
+
useSdk
|
|
187
|
+
}, rest);
|
|
145
188
|
}
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
env
|
|
150
|
-
};
|
|
189
|
+
|
|
190
|
+
//#endregion
|
|
191
|
+
export { createAlokaiContext, createSfStateProvider, env };
|