@viu/emporix-sdk-react 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.
- package/CHANGELOG.md +519 -0
- package/LICENSE +21 -0
- package/README.md +85 -0
- package/dist/chunk-D43CSHK3.js +410 -0
- package/dist/chunk-D43CSHK3.js.map +1 -0
- package/dist/chunk-FBQY2N7S.js +212 -0
- package/dist/chunk-FBQY2N7S.js.map +1 -0
- package/dist/chunk-N3VDSKCT.js +1128 -0
- package/dist/chunk-N3VDSKCT.js.map +1 -0
- package/dist/chunk-TIS4BKHK.js +25 -0
- package/dist/chunk-TIS4BKHK.js.map +1 -0
- package/dist/hooks.cjs +1212 -0
- package/dist/hooks.cjs.map +1 -0
- package/dist/hooks.d.cts +529 -0
- package/dist/hooks.d.ts +529 -0
- package/dist/hooks.js +5 -0
- package/dist/hooks.js.map +1 -0
- package/dist/index.cjs +1849 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +67 -0
- package/dist/index.d.ts +67 -0
- package/dist/index.js +30 -0
- package/dist/index.js.map +1 -0
- package/dist/provider-BhvQWnnh.d.cts +133 -0
- package/dist/provider-fvcYdqqX.d.ts +133 -0
- package/dist/provider.cjs +480 -0
- package/dist/provider.cjs.map +1 -0
- package/dist/provider.d.cts +5 -0
- package/dist/provider.d.ts +5 -0
- package/dist/provider.js +4 -0
- package/dist/provider.js.map +1 -0
- package/dist/ssr.cjs +29 -0
- package/dist/ssr.cjs.map +1 -0
- package/dist/ssr.d.cts +23 -0
- package/dist/ssr.d.ts +23 -0
- package/dist/ssr.js +3 -0
- package/dist/ssr.js.map +1 -0
- package/dist/storage.cjs +218 -0
- package/dist/storage.cjs.map +1 -0
- package/dist/storage.d.cts +65 -0
- package/dist/storage.d.ts +65 -0
- package/dist/storage.js +3 -0
- package/dist/storage.js.map +1 -0
- package/package.json +104 -0
package/dist/ssr.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { QueryClient } from '@tanstack/react-query';
|
|
2
|
+
import { EmporixClient, AuthContext } from '@viu/emporix-sdk';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Server-side prefetch of a product into a {@link QueryClient}, using the same
|
|
6
|
+
* query key shape as `useProduct` so client hydration is a cache hit.
|
|
7
|
+
* Create the `EmporixClient` once per server, never per request.
|
|
8
|
+
*/
|
|
9
|
+
declare function prefetchProduct(qc: QueryClient, client: EmporixClient, productId: string, authCtx?: AuthContext): Promise<void>;
|
|
10
|
+
/**
|
|
11
|
+
* Server-side prefetch of a cart. Pass the customer/anonymous context resolved
|
|
12
|
+
* from the request (e.g. a token read from an httpOnly cookie).
|
|
13
|
+
*/
|
|
14
|
+
declare function prefetchCart(qc: QueryClient, client: EmporixClient, cartId: string, authCtx: AuthContext): Promise<void>;
|
|
15
|
+
/**
|
|
16
|
+
* Server-side prefetch of a single customer order. Writes the same cache key
|
|
17
|
+
* `useOrder(orderId)` reads, so client hydration is a cache hit.
|
|
18
|
+
*/
|
|
19
|
+
declare function prefetchOrder(qc: QueryClient, client: EmporixClient, orderId: string, authCtx: AuthContext, opts?: {
|
|
20
|
+
saasToken?: string;
|
|
21
|
+
}): Promise<void>;
|
|
22
|
+
|
|
23
|
+
export { prefetchCart, prefetchOrder, prefetchProduct };
|
package/dist/ssr.js
ADDED
package/dist/ssr.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","file":"ssr.js"}
|
package/dist/storage.cjs
ADDED
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/storage/memory.ts
|
|
4
|
+
function createMemoryStorage(opts = {}) {
|
|
5
|
+
let token = opts.initial ?? null;
|
|
6
|
+
let cartId = null;
|
|
7
|
+
let anon = null;
|
|
8
|
+
let siteCode = null;
|
|
9
|
+
let activeLegalEntityId = null;
|
|
10
|
+
let refreshToken = null;
|
|
11
|
+
const tokenListeners = /* @__PURE__ */ new Set();
|
|
12
|
+
const all = createListenerSet();
|
|
13
|
+
return {
|
|
14
|
+
getCustomerToken: () => token,
|
|
15
|
+
setCustomerToken: (t) => {
|
|
16
|
+
token = t;
|
|
17
|
+
for (const l of tokenListeners) l(token);
|
|
18
|
+
all.notify("customerToken");
|
|
19
|
+
},
|
|
20
|
+
subscribe: (l) => {
|
|
21
|
+
tokenListeners.add(l);
|
|
22
|
+
return () => tokenListeners.delete(l);
|
|
23
|
+
},
|
|
24
|
+
getCartId: () => cartId,
|
|
25
|
+
setCartId: (id) => {
|
|
26
|
+
cartId = id;
|
|
27
|
+
all.notify("cartId");
|
|
28
|
+
},
|
|
29
|
+
getAnonymousSession: () => anon,
|
|
30
|
+
setAnonymousSession: (s) => {
|
|
31
|
+
anon = s;
|
|
32
|
+
all.notify("anonymousSession");
|
|
33
|
+
},
|
|
34
|
+
getSiteCode: () => siteCode,
|
|
35
|
+
setSiteCode: (code) => {
|
|
36
|
+
siteCode = code;
|
|
37
|
+
all.notify("siteCode");
|
|
38
|
+
},
|
|
39
|
+
getActiveLegalEntityId: () => activeLegalEntityId,
|
|
40
|
+
setActiveLegalEntityId: (id) => {
|
|
41
|
+
activeLegalEntityId = id;
|
|
42
|
+
all.notify("activeLegalEntityId");
|
|
43
|
+
},
|
|
44
|
+
getRefreshToken: () => refreshToken,
|
|
45
|
+
setRefreshToken: (t) => {
|
|
46
|
+
refreshToken = t;
|
|
47
|
+
all.notify("refreshToken");
|
|
48
|
+
},
|
|
49
|
+
subscribeAll: (l) => all.add(l)
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// src/storage/local-storage.ts
|
|
54
|
+
var DEFAULT_TOKEN_KEY = "emporix.customerToken";
|
|
55
|
+
var CART_KEY = "emporix.cartId";
|
|
56
|
+
var ANON_KEY = "emporix.anonymousSession";
|
|
57
|
+
var SITE_KEY = "emporix.siteCode";
|
|
58
|
+
var ACTIVE_LE_KEY = "emporix.activeLegalEntityId";
|
|
59
|
+
var REFRESH_KEY = "emporix.refreshToken";
|
|
60
|
+
function createLocalStorageStorage(opts = {}) {
|
|
61
|
+
const tokenKey = opts.key ?? DEFAULT_TOKEN_KEY;
|
|
62
|
+
const available = typeof globalThis !== "undefined" && typeof globalThis.localStorage !== "undefined";
|
|
63
|
+
if (!available) {
|
|
64
|
+
console.warn("[emporix] localStorage unavailable; falling back to in-memory storage");
|
|
65
|
+
return createMemoryStorage();
|
|
66
|
+
}
|
|
67
|
+
const ls = globalThis.localStorage;
|
|
68
|
+
const tokenListeners = /* @__PURE__ */ new Set();
|
|
69
|
+
const all = createListenerSet();
|
|
70
|
+
return {
|
|
71
|
+
getCustomerToken: () => ls.getItem(tokenKey),
|
|
72
|
+
setCustomerToken: (t) => {
|
|
73
|
+
if (t === null) ls.removeItem(tokenKey);
|
|
74
|
+
else ls.setItem(tokenKey, t);
|
|
75
|
+
for (const l of tokenListeners) l(t);
|
|
76
|
+
all.notify("customerToken");
|
|
77
|
+
},
|
|
78
|
+
subscribe: (l) => {
|
|
79
|
+
tokenListeners.add(l);
|
|
80
|
+
return () => tokenListeners.delete(l);
|
|
81
|
+
},
|
|
82
|
+
getCartId: () => ls.getItem(CART_KEY),
|
|
83
|
+
setCartId: (id) => {
|
|
84
|
+
if (id === null) ls.removeItem(CART_KEY);
|
|
85
|
+
else ls.setItem(CART_KEY, id);
|
|
86
|
+
all.notify("cartId");
|
|
87
|
+
},
|
|
88
|
+
getAnonymousSession: () => parseAnonymousSession(ls.getItem(ANON_KEY)),
|
|
89
|
+
setAnonymousSession: (s) => {
|
|
90
|
+
if (s === null) ls.removeItem(ANON_KEY);
|
|
91
|
+
else ls.setItem(ANON_KEY, JSON.stringify({ refreshToken: s.refreshToken, sessionId: s.sessionId }));
|
|
92
|
+
all.notify("anonymousSession");
|
|
93
|
+
},
|
|
94
|
+
getSiteCode: () => ls.getItem(SITE_KEY),
|
|
95
|
+
setSiteCode: (code) => {
|
|
96
|
+
if (code === null) ls.removeItem(SITE_KEY);
|
|
97
|
+
else ls.setItem(SITE_KEY, code);
|
|
98
|
+
all.notify("siteCode");
|
|
99
|
+
},
|
|
100
|
+
getActiveLegalEntityId: () => ls.getItem(ACTIVE_LE_KEY),
|
|
101
|
+
setActiveLegalEntityId: (id) => {
|
|
102
|
+
if (id === null) ls.removeItem(ACTIVE_LE_KEY);
|
|
103
|
+
else ls.setItem(ACTIVE_LE_KEY, id);
|
|
104
|
+
all.notify("activeLegalEntityId");
|
|
105
|
+
},
|
|
106
|
+
getRefreshToken: () => ls.getItem(REFRESH_KEY),
|
|
107
|
+
setRefreshToken: (t) => {
|
|
108
|
+
if (t === null) ls.removeItem(REFRESH_KEY);
|
|
109
|
+
else ls.setItem(REFRESH_KEY, t);
|
|
110
|
+
all.notify("refreshToken");
|
|
111
|
+
},
|
|
112
|
+
subscribeAll: (l) => all.add(l)
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// src/storage/cookie.ts
|
|
117
|
+
var DEFAULT_TOKEN_NAME = "emporix.customerToken";
|
|
118
|
+
var CART_NAME = "emporix.cartId";
|
|
119
|
+
var ANON_NAME = "emporix.anonymousSession";
|
|
120
|
+
var SITE_NAME = "emporix.siteCode";
|
|
121
|
+
var ACTIVE_LE_NAME = "emporix.activeLegalEntityId";
|
|
122
|
+
var REFRESH_NAME = "emporix.refreshToken";
|
|
123
|
+
function createCookieStorage(opts = {}) {
|
|
124
|
+
const tokenName = opts.name ?? DEFAULT_TOKEN_NAME;
|
|
125
|
+
const sameSite = opts.sameSite ?? "lax";
|
|
126
|
+
const secure = opts.secure ?? false;
|
|
127
|
+
if (typeof document === "undefined") {
|
|
128
|
+
console.warn("[emporix] document unavailable; cookie storage falling back to in-memory");
|
|
129
|
+
return createMemoryStorage();
|
|
130
|
+
}
|
|
131
|
+
const attrs = `path=/; SameSite=${sameSite}${secure ? "; Secure" : ""}`;
|
|
132
|
+
const readCookie = (name) => {
|
|
133
|
+
for (const part of document.cookie.split("; ")) {
|
|
134
|
+
const [k, ...v] = part.split("=");
|
|
135
|
+
if (k === name) return decodeURIComponent(v.join("=")) || null;
|
|
136
|
+
}
|
|
137
|
+
return null;
|
|
138
|
+
};
|
|
139
|
+
const writeCookie = (name, value) => {
|
|
140
|
+
document.cookie = value === null ? `${name}=; expires=Thu, 01 Jan 1970 00:00:00 GMT; ${attrs}` : `${name}=${encodeURIComponent(value)}; ${attrs}`;
|
|
141
|
+
};
|
|
142
|
+
const all = createListenerSet();
|
|
143
|
+
return {
|
|
144
|
+
getCustomerToken: () => readCookie(tokenName),
|
|
145
|
+
setCustomerToken: (t) => {
|
|
146
|
+
writeCookie(tokenName, t);
|
|
147
|
+
all.notify("customerToken");
|
|
148
|
+
},
|
|
149
|
+
getCartId: () => readCookie(CART_NAME),
|
|
150
|
+
setCartId: (id) => {
|
|
151
|
+
writeCookie(CART_NAME, id);
|
|
152
|
+
all.notify("cartId");
|
|
153
|
+
},
|
|
154
|
+
getAnonymousSession: () => parseAnonymousSession(readCookie(ANON_NAME)),
|
|
155
|
+
setAnonymousSession: (s) => {
|
|
156
|
+
writeCookie(
|
|
157
|
+
ANON_NAME,
|
|
158
|
+
s === null ? null : JSON.stringify({ refreshToken: s.refreshToken, sessionId: s.sessionId })
|
|
159
|
+
);
|
|
160
|
+
all.notify("anonymousSession");
|
|
161
|
+
},
|
|
162
|
+
getSiteCode: () => readCookie(SITE_NAME),
|
|
163
|
+
setSiteCode: (code) => {
|
|
164
|
+
writeCookie(SITE_NAME, code);
|
|
165
|
+
all.notify("siteCode");
|
|
166
|
+
},
|
|
167
|
+
getActiveLegalEntityId: () => readCookie(ACTIVE_LE_NAME),
|
|
168
|
+
setActiveLegalEntityId: (id) => {
|
|
169
|
+
writeCookie(ACTIVE_LE_NAME, id);
|
|
170
|
+
all.notify("activeLegalEntityId");
|
|
171
|
+
},
|
|
172
|
+
getRefreshToken: () => readCookie(REFRESH_NAME),
|
|
173
|
+
setRefreshToken: (t) => {
|
|
174
|
+
writeCookie(REFRESH_NAME, t);
|
|
175
|
+
all.notify("refreshToken");
|
|
176
|
+
},
|
|
177
|
+
subscribeAll: (l) => all.add(l)
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// src/storage/index.ts
|
|
182
|
+
function createListenerSet() {
|
|
183
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
184
|
+
return {
|
|
185
|
+
add(l) {
|
|
186
|
+
listeners.add(l);
|
|
187
|
+
return () => listeners.delete(l);
|
|
188
|
+
},
|
|
189
|
+
notify(value) {
|
|
190
|
+
for (const l of listeners) {
|
|
191
|
+
try {
|
|
192
|
+
l(value);
|
|
193
|
+
} catch {
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
function parseAnonymousSession(raw) {
|
|
200
|
+
if (!raw) return null;
|
|
201
|
+
try {
|
|
202
|
+
const parsed = JSON.parse(raw);
|
|
203
|
+
if (typeof parsed.refreshToken === "string" && typeof parsed.sessionId === "string") {
|
|
204
|
+
return { refreshToken: parsed.refreshToken, sessionId: parsed.sessionId };
|
|
205
|
+
}
|
|
206
|
+
return null;
|
|
207
|
+
} catch {
|
|
208
|
+
return null;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
exports.createCookieStorage = createCookieStorage;
|
|
213
|
+
exports.createListenerSet = createListenerSet;
|
|
214
|
+
exports.createLocalStorageStorage = createLocalStorageStorage;
|
|
215
|
+
exports.createMemoryStorage = createMemoryStorage;
|
|
216
|
+
exports.parseAnonymousSession = parseAnonymousSession;
|
|
217
|
+
//# sourceMappingURL=storage.cjs.map
|
|
218
|
+
//# sourceMappingURL=storage.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/storage/memory.ts","../src/storage/local-storage.ts","../src/storage/cookie.ts","../src/storage/index.ts"],"names":[],"mappings":";;;AAQO,SAAS,mBAAA,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AC,CAAA;AAC3B,MAAA,GAAA,CAAI,OAAO,cAAc,CAAA;AAAA,IAC3B,CAAA;AAAA,IACA,YAAA,EAAc,CAAC,CAAA,KAAM,GAAA,CAAI,IAAI,CAAC;AAAA,GAChC;AACF;;;AClBO,SAAS,iBAAA,GAGd;AACA,EAAA,MAAM,SAAA,uBAAgB,GAAA,EAAoB;AAC1C,EAAA,OAAO;AAAA,IACL,IAAI,CAAA,EAAG;AACL,MAAA,SAAA,CAAU,IAAI,CAAC,CAAA;AACf,MAAA,OAAO,MAAM,SAAA,CAAU,MAAA,CAAO,CAAC,CAAA;AAAA,IACjC,CAAA;AAAA,IACA,OAAO,KAAA,EAAO;AACZ,MAAA,KAAA,MAAW,KAAK,SAAA,EAAW;AACzB,QAAA,IAAI;AACF,UAAA,CAAA,CAAE,KAAK,CAAA;AAAA,QACT,CAAA,CAAA,MAAQ;AAAA,QAER;AAAA,MACF;AAAA,IACF;AAAA,GACF;AACF;AAOO,SAAS,sBAAsB,GAAA,EAAsD;AAC1F,EAAA,IAAI,CAAC,KAAK,OAAO,IAAA;AACjB,EAAA,IAAI;AACF,IAAA,MAAM,MAAA,GAAS,IAAA,CAAK,KAAA,CAAM,GAAG,CAAA;AAC7B,IAAA,IAAI,OAAO,MAAA,CAAO,YAAA,KAAiB,YAAY,OAAO,MAAA,CAAO,cAAc,QAAA,EAAU;AACnF,MAAA,OAAO,EAAE,YAAA,EAAc,MAAA,CAAO,YAAA,EAAc,SAAA,EAAW,OAAO,SAAA,EAAU;AAAA,IAC1E;AACA,IAAA,OAAO,IAAA;AAAA,EACT,CAAA,CAAA,MAAQ;AACN,IAAA,OAAO,IAAA;AAAA,EACT;AACF","file":"storage.cjs","sourcesContent":["import {\n createListenerSet,\n type EmporixStorage,\n type EmporixStorageKey,\n type PersistedAnonymousSession,\n} from \"./index\";\n\n/** In-memory token store. Default, SSR-safe, no persistence. */\nexport function createMemoryStorage(opts: { initial?: string } = {}): EmporixStorage {\n let token: string | null = opts.initial ?? null;\n let cartId: string | null = null;\n let anon: PersistedAnonymousSession | null = null;\n let siteCode: string | null = null;\n let activeLegalEntityId: string | null = null;\n let refreshToken: string | null = null;\n const tokenListeners = new Set<(t: string | null) => void>();\n const all = createListenerSet<EmporixStorageKey>();\n return {\n getCustomerToken: () => token,\n setCustomerToken: (t) => {\n token = t;\n for (const l of tokenListeners) l(token);\n all.notify(\"customerToken\");\n },\n subscribe: (l) => {\n tokenListeners.add(l);\n return () => tokenListeners.delete(l);\n },\n getCartId: () => cartId,\n setCartId: (id) => {\n cartId = id;\n all.notify(\"cartId\");\n },\n getAnonymousSession: () => anon,\n setAnonymousSession: (s) => {\n anon = s;\n all.notify(\"anonymousSession\");\n },\n getSiteCode: () => siteCode,\n setSiteCode: (code) => {\n siteCode = code;\n all.notify(\"siteCode\");\n },\n getActiveLegalEntityId: () => activeLegalEntityId,\n setActiveLegalEntityId: (id) => {\n activeLegalEntityId = id;\n all.notify(\"activeLegalEntityId\");\n },\n getRefreshToken: () => refreshToken,\n setRefreshToken: (t) => {\n refreshToken = t;\n all.notify(\"refreshToken\");\n },\n subscribeAll: (l) => all.add(l),\n };\n}\n","import {\n createListenerSet,\n parseAnonymousSession,\n type EmporixStorage,\n type EmporixStorageKey,\n} from \"./index\";\nimport { createMemoryStorage } from \"./memory\";\n\nconst DEFAULT_TOKEN_KEY = \"emporix.customerToken\";\nconst CART_KEY = \"emporix.cartId\";\nconst ANON_KEY = \"emporix.anonymousSession\";\nconst SITE_KEY = \"emporix.siteCode\";\nconst ACTIVE_LE_KEY = \"emporix.activeLegalEntityId\";\nconst REFRESH_KEY = \"emporix.refreshToken\";\n\n/** Browser `localStorage`-backed store. Falls back to memory on the server. */\nexport function createLocalStorageStorage(opts: { key?: string } = {}): EmporixStorage {\n const tokenKey = opts.key ?? DEFAULT_TOKEN_KEY;\n const available =\n typeof globalThis !== \"undefined\" &&\n typeof (globalThis as { localStorage?: Storage }).localStorage !== \"undefined\";\n if (!available) {\n // eslint-disable-next-line no-console\n console.warn(\"[emporix] localStorage unavailable; falling back to in-memory storage\");\n return createMemoryStorage();\n }\n const ls = (globalThis as unknown as { localStorage: Storage }).localStorage;\n const tokenListeners = new Set<(t: string | null) => void>();\n const all = createListenerSet<EmporixStorageKey>();\n return {\n getCustomerToken: () => ls.getItem(tokenKey),\n setCustomerToken: (t) => {\n if (t === null) ls.removeItem(tokenKey);\n else ls.setItem(tokenKey, t);\n for (const l of tokenListeners) l(t);\n all.notify(\"customerToken\");\n },\n subscribe: (l) => {\n tokenListeners.add(l);\n return () => tokenListeners.delete(l);\n },\n getCartId: () => ls.getItem(CART_KEY),\n setCartId: (id) => {\n if (id === null) ls.removeItem(CART_KEY);\n else ls.setItem(CART_KEY, id);\n all.notify(\"cartId\");\n },\n getAnonymousSession: () => parseAnonymousSession(ls.getItem(ANON_KEY)),\n setAnonymousSession: (s) => {\n if (s === null) ls.removeItem(ANON_KEY);\n else ls.setItem(ANON_KEY, JSON.stringify({ refreshToken: s.refreshToken, sessionId: s.sessionId }));\n all.notify(\"anonymousSession\");\n },\n getSiteCode: () => ls.getItem(SITE_KEY),\n setSiteCode: (code) => {\n if (code === null) ls.removeItem(SITE_KEY);\n else ls.setItem(SITE_KEY, code);\n all.notify(\"siteCode\");\n },\n getActiveLegalEntityId: () => ls.getItem(ACTIVE_LE_KEY),\n setActiveLegalEntityId: (id) => {\n if (id === null) ls.removeItem(ACTIVE_LE_KEY);\n else ls.setItem(ACTIVE_LE_KEY, id);\n all.notify(\"activeLegalEntityId\");\n },\n getRefreshToken: () => ls.getItem(REFRESH_KEY),\n setRefreshToken: (t) => {\n if (t === null) ls.removeItem(REFRESH_KEY);\n else ls.setItem(REFRESH_KEY, t);\n all.notify(\"refreshToken\");\n },\n subscribeAll: (l) => all.add(l),\n };\n}\n","import {\n createListenerSet,\n parseAnonymousSession,\n type EmporixStorage,\n type EmporixStorageKey,\n} from \"./index\";\nimport { createMemoryStorage } from \"./memory\";\n\nconst DEFAULT_TOKEN_NAME = \"emporix.customerToken\";\nconst CART_NAME = \"emporix.cartId\";\nconst ANON_NAME = \"emporix.anonymousSession\";\nconst SITE_NAME = \"emporix.siteCode\";\nconst ACTIVE_LE_NAME = \"emporix.activeLegalEntityId\";\nconst REFRESH_NAME = \"emporix.refreshToken\";\n\n/** Cookie-backed store. Consumer must set SameSite/Secure for CSRF safety. */\nexport function createCookieStorage(\n opts: { name?: string; secure?: boolean; sameSite?: \"lax\" | \"strict\" | \"none\" } = {},\n): EmporixStorage {\n const tokenName = opts.name ?? DEFAULT_TOKEN_NAME;\n const sameSite = opts.sameSite ?? \"lax\";\n const secure = opts.secure ?? false;\n if (typeof document === \"undefined\") {\n // eslint-disable-next-line no-console\n console.warn(\"[emporix] document unavailable; cookie storage falling back to in-memory\");\n return createMemoryStorage();\n }\n const attrs = `path=/; SameSite=${sameSite}${secure ? \"; Secure\" : \"\"}`;\n const readCookie = (name: string): string | null => {\n for (const part of document.cookie.split(\"; \")) {\n const [k, ...v] = part.split(\"=\");\n if (k === name) return decodeURIComponent(v.join(\"=\")) || null;\n }\n return null;\n };\n const writeCookie = (name: string, value: string | null): void => {\n document.cookie =\n value === null\n ? `${name}=; expires=Thu, 01 Jan 1970 00:00:00 GMT; ${attrs}`\n : `${name}=${encodeURIComponent(value)}; ${attrs}`;\n };\n const all = createListenerSet<EmporixStorageKey>();\n return {\n getCustomerToken: () => readCookie(tokenName),\n setCustomerToken: (t) => {\n writeCookie(tokenName, t);\n all.notify(\"customerToken\");\n },\n getCartId: () => readCookie(CART_NAME),\n setCartId: (id) => {\n writeCookie(CART_NAME, id);\n all.notify(\"cartId\");\n },\n getAnonymousSession: () => parseAnonymousSession(readCookie(ANON_NAME)),\n setAnonymousSession: (s) => {\n writeCookie(\n ANON_NAME,\n s === null\n ? null\n : JSON.stringify({ refreshToken: s.refreshToken, sessionId: s.sessionId }),\n );\n all.notify(\"anonymousSession\");\n },\n getSiteCode: () => readCookie(SITE_NAME),\n setSiteCode: (code) => {\n writeCookie(SITE_NAME, code);\n all.notify(\"siteCode\");\n },\n getActiveLegalEntityId: () => readCookie(ACTIVE_LE_NAME),\n setActiveLegalEntityId: (id) => {\n writeCookie(ACTIVE_LE_NAME, id);\n all.notify(\"activeLegalEntityId\");\n },\n getRefreshToken: () => readCookie(REFRESH_NAME),\n setRefreshToken: (t) => {\n writeCookie(REFRESH_NAME, t);\n all.notify(\"refreshToken\");\n },\n subscribeAll: (l) => all.add(l),\n };\n}\n","/** Pluggable persistence for SDK session state. SSR-safe by default (memory). */\nexport interface EmporixStorage {\n // Customer token (unchanged).\n getCustomerToken(): string | null;\n setCustomerToken(token: string | null): void;\n subscribe?(listener: (token: string | null) => void): () => void;\n\n // Active guest / customer cart id.\n getCartId(): string | null;\n setCartId(id: string | null): void;\n\n // Anonymous session — used by DefaultTokenProvider (via EmporixProvider\n // wiring) to preserve sessionId across page reloads.\n getAnonymousSession(): PersistedAnonymousSession | null;\n setAnonymousSession(session: PersistedAnonymousSession | null): void;\n\n // Active site code (MS-2). `null` = no site bound yet.\n getSiteCode(): string | null;\n setSiteCode(code: string | null): void;\n\n // Active legal entity id (B2B). `null` = B2C mode.\n getActiveLegalEntityId(): string | null;\n setActiveLegalEntityId(id: string | null): void;\n\n // Refresh token — optional persistence. When absent, B2B company-switch\n // falls back to a local-state-only update (no server-side token rescope).\n getRefreshToken(): string | null;\n setRefreshToken(token: string | null): void;\n\n /**\n * Subscribe to any storage write. The listener receives the key that\n * changed. Returns an unsubscribe function. Optional — backends may no-op.\n * Used by the telemetry layer to emit `storage.write` events.\n */\n subscribeAll?(\n listener: (key: EmporixStorageKey) => void,\n ): () => void;\n}\n\n/** Minimal subset of `AnonymousSession` that needs to outlive a page load. */\nexport interface PersistedAnonymousSession {\n refreshToken: string;\n sessionId: string;\n}\n\n/** Backward-compat alias. New code should prefer `EmporixStorage`. */\nexport type TokenStorage = EmporixStorage;\n\n/** Keys that participate in {@link EmporixStorage.subscribeAll}. */\nexport type EmporixStorageKey =\n | \"customerToken\"\n | \"cartId\"\n | \"siteCode\"\n | \"anonymousSession\"\n | \"activeLegalEntityId\"\n | \"refreshToken\";\n\n/**\n * Internal: create a swallow-on-throw listener set used by all three storage\n * backends for `subscribeAll`. Centralizes the try/catch wrapper so a buggy\n * telemetry handler never breaks a storage write.\n */\nexport function createListenerSet<T>(): {\n add(l: (value: T) => void): () => void;\n notify(value: T): void;\n} {\n const listeners = new Set<(v: T) => void>();\n return {\n add(l) {\n listeners.add(l);\n return () => listeners.delete(l);\n },\n notify(value) {\n for (const l of listeners) {\n try {\n l(value);\n } catch {\n // Swallow handler errors; telemetry must never break writes.\n }\n }\n },\n };\n}\n\n/**\n * Internal: parses a raw `anonymousSession` JSON payload (from localStorage\n * or a cookie) into a {@link PersistedAnonymousSession}. Returns `null` for\n * any malformed or missing input.\n */\nexport function parseAnonymousSession(raw: string | null): PersistedAnonymousSession | null {\n if (!raw) return null;\n try {\n const parsed = JSON.parse(raw) as Partial<PersistedAnonymousSession>;\n if (typeof parsed.refreshToken === \"string\" && typeof parsed.sessionId === \"string\") {\n return { refreshToken: parsed.refreshToken, sessionId: parsed.sessionId };\n }\n return null;\n } catch {\n return null;\n }\n}\n\nexport { createMemoryStorage } from \"./memory\";\nexport { createLocalStorageStorage } from \"./local-storage\";\nexport { createCookieStorage } from \"./cookie\";\n"]}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/** In-memory token store. Default, SSR-safe, no persistence. */
|
|
2
|
+
declare function createMemoryStorage(opts?: {
|
|
3
|
+
initial?: string;
|
|
4
|
+
}): EmporixStorage;
|
|
5
|
+
|
|
6
|
+
/** Browser `localStorage`-backed store. Falls back to memory on the server. */
|
|
7
|
+
declare function createLocalStorageStorage(opts?: {
|
|
8
|
+
key?: string;
|
|
9
|
+
}): EmporixStorage;
|
|
10
|
+
|
|
11
|
+
/** Cookie-backed store. Consumer must set SameSite/Secure for CSRF safety. */
|
|
12
|
+
declare function createCookieStorage(opts?: {
|
|
13
|
+
name?: string;
|
|
14
|
+
secure?: boolean;
|
|
15
|
+
sameSite?: "lax" | "strict" | "none";
|
|
16
|
+
}): EmporixStorage;
|
|
17
|
+
|
|
18
|
+
/** Pluggable persistence for SDK session state. SSR-safe by default (memory). */
|
|
19
|
+
interface EmporixStorage {
|
|
20
|
+
getCustomerToken(): string | null;
|
|
21
|
+
setCustomerToken(token: string | null): void;
|
|
22
|
+
subscribe?(listener: (token: string | null) => void): () => void;
|
|
23
|
+
getCartId(): string | null;
|
|
24
|
+
setCartId(id: string | null): void;
|
|
25
|
+
getAnonymousSession(): PersistedAnonymousSession | null;
|
|
26
|
+
setAnonymousSession(session: PersistedAnonymousSession | null): void;
|
|
27
|
+
getSiteCode(): string | null;
|
|
28
|
+
setSiteCode(code: string | null): void;
|
|
29
|
+
getActiveLegalEntityId(): string | null;
|
|
30
|
+
setActiveLegalEntityId(id: string | null): void;
|
|
31
|
+
getRefreshToken(): string | null;
|
|
32
|
+
setRefreshToken(token: string | null): void;
|
|
33
|
+
/**
|
|
34
|
+
* Subscribe to any storage write. The listener receives the key that
|
|
35
|
+
* changed. Returns an unsubscribe function. Optional — backends may no-op.
|
|
36
|
+
* Used by the telemetry layer to emit `storage.write` events.
|
|
37
|
+
*/
|
|
38
|
+
subscribeAll?(listener: (key: EmporixStorageKey) => void): () => void;
|
|
39
|
+
}
|
|
40
|
+
/** Minimal subset of `AnonymousSession` that needs to outlive a page load. */
|
|
41
|
+
interface PersistedAnonymousSession {
|
|
42
|
+
refreshToken: string;
|
|
43
|
+
sessionId: string;
|
|
44
|
+
}
|
|
45
|
+
/** Backward-compat alias. New code should prefer `EmporixStorage`. */
|
|
46
|
+
type TokenStorage = EmporixStorage;
|
|
47
|
+
/** Keys that participate in {@link EmporixStorage.subscribeAll}. */
|
|
48
|
+
type EmporixStorageKey = "customerToken" | "cartId" | "siteCode" | "anonymousSession" | "activeLegalEntityId" | "refreshToken";
|
|
49
|
+
/**
|
|
50
|
+
* Internal: create a swallow-on-throw listener set used by all three storage
|
|
51
|
+
* backends for `subscribeAll`. Centralizes the try/catch wrapper so a buggy
|
|
52
|
+
* telemetry handler never breaks a storage write.
|
|
53
|
+
*/
|
|
54
|
+
declare function createListenerSet<T>(): {
|
|
55
|
+
add(l: (value: T) => void): () => void;
|
|
56
|
+
notify(value: T): void;
|
|
57
|
+
};
|
|
58
|
+
/**
|
|
59
|
+
* Internal: parses a raw `anonymousSession` JSON payload (from localStorage
|
|
60
|
+
* or a cookie) into a {@link PersistedAnonymousSession}. Returns `null` for
|
|
61
|
+
* any malformed or missing input.
|
|
62
|
+
*/
|
|
63
|
+
declare function parseAnonymousSession(raw: string | null): PersistedAnonymousSession | null;
|
|
64
|
+
|
|
65
|
+
export { type EmporixStorage, type EmporixStorageKey, type PersistedAnonymousSession, type TokenStorage, createCookieStorage, createListenerSet, createLocalStorageStorage, createMemoryStorage, parseAnonymousSession };
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/** In-memory token store. Default, SSR-safe, no persistence. */
|
|
2
|
+
declare function createMemoryStorage(opts?: {
|
|
3
|
+
initial?: string;
|
|
4
|
+
}): EmporixStorage;
|
|
5
|
+
|
|
6
|
+
/** Browser `localStorage`-backed store. Falls back to memory on the server. */
|
|
7
|
+
declare function createLocalStorageStorage(opts?: {
|
|
8
|
+
key?: string;
|
|
9
|
+
}): EmporixStorage;
|
|
10
|
+
|
|
11
|
+
/** Cookie-backed store. Consumer must set SameSite/Secure for CSRF safety. */
|
|
12
|
+
declare function createCookieStorage(opts?: {
|
|
13
|
+
name?: string;
|
|
14
|
+
secure?: boolean;
|
|
15
|
+
sameSite?: "lax" | "strict" | "none";
|
|
16
|
+
}): EmporixStorage;
|
|
17
|
+
|
|
18
|
+
/** Pluggable persistence for SDK session state. SSR-safe by default (memory). */
|
|
19
|
+
interface EmporixStorage {
|
|
20
|
+
getCustomerToken(): string | null;
|
|
21
|
+
setCustomerToken(token: string | null): void;
|
|
22
|
+
subscribe?(listener: (token: string | null) => void): () => void;
|
|
23
|
+
getCartId(): string | null;
|
|
24
|
+
setCartId(id: string | null): void;
|
|
25
|
+
getAnonymousSession(): PersistedAnonymousSession | null;
|
|
26
|
+
setAnonymousSession(session: PersistedAnonymousSession | null): void;
|
|
27
|
+
getSiteCode(): string | null;
|
|
28
|
+
setSiteCode(code: string | null): void;
|
|
29
|
+
getActiveLegalEntityId(): string | null;
|
|
30
|
+
setActiveLegalEntityId(id: string | null): void;
|
|
31
|
+
getRefreshToken(): string | null;
|
|
32
|
+
setRefreshToken(token: string | null): void;
|
|
33
|
+
/**
|
|
34
|
+
* Subscribe to any storage write. The listener receives the key that
|
|
35
|
+
* changed. Returns an unsubscribe function. Optional — backends may no-op.
|
|
36
|
+
* Used by the telemetry layer to emit `storage.write` events.
|
|
37
|
+
*/
|
|
38
|
+
subscribeAll?(listener: (key: EmporixStorageKey) => void): () => void;
|
|
39
|
+
}
|
|
40
|
+
/** Minimal subset of `AnonymousSession` that needs to outlive a page load. */
|
|
41
|
+
interface PersistedAnonymousSession {
|
|
42
|
+
refreshToken: string;
|
|
43
|
+
sessionId: string;
|
|
44
|
+
}
|
|
45
|
+
/** Backward-compat alias. New code should prefer `EmporixStorage`. */
|
|
46
|
+
type TokenStorage = EmporixStorage;
|
|
47
|
+
/** Keys that participate in {@link EmporixStorage.subscribeAll}. */
|
|
48
|
+
type EmporixStorageKey = "customerToken" | "cartId" | "siteCode" | "anonymousSession" | "activeLegalEntityId" | "refreshToken";
|
|
49
|
+
/**
|
|
50
|
+
* Internal: create a swallow-on-throw listener set used by all three storage
|
|
51
|
+
* backends for `subscribeAll`. Centralizes the try/catch wrapper so a buggy
|
|
52
|
+
* telemetry handler never breaks a storage write.
|
|
53
|
+
*/
|
|
54
|
+
declare function createListenerSet<T>(): {
|
|
55
|
+
add(l: (value: T) => void): () => void;
|
|
56
|
+
notify(value: T): void;
|
|
57
|
+
};
|
|
58
|
+
/**
|
|
59
|
+
* Internal: parses a raw `anonymousSession` JSON payload (from localStorage
|
|
60
|
+
* or a cookie) into a {@link PersistedAnonymousSession}. Returns `null` for
|
|
61
|
+
* any malformed or missing input.
|
|
62
|
+
*/
|
|
63
|
+
declare function parseAnonymousSession(raw: string | null): PersistedAnonymousSession | null;
|
|
64
|
+
|
|
65
|
+
export { type EmporixStorage, type EmporixStorageKey, type PersistedAnonymousSession, type TokenStorage, createCookieStorage, createListenerSet, createLocalStorageStorage, createMemoryStorage, parseAnonymousSession };
|
package/dist/storage.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","file":"storage.js"}
|
package/package.json
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@viu/emporix-sdk-react",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "React bindings for the Emporix SDK",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": {
|
|
7
|
+
"name": "viu",
|
|
8
|
+
"url": "https://github.com/viuteam"
|
|
9
|
+
},
|
|
10
|
+
"homepage": "https://github.com/viuteam/emporix-sdk#readme",
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+https://github.com/viuteam/emporix-sdk.git",
|
|
14
|
+
"directory": "packages/react"
|
|
15
|
+
},
|
|
16
|
+
"bugs": {
|
|
17
|
+
"url": "https://github.com/viuteam/emporix-sdk/issues"
|
|
18
|
+
},
|
|
19
|
+
"keywords": [
|
|
20
|
+
"emporix",
|
|
21
|
+
"ecommerce",
|
|
22
|
+
"commerce",
|
|
23
|
+
"react",
|
|
24
|
+
"react-query",
|
|
25
|
+
"hooks",
|
|
26
|
+
"typescript",
|
|
27
|
+
"storefront"
|
|
28
|
+
],
|
|
29
|
+
"type": "module",
|
|
30
|
+
"sideEffects": false,
|
|
31
|
+
"engines": {
|
|
32
|
+
"node": ">=20.19.0"
|
|
33
|
+
},
|
|
34
|
+
"files": [
|
|
35
|
+
"dist",
|
|
36
|
+
"README.md",
|
|
37
|
+
"CHANGELOG.md",
|
|
38
|
+
"LICENSE"
|
|
39
|
+
],
|
|
40
|
+
"exports": {
|
|
41
|
+
".": {
|
|
42
|
+
"types": "./dist/index.d.ts",
|
|
43
|
+
"import": "./dist/index.js",
|
|
44
|
+
"require": "./dist/index.cjs"
|
|
45
|
+
},
|
|
46
|
+
"./provider": {
|
|
47
|
+
"types": "./dist/provider.d.ts",
|
|
48
|
+
"import": "./dist/provider.js",
|
|
49
|
+
"require": "./dist/provider.cjs"
|
|
50
|
+
},
|
|
51
|
+
"./hooks": {
|
|
52
|
+
"types": "./dist/hooks.d.ts",
|
|
53
|
+
"import": "./dist/hooks.js",
|
|
54
|
+
"require": "./dist/hooks.cjs"
|
|
55
|
+
},
|
|
56
|
+
"./storage": {
|
|
57
|
+
"types": "./dist/storage.d.ts",
|
|
58
|
+
"import": "./dist/storage.js",
|
|
59
|
+
"require": "./dist/storage.cjs"
|
|
60
|
+
},
|
|
61
|
+
"./ssr": {
|
|
62
|
+
"types": "./dist/ssr.d.ts",
|
|
63
|
+
"import": "./dist/ssr.js",
|
|
64
|
+
"require": "./dist/ssr.cjs"
|
|
65
|
+
}
|
|
66
|
+
},
|
|
67
|
+
"publishConfig": {
|
|
68
|
+
"access": "public",
|
|
69
|
+
"provenance": false
|
|
70
|
+
},
|
|
71
|
+
"peerDependencies": {
|
|
72
|
+
"@tanstack/react-query": "^5.51.0",
|
|
73
|
+
"react": "^18.0.0 || ^19.0.0",
|
|
74
|
+
"@viu/emporix-sdk": "^1.0.0"
|
|
75
|
+
},
|
|
76
|
+
"devDependencies": {
|
|
77
|
+
"@tanstack/react-query": "^5.51.0",
|
|
78
|
+
"@testing-library/jest-dom": "^6.4.0",
|
|
79
|
+
"@testing-library/react": "^16.0.0",
|
|
80
|
+
"@types/node": "^24.0.0",
|
|
81
|
+
"@types/react": "^18.3.0",
|
|
82
|
+
"@typescript-eslint/eslint-plugin": "^8.0.0",
|
|
83
|
+
"@typescript-eslint/parser": "^8.0.0",
|
|
84
|
+
"@vitejs/plugin-react": "^4.3.0",
|
|
85
|
+
"@vitest/coverage-v8": "^2.0.0",
|
|
86
|
+
"eslint": "^9.0.0",
|
|
87
|
+
"eslint-plugin-react-hooks": "^5.0.0",
|
|
88
|
+
"jsdom": "^25.0.0",
|
|
89
|
+
"msw": "^2.4.0",
|
|
90
|
+
"react": "^18.3.1",
|
|
91
|
+
"react-dom": "^18.3.1",
|
|
92
|
+
"tsup": "^8.2.0",
|
|
93
|
+
"undici": "^6.19.0",
|
|
94
|
+
"typescript": "^5.6.0",
|
|
95
|
+
"vitest": "^2.0.0",
|
|
96
|
+
"@viu/emporix-sdk": "1.0.0"
|
|
97
|
+
},
|
|
98
|
+
"scripts": {
|
|
99
|
+
"build": "tsup",
|
|
100
|
+
"test": "vitest run --coverage",
|
|
101
|
+
"lint": "eslint src",
|
|
102
|
+
"typecheck": "tsc --noEmit"
|
|
103
|
+
}
|
|
104
|
+
}
|